@@ -29,6 +29,7 @@ class TestNSArray : XCTestCase {
29
29
( " test_getObjects " , test_getObjects) ,
30
30
( " test_objectAtIndex " , test_objectAtIndex) ,
31
31
( " test_binarySearch " , test_binarySearch) ,
32
+ ( " test_binarySearchFringeCases " , test_binarySearchFringeCases) ,
32
33
( " test_replaceObjectsInRange_withObjectsFromArray " , test_replaceObjectsInRange_withObjectsFromArray) ,
33
34
( " test_replaceObjectsInRange_withObjectsFromArray_range " , test_replaceObjectsInRange_withObjectsFromArray_range) ,
34
35
]
@@ -140,8 +141,42 @@ class TestNSArray : XCTestCase {
140
141
let indexOfLeastGreaterObjectThanFive = objectIndexInArray ( array, value: 5 , startingFrom: 0 , length: 10 , options: [ . InsertionIndex, . LastEqual] )
141
142
XCTAssertTrue ( indexOfLeastGreaterObjectThanFive == 7 , " If both .InsertionIndex and .LastEqual are specified NSArray returns the index of the least greater object... " )
142
143
143
- let endOfArray = objectIndexInArray ( array, value: 10 , startingFrom: 0 , length: 13 , options: [ . InsertionIndex, . LastEqual] )
144
- XCTAssertTrue ( endOfArray == array. count, " ...or the index at the end of the array if the object is larger than all other elements. " )
144
+ let rangeStart = 0
145
+ let rangeLength = 13
146
+ let endOfArray = objectIndexInArray ( array, value: 10 , startingFrom: rangeStart, length: rangeLength, options: [ . InsertionIndex, . LastEqual] )
147
+ XCTAssertTrue ( endOfArray == ( rangeStart + rangeLength) , " ...or the index at the end of the array if the object is larger than all other elements. " )
148
+ }
149
+
150
+ func test_binarySearchFringeCases( ) {
151
+ let array = NSArray ( array: [
152
+ NSNumber ( int: 0 ) , NSNumber ( int: 1 ) , NSNumber ( int: 2 ) , NSNumber ( int: 2 ) , NSNumber ( int: 3 ) ,
153
+ NSNumber ( int: 4 ) , NSNumber ( int: 4 ) , NSNumber ( int: 6 ) , NSNumber ( int: 7 ) , NSNumber ( int: 7 ) ,
154
+ NSNumber ( int: 7 ) , NSNumber ( int: 8 ) , NSNumber ( int: 9 ) , NSNumber ( int: 9 ) ] )
155
+
156
+ let emptyArray = NSArray ( )
157
+ // Same as for non empty NSArray but error message ends with 'bounds for empty array'.
158
+ // let _ = objectIndexInArray(emptyArray, value: 0, startingFrom: 0, length: 1)
159
+
160
+ let notFoundInEmptyArray = objectIndexInArray ( emptyArray, value: 9 , startingFrom: 0 , length: 0 )
161
+ XCTAssertEqual ( notFoundInEmptyArray, NSNotFound, " Empty NSArray return NSNotFound for any valid arguments. " )
162
+
163
+ let startIndex = objectIndexInArray ( emptyArray, value: 7 , startingFrom: 0 , length: 0 , options: [ . InsertionIndex] )
164
+ XCTAssertTrue ( startIndex == 0 , " For Empty NSArray any objects should be inserted at start. " )
165
+
166
+ let rangeStart = 0
167
+ let rangeLength = 13
168
+
169
+ let leastSearch = objectIndexInArray ( array, value: - 1 , startingFrom: rangeStart, length: rangeLength)
170
+ XCTAssertTrue ( leastSearch == NSNotFound, " If object is less than least object in the range then there is no change it could be found. " )
171
+
172
+ let greatestSearch = objectIndexInArray ( array, value: 15 , startingFrom: rangeStart, length: rangeLength)
173
+ XCTAssertTrue ( greatestSearch == NSNotFound, " If object is greater than greatest object in the range then there is no change it could be found. " )
174
+
175
+ let leastInsert = objectIndexInArray ( array, value: - 1 , startingFrom: rangeStart, length: rangeLength, options: . InsertionIndex)
176
+ XCTAssertTrue ( leastInsert == rangeStart, " If object is less than least object in the range it should be inserted at range' location. " )
177
+
178
+ let greatestInsert = objectIndexInArray ( array, value: 15 , startingFrom: rangeStart, length: rangeLength, options: . InsertionIndex)
179
+ XCTAssertTrue ( greatestInsert == ( rangeStart + rangeLength) , " If object is greater than greatest object in the range it should be inserted at range' end. " )
145
180
}
146
181
147
182
func objectIndexInArray( array: NSArray , value: Int , startingFrom: Int , length: Int , options: NSBinarySearchingOptions = [ ] ) -> Int {
0 commit comments