Example #1
0
        public void TestHeap_PriorityQueue()
        {
            Random random = new Random();

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

            // make a heap
            list.ToSublist().MakeHeap().InPlace();
            Assert.IsTrue(list.ToSublist().IsHeap(), "The list is not a heap."); // confirm we have a heap

            // let's push a value onto the heap and make it the highest priority
            list.Add(100);
            list.ToSublist().HeapAdd();
            Assert.AreEqual(100, list[0], "The value was not moved to the top of the heap.");
            Assert.IsTrue(list.ToSublist().IsHeap(), "The list is not a heap.");

            // now let's remove it
            list.ToSublist().HeapRemove();
            Assert.AreEqual(100, list[list.Count - 1], "The value not moved to the bottom of the heap.");
            Assert.AreEqual(list.Count - 1, list.ToSublist().IsHeap(), "Could not find the end of the heap.");
            list.RemoveAt(list.Count - 1);
            Assert.IsTrue(list.ToSublist().IsHeap(), "The list is not a heap.");

            // we can sort a heap
            list.ToSublist().HeapSort();
            Assert.IsTrue(list.ToSublist().IsSorted(), "The list was not sorted.");
        }
        public void TestSymmetricExceptAdd_FindUniqueAcrossLists()
        {
            Random random = new Random();

            // build two lists
            var list1 = new List<int>(100);
            Sublist.Generate(100, i => random.Next(100)).AddTo(list1.ToSublist());
            var list2 = new List<int>(100);
            Sublist.Generate(100, i => random.Next(100)).AddTo(list2.ToSublist());

            // make the lists sets
            list1.ToSublist(list1.ToSublist().MakeSet().InPlace()).Clear();
            list2.ToSublist(list2.ToSublist().MakeSet().InPlace()).Clear();

            // find the unique values
            var difference = new List<int>();
            list1.ToSublist().SymmetricExcept(list2.ToSublist()).AddTo(difference.ToSublist());

            // this is the opposite of the intersection, so they should share no items
            var intersection = new List<int>();
            list1.ToSublist().Intersect(list2.ToSublist()).AddTo(intersection.ToSublist());

            bool result = intersection.ToSublist().FindAny(difference.ToSublist());
            Assert.IsFalse(result, "Found items in common in the intersection and symmetric difference.");
        }
Example #3
0
        public void TestIntersectAdd_FindNumbersDivisibleByTwoAndThree()
        {
            Random random = new Random();

            // build all multiples of two
            var list1 = new List<int>(50);
            Sublist.Generate(50, i => random.Next(50) * 2).AddTo(list1.ToSublist());

            // build all multiples of three
            var list2 = new List<int>(33);
            Sublist.Generate(33, i => random.Next(33) * 3).AddTo(list2.ToSublist());

            // make sets
            list1.ToSublist(list1.ToSublist().MakeSet().InPlace()).Clear();
            list2.ToSublist(list2.ToSublist().MakeSet().InPlace()).Clear();

            // find the intersection
            var destination = new List<int>();
            list1.ToSublist().Intersect(list2.ToSublist()).AddTo(destination.ToSublist());

            // make sure all values are divisible by two and three
            bool result = destination.ToSublist().Find(i => i % 2 != 0 || i % 3 != 0);
            Assert.IsFalse(result, "Some of the items didn't meet the criteria.");

            // the result should be all multiple of six
            var expected = new List<int>(17); // space for zero
            Sublist.Generate(17, i => i * 6).AddTo(expected.ToSublist());
            bool containsAll = destination.ToSublist().IsSubset(expected.ToSublist());
            Assert.IsTrue(containsAll, "Some of the items weren't multiples of six.");
        }
Example #4
0
        public void TestRotateLeftAdd()
        {
            Random random = new Random();

            // build list of numbers that divide evenly into 100 and pick one
            var divisors = new List<int>(Enumerable.Range(1, 50).Where(i => 100 % i == 0));
            var samples = new List<int>(1);
            divisors.ToSublist().RandomSamples(1, random).AddTo(samples.ToSublist());
            int repeat = samples[0];

            // build a list with the numbers 0-4 reoccurring
            var list = new List<int>(100);
            Sublist.Generate(100, i => i % repeat).AddTo(list.ToSublist());

            // try different shifts, looking for reoccurrences
            int shift = 1;
            while (shift != 100)
            {
                var rotated = new List<int>(list.Count);
                list.ToSublist().RotateLeft(shift).AddTo(rotated.ToSublist());
                if (list.ToSublist().IsEqualTo(rotated.ToSublist()))
                {
                    break;
                }
                ++shift;
            }
            Assert.AreEqual(repeat, shift, "Did not detect the reoccurrence where expected.");
        }
Example #5
0
        public void TestCompareTo_ItemByItemComparison()
        {
            Random random = new Random();

            var list1 = new List<int>(10);
            Sublist.Generate(10, i => random.Next(0, 10)).AddTo(list1.ToSublist());

            var list2 = new List<int>(10);
            Sublist.Generate(10, i => random.Next(0, 10)).AddTo(list2.ToSublist());

            // we know list1 and list2 should be equal to themselves
            Assert.AreEqual(0, list1.ToSublist().CompareTo(list1.ToSublist()), "The first list did not equal itself.");
            Assert.AreEqual(0, list2.ToSublist().CompareTo(list2.ToSublist()), "The second list did not equal itself.");

            // we can use mismatch to confirm that Comparer returned the correct value
            int result = list1.ToSublist().CompareTo(list2.ToSublist());
            int difference = list1.ToSublist().Mismatch(list2.ToSublist());
            if (difference == list1.Count)
            {
                Assert.AreEqual(0, result, "Mismatch found no differences, but CompareTo did not return zero.");
            }
            else
            {
                int first = list1[difference];
                int second = list2[difference];
                int expected = Comparer<int>.Default.Compare(first, second);
                Assert.AreEqual(expected, result, "The mismatching items did not compare the same as the result of CompareTo.");
            }
        }
Example #6
0
        public void TestPartitionAdd_BreakApartEvensAndOdds()
        {
            Random random = new Random();

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

            // partition into two
            var evens = new List<int>();
            var odds = new List<int>();
            list.ToSublist().Partition(i => i % 2 == 0).AddTo(evens.ToSublist(), odds.ToSublist());

            // sort all three lists -- we need to check if all values were added
            list.ToSublist().Sort().InPlace();
            evens.ToSublist().Sort().InPlace();
            odds.ToSublist().Sort().InPlace();

            bool hasOdd = evens.ToSublist().Find(i => i % 2 != 0);
            Assert.IsFalse(hasOdd, "Some odds were added to the wrong list.");

            bool hasEven = odds.ToSublist().Find(i => i % 2 == 0);
            Assert.IsFalse(hasEven, "Some evens were added to the wrong list.");

            var combined = new List<int>(100);
            evens.ToSublist().AddTo(combined.ToSublist());
            odds.ToSublist().AddTo(combined.ToSublist());
            combined.ToSublist().Sort().InPlace();
            bool hasAllItems = list.ToSublist().IsEqualTo(combined.ToSublist());
            Assert.IsTrue(hasAllItems, "Not all items were partitioned.");
        }
Example #7
0
        public void TestUnionCopy()
        {
            Random random = new Random();

            // build two lists
            var list1 = new List<int>(50);
            Sublist.Generate(50, i => random.Next(100)).AddTo(list1.ToSublist());
            var list2 = new List<int>(50);
            Sublist.Generate(50, i => random.Next(100)).AddTo(list2.ToSublist());

            // we must make both lists sets
            list1.ToSublist(list1.ToSublist().MakeSet().InPlace()).Clear();
            list2.ToSublist(list2.ToSublist().MakeSet().InPlace()).Clear();

            // now we'll build a new set containing all the items
            var destination = new List<int>(100);
            Sublist.Generate(100, 0).AddTo(destination.ToSublist());
            int result = list1.ToSublist().Union(list2.ToSublist()).CopyTo(destination.ToSublist());
            destination.ToSublist(result).Clear();

            // make sure the new set contains both of the original sets
            bool contains1 = list1.ToSublist().IsSubset(destination.ToSublist());
            Assert.IsTrue(contains1, "The union did not contain all the items from the first set.");
            bool contains2 = list2.ToSublist().IsSubset(destination.ToSublist());
            Assert.IsTrue(contains1, "The union did not contain all the items from the second set.");
        }
Example #8
0
        public void TestZipCopy_MultiplyTwoLists()
        {
            Random random = new Random();

            // build the first list
            var list1 = new List<int>(100);
            Sublist.Generate(100, i => random.Next(100)).AddTo(list1.ToSublist());

            // build the second list
            var list2 = new List<int>(100);
            Sublist.Generate(100, i => random.Next(100)).AddTo(list2.ToSublist());

            var destination = new List<int>(100);
            Sublist.Generate(100, 0).AddTo(destination.ToSublist());

            // multiply the values at each index together
            int destinationIndex = list1.ToSublist().Zip(list2.ToSublist(), (i, j) => i * j).CopyTo(destination.ToSublist());
            Assert.AreEqual(destination.Count, destinationIndex, "Not all the values were multiplied.");

            // check that each value in the destination is the product
            for (int index = 0; index != destination.Count; ++index)
            {
                int product = list1[index] * list2[index];
                Assert.AreEqual(product, destination[index], "The destination did not hold the product at index = {0}.", index);
            }
        }
Example #9
0
        public void TestUpperBound_BuildSet()
        {
            Random random = new Random();

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

            // only add unique items in sorted order
            var set = new List<int>();
            foreach (int value in list)
            {
                int index = set.ToSublist().UpperBound(value);
                if (index == 0 || set[index - 1] != value)
                {
                    set.Insert(index, value);
                }
            }

            // check that all items are present, sorted and unique
            list.ToSublist().Sort().InPlace();
            Assert.IsTrue(set.ToSublist().IsSorted(), "The set is not sorted.");
            bool hasValues = set.ToSublist().IsSubset(list.ToSublist());
            Assert.IsTrue(hasValues, "Not all of the values were copied.");
            Assert.IsFalse(set.ToSublist().FindDuplicates(), "A duplicate was found.");
        }
Example #10
0
        public void TestPartialSortAdd_GrabTopValues()
        {
            Random random = new Random();

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

            // create a list to hold the top three
            const int numberOfItems = 10;
            var destination = new List<int>(numberOfItems);

            // store the top three results
            Func<int, int, int> comparison = (x, y) => Comparer<int>.Default.Compare(y, x);
            list.ToSublist().PartialSort(numberOfItems, comparison).AddTo(destination.ToSublist());

            // grab the three largest values from largest to smallest
            var expected = new List<int>(numberOfItems);
            for (int round = 0; round != numberOfItems; ++round)
            {
                int maxIndex = list.ToSublist().Maximum();
                expected.Add(list[maxIndex]);
                list.RemoveAt(maxIndex);
            }

            Assert.IsTrue(expected.ToSublist().IsEqualTo(destination.ToSublist()), "The top values weren't grabbed.");
        }
Example #11
0
 public void TestGenerateAdd_Counting()
 {
     List<int> values = new List<int>();
     Sublist.Generate(10, i => i).AddTo(values.ToSublist());
     int[] expected = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
     Assert.IsTrue(expected.ToSublist().IsEqualTo(values.ToSublist()), "The items were not set as expected.");
 }
Example #12
0
        public void TestMergeCopy_CombineSortedLists_StaySorted()
        {
            Random random = new Random();

            // builds the first list
            var list1 = new List<int>(50);
            Sublist.Generate(50, i => random.Next(100)).AddTo(list1.ToSublist());

            // builds the second list
            var list2 = new List<int>(50);
            Sublist.Generate(50, i => random.Next(100)).AddTo(list2.ToSublist());

            // merging requires sorted lists
            list1.ToSublist().Sort().InPlace();
            list2.ToSublist().Sort().InPlace();

            // merge the lists
            var destination = new List<int>(100);
            Sublist.Generate(100, 0).AddTo(destination.ToSublist());
            int result = list1.ToSublist().Merge(list2.ToSublist()).CopyTo(destination.ToSublist());
            Assert.AreEqual(destination.Count, result, "Not all of the items were copied.");

            // make sure the destination is still sorted
            bool isSorted = destination.ToSublist().IsSorted();
            Assert.IsTrue(isSorted, "Merge caused the items to become unsorted.");
        }
Example #13
0
        public void TestReverseAdd_InefficientPalindromeChecker()
        {
            var list = new List<int>() { 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1, };

            var copy = new List<int>(list.Count);

            list.ToSublist().Reverse().AddTo(copy.ToSublist());

            Assert.IsTrue(list.ToSublist().IsEqualTo(copy.ToSublist()), "The list was not reversed as expected.");
        }
Example #14
0
        public void TestWhereAdd_CopyEvenItems()
        {
            var list = new List<int>() { 1, 2, 3, 4, 5, 6 };
            var destination = new List<int>();

            // only keep the even items
            list.ToSublist().Where(item => item % 2 == 0).AddTo(destination.ToSublist());

            int[] expected = { 2, 4, 6 };
            Assert.IsTrue(expected.ToSublist().IsEqualTo(destination.ToSublist()), "The items were not where they were expected.");
        }
Example #15
0
 public void TestAddTo_IEnumerableSource_MiddleOfList_AddsItemsToBackOfList()
 {
     var source = Enumerable.Range(3, 2);
     var list = new List<int>() { 1, 2, 5, 6 };
     var destination = list.ToSublist(2, 0);
     destination = source.AddTo(destination);
     var expected = new List<int>() { 1, 2, 3, 4, 5, 6 }.ToSublist();
     Assert.AreEqual(6, list.Count, "The items were not added to the original list.");
     Assert.AreEqual(2, destination.Count, "The size of the sublist was not adjusted.");
     Assert.IsTrue(expected.IsEqualTo(list.ToSublist()), "The items were not added as expected.");
 }
Example #16
0
        public void TestGenerateAdd_Defaulting()
        {
            List<DateTime> values = new List<DateTime>(); // defaults to 01/01/0001
            DateTime defaultDate = DateTime.Today;
            Sublist.Generate(10, defaultDate).AddTo(values.ToSublist()); // change default to today

            DateTime[] expected = new DateTime[10];
            expected.ToSublist().Select(i => defaultDate).CopyTo(expected.ToSublist()); // replace all with default

            Assert.IsTrue(expected.ToSublist().IsEqualTo(values.ToSublist()), "The items were not set as expected.");
        }
Example #17
0
        public void TestReverseCopy_InefficientPalindromeChecker()
        {
            var list = new List<int>() { 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1, };

            var copy = new List<int>(list.Count);
            Sublist.Generate(list.Count, 0).AddTo(copy.ToSublist());

            int result = list.ToSublist().Reverse().CopyTo(copy.ToSublist());
            Assert.AreEqual(copy.Count, result, "The wrong index was returned.");

            Assert.IsTrue(list.ToSublist().IsEqualTo(copy.ToSublist()), "The list was not reversed as expected.");
        }
Example #18
0
        public void TestReplaceInPlace_AbsoluteValue()
        {
            Random random = new Random();

            // build a list of numbers
            var list = new List<int>(100);
            Sublist.Generate(100, i => random.Next(-49, 50)).AddTo(list.ToSublist());

            list.ToSublist().Replace(i => i < 0, i => -i).InPlace();

            Assert.IsFalse(list.ToSublist().Find(i => i < 0), "Not all values were positive.");
        }
Example #19
0
        public void TestMakeSetInPlace_FromRandomList()
        {
            Random random = new Random();

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

            // make it a set and remove trailing garbage
            list.ToSublist(list.ToSublist().MakeSet().InPlace()).Clear();

            // the set should be sorted and have all unique values
            Assert.IsTrue(list.ToSublist().IsSorted(), "The list was not sorted.");
            Assert.IsFalse(list.ToSublist().FindDuplicates(), "The list had duplicates.");
        }
Example #20
0
        public void TestMismatch_FindWhereListsDoNotMatch()
        {
            Random random = new Random();

            // build two, small lists
            var list1 = new List<int>(5);
            Sublist.Generate(5, i => random.Next(5)).AddTo(list1.ToSublist());
            var list2 = new List<int>(5);
            Sublist.Generate(5, i => random.Next(5)).AddTo(list2.ToSublist());

            // now find the differences
            int index = list1.ToSublist().Mismatch(list2.ToSublist());
            Assert.IsTrue(index == list1.Count || !EqualityComparer<int>.Default.Equals(list1[index], list2[index]),
                "The wrong index was returned.");
        }
Example #21
0
        public void TestMaximum_FindLargestItem()
        {
            Random random = new Random();

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

            int index = list.ToSublist().Maximum();
            Assert.AreNotEqual(list.Count, index, "The index cannot be past the end of the list.");

            int maxValue = list[index];
            list.ToSublist().Sort((x, y) => Comparer<int>.Default.Compare(y, x)).InPlace(); // puts the largest item in the first slot
            int expected = list[0];
            Assert.AreEqual(expected, maxValue, "The wrong index was returned.");
        }
Example #22
0
        public void TestBinarySearch_BuildUnique()
        {
            Random random = new Random();

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

            // now build one with just the unique values
            var set = new List<int>();
            foreach (int value in values)
            {
                SearchResult result = set.ToSublist().BinarySearch(value);
                if (!result.Exists)
                {
                    set.Insert(result.Index, value);
                }
            }

            // check that we have every value only once
            values.ToSublist().Sort().InPlace();
            Assert.IsTrue(set.ToSublist().IsSorted(), "The set is not sorted.");
            bool hasValues = set.ToSublist().IsSubset(values.ToSublist());
            Assert.IsTrue(hasValues, "Not all of the values were copied.");
            Assert.IsFalse(set.ToSublist().FindDuplicates(), "A duplicate was found.");
        }
Example #23
0
        public void TestReplaceCopy_AbsoluteValue()
        {
            Random random = new Random();

            // build a list of numbers
            var list = new List<int>(100);
            Sublist.Generate(100, i => random.Next(-49, 50)).AddTo(list.ToSublist());

            var destination = new List<int>(100);
            Sublist.Generate(100, 0).AddTo(destination.ToSublist());

            int result = list.ToSublist().Replace(i => i < 0, i => -i).CopyTo(destination.ToSublist());
            Assert.AreEqual(destination.Count, result, "The wrong index was returned.");

            Assert.IsFalse(destination.ToSublist().Find(i => i < 0), "Not all values were positive.");
        }
Example #24
0
        public void TestAddTo_ConcatenateLists()
        {
            Random random = new Random();

            // build a list of values to concatenate.
            var list = new List<int>();
            Sublist.Generate(50, i => random.Next(0, 100)).AddTo(list.ToSublist());

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

            list.ToSublist().AddTo(destination.ToSublist());

            Assert.IsTrue(destination.ToSublist(50).IsEqualTo(list.ToSublist()), "The items were not added as expected.");
        }
Example #25
0
        public void TestReverseInPlace_ReverseOrder()
        {
            Random random = new Random();

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

            // sort the list in ascending order
            list.ToSublist().Sort().InPlace();

            // now reverse the order
            list.ToSublist().Reverse().InPlace();

            // it should be in reverse order
            Assert.IsTrue(list.ToSublist().IsSorted((x, y) => Comparer<int>.Default.Compare(y, x)), "The list was not reversed.");
        }
Example #26
0
        public void TestIsSet_BuildSet()
        {
            Random random = new Random();

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

            // build a set in place - could call QuickSort then RemoveDuplicates, or MakeSet.
            int index = list.ToSublist().IsSet();
            for (int next = index; next < list.Count; ++next)
            {
                var set = list.ToSublist(0, index);
                int value = list[next];
                int location = set.BinarySearch(value);
                // if the value is unique
                if (location < 0)
                {
                    location = ~location;
                    // shift everything over one and stick in the value
                    int size = index - location;
                    var backend = list.ToSublist(location, size);
                    var offset = list.ToSublist(location + 1, size);
                    backend.Reversed().CopyTo(offset.Reversed());
                    //copyBackward(backend, offset);
                    list[location] = value;
                    ++index; // the set grew
                }
            }
            list.ToSublist(index).Clear(); // removes dangling items
            Assert.IsTrue(list.ToSublist().IsSet(), "Did not build a valid set.");
        }
Example #27
0
 public void TestCopyTo_FromListToArray()
 {
     var list = new List<int>() { 1, 2, 3 };
     var array = new int[3];
     int result = list.ToSublist().CopyTo(array.ToSublist());
     Assert.AreEqual(array.Length, result, "The result was not at the expected index.");
     int[] expected = { 1, 2, 3 };
     Assert.IsTrue(expected.ToSublist().IsEqualTo(array.ToSublist()), "The items were not copied correctly.");
 }
Example #28
0
        public void TestItemAtInPlace_Find3rdPlace()
        {
            Random random = new Random();

            // build a list, leaving space for zero, one and two
            var list = new List<int>(97);
            Sublist.Generate(97, i => random.Next(3, 100)).AddTo(list.ToSublist());

            // insert 0-2 at random positions in the list
            list.Insert(random.Next(0, list.Count + 1), 0);
            list.Insert(random.Next(0, list.Count + 1), 1);
            list.Insert(random.Next(0, list.Count + 1), 2);

            // now find what item belongs in the second position, as if the list was sorted
            list.ToSublist().ItemAt(2).InPlace();
            int actual = list[2];
            Assert.AreEqual(2, actual, "The 2 was not moved to the second position.");
        }
Example #29
0
        public void TestMinimumMaximum_FindLargestAndSmallestItems()
        {
            Random random = new Random();

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

            MinimumMaximumResult result = list.ToSublist().MinimumMaximum();
            Assert.AreNotEqual(list.Count, result.MinimumIndex, "The index cannot be past the end of the list.");
            Assert.AreNotEqual(list.Count, result.MaximumIndex, "The index cannot be past the end of the list.");

            int minValue = list[result.MinimumIndex];
            int maxValue = list[result.MaximumIndex];
            list.ToSublist().Sort(Comparer<int>.Default).InPlace(); // puts the largest item in the first slot
            Assert.AreEqual(list[0], minValue, "The wrong index was returned.");
            Assert.AreEqual(list[list.Count - 1], maxValue, "The wrong index was returned.");
        }
Example #30
0
        public void TestSelectInPlace_DoubleValues()
        {
            Random random = new Random();

            // build a list of numbers
            var numbers = new List<int>(100);
            Sublist.Generate(100, i => i).AddTo(numbers.ToSublist());

            // double the values
            numbers.ToSublist().Select(i => i * 2).InPlace();

            // build the expected values
            var expected = new List<int>(100);
            Sublist.Generate(100, i => i * 2).AddTo(expected.ToSublist());

            // verify that the values were doubled in-place
            Assert.IsTrue(expected.ToSublist().IsEqualTo(numbers.ToSublist()), "Could not convert between strings and numbers.");
        }