@@ -6,49 +6,76 @@ namespace UnitTest.AlgorithmsTests
6
6
{
7
7
public static class BinarySearcherTest
8
8
{
9
-
10
-
11
9
[ Fact ]
12
- public static void BinarySearchTest ( )
10
+ public static void IntBinarySearchTest ( )
13
11
{
14
12
//list of ints
15
13
IList < int > list = new List < int > { 9 , 3 , 7 , 1 , 6 , 10 } ;
16
14
IList < int > sortedList = new List < int > { 1 , 3 , 6 , 7 , 9 , 10 } ;
17
- BinarySearcher < int > intSearcher = new BinarySearcher < int > ( list , Comparer < int > . Default ) ;
18
15
int numToSearch = 6 ;
19
- int itemIndex = intSearcher . BinarySearch ( numToSearch ) ;
16
+ BinarySearcher < int > intSearcher = new BinarySearcher < int > ( list , Comparer < int > . Default ) ;
17
+ intSearcher . SearchFor ( numToSearch ) ;
18
+ int itemIndex = intSearcher . BinarySearch ( ) ;
20
19
int expectedIndex = sortedList . IndexOf ( numToSearch ) ;
21
20
22
21
Assert . Equal ( expectedIndex , itemIndex ) ;
23
22
Assert . Equal ( numToSearch , intSearcher . Current ) ;
24
23
25
24
numToSearch = 20 ;
26
- int itemNotExists = intSearcher . BinarySearch ( numToSearch ) ;
25
+ intSearcher . SearchFor ( numToSearch ) ;
26
+ int itemNotExists = intSearcher . BinarySearch ( ) ;
27
27
Assert . Equal ( - 1 , itemNotExists ) ;
28
+ }
28
29
29
- intSearcher . Dispose ( ) ;
30
-
30
+ [ Fact ]
31
+ public static void StringBinarySearchTest ( )
32
+ {
31
33
//list of strings
32
- IList < string > animals = new List < string > { "lion" , "cat" , "tiger" , "bee" , "sparrow" } ;
34
+ IList < string > animals = new List < string > { "lion" , "cat" , "tiger" , "bee" , "sparrow" } ;
33
35
IList < string > sortedAnimals = new List < string > { "bee" , "cat" , "lion" , "sparrow" , "tiger" } ;
34
- BinarySearcher < string > strSearcher = new BinarySearcher < string > ( animals , Comparer < string > . Default ) ;
35
36
string itemToSearch = "bee" ;
36
- int actualIndex = strSearcher . BinarySearch ( itemToSearch ) ;
37
+ BinarySearcher < string > strSearcher = new BinarySearcher < string > ( animals , Comparer < string > . Default ) ;
38
+ strSearcher . SearchFor ( itemToSearch ) ;
39
+ int actualIndex = strSearcher . BinarySearch ( ) ;
37
40
int expectedAnimalIndex = sortedAnimals . IndexOf ( itemToSearch ) ;
38
41
39
42
Assert . Equal ( expectedAnimalIndex , actualIndex ) ;
40
43
Assert . Equal ( itemToSearch , strSearcher . Current ) ;
41
44
42
- strSearcher . Dispose ( ) ;
45
+ itemToSearch = "shark" ;
46
+ strSearcher . SearchFor ( itemToSearch ) ;
47
+ int itemNotExists = strSearcher . BinarySearch ( ) ;
48
+ Assert . Equal ( - 1 , itemNotExists ) ;
43
49
}
44
50
51
+ [ Fact ]
52
+ public static void MoveNextTest ( )
53
+ {
54
+ IList < int > items = new List < int > { 3 , 5 , 2 , 6 , 1 , 4 } ;
55
+ BinarySearcher < int > searcher = new BinarySearcher < int > ( items , Comparer < int > . Default ) ;
56
+ searcher . SearchFor ( 1 ) ;
57
+ IList < int > leftEnumeratedValues = new List < int > { 3 , 2 , 1 } ;
58
+ int i = 0 ;
59
+ while ( searcher . MoveNext ( ) )
60
+ {
61
+ Assert . Equal ( leftEnumeratedValues [ i ++ ] , searcher . Current ) ;
62
+ }
63
+
64
+ searcher . SearchFor ( 6 ) ;
65
+ IList < int > rightEnumeratedValues = new List < int > { 3 , 5 , 6 } ;
66
+ i = 0 ;
67
+ while ( searcher . MoveNext ( ) )
68
+ {
69
+ Assert . Equal ( rightEnumeratedValues [ i ++ ] , searcher . Current ) ;
70
+ }
71
+
72
+ }
45
73
46
74
[ Fact ]
47
75
public static void NullCollectionExceptionTest ( )
48
76
{
49
77
IList < int > list = null ;
50
78
Assert . Throws < System . NullReferenceException > ( ( ) => new BinarySearcher < int > ( list , Comparer < int > . Default ) ) ;
51
79
}
52
-
53
80
}
54
- }
81
+ }
0 commit comments