Ejemplo n.º 1
0
        public void InANotInB_RandomLists_CorrectResult(int firstSize, int secondSize, int?maxDistance = null)
        {
            var firstArray  = new int[firstSize];
            var secondArray = new int[secondSize];
            int offset      = _random.Next();

            int secondOffset = maxDistance != null?
                               _random.Next(offset - maxDistance.Value, offset + maxDistance.Value + 1) :
                                   _random.Next();

            // Generating input lists.
            Parallel.Invoke(
                () => Parallel.For(0, firstSize, (i) => firstArray[i]   = i + offset),
                () => Parallel.For(0, secondSize, (i) => secondArray[i] = i + secondOffset));

            // Shuffling them using modern version of Fisher–Yates algorithm.
            for (int i = firstArray.Length - 1; i >= 0; i--)
            {
                int j    = _random.Next(i + 1);
                var temp = firstArray[i];

                firstArray[i] = firstArray[j];
                firstArray[j] = temp;
            }

            for (int i = secondArray.Length - 1; i >= 0; i--)
            {
                int j    = _random.Next(i + 1);
                var temp = secondArray[i];

                secondArray[i] = secondArray[j];
                secondArray[j] = temp;
            }

            // Assuming, that result is already sorted.
            var result = QSortMerge.InANotInB(new List <int>(firstArray), new List <int>(secondArray));

            // OrderBy is used to reduce CollectionAssert complexity from O(n^2) to O(n).
            // Otherwise, CollectionAssert.AreEquivalent would be used which is very slow.
            var expected = firstArray.AsParallel().Except(secondArray.AsParallel()).OrderBy(n => n);

            CollectionAssert.AreEqual(expected, result);
        }
Ejemplo n.º 2
0
        public void InANotInB_ComparerNull_ArgumentNullException()
        {
            List <int> first = new List <int>(0);

            Assert.Throws <ArgumentNullException>(() => QSortMerge.InANotInB(first, first, null));
        }
Ejemplo n.º 3
0
        public void InANotInB_FirstListNull_ArgumentNullException()
        {
            List <int> second = new List <int>(0);

            Assert.Throws <ArgumentNullException>(() => QSortMerge.InANotInB(null, second));
        }