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