@@ -223,145 +223,85 @@ public static void TestTrivials()
223
223
Assert . Throws < RankException > ( ( ) => il4 . IndexOf ( 0 ) ) ;
224
224
}
225
225
226
- [ Fact ]
227
- public static void TestBinarySearch ( )
226
+ public static IEnumerable < object [ ] > BinarySearchTestData
228
227
{
229
- //@todo: Must pass in a non-null comparer for now (due to incomplete support in underlying Compare class)
230
- //
231
- IComparer comparer = new IntegerComparer ( ) ;
232
- IComparer < int > icomparer = new IntegerComparer ( ) ;
233
- int idx ;
234
- int [ ] ia = { 1 , 3 , 6 , 6 , 8 , 10 , 12 , 16 } ;
235
- idx = Array . BinarySearch ( ( Array ) ia , 8 , comparer ) ;
236
- Assert . Equal ( idx , 4 ) ;
237
-
238
- //Return value semantics: Quoted from MSDN without comment.
239
- //
240
- //The index of the specified value in the specified array,
241
- //if value is found. If value is not found and value is
242
- //less than one or more elements in array, a negative
243
- //number which is the bitwise complement of the index of
244
- //the first element that is larger than value.
245
- //If value is not found and value is greater than any
246
- //of the elements in array, a negative number which is the bitwise complement of (the index of the last element plus 1).
247
- idx = Array . BinarySearch ( ( Array ) ia , 99 , comparer ) ;
248
- Assert . Equal ( idx , ~ ( ia . Length ) ) ;
249
-
250
- idx = Array . BinarySearch < int > ( ia , 0 , 8 , 99 , icomparer ) ;
251
- Assert . Equal ( idx , ~ ( ia . Length ) ) ;
252
-
253
- idx = Array . BinarySearch ( ( Array ) ia , 6 , comparer ) ;
254
- Assert . True ( idx == 2 || idx == 3 ) ; // Duplicates are allowed but no promises on which one
255
-
256
- idx = Array . BinarySearch ( ( Array ) ia , 0 , 8 , 6 , comparer ) ;
257
- Assert . True ( idx == 2 || idx == 3 ) ; // Duplicates are allowed but no promises on which one
258
-
259
- idx = Array . BinarySearch < int > ( ia , 6 , icomparer ) ;
260
- Assert . True ( idx == 2 || idx == 3 ) ; // Duplicates are allowed but no promises on which one
261
-
262
- idx = Array . BinarySearch < int > ( ia , 0 , 8 , 6 , icomparer ) ;
263
- Assert . True ( idx == 2 || idx == 3 ) ; // Duplicates are allowed but no promises on which one
264
-
265
- comparer = new StringComparer ( ) ;
266
- IComparer < String > scomparer = new StringComparer ( ) ;
267
-
268
- String [ ] sa = { null , "aa" , "bb" , "bb" , "cc" , "dd" , "ee" } ;
269
- idx = Array . BinarySearch ( ( Array ) sa , "bb" , comparer ) ;
270
- Assert . Equal ( idx , 3 ) ;
271
-
272
- idx = Array . BinarySearch < String > ( sa , 0 , sa . Length , "bb" , scomparer ) ;
273
- Assert . Equal ( idx , 3 ) ;
274
-
275
- idx = Array . BinarySearch ( ( Array ) sa , 3 , 4 , "bb" , comparer ) ;
276
- Assert . Equal ( idx , 3 ) ;
277
-
278
- idx = Array . BinarySearch < String > ( sa , 3 , 4 , "bb" , scomparer ) ;
279
- Assert . Equal ( idx , 3 ) ;
280
-
281
- idx = Array . BinarySearch ( ( Array ) sa , 4 , 3 , "bb" , comparer ) ;
282
- Assert . Equal ( idx , - 5 ) ;
283
-
284
- idx = Array . BinarySearch < String > ( sa , 4 , 3 , "bb" , scomparer ) ;
285
- Assert . Equal ( idx , - 5 ) ;
286
-
287
- idx = Array . BinarySearch ( ( Array ) sa , 4 , 0 , "bb" , comparer ) ;
288
- Assert . Equal ( idx , - 5 ) ;
289
-
290
- idx = Array . BinarySearch < String > ( sa , 4 , 0 , "bb" , scomparer ) ;
291
- Assert . Equal ( idx , - 5 ) ;
228
+ get
229
+ {
230
+ int [ ] intArray = { 1 , 3 , 6 , 6 , 8 , 10 , 12 , 16 } ;
231
+ IComparer intComparer = new IntegerComparer ( ) ;
232
+ IComparer < int > intGenericComparer = new IntegerComparer ( ) ;
292
233
293
- idx = Array . BinarySearch ( ( Array ) sa , 0 , 7 , null , comparer ) ;
294
- Assert . Equal ( idx , 0 ) ;
234
+ string [ ] strArray = { null , "aa" , "bb" , "bb" , "cc" , "dd" , "ee" } ;
235
+ IComparer strComparer = new StringComparer ( ) ;
236
+ IComparer < string > strGenericComparer = new StringComparer ( ) ;
295
237
296
- idx = Array . BinarySearch < String > ( sa , 0 , 7 , null , scomparer ) ;
297
- Assert . Equal ( idx , 0 ) ;
238
+ return new [ ]
239
+ {
240
+ new object [ ] { intArray , 8 , intComparer , intGenericComparer , new Func < int , bool > ( i => i == 4 ) } ,
241
+ new object [ ] { intArray , 99 , intComparer , intGenericComparer , new Func < int , bool > ( i => i == ~ ( intArray . Length ) ) } ,
242
+ new object [ ] { intArray , 6 , intComparer , intGenericComparer , new Func < int , bool > ( i => i == 2 || i == 3 ) } ,
243
+ new object [ ] { strArray , "bb" , strComparer , strGenericComparer , new Func < int , bool > ( i => i == 2 || i == 3 ) } ,
244
+ new object [ ] { strArray , null , strComparer , null , new Func < int , bool > ( i => i == 0 ) } ,
245
+ } ;
246
+ }
298
247
}
299
248
300
- [ Fact ]
301
- public static void TestBinarySearch_NoIComparer ( )
249
+ [ Theory , MemberData ( "BinarySearchTestData" ) ]
250
+ public static void TestBinarySearch < T > ( T [ ] array , T value , IComparer comparer , IComparer < T > genericComparer , Func < int , bool > verifier )
302
251
{
303
- int idx ;
304
- int [ ] ia = { 1 , 3 , 6 , 6 , 8 , 10 , 12 , 16 } ;
305
- idx = Array . BinarySearch ( ( Array ) ia , 8 ) ;
306
- Assert . Equal ( idx , 4 ) ;
307
-
308
- //Return value semantics: Quoted from MSDN without comment.
309
- //
310
- //The index of the specified value in the specified array,
311
- //if value is found. If value is not found and value is
312
- //less than one or more elements in array, a negative
313
- //number which is the bitwise complement of the index of
314
- //the first element that is larger than value.
315
- //If value is not found and value is greater than any
316
- //of the elements in array, a negative number which is the bitwise complement of (the index of the last element plus 1).
317
- idx = Array . BinarySearch ( ( Array ) ia , 99 ) ;
318
- Assert . Equal ( idx , ~ ( ia . Length ) ) ;
252
+ int idx = Array . BinarySearch ( ( Array ) array , value , comparer ) ;
253
+ Assert . True ( verifier ( idx ) ) ;
319
254
320
- idx = Array . BinarySearch < int > ( ia , 0 , 8 , 99 ) ;
321
- Assert . Equal ( idx , ~ ( ia . Length ) ) ;
255
+ idx = Array . BinarySearch < T > ( array , value , genericComparer ) ;
256
+ Assert . True ( verifier ( idx ) ) ;
322
257
323
- idx = Array . BinarySearch ( ( Array ) ia , 6 ) ;
324
- Assert . True ( idx == 2 || idx == 3 ) ; // Duplicates are allowed but no promises on which one
258
+ idx = Array . BinarySearch ( ( Array ) array , value ) ;
259
+ Assert . True ( verifier ( idx ) ) ;
325
260
326
- idx = Array . BinarySearch ( ( Array ) ia , 0 , 8 , 6 ) ;
327
- Assert . True ( idx == 2 || idx == 3 ) ; // Duplicates are allowed but no promises on which one
328
-
329
- idx = Array . BinarySearch < int > ( ia , 6 ) ;
330
- Assert . True ( idx == 2 || idx == 3 ) ; // Duplicates are allowed but no promises on which one
331
-
332
- idx = Array . BinarySearch < int > ( ia , 0 , 8 , 6 ) ;
333
- Assert . True ( idx == 2 || idx == 3 ) ; // Duplicates are allowed but no promises on which one
334
-
335
- String [ ] sa = { null , "aa" , "bb" , "bb" , "cc" , "dd" , "ee" } ;
336
- idx = Array . BinarySearch ( ( Array ) sa , "bb" ) ;
337
- Assert . Equal ( idx , 3 ) ;
338
-
339
- idx = Array . BinarySearch < String > ( sa , 0 , sa . Length , "bb" ) ;
340
- Assert . Equal ( idx , 3 ) ;
341
-
342
- idx = Array . BinarySearch ( ( Array ) sa , 3 , 4 , "bb" ) ;
343
- Assert . Equal ( idx , 3 ) ;
261
+ idx = Array . BinarySearch < T > ( array , value ) ;
262
+ Assert . True ( verifier ( idx ) ) ;
263
+ }
344
264
345
- idx = Array . BinarySearch < String > ( sa , 3 , 4 , "bb" ) ;
346
- Assert . Equal ( idx , 3 ) ;
265
+ public static IEnumerable < object [ ] > BinarySearchTestDataInRange
266
+ {
267
+ get
268
+ {
269
+ int [ ] intArray = { 1 , 3 , 6 , 6 , 8 , 10 , 12 , 16 } ;
270
+ IComparer intComparer = new IntegerComparer ( ) ;
271
+ IComparer < int > intGenericComparer = new IntegerComparer ( ) ;
347
272
348
- idx = Array . BinarySearch ( ( Array ) sa , 4 , 3 , "bb" ) ;
349
- Assert . Equal ( idx , - 5 ) ;
273
+ string [ ] strArray = { null , "aa" , "bb" , "bb" , "cc" , "dd" , "ee" } ;
274
+ IComparer strComparer = new StringComparer ( ) ;
275
+ IComparer < string > strGenericComparer = new StringComparer ( ) ;
350
276
351
- idx = Array . BinarySearch < String > ( sa , 4 , 3 , "bb" ) ;
352
- Assert . Equal ( idx , - 5 ) ;
277
+ return new [ ]
278
+ {
279
+ new object [ ] { intArray , 0 , 8 , 99 , intComparer , intGenericComparer , new Func < int , bool > ( i => i == ~ ( intArray . Length ) ) } ,
280
+ new object [ ] { intArray , 0 , 8 , 6 , intComparer , intGenericComparer , new Func < int , bool > ( i => i == 2 || i == 3 ) } ,
281
+ new object [ ] { intArray , 1 , 5 , 16 , intComparer , intGenericComparer , new Func < int , bool > ( i => i == - 7 ) } ,
282
+ new object [ ] { strArray , 0 , strArray . Length , "bb" , strComparer , strGenericComparer , new Func < int , bool > ( i => i == 2 || i == 3 ) } ,
283
+ new object [ ] { strArray , 3 , 4 , "bb" , strComparer , strGenericComparer , new Func < int , bool > ( i => i == 3 ) } ,
284
+ new object [ ] { strArray , 4 , 3 , "bb" , strComparer , strGenericComparer , new Func < int , bool > ( i => i == - 5 ) } ,
285
+ new object [ ] { strArray , 4 , 0 , "bb" , strComparer , strGenericComparer , new Func < int , bool > ( i => i == - 5 ) } ,
286
+ new object [ ] { strArray , 0 , 7 , null , strComparer , null , new Func < int , bool > ( i => i == 0 ) } ,
287
+ } ;
288
+ }
289
+ }
353
290
354
- idx = Array . BinarySearch ( ( Array ) sa , 4 , 0 , "bb" ) ;
355
- Assert . Equal ( idx , - 5 ) ;
291
+ [ Theory , MemberData ( "BinarySearchTestDataInRange" ) ]
292
+ public static void TestBinarySearchInRange < T > ( T [ ] array , int index , int length , T value , IComparer comparer , IComparer < T > genericComparer , Func < int , bool > verifier )
293
+ {
294
+ int idx = Array . BinarySearch ( ( Array ) array , index , length , value , comparer ) ;
295
+ Assert . True ( verifier ( idx ) ) ;
356
296
357
- idx = Array . BinarySearch < String > ( sa , 4 , 0 , "bb" ) ;
358
- Assert . Equal ( idx , - 5 ) ;
297
+ idx = Array . BinarySearch < T > ( array , index , length , value , genericComparer ) ;
298
+ Assert . True ( verifier ( idx ) ) ;
359
299
360
- idx = Array . BinarySearch ( ( Array ) sa , 0 , 7 , null ) ;
361
- Assert . Equal ( idx , 0 ) ;
300
+ idx = Array . BinarySearch ( ( Array ) array , index , length , value ) ;
301
+ Assert . True ( verifier ( idx ) ) ;
362
302
363
- idx = Array . BinarySearch < String > ( sa , 0 , 7 , null ) ;
364
- Assert . Equal ( idx , 0 ) ;
303
+ idx = Array . BinarySearch < T > ( array , index , length , value ) ;
304
+ Assert . True ( verifier ( idx ) ) ;
365
305
}
366
306
367
307
[ Fact ]
0 commit comments