//
//  MPRangeTests.m
//  MathKit
//
//  Created by Kim Wittenburg on 21.04.14.
//  Copyright (c) 2014 Kim Wittenburg. All rights reserved.
//

#import <XCTest/XCTest.h>

#import "MPRangePath.h"

@interface MPRangeTests : XCTestCase

@end

@implementation MPRangeTests

- (void)testInitialization {
    MPRangePath *rangePath = [[MPRangePath alloc] initWithLocation:[[NSIndexPath alloc] initWithIndex:1] length:5];
    XCTAssertEqualObjects(rangePath.location, [[NSIndexPath alloc] initWithIndex:1]);
    XCTAssertEqual(rangePath.length, 5);
    
    rangePath = [[MPRangePath alloc] initWithRange:NSMakeRange(3, 2)];
    XCTAssertEqualObjects(rangePath.location, [[NSIndexPath alloc] initWithIndex:3]);
    XCTAssertEqual(rangePath.length, 2);
    
    rangePath = [MPRangePath rangePath];
    XCTAssertEqualObjects(rangePath.location, [[NSIndexPath alloc] init]);
    XCTAssertEqual(rangePath.length, 0);
}

- (void)testMaxRange {
    NSUInteger indexes[] = {5, 2, 7, 3};
    MPRangePath *rangePath = [[MPRangePath alloc] initWithLocation:[[NSIndexPath alloc] initWithIndexes:indexes length:4] length:5];
    NSUInteger expectedIndexes[] = {5, 2, 7, 8};
    XCTAssertEqualObjects([rangePath maxRangePath], [[NSIndexPath alloc] initWithIndexes:expectedIndexes length:4]);
}

- (void)testRangeAtLastIndex {
    NSUInteger indexes[] = {5, 2, 7, 3};
    MPRangePath *rangePath = [[MPRangePath alloc] initWithLocation:[[NSIndexPath alloc] initWithIndexes:indexes length:4] length:5];
    XCTAssertTrue(NSEqualRanges(rangePath.rangeAtLastIndex, NSMakeRange(3, 5)));
}

- (void)testEquality {
    NSUInteger indexes[] = {5, 2, 7, 3};
    MPRangePath *rangePath = [[MPRangePath alloc] initWithLocation:[[NSIndexPath alloc] initWithIndexes:indexes length:4] length:5];
    NSUInteger otherIndexes[] = {5, 2, 7, 3};
    MPRangePath *otherRange = [[MPRangePath alloc] initWithLocation:[[NSIndexPath alloc] initWithIndexes:otherIndexes length:4] length:5];
    NSUInteger yetOtherIndexes[] = {5, 2, 6, 3};
    MPRangePath *yetAnotherRange = [[MPRangePath alloc] initWithLocation:[[NSIndexPath alloc] initWithIndexes:yetOtherIndexes length:4] length:5];
    XCTAssertTrue([rangePath isEqual:otherRange]);
    XCTAssertFalse([rangePath isEqual:yetAnotherRange]);
}

- (void)testCopying {
    NSUInteger indexes[] = {5, 2, 7, 3};
    MPRangePath *rangePath = [[MPRangePath alloc] initWithLocation:[[NSIndexPath alloc] initWithIndexes:indexes length:4] length:5];
    MPRangePath *copy = [rangePath copy];
    XCTAssertNotEqual(rangePath, copy);
    XCTAssertEqualObjects(rangePath, copy);
}

- (void)testContainsRange {
    NSUInteger indexes[] = {5, 2, 7, 3};
    MPRangePath *rangePath = [[MPRangePath alloc] initWithLocation:[[NSIndexPath alloc] initWithIndexes:indexes length:4] length:5];

    NSUInteger shorterPathIndexes[] = {5, 2};
    MPRangePath *shorterPathRange = [[MPRangePath alloc] initWithLocation:[[NSIndexPath alloc] initWithIndexes:shorterPathIndexes length:2] length:5];
    
    NSUInteger longerPathIndexes[] = {5, 2, 7, 3, 5};
    MPRangePath *longerPathRange = [[MPRangePath alloc] initWithLocation:[[NSIndexPath alloc] initWithIndexes:longerPathIndexes length:5] length:5];

    NSUInteger differentPathIndexes[] = {5, 3, 7, 3};
    MPRangePath *differentPathRange = [[MPRangePath alloc] initWithLocation:[[NSIndexPath alloc] initWithIndexes:differentPathIndexes length:4] length:5];

    NSUInteger equalRangeIndexes[] = {5, 2, 7, 3};
    MPRangePath *equalRange = [[MPRangePath alloc] initWithLocation:[[NSIndexPath alloc] initWithIndexes:equalRangeIndexes length:4] length:5];
    
    NSUInteger containedRangeIndexes[] = {5, 2, 7, 4};
    MPRangePath *containedRange = [[MPRangePath alloc] initWithLocation:[[NSIndexPath alloc] initWithIndexes:containedRangeIndexes length:4] length:2];

    NSUInteger largerRangeIndexes[] = {5, 2, 7, 4};
    MPRangePath *largerRange = [[MPRangePath alloc] initWithLocation:[[NSIndexPath alloc] initWithIndexes:largerRangeIndexes length:4] length:7];

    XCTAssertFalse([rangePath containsRangePath:shorterPathRange]);
    XCTAssertTrue([rangePath containsRangePath:longerPathRange]);
    XCTAssertFalse([rangePath containsRangePath:differentPathRange]);
    XCTAssertTrue([rangePath containsRangePath:equalRange]);
    XCTAssertTrue([rangePath containsRangePath:containedRange]);
    XCTAssertFalse([rangePath containsRangePath:largerRange]);
}

@end
