public virtual void TestEmptyArraySort() { #if FEATURE_ARRAYEMPTY int[] a = Array.Empty <int>(); #else int[] a = new int[0]; #endif ArrayUtil.IntroSort(a); ArrayUtil.TimSort(a); ArrayUtil.IntroSort(a, Collections.ReverseOrder <int>()); ArrayUtil.TimSort(a, Collections.ReverseOrder <int>()); }
public virtual void TestTimSortStability() { Random rnd = Random; Item[] items = new Item[100]; for (int i = 0; i < items.Length; i++) { // half of the items have value but same order. The value of this items is sorted, // so they should always be in order after sorting. // The other half has defined order, but no (-1) value (they should appear after // all above, when sorted). bool equal = rnd.NextBoolean(); items[i] = new Item(equal ? (i + 1) : -1, equal ? 0 : (rnd.Next(1000) + 1)); } if (VERBOSE) { Console.WriteLine("Before: " + Arrays.ToString(items)); } // if you replace this with ArrayUtil.quickSort(), test should fail: ArrayUtil.TimSort(items); if (VERBOSE) { Console.WriteLine("Sorted: " + Arrays.ToString(items)); } Item last = items[0]; for (int i = 1; i < items.Length; i++) { Item act = items[i]; if (act.Order == 0) { // order of "equal" items should be not mixed up Assert.IsTrue(act.Val > last.Val); } Assert.IsTrue(act.Order >= last.Order); last = act; } }
public virtual void TestTimSort() { int num = AtLeast(50); for (int i = 0; i < num; i++) { int[] a1 = CreateRandomArray(2000), a2 = (int[])a1.Clone(); ArrayUtil.TimSort(a1); Array.Sort(a2); Assert.AreEqual(a2, a1); a1 = CreateRandomArray(2000); a2 = (int[])a1.Clone(); ArrayUtil.TimSort(a1, Collections.ReverseOrder <int>()); Array.Sort(a2, Collections.ReverseOrder <int>()); Assert.AreEqual(a2, a1); // reverse back, so we can test that completely backwards sorted array (worst case) is working: ArrayUtil.TimSort(a1); Array.Sort(a2); Assert.AreEqual(a2, a1); } }