Beispiel #1
0
        void DoMonotoneTest(MonotoneType monotoneType)
        {
            int n = 8;
            var p = PermutationUtils.First(n);

            do
            {
                var copyP = (int[])p.Clone();
                var my    = SequenceUtils.GetLongestMonotoneSubsequenceLength(copyP, monotoneType);
                Assert.IsTrue(p.SequenceEqual(copyP));
                var correct = GetLongestMonotoneSubsequenceLengthNaive(p, monotoneType);
                Assert.AreEqual(correct, my);

                var mlist = SequenceUtils.GetLongestMonotoneSubsequence(copyP, monotoneType);
                Assert.IsTrue(p.SequenceEqual(copyP));

                Assert.AreEqual(my, mlist.Length);
                for (int i = 1; i < mlist.Length; ++i)
                {
                    Assert.IsTrue(mlist[i] >= 0 && mlist[i] < n);
                    Assert.IsTrue(mlist[i] > mlist[i - 1]);

                    switch (monotoneType)
                    {
                    case MonotoneType.Increasing:
                        Assert.IsTrue(p[mlist[i]] > p[mlist[i - 1]]);
                        break;

                    case MonotoneType.Decreasing:
                        Assert.IsTrue(p[mlist[i]] < p[mlist[i - 1]]);
                        break;

                    case MonotoneType.NonDecreasing:
                        Assert.IsTrue(p[mlist[i]] >= p[mlist[i - 1]]);
                        break;

                    case MonotoneType.NonIncreasing:
                        Assert.IsTrue(p[mlist[i]] <= p[mlist[i - 1]]);
                        break;

                    default:
                        throw new System.Exception("Unknown monotone type");
                    }
                }
            } while (PermutationUtils.Next(p));

            var rnd = new Random(123);

            for (int times = 0; times < 100; ++times)
            {
                n = rnd.Next(50) + 10;
                p = new int[n];
                for (int i = 0; i < n; i++)
                {
                    p[i] = rnd.Next(20);
                }
                var correct = GetLongestMonotoneSubsequenceLengthNaive(p, monotoneType);
                var my      = GetLongestMonotoneSubsequenceLengthNaive(p, monotoneType);
                Assert.AreEqual(correct, my);

                n = 8;
                p = new int[n];
                for (int i = 0; i < n; i++)
                {
                    p[i] = rnd.Next(5);
                }
                Array.Sort(p);
                do
                {
                    correct = GetLongestMonotoneSubsequenceLengthNaive(p, monotoneType);
                    my      = GetLongestMonotoneSubsequenceLengthNaive(p, monotoneType);
                    Assert.AreEqual(correct, my);
                } while (PermutationUtils.Next(p));
            }
        }