Skip to content

Commit 0408a25

Browse files
committed
Update tests
1 parent 30b7a9d commit 0408a25

File tree

1 file changed

+20
-30
lines changed

1 file changed

+20
-30
lines changed

UnitTest/AlgorithmsTests/BinarySearcherTest.cs

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,56 +8,46 @@ public static class BinarySearcherTest
88
{
99

1010

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-
3211
[Fact]
3312
public static void BinarySearchTest()
3413
{
3514
//list of ints
3615
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);
4121

4222
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();
4330

4431
//list of strings
4532
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);
5038

5139
Assert.Equal(expectedAnimalIndex, actualIndex);
40+
Assert.Equal(itemToSearch, strSearcher.Current);
5241

42+
strSearcher.Dispose();
5343
}
5444

5545

5646
[Fact]
5747
public static void NullCollectionExceptionTest()
5848
{
5949
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));
6151
}
6252

6353
}

0 commit comments

Comments
 (0)