public void IndexOf_Random() { for (int k = 0; k < 10; k++) { for (int n = 0; n < 10; n++) { Test(n); } for (int n = 1000; n < 1010; n++) { Test(n); } } void Test(int n) { var a = Enumerable.Range(0, n).Select(_ => random.Next(0, n)).OrderBy(x => x).ToArray(); for (int x = -2; x < n + 2; x++) { var expected = Array.BinarySearch(a, x); var actual = SearchSample0.IndexOf(a, x); if (expected >= 0) { Assert.AreEqual(x, a[actual]); Assert.IsTrue(actual == 0 || a[actual - 1] < x); } else { Assert.AreEqual(expected, actual); } } } }
public void IndexOf_Time() { var n = 500000; var a = Enumerable.Range(0, n).Select(_ => random.Next(0, n)).OrderBy(x => x).ToArray(); var l = a.ToList(); TestHelper.MeasureTime(() => { for (int i = 0; i < n; i++) { Array.BinarySearch(a, i); } }); TestHelper.MeasureTime(() => { for (int i = 0; i < n; i++) { l.BinarySearch(i); } }); TestHelper.MeasureTime(() => { for (int i = 0; i < n; i++) { SearchSample0.IndexOf(a, i); } }); TestHelper.MeasureTime(() => { for (int i = 0; i < n; i++) { SearchSample1.IndexOf(a, i); } }); TestHelper.MeasureTime(() => { for (int i = 0; i < n; i++) { SearchSample0.IndexForInsert(a, i); } }); TestHelper.MeasureTime(() => { for (int i = 0; i < n; i++) { SearchSample1.IndexForInsert(a, i); } }); }