public void SearchString_Test() { var data = Encoding.UTF8.GetBytes("В лесу родилась ёлочка, в лесу она росла"); var results = BinarySearcher.Search(new Datagram(data, DataDirection.ClientToServer), "лесу"); Assert.AreEqual(2, results.Count); Assert.AreEqual(2, results.First().Position); Assert.AreEqual(4, results.First().Length); Assert.AreEqual(26, results.Skip(1).First().Position); Assert.AreEqual(4, results.Skip(1).First().Length); results = BinarySearcher.Search(EmptyDatagram, "Test"); Assert.AreEqual(0, results.Count); results = BinarySearcher.Search(new Datagram(Data1, DataDirection.ClientToServer), "LFG"); Assert.AreEqual(3, results.Single().Length); results = BinarySearcher.Search(new Datagram(Data1, DataDirection.ClientToServer), "DPS"); Assert.AreEqual(3, results.Single().Length); }
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 BinarySearcher_Array_3Point7() { double[] array = new double[] { 1, 1.5, 2.7, 2.8, 3.5, 3.7, 15.32 }; int expected = 5; int actual = BinarySearcher <double> .RequiredNumber(array, 3.7, (a, b) => { if (a - b > 0) { return(1); } if (a - b < 0) { return(-1); } return(0); }); Assert.AreEqual(expected, actual); }
static void SearchDemo() { string needle = "ardcara"; string haystack = "abataradabardardcaraadatatabat"; Console.WriteLine(); Console.WriteLine("Needle:"); Console.WriteLine(needle); Console.WriteLine(); Console.WriteLine("Haystack:"); Console.WriteLine(haystack); Console.WriteLine(); IStringSearcher stringSearcher = new BoyerMooreSearcher(); Console.WriteLine("BM found position: {0}", stringSearcher.Search(needle, haystack)); stringSearcher = new BoyerMooreHorspoolSearcher(); Console.WriteLine("BMH found position: {0}", stringSearcher.Search(needle, haystack)); stringSearcher = new KnuthMorrisPrattSearcher(); Console.WriteLine("KMP found position: {0}", stringSearcher.Search(needle, haystack)); Console.WriteLine(); Console.WriteLine("Array:"); int[] arr = { 1, 4, 7, 9, 12, 14, 15, 17, 19, 20, 23, 25, 27, 29, 30, 31, 32, 34, 35, 36, 37, 38, 39 }; int element = 37; ISearcher <int> searcher = new BinarySearcher(); Console.WriteLine("Binary search of element {0}: position {1}", element, searcher.Search(element, arr, arr.Length)); searcher = new InterpolationSearcher(); Console.WriteLine("Interpolation search of element {0}: position {1}", element, searcher.Search(element, arr, arr.Length)); Console.ReadKey(); Console.Clear(); }
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(); }
public void readBinaryData(string fname, out int Height, out int Width, out byte[] stringBytes) { string input = File.ReadAllText(fname); input = input.Replace("\r", ""); string[] StringArray = input.Split(new string[] { "\n" }, StringSplitOptions.None); //Array of all values in the file int mNumberCounter = 0, dimCounter = 0, result; Width = 0; Height = 0; bool result1; stringBytes = null; for (int i = 0; i < StringArray.Length; i++) { //Check to see whether or not the line starts with "# " if (StringArray[i].StartsWith("# ")) { continue; } //This line is not a comment. else if (mNumberCounter == 0) { string[] BrokenUp = StringArray[i].Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); switch (BrokenUp.Length) { case 1: //we have only got the mn mNumberCounter = 1; break; case 2: //we have only got the Width mNumberCounter = 1; result1 = int.TryParse(BrokenUp[1], NumberStyles.Integer, CultureInfo.CurrentCulture, out result); if (result1) { Width = result; dimCounter++; } else { continue; } break; case 3: //we have everything mNumberCounter = 1; result1 = int.TryParse(BrokenUp[1], NumberStyles.Integer, CultureInfo.CurrentCulture, out result); if (result1) { Width = result; dimCounter++; } else { continue; } result1 = int.TryParse(BrokenUp[2], NumberStyles.Integer, CultureInfo.CurrentCulture, out result); if (result1) { Height = result; dimCounter++; } else { continue; } break; } } else if (dimCounter == 0) { string[] BrokenUp = StringArray[i].Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); switch (BrokenUp.Length) { case 1: //we have only got the Width result1 = int.TryParse(BrokenUp[0], NumberStyles.Integer, CultureInfo.CurrentCulture, out result); if (result1) { Width = result; dimCounter++; } else { continue; } break; case 2: //we have only got the Height mNumberCounter = 1; result1 = int.TryParse(BrokenUp[0], NumberStyles.Integer, CultureInfo.CurrentCulture, out result); if (result1) { Width = result; dimCounter++; } else { continue; } result1 = int.TryParse(BrokenUp[1], NumberStyles.Integer, CultureInfo.CurrentCulture, out result); if (result1) { Height = result; dimCounter++; } else { continue; } break; } } else if (dimCounter == 1)//Width and Height will be found hopefully { string[] BrokenUp = StringArray[i].Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); result1 = int.TryParse(BrokenUp[0], NumberStyles.Integer, CultureInfo.CurrentCulture, out result); if (result1) { Height = result; dimCounter++; } else { continue; } } else //take it that this is the start of the pixel data. { int nCount = i;//This is the number of \n's that we've encountered. BinarySearcher b = new BinarySearcher(); FileStream fs = new FileStream(fname, FileMode.Open, FileAccess.Read); int[] offsets = b.SearchToArray(0x0a, fname, nCount); int stride = ((Width * ((1 + 7) / 8)) + 4 - ((Width * ((1 + 7) / 8)) % 4)); int X = 0; using (BinaryReader e = new BinaryReader(fs)) { e.BaseStream.Position = offsets[nCount - 1] + 1; long bufferSize = e.BaseStream.Length; stringBytes = e.ReadBytes((int)bufferSize - offsets[nCount - 1] + ((stride - Width) * Height)); stringBytes = ConvertBytes(stringBytes); } break; } } }
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));
public void BinarySearcher_Array_Exception1() => BinarySearcher <int> .RequiredNumber(null, 1, compar);
public void TestEmptyList() { var searcher = new BinarySearcher(); var output = searcher.Search(new List<int> { }, 1); Assert.IsFalse(output); }
public void BinarySearcher_Array_Exception4() => BinarySearcher <int> .RequiredNumber(new int[] { 1, 2, 3 }, 5, compar);
public int?BinarySearch_PositivTestInt(int[] array, int element) { BinarySearcher <int> binarySearcher = new BinarySearcher <int>(new ComparerInt()); return(binarySearcher.BinarySearch(array, element)); }
public void BinarySearcher_Array_Exception2() => BinarySearcher <int> .RequiredNumber(new int[] { }, 1, compar);
private static int BinarySearch() { Console.WriteLine(BinarySearcher.Search(new int[] { 1, 2, 4, 5, 6, 7, 8, 11, 15, 20, 25 }, 25).ToString()); return(0); }
public void SeacherTestsInterface <T>(T[] collection, T item, int index) { int result = BinarySearcher <T> .Search(collection, item, (IComparator <T>) null); Assert.AreEqual(index, result); }
public void BinarySearchTest(int[] sorted, int sort, int expected, int found, BinarySearcher binarySearcher) { "Given a sorted array" .x(() => { }); "And a BinarySearch" .x(() => { binarySearcher = new BinarySearcher(); }); "When I search for" .x(() => { found = binarySearcher.Search(sorted, sort); }); "Then it is sorted as I expect" .x(() => { Xunit.Assert.Equal(expected, found); }); }
public void SeacherTestsDelegate <T>(T[] collection, T item, int index) { int result = BinarySearcher <T> .Search(collection, item, (a, b) => ((IComparable)a).CompareTo(b)); Assert.AreEqual(index, result); }
public void BinarySearcher_Array_Exception3() => BinarySearcher <int> .RequiredNumber(new int[] { 1, 2, 3 }, 1, null);
public int?BinarySearch_PositivTestString(string[] array, string element) { BinarySearcher <string> binarySearcher = new BinarySearcher <string>(new ComparerString()); return(binarySearcher.BinarySearch(array, element)); }
public void Search_NullComparer_ArgumentNullException() => Assert.Catch <ArgumentNullException>(() => BinarySearcher.Search(new int[] { 1, 2 }, 1, null));
public void TestNullList() { var searcher = new BinarySearcher(); var output = searcher.Search(null, 1); Assert.IsFalse(output); }
public int Search_IntData_CorrectResult(int value, params int[] array) { return(BinarySearcher.Search(array, value, new IntComparer())); }
public void TestWhenElementPresent() { var searcher = new BinarySearcher(); var output = searcher.Search(new List<int> { 3, 2, 1, 9, 7 }, 1); Assert.IsTrue(output); }
public void Setup() { _binarySearcher = new BinarySearcher <int>(); _arr = null; }