public void CreateStringTable(int count, int subCount) { Report.BeginTimed("creating string table with {0} items", count); m_stringTable = new string[count]; for (int i = 0; i < count; i++) { m_stringTable[i] = Guid.NewGuid().ToString(); } //+ Guid.NewGuid().ToString() + Guid.NewGuid().ToString() //+ Guid.NewGuid().ToString() + Guid.NewGuid().ToString(); Report.End(); m_symbolTable = new Symbol[count]; Report.BeginTimed("create symbols from strings"); for (int i = 0; i < count; i++) { m_symbolTable[i] = Symbol.Create(m_stringTable[i]); } Report.End(); Report.BeginTimed("create subset"); var rnd = new RandomSystem(13); var perm = rnd.CreatePermutationArray(count); m_stringSubsetTable = new string[subCount]; m_symbolSubsetTable = new Symbol[subCount]; for (int i = 0; i < subCount; i++) { m_stringSubsetTable[i] = m_stringTable[perm[i]]; m_symbolSubsetTable[i] = m_symbolTable[perm[i]]; } Report.End(); }
public void MedianTests(int maxCount) { Test.Begin("median tests"); foreach (var pm in new[] { false, true }) { for (int count = 1; count <= maxCount; count *= 2) { var rnd = new RandomSystem(count); var array = rnd.CreatePermutationArray(count); var a = new int[count]; Test.Begin("{0}quick-median {1} items", pm ? "permutation " : "", count); if (pm) { for (int i = 0; i < a.Length; i++) { a.SetByIndex(pi => pi); a.PermutationQuickMedianAscending(array, i); Test.IsTrue(array[a[i]] == i); } } else { for (int i = 0; i < a.Length; i++) { array.CopyTo(a, 0); a.QuickMedianAscending(i); Test.IsTrue(a[i] == i); } } Test.End(); } } Test.End(); }
public void SortTests(int maxCount) { var sortDict = new Dict <string, Action <int[]> > { { "heap", SortingExtensions.HeapSortAscending }, { "quick", SortingExtensions.QuickSortAscending }, { "smooth", SortingExtensions.SmoothSortAscending }, { "tim", SortingExtensions.TimSortAscending }, }; var permSortDict = new Dict <string, Action <int[], int[]> > { { "heap", SortingExtensions.PermutationHeapSortAscending }, { "quick", SortingExtensions.PermutationQuickSortAscending }, { "smooth", SortingExtensions.PermutationSmoothSortAscending }, { "tim", SortingExtensions.PermutationTimSortAscending }, }; Test.Begin("sort tests"); foreach (var pm in new[] { false, true }) { for (int count = 1; count <= maxCount; count *= 2) { var rnd = new RandomSystem(count); var array = rnd.CreatePermutationArray(count); var a = new int[count]; foreach (var sort in new[] { "heap", "quick", "smooth", "tim" }) { Test.Begin("{0}{1}-sort {2} items", pm ? "permutation " : "", sort, count); if (pm) { a.SetByIndex(pi => pi); permSortDict[sort](a, array); for (int i = 0; i < count; i++) { Test.IsTrue(array[a[i]] == i); } } else { array.CopyTo(a, 0); sortDict[sort](a); for (int i = 0; i < count; i++) { Test.IsTrue(a[i] == i); } } Test.End(); } } } Test.End(); }
public void ListHeapTest(int count, bool print) { Test.Begin("heap size {0}", count); var rnd = new RandomSystem(count); Report.BeginTimed("creating array"); var array = rnd.CreatePermutationArray(count); Report.End(); var heap = new List <int>(); Report.BeginTimed("enqueueing items"); foreach (var item in array) { heap.HeapAscendingEnqueue(item); } Report.End(); Test.Begin("dequeueing items"); var pos = 0; if (print) { while (heap.Count > 0) { var item = heap.HeapAscendingDequeue(); Test.IsTrue(pos == item, "item[{0}] = {1}", pos, item); ++pos; Report.Text("{0,3},", item); if ((pos & 0xf) == 0) { Report.Line(); } } } else { while (heap.Count > 0) { var item = heap.HeapAscendingDequeue(); Test.IsTrue(pos == item, "item[{0}] = {1}", pos, item); ++pos; } } Test.End(); Test.End(); }
public void ListHeapRandomDequeueTest(int count, bool print) { Test.Begin("heap size {0}", count); var rnd = new RandomSystem(count); Report.BeginTimed("creating array"); var array = rnd.CreatePermutationArray(count); Report.End(); var heap = new List <int>(); Report.BeginTimed("enqueueing items"); foreach (var item in array) { heap.HeapAscendingEnqueue(item); } Report.End(); Report.BeginTimed("dequeueing random items"); var last = -1; int removed; for (removed = 0; last != 0; removed++) { last = heap.HeapAscendingRemoveAt(rnd.UniformInt(heap.Count)); } Report.End(": {0}", removed); Test.Begin("dequeueing items"); if (print) { int pos = 0; int old = -1; while (heap.Count > 0) { var item = heap.HeapAscendingDequeue(); Test.IsTrue(old <= item, "wrong order: {0} > {1}", old, item); old = item; Report.Text("{0,3},", item); if ((++pos & 0xf) == 0) { Report.Line(); } } } else { int old = -1; while (heap.Count > 0) { var item = heap.HeapAscendingDequeue(); Test.IsTrue(old <= item, "wrong order: {0} > {1}", old, item); old = item; } } Test.End(); Test.End(); }