private void TestCase(int itrsWithVal, int specifiedValsOnItr, bool removeDups)
        {
            // Build a random number of lists
            IList <int?> expected = new List <int?>();
            Random       random   = new Random(Random.Next());
            int          numLists = itrsWithVal + random.Next(1000 - itrsWithVal);

            IList <int>[] lists = new IList <int> [numLists];
            for (int i = 0; i < numLists; i++)
            {
                lists[i] = new List <int>();
            }
            int start = random.Next(1000000);
            int end   = start + VALS_TO_MERGE / itrsWithVal / Math.Abs(specifiedValsOnItr);

            for (int i = start; i < end; i++)
            {
                int maxList      = lists.Length;
                int maxValsOnItr = 0;
                int sumValsOnItr = 0;
                for (int itrWithVal = 0; itrWithVal < itrsWithVal; itrWithVal++)
                {
                    int list      = random.Next(maxList);
                    int valsOnItr = specifiedValsOnItr < 0 ? (1 + random.Next(-specifiedValsOnItr)) : specifiedValsOnItr;
                    maxValsOnItr  = Math.Max(maxValsOnItr, valsOnItr);
                    sumValsOnItr += valsOnItr;
                    for (int valOnItr = 0; valOnItr < valsOnItr; valOnItr++)
                    {
                        lists[list].Add(i);
                    }
                    maxList = maxList - 1;
                    ArrayUtil.Swap(lists, list, maxList);
                }
                int maxCount = removeDups ? maxValsOnItr : sumValsOnItr;
                for (int count = 0; count < maxCount; count++)
                {
                    expected.Add(i);
                }
            }
            // Now check that they get merged cleanly
            IEnumerator <int>[] itrs = new IEnumerator <int> [numLists];
            for (int i = 0; i < numLists; i++)
            {
                itrs[i] = lists[i].GetEnumerator();
            }

            MergedIterator <int> mergedItr   = new MergedIterator <int>(removeDups, itrs);
            IEnumerator <int?>   expectedItr = expected.GetEnumerator();

            while (expectedItr.MoveNext())
            {
                Assert.IsTrue(mergedItr.MoveNext());
                Assert.AreEqual(expectedItr.Current, mergedItr.Current);
            }
            Assert.IsFalse(mergedItr.MoveNext());
        }
Exemple #2
0
 protected override void Swap(int i, int j)
 {
     ArrayUtil.Swap(Arr, i, j);
 }