private static void TestSimpleTextCounts(IPrefixCounter<char[]> counter, int testCaseId) { int[] expectedCount; var s = TestCases.GetSimpleCase(testCaseId, out expectedCount); TestSimpleTextCounts(counter, s, expectedCount); }
public static double TimePrefixCounting(IPrefixCounter<char[]> counter, char[] s, out int[] count) { var stopwatch = new Stopwatch(); stopwatch.Start(); count = counter.CountPrefixes(s); stopwatch.Stop(); return stopwatch.Elapsed.TotalSeconds; }
// I realize this is a silly unit test, and performance will vary // by hardware. However, both algorithms should be able to pass // this test easily on modest hardware. public static void TestAverageCountTime(IPrefixCounter<char[]> counter) { const int runs = 100; // Keep runs small so unit tests don't run long const int chars = 300000; // Either algorithm hould be able to process const double maxAvg = 0.1; // 300k characters in a 10th of a second easily. const double macheps = Double.Epsilon; var avgTime = TimeAverageCount(counter, runs, chars); Assert.IsTrue((maxAvg - avgTime) >= macheps); }
private static void TestSmallCyclicTextCounts(IPrefixCounter<char[]> counter) { // These counts were pre-computed by Z and verified // with KMP for s = TestCases.SmallCyclicTestCase // // They can be trusted as reliable because these algorithms // were also used to pass a competitive programming problem. var s = TestCases.SmallCyclicTestCase; var expected = TestCases.SmallCyclicExpectedCounts; TestSimpleTextCounts(counter, s, expected); }
private static void TestTinyTextCounts(IPrefixCounter<char[]> counter) { const string s1 = "a"; const string s2 = "ab"; const string s3 = "aa"; var expectedCounts1 = new[] { 0, 1 }; var expectedCounts2 = new[] { 0, 1, 1 }; var expectedCounts3 = new[] { 0, 2, 1 }; TestSimpleTextCounts(counter, s1.Chars(), expectedCounts1); TestSimpleTextCounts(counter, s2.Chars(), expectedCounts2); TestSimpleTextCounts(counter, s3.Chars(), expectedCounts3); }
private static void TestSimpleTextCounts(IPrefixCounter<char[]> counter, char[] s, int[] expectedCounts) { var n = s.Length; if (expectedCounts.Length != n + 1) { throw new ArgumentException("expectedCounts.Length != s.Length + 1"); } var prefixCounts = counter.CountPrefixes(s); Assert.AreEqual(n + 1, prefixCounts.Length); for (int i = 0; i < n; i++) { Assert.AreEqual(expectedCounts[i], prefixCounts[i]); } }
public static double TimeAverageCount(IPrefixCounter<char[]> counter, int runs, int chars) { if (runs < 100) { throw new ArgumentOutOfRangeException("runs < 100 will produce unreliable results."); } if (chars < 1) { throw new ArgumentOutOfRangeException("chars < 1"); } double seconds = 0.0; foreach (var s in TestCases.RandomCharArrays(runs, chars)) { int[] count; seconds += TimePrefixCounting(counter, s, out count); } return seconds / runs; }