Esempio n. 1
0
        public void TestStableSortCopy_SortRandomList_TinyBuffer_KeepEquivalentItemsOrdered()
        {
            Random random = new Random();

            // build a list
            var list = new List<Tuple<int, int>>(100);
            Sublist.Generate(100, i => Tuple.Create(random.Next(100), i)).AddTo(list.ToSublist());

            // sort the list
            Tuple<int, int>[] buffer = new Tuple<int, int>[3]; // no space to merge - bad for performance!
            var destination = new Tuple<int, int>[100];
            list.ToSublist().StableSort(buffer.ToSublist()).CopyTo(destination.ToSublist());

            // first make sure the list is sorted by the first value
            bool isFirstSorted = destination.ToSublist().IsSorted((t1, t2) => Comparer<int>.Default.Compare(t1.Item1, t2.Item1));
            Assert.IsTrue(isFirstSorted, "The items were not sorted according to the first item.");

            // then make sure the list is sorted by the second value
            // tuples compare by first comparing the first items, then, if necessary, the second items
            bool isSorted = destination.ToSublist().IsSorted();
            Assert.IsTrue(isSorted, "The equivalent items did not remain in the same order.");
        }