public static void MoveNextTest() { IList <int> items = new List <int> { 3, 5, 2, 6, 1, 4 }; BinarySearcher <int> searcher = new BinarySearcher <int>(items, Comparer <int> .Default); searcher.BinarySearch(1); //reset indices to test MoveNext() searcher.Reset(); IList <int> leftEnumeratedValues = new List <int> { 3, 2, 1 }; int i = 0; while (searcher.MoveNext()) { Assert.AreEqual(leftEnumeratedValues[i++], searcher.Current); } searcher.BinarySearch(6); //reset indices to test MoveNext() searcher.Reset(); IList <int> rightEnumeratedValues = new List <int> { 3, 5, 6 }; i = 0; while (searcher.MoveNext()) { Assert.AreEqual(rightEnumeratedValues[i++], searcher.Current); } }
public void Test1() { IList <int> items = new List <int> { 1, 2, 3, 4, 5, 6, 7 }; BinarySearcher <int> binarySearcher = new BinarySearcher <int>(items, Comparer <int> .Default); var x = binarySearcher.BinarySearch(2); Assert.Equal(1, x); }
public void BinarySearcher_BinarySearch_Valid_Search_Must_Return_Index() { // Arrange int[] array = { 3, 6, 8, 9, 12, 45, 89, 112 }; // Act var index = BinarySearcher.BinarySearch(array, 45); // Assert Assert.AreEqual(5, index); }
public void BinarySearchTest() { var inputList = new List <int> { 1, 2, 3, 4, 5 }; inputList = BinarySearcher.SortList(inputList); var numberToSearch = 3; var actual = BinarySearcher.BinarySearch(inputList, numberToSearch); var expected = 3; Assert.AreEqual(expected, actual, "{0} != {1}", expected, actual); }
public static void StringBinarySearchTest() { //list of strings IList <string> animals = new List <string> { "lion", "cat", "tiger", "bee", "sparrow" }; IList <string> sortedAnimals = new List <string> { "bee", "cat", "lion", "sparrow", "tiger" }; string itemToSearch = "bee"; BinarySearcher <string> strSearcher = new BinarySearcher <string>(animals, Comparer <string> .Default); int actualIndex = strSearcher.BinarySearch(itemToSearch); int expectedAnimalIndex = sortedAnimals.IndexOf(itemToSearch); Assert.AreEqual(expectedAnimalIndex, actualIndex); Assert.AreEqual(itemToSearch, strSearcher.Current); itemToSearch = "shark"; int itemNotExist = strSearcher.BinarySearch(itemToSearch); Assert.AreEqual(-1, itemNotExist); }
public void IntBinarySearchTest() { //list of ints IList <int> list = new List <int> { 9, 3, 7, 1, 6, 10 }; IList <int> sortedList = new List <int> { 1, 3, 6, 7, 9, 10 }; int numToSearch = 6; BinarySearcher <int> intSearcher = new BinarySearcher <int>(list, Comparer <int> .Default); int actualIndex = intSearcher.BinarySearch(numToSearch); int expectedIndex = sortedList.IndexOf(numToSearch); Assert.AreEqual(expectedIndex, actualIndex); Assert.AreEqual(numToSearch, intSearcher.Current); numToSearch = 20; int itemNotExists = intSearcher.BinarySearch(numToSearch); Assert.AreEqual(-1, itemNotExists); }
static void Main(string[] args) { int[] result = FibonacciNumbersGenerator.Generate(15); foreach (int value in result) { Console.WriteLine(value); } _intArray = new int[6] { 1, 4, 7, 9, 11, 13 }; _intSearcher = new BinarySearcher <int>(_intArray); for (int i = 0; i < _intArray.Length; i++) { Console.WriteLine(_intSearcher.BinarySearch(_intArray[i])); } Console.WriteLine(_intSearcher.BinarySearch(5)); Console.ReadLine(); }
private int GetInsertIndex3(int to, T value) { bool found; int pos; BinarySearcher.BinarySearch(A, 0, to, value, out found, out pos); if (found) { return(pos + 1); } else { return(pos); } }
public int StringSearchMethodTest(string value) { return(_stringSearcher.BinarySearch(value)); }
public int IntSearchMethodTest(int value) { return(_intSearcher.BinarySearch(value)); }
public int?BinarySearch_PositivTestString(string[] array, string element) { BinarySearcher <string> binarySearcher = new BinarySearcher <string>(new ComparerString()); return(binarySearcher.BinarySearch(array, element)); }
public int?BinarySearch_PositivTestInt(int[] array, int element) { BinarySearcher <int> binarySearcher = new BinarySearcher <int>(new ComparerInt()); return(binarySearcher.BinarySearch(array, element)); }
public void BinarySearch_NullArgument_ArgumentNullException() => Assert.Throws <ArgumentNullException>(() => BinarySearcher <int> .BinarySearch(null, 3));
public int?BinarySearch_PassedArgumentsIndex_CorrectResults(int[] array, int key) => BinarySearcher <int> .BinarySearch(array, 1, 4, key);
public void BinarySearch_ZeroArrayLength_ArgumentException() => Assert.Throws <ArgumentException>(() => BinarySearcher <int> .BinarySearch(new int[] { }, 3));