aboutsummaryrefslogtreecommitdiff
path: root/Foundation/GTMCalculatedRangeTest.m
blob: 0806a0d7a87094d25f0d19062c61842ba1ac1335 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
//  GTMCalculatedRangeTest.m
//
//  Copyright 2006-2008 Google Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
// 
//  http://www.apache.org/licenses/LICENSE-2.0
// 
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations under
//  the License.
//

#import "GTMCalculatedRange.h"
#import "GTMSenTestCase.h"

@interface GTMCalculatedRangeTest : GTMTestCase {
  GTMCalculatedRange *range_;
}
@end

@implementation GTMCalculatedRangeTest
NSString *kStrings[] = { @"Fee", @"Fi", @"Fo", @"Fum" };
const NSUInteger kStringCount = sizeof(kStrings) / sizeof(NSString*);
const CGFloat kOddPosition = 0.14159265f;
const CGFloat kExistingPosition = 0.5f;
const NSUInteger kExisitingIndex = 2;

- (void)setUp {
  range_ = [[GTMCalculatedRange alloc] init];
  for(NSUInteger i = kStringCount; i > 0; --i) {
    [range_ insertStop:kStrings[kStringCount - i] atPosition:(CGFloat)(1.0 / i)];
  }
}

- (void)tearDown {
  [range_ release];
}

- (void)testInsertStop {
  // new position
  NSString *theString = @"I smell the blood of an Englishman!";
  [range_ insertStop:theString atPosition:kOddPosition];
  STAssertEquals([range_ stopCount], kStringCount + 1, @"Stop count was bad");
  NSString *getString = [range_ valueAtPosition:kOddPosition];
  STAssertNotNil(getString, @"String was bad");
  STAssertEquals(theString, getString, @"Stops weren't equal");
  // existing position
  NSString *theStringTake2 = @"I smell the blood of an Englishman! Take 2";
  [range_ insertStop:theStringTake2 atPosition:kOddPosition];
  STAssertEquals([range_ stopCount], kStringCount + 1, @"Stop count was bad");
  getString = [range_ valueAtPosition:kOddPosition];
  STAssertNotNil(getString, @"String was bad");
  STAssertEquals(theStringTake2, getString, @"Stops weren't equal");
  STAssertNotEquals(theString, getString, @"Should be the new value");
  STAssertNotEqualObjects(theString, getString, @"Should be the new value");
}

- (void)testRemoveStopAtPosition {
  STAssertFalse([range_ removeStopAtPosition: kOddPosition], @"Was able to remove non-existant stop");
  STAssertTrue([range_ removeStopAtPosition: kExistingPosition], @"Was unable to remove good stop");
  STAssertEquals([range_ stopCount], kStringCount - 1, @"Removing stop should adjust stop count");
}

- (void)testRemoveStopAtIndex {
  STAssertThrows([range_ removeStopAtIndex: kStringCount], @"Was able to remove non-existant stop");
  STAssertNoThrow([range_ removeStopAtIndex: kStringCount - 1], @"Was unable to remove good stop");
  STAssertEquals([range_ stopCount], kStringCount - 1, @"Removing stop should adjust stop count");  
}

- (void)testStopCount {
  STAssertEquals([range_ stopCount], kStringCount, @"Bad stop count");
}

- (void)testValueAtPosition {
  STAssertEqualObjects([range_ valueAtPosition: kExistingPosition], kStrings[kExisitingIndex], nil);
  STAssertNotEqualObjects([range_ valueAtPosition: kExistingPosition], kStrings[kStringCount - 1], nil);
  STAssertNil([range_ valueAtPosition: kOddPosition], nil);
}

- (void)testStopAtIndex {
  CGFloat thePosition;
  
  STAssertEqualObjects([range_ stopAtIndex:kStringCount - 1 position:nil], kStrings[kStringCount - 1], nil);
  STAssertEqualObjects([range_ stopAtIndex:kExisitingIndex position:&thePosition], kStrings[kExisitingIndex], nil);
  STAssertEquals(thePosition, kExistingPosition, nil);
  STAssertNotEqualObjects([range_ stopAtIndex:kStringCount - 1 position:nil], kStrings[2], nil);
  STAssertThrows([range_ stopAtIndex:kStringCount position:nil], nil);
}

- (void)testDescription {
  // we expect a description of atleast a few chars
  STAssertGreaterThan([[range_ description] length], (NSUInteger)10, nil);
}

@end