|
| 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 | + |
| 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 | + [Fact] |
| 33 | + public static void BinarySearchTest() |
| 34 | + { |
| 35 | + //list of ints |
| 36 | + 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); |
| 41 | + |
| 42 | + Assert.Equal(expectedIndex, itemIndex); |
| 43 | + |
| 44 | + //list of strings |
| 45 | + 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"); |
| 50 | + |
| 51 | + Assert.Equal(expectedAnimalIndex, actualIndex); |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public static void NullCollectionExceptionTest() |
| 58 | + { |
| 59 | + IList<int> list = null; |
| 60 | + Assert.Throws<System.NullReferenceException>(() => BinarySearcher.BinarySearch<int>(list,0)); |
| 61 | + } |
| 62 | + |
| 63 | + } |
| 64 | +} |
0 commit comments