public void StressMc_Ctor2B()
        {
            // Use higher maxChoices values for a longer running test.
#if STRESS
            const int maxChoices = 7;
#else
            const int maxChoices = 4;
#endif
            int counter = 0;

            for (int choices = 1; choices <= maxChoices; ++choices)
            {
                for (int picks = 0; picks <= choices + 2; ++picks)
                {
                    long maxRank = picks == 0 ? 0 : Combinatoric.BinomialCoefficient(picks + choices - 1, picks);

                    for (long rank = 0; rank < maxRank; ++rank)
                    {
                        Multicombination row1 = new Multicombination(choices, picks, rank);

                        int[] source = new int[picks];
                        row1.CopyTo(source);

                        Multicombination row2 = new Multicombination(choices, source);

                        // verify that rank(unrank(x)) = x
                        Assert.AreEqual(rank, row1.Rank);
                        Assert.AreEqual(rank, row2.Rank);
                        ++counter;
                    }
                }
            }
        }
        public void UnitMc_Ctor3A3B()
        {
            Multicombination mc = new Multicombination(4, 0, 9);

            Assert.AreEqual(0, mc.Rank);
            Assert.AreEqual(0, mc.RowCount);
        }
        public void UnitMc_Ctor2B1Empty()
        {
            int[]            array = new int[] { };
            Multicombination mc    = new Multicombination(0, array);

            Assert.AreEqual(0, mc.RowCount);
        }
        public void CrashMc_CopyTo_Argument()
        {
            Multicombination mc = new Multicombination(3, 3, 4);

            int[] nullTarget = new int[2];
            mc.CopyTo(nullTarget);
        }
        public void UnitMc_ToString1()
        {
            Multicombination mc     = new Multicombination(1);
            string           actual = mc.ToString();

            Assert.AreEqual("{ 0 }", actual);
        }
        public void UnitMc_GetRows()
        {
            var expect = new int[][]
            {
                new int[] { 0, 0, 0, 0 }, new int[] { 0, 0, 0, 1 }, new int[] { 0, 0, 0, 2 },
                new int[] { 0, 0, 1, 1 }, new int[] { 0, 0, 1, 2 }, new int[] { 0, 0, 2, 2 },
                new int[] { 0, 1, 1, 1 }, new int[] { 0, 1, 1, 2 }, new int[] { 0, 1, 2, 2 },
                new int[] { 0, 2, 2, 2 }, new int[] { 1, 1, 1, 1 }, new int[] { 1, 1, 1, 2 },
                new int[] { 1, 1, 2, 2 }, new int[] { 1, 2, 2, 2 }, new int[] { 2, 2, 2, 2 }
            };

            long startRank = 2;
            var  mc0       = new Multicombination(3, 4, startRank);

            Assert.AreEqual(expect.Length, mc0.RowCount);
            var beginData = new int[4];

            mc0.CopyTo(beginData);

            int actualCount = 0;

            foreach (Multicombination mc in mc0.GetRows())
            {
                long expectRank = (actualCount + startRank) % expect.Length;
                Assert.AreEqual(expectRank, mc.Rank);
                Assert.AreEqual(expectRank, mc0.Rank);
                Assert.IsTrue(Enumerable.SequenceEqual(expect[expectRank], mc));
                Assert.IsTrue(Enumerable.SequenceEqual(mc, mc0));
                ++actualCount;
            }

            Assert.AreEqual(expect.Length, actualCount);
            Assert.AreEqual(startRank, mc0.Rank);
            Assert.IsTrue(Enumerable.SequenceEqual(beginData, mc0));
        }
        public void UnitMc_GetRowsForPicksC()
        {
            var expect = new int[][]
            {
                new int[] { 0 }, new int[] { 1 },
                new int[] { 0, 0 }, new int[] { 0, 1 }, new int[] { 1, 1 },
                new int[] { 0, 0, 0 }, new int[] { 0, 0, 1 }, new int[] { 0, 1, 1 }, new int[] { 1, 1, 1 }
            };

            long startRank = 1;
            var  mc0       = new Multicombination(2, 3, startRank);
            var  beginData = new int[mc0.Picks];

            mc0.CopyTo(beginData);

            int actualCount = 0;

            foreach (Multicombination mc in mc0.GetRowsForPicks(1, 3))
            {
                Assert.IsTrue(Enumerable.SequenceEqual(expect[actualCount], mc));
                Assert.IsTrue(Enumerable.SequenceEqual(mc, mc0));
                ++actualCount;
            }

            Assert.AreEqual(expect.Length, actualCount);
            Assert.AreEqual(startRank, mc0.Rank);
            Assert.IsTrue(Enumerable.SequenceEqual(beginData, mc0));
        }
        public void CrashMc_Permute_ArgumentNull2()
        {
            Multicombination mc = new Multicombination(2);

            string[]      nullSource = null;
            List <string> item       = Multicombination.Permute(mc, nullSource);
        }
        public void UnitMc_CompareTo()
        {
            Multicombination mcNull = null;
            Multicombination mc520  = new Multicombination(5, 2, 0);
            Multicombination mc521  = new Multicombination(5, 2, 1);
            Multicombination mc421  = new Multicombination(4, 2, 1);

            int actual1 = mc520.CompareTo(mcNull);

            Assert.IsTrue(actual1 > 0);

            int actual2 = mc520.CompareTo(mc521);

            Assert.IsTrue(actual2 < 0);

            int actual3 = mc520.CompareTo(mc520);

            Assert.AreEqual(0, actual3);

            int actual4 = mc521.CompareTo(mc520);

            Assert.IsTrue(actual4 > 0);

            int actual5 = mc521.CompareTo(mc421);

            Assert.IsTrue(actual5 != 0);
        }
        public void UnitMc_ToString3()
        {
            Multicombination mc     = new Multicombination(5, 3, 20);
            string           actual = mc.ToString();

            Assert.AreEqual("{ 1, 2, 3 }", actual);
        }
        public void CrashMc_Permute_ArgumentNull1()
        {
            Multicombination mcNull = null;

            string[]      letters = new string[] { "A", "B" };
            List <string> item    = Multicombination.Permute(mcNull, letters);
        }
        public void CrashMc_GetRowsForPicks_ArgumentOutOfRange2()
        {
            Multicombination mc2 = new Multicombination(2, 3);

            foreach (Multicombination row in mc2.GetRowsForPicks(2, 1))
            {
            }
        }
        public void UnitMc_GetRowsForPicksEmpty2()
        {
            Multicombination mc2 = new Multicombination(2, 3);

            foreach (Multicombination row in mc2.GetRowsForPicks(0, 0))
            {
                Assert.Fail("Enumeration should be empty");
            }
        }
        public void UnitMc_GetRowsForPicksEmpty1()
        {
            Multicombination mc0 = new Multicombination(0, 0);

            foreach (Multicombination row in mc0.GetRowsForPicks(1, 2))
            {
                Assert.Fail("Enumeration should be empty");
            }
        }
        public void CrashMc_Permute_Argument()
        {
            Multicombination mc = new Multicombination(6);

            string[] letters = new string[] { "A", "B", "C", "D" };

            // Not enough letters throws
            List <string> item = Multicombination.Permute(mc, letters);
        }
        public void UnitMc_GetRows0Empty()
        {
            Multicombination mc = new Multicombination();

            foreach (Multicombination row in mc.GetRows())
            {
                Assert.Fail("Empty multicombo iterates");
            }
        }
        public void UnitMc_Inheritance()
        {
            Multicombination mc = new Multicombination(4, 2);

            Assert.IsNotNull(mc as IComparable);
            Assert.IsNotNull(mc as System.Collections.IEnumerable);
            Assert.IsNotNull(mc as IComparable <Multicombination>);
            Assert.IsNotNull(mc as IEquatable <Multicombination>);
            Assert.IsNotNull(mc as IEnumerable <int>);
        }
        public void UnitMc_Ctor1b2()
        {
            int n = 0;
            Multicombination mc = new Multicombination(n);

            Assert.AreEqual(n, mc.Choices);
            Assert.AreEqual(n, mc.Picks);
            Assert.AreEqual(0, mc.Rank);
            Assert.AreEqual(0, mc.RowCount);
        }
        public void UnitMc_RankDecrement()
        {
            Multicombination mc = new Multicombination(4, 3, 1);

            --mc.Rank;
            Assert.AreEqual(0, mc.Rank);

            mc.Rank--;
            Assert.AreEqual(19, mc.Rank);
        }
        public void UnitMc_RankIncrement()
        {
            Multicombination mc = new Multicombination(4, 3, 1);

            ++mc.Rank;
            Assert.AreEqual(2, mc.Rank);

            mc.Rank++;
            Assert.AreEqual(3, mc.Rank);
        }
        public void UnitMc_Ctor3A3C()
        {
            Multicombination mc = new Multicombination(4, 3, 14);

            Assert.AreEqual(1, mc[0]);
            Assert.AreEqual(2, mc[1]);
            Assert.AreEqual(3, mc[2]);
            Assert.AreEqual(14, mc.Rank);
            Assert.AreEqual(20, mc.RowCount);
        }
        public void UnitMc_Ctor1A()
        {
            Multicombination smc = new Multicombination(5, 3, 1);

            Multicombination mc = new Multicombination(smc);

            Assert.AreEqual(5, mc.Choices);
            Assert.AreEqual(3, mc.Picks);
            Assert.AreEqual(1, mc.Rank);
            Assert.AreEqual(35, mc.RowCount);
        }
        public void UnitMc_CopyTo1()
        {
            Multicombination mc3 = new Multicombination(3, 3, 4);

            int[] target = new int[3];
            mc3.CopyTo(target);

            Assert.AreEqual(0, target[0]);
            Assert.AreEqual(1, target[1]);
            Assert.AreEqual(2, target[2]);
        }
Пример #24
0
        static void Main()
        {
            var mc = new Multicombination(choices: 4, picks: 3);

            Console.WriteLine($"n={mc.Choices}, k={mc.Picks}:\n");

            foreach (var row in mc.GetRows())
            {
                Console.WriteLine($"{row.Rank,2}:  {row}");
            }
        }
        public void UnitMc_Ctor2A2()
        {
            int n = 5;
            int k = 3;

            Multicombination mc53 = new Multicombination(n, k);

            Assert.AreEqual(n, mc53.Choices);
            Assert.AreEqual(k, mc53.Picks);
            Assert.AreEqual(0, mc53.Rank);
            Assert.AreEqual(35, mc53.RowCount);
        }
        public void UnitMc_ComparisonOps()
        {
            Multicombination c0  = null;
            Multicombination d0  = null;
            Multicombination c1  = new Multicombination(6, 3, 7);
            Multicombination c11 = new Multicombination(6, 3, 7);
            Multicombination c2  = new Multicombination(6, 3, 9);
            Multicombination c3  = new Multicombination(9, 2, 15);
            Multicombination c4  = new Multicombination(8, 2, 15);

            Assert.IsTrue(c0 == d0);
            Assert.IsFalse(c0 == c1);
            Assert.IsFalse(c1 == c0);
            Assert.IsTrue(c1 == c11);
            Assert.IsFalse(c1 == c2);

            Assert.IsFalse(c0 != d0);
            Assert.IsTrue(c0 != c1);
            Assert.IsTrue(c1 != c0);
            Assert.IsFalse(c1 != c11);
            Assert.IsTrue(c1 != c2);

            Assert.IsFalse(c0 < d0);
            Assert.IsTrue(c0 < c1);
            Assert.IsFalse(c1 < c0);
            Assert.IsFalse(c1 < c11);
            Assert.IsTrue(c1 < c2);
            Assert.IsFalse(c2 < c1);

            Assert.IsTrue(c0 >= d0);
            Assert.IsFalse(c0 >= c1);
            Assert.IsTrue(c1 >= c0);
            Assert.IsTrue(c1 >= c11);
            Assert.IsFalse(c1 >= c2);
            Assert.IsTrue(c2 >= c1);

            Assert.IsFalse(c0 > d0);
            Assert.IsFalse(c0 > c1);
            Assert.IsTrue(c1 > c0);
            Assert.IsFalse(c1 > c11);
            Assert.IsFalse(c1 > c2);
            Assert.IsTrue(c2 > c1);

            Assert.IsTrue(c0 <= d0);
            Assert.IsTrue(c0 <= c1);
            Assert.IsFalse(c1 <= c0);
            Assert.IsTrue(c1 <= c11);
            Assert.IsTrue(c1 <= c2);
            Assert.IsFalse(c2 <= c1);

            Assert.IsTrue(c3 < c1);
            Assert.IsTrue(c3 != c4);
        }
        public void UnitMc_Rank1()
        {
            Multicombination mc = new Multicombination(1);

            mc.Rank = 1;
            Assert.AreEqual(0, mc.Rank);

            mc.Rank = 2;
            Assert.AreEqual(0, mc.Rank);

            mc.Rank = -1;
            Assert.AreEqual(0, mc.Rank);
        }
        public void UnitMc_Rank2()
        {
            Multicombination mc = new Multicombination(6, 4);

            mc.Rank = 1;
            Assert.AreEqual(1, mc.Rank);

            mc.Rank = 2;
            Assert.AreEqual(2, mc.Rank);

            mc.Rank = -1;
            Assert.AreEqual(125, mc.Rank);
        }
        public void UnitMc_Equals()
        {
            Multicombination nullMc = null;
            Multicombination c438a  = new Multicombination(4, 3, 8);
            Multicombination c438b  = new Multicombination(4, 3, 8);
            Multicombination c439   = new Multicombination(4, 3, 9);
            Multicombination c6     = new Multicombination(8, 6, 4);

            Assert.IsFalse(c438a.Equals(nullMc));
            Assert.IsTrue(c438a.Equals(c438b));
            Assert.IsFalse(c438a.Equals(c439));
            Assert.IsFalse(c438a.Equals(c6));
        }
        public void UnitMc_GetRowsForPicksB()
        {
            Multicombination mc2 = new Multicombination(2, 5);

            int counter = 0;

            foreach (Multicombination row in mc2.GetRowsForPicks(0, 2))
            {
                ++counter;
            }

            Assert.AreEqual(5, counter);
        }