@@ -8,56 +8,46 @@ public static class BinarySearcherTest
8
8
{
9
9
10
10
11
- [ Fact ]
12
- public static void MergeSortTest ( )
13
- {
14
- //a list of int
15
- IList < int > list = new List < int > { 9 , 3 , 7 , 1 , 6 , 10 } ;
16
-
17
- IList < int > sortedList = BinarySearcher . MergeSort < int > ( list ) ;
18
- IList < int > expectedList = new List < int > { 1 , 3 , 6 , 7 , 9 , 10 } ;
19
-
20
- Assert . Equal ( expectedList , sortedList ) ;
21
-
22
- //a list of strings
23
- IList < string > animals = new List < string > { "lion" , "cat" , "tiger" , "bee" } ;
24
- IList < string > sortedAnimals = BinarySearcher . MergeSort < string > ( animals ) ;
25
- IList < string > expectedAnimals = new List < string > { "bee" , "cat" , "lion" , "tiger" } ;
26
-
27
- Assert . Equal ( expectedAnimals , sortedAnimals ) ;
28
-
29
- }
30
-
31
-
32
11
[ Fact ]
33
12
public static void BinarySearchTest ( )
34
13
{
35
14
//list of ints
36
15
IList < int > list = new List < int > { 9 , 3 , 7 , 1 , 6 , 10 } ;
37
- IList < int > sortedList = BinarySearcher . MergeSort < int > ( list ) ;
38
-
39
- int itemIndex = BinarySearcher . BinarySearch < int > ( list , 6 ) ;
40
- int expectedIndex = sortedList . IndexOf ( 6 ) ;
16
+ IList < int > sortedList = new List < int > { 1 , 3 , 6 , 7 , 9 , 10 } ;
17
+ BinarySearcher < int > intSearcher = new BinarySearcher < int > ( list , Comparer < int > . Default ) ;
18
+ int numToSearch = 6 ;
19
+ int itemIndex = intSearcher . BinarySearch ( numToSearch ) ;
20
+ int expectedIndex = sortedList . IndexOf ( numToSearch ) ;
41
21
42
22
Assert . Equal ( expectedIndex , itemIndex ) ;
23
+ Assert . Equal ( numToSearch , intSearcher . Current ) ;
24
+
25
+ numToSearch = 20 ;
26
+ int itemNotExists = intSearcher . BinarySearch ( numToSearch ) ;
27
+ Assert . Equal ( - 1 , itemNotExists ) ;
28
+
29
+ intSearcher . Dispose ( ) ;
43
30
44
31
//list of strings
45
32
IList < string > animals = new List < string > { "lion" , "cat" , "tiger" , "bee" , "sparrow" } ;
46
- IList < string > sortedAnimals = BinarySearcher . MergeSort < string > ( animals ) ;
47
-
48
- int actualIndex = BinarySearcher . BinarySearch < string > ( animals , "cat" ) ;
49
- int expectedAnimalIndex = sortedAnimals . IndexOf ( "cat" ) ;
33
+ IList < string > sortedAnimals = new List < string > { "bee" , "cat" , "lion" , "sparrow" , "tiger" } ;
34
+ BinarySearcher < string > strSearcher = new BinarySearcher < string > ( animals , Comparer < string > . Default ) ;
35
+ string itemToSearch = "bee" ;
36
+ int actualIndex = strSearcher . BinarySearch ( itemToSearch ) ;
37
+ int expectedAnimalIndex = sortedAnimals . IndexOf ( itemToSearch ) ;
50
38
51
39
Assert . Equal ( expectedAnimalIndex , actualIndex ) ;
40
+ Assert . Equal ( itemToSearch , strSearcher . Current ) ;
52
41
42
+ strSearcher . Dispose ( ) ;
53
43
}
54
44
55
45
56
46
[ Fact ]
57
47
public static void NullCollectionExceptionTest ( )
58
48
{
59
49
IList < int > list = null ;
60
- Assert . Throws < System . NullReferenceException > ( ( ) => BinarySearcher . BinarySearch < int > ( list , 0 ) ) ;
50
+ Assert . Throws < System . NullReferenceException > ( ( ) => new BinarySearcher < int > ( list , Comparer < int > . Default ) ) ;
61
51
}
62
52
63
53
}
0 commit comments