Skip to content

Commit 30ea305

Browse files
committed
Introduce test for the enumerator
1 parent 936fb30 commit 30ea305

File tree

1 file changed

+41
-14
lines changed

1 file changed

+41
-14
lines changed

UnitTest/AlgorithmsTests/BinarySearcherTest.cs

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,76 @@ namespace UnitTest.AlgorithmsTests
66
{
77
public static class BinarySearcherTest
88
{
9-
10-
119
[Fact]
12-
public static void BinarySearchTest()
10+
public static void IntBinarySearchTest()
1311
{
1412
//list of ints
1513
IList<int> list = new List<int> { 9, 3, 7, 1, 6, 10 };
1614
IList<int> sortedList = new List<int> { 1, 3, 6, 7, 9, 10 };
17-
BinarySearcher<int> intSearcher = new BinarySearcher<int>(list, Comparer<int>.Default);
1815
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();
2019
int expectedIndex = sortedList.IndexOf(numToSearch);
2120

2221
Assert.Equal(expectedIndex, itemIndex);
2322
Assert.Equal(numToSearch, intSearcher.Current);
2423

2524
numToSearch = 20;
26-
int itemNotExists = intSearcher.BinarySearch(numToSearch);
25+
intSearcher.SearchFor(numToSearch);
26+
int itemNotExists = intSearcher.BinarySearch();
2727
Assert.Equal(-1, itemNotExists);
28+
}
2829

29-
intSearcher.Dispose();
30-
30+
[Fact]
31+
public static void StringBinarySearchTest()
32+
{
3133
//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" };
3335
IList<string> sortedAnimals = new List<string> { "bee", "cat", "lion", "sparrow", "tiger" };
34-
BinarySearcher<string> strSearcher = new BinarySearcher<string>(animals, Comparer<string>.Default);
3536
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();
3740
int expectedAnimalIndex = sortedAnimals.IndexOf(itemToSearch);
3841

3942
Assert.Equal(expectedAnimalIndex, actualIndex);
4043
Assert.Equal(itemToSearch, strSearcher.Current);
4144

42-
strSearcher.Dispose();
45+
itemToSearch = "shark";
46+
strSearcher.SearchFor(itemToSearch);
47+
int itemNotExists = strSearcher.BinarySearch();
48+
Assert.Equal(-1, itemNotExists);
4349
}
4450

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+
}
4573

4674
[Fact]
4775
public static void NullCollectionExceptionTest()
4876
{
4977
IList<int> list = null;
5078
Assert.Throws<System.NullReferenceException>(() => new BinarySearcher<int>(list, Comparer<int>.Default));
5179
}
52-
5380
}
54-
}
81+
}

0 commit comments

Comments
 (0)