Ejemplo n.º 1
0
        private bool TestRankComboulong(int numItems, int groupSize)
        {
            // This method tests getting the rank and combination for the specified case.
            // If the test failed, then false is returned. Otherwise true is returned.
            string s;
            bool   overflow;
            int    n = numItems, k = groupSize;
            // Create the bin coeff object required to get all the combos for this n choose k combination.
            BinCoeffL bcl = new BinCoeffL(n, k);

            // The Kindexes array specifies the indexes for a combination.
            int[] kIndexes = new int[k];
            ulong numCombos = bcl.TotalCombos, rankLoop;

            bcl.GetCombFromRank(numCombos - 1, kIndexes);
            uint numCombos2 = (uint)BinCoeffBase.GetRankAlt(kIndexes, n, k, out overflow) + 1;

            if (numCombos != numCombos2)
            {
                s = $"TestRankComboLong: {n} choose {k}: # of combos not the same with alternate method - {numCombos} .vs. {numCombos2}";
                Console.WriteLine(s);
                return(false);
            }
            ulong rank, offset = 1;

            // Loop thru all the combinations for this n choose k case if # of combos <= 1000000.
            // Otherwise, choose a random rank between 100 & 200.
            if (numCombos > 1000000)
            {
                Random rand = new Random(-12345);
                offset = (ulong)rand.Next(100, 200);
            }
            for (rankLoop = 0; rankLoop < numCombos; rankLoop += offset)
            {
                // Get the k-indexes for this combination.
                bcl.GetCombFromRank(rankLoop, kIndexes);
                // Verify that the Kindexes returned can be used to retrieve the rank of the combination.
                rank = bcl.GetRank(true, kIndexes, out overflow, groupSize);
                if (rank != rankLoop)
                {
                    s = $"TestRankComboLong: {n} choose {k}: GetRank or GetCombFromRank failed - value of {rank} != rank at {rankLoop}";
                    Console.WriteLine(s);
                    return(false);
                }
            }
            s = $"TestRankComboLong: {n} choose {k} completed successfully.";
            Console.WriteLine(s);
            return(true);
        }
Ejemplo n.º 2
0
        private bool TestRankCombo(int numItems, int groupSize)
        {
            // This method tests getting the rank and combination for the specified 32-bit int case.
            // If the test failed, then false is returned. Otherwise true is returned.
            string s;
            int    n = numItems, k = groupSize;
            bool   overflow;
            // Create the bin coeff object required to get all the combos for this n choose k combination.
            BinCoeff <int> bc = new BinCoeff <int>(n, k);

            // The Kindexes array specifies the indexes for a combination.
            int[] kIndexes = new int[k];
            uint  numCombos = bc.TotalCombos, rankLoop;

            bc.GetCombFromRank(numCombos - 1, kIndexes);
            uint numCombos2 = (uint)BinCoeffBase.GetRankAlt(kIndexes, n, k, out overflow) + 1;

            if (overflow)
            {
                s = $"TestRankCombo: {n} choose {k}: # of combos overflowed with alt method.";
                Console.WriteLine(s);
                return(false);
            }
            if (numCombos != numCombos2)
            {
                s = $"TestRankCombo: {n} choose {k}: # of combos not the same with alternate method - {numCombos} .vs. {numCombos2}";
                Console.WriteLine(s);
                return(false);
            }
            uint rank2, rankHigh;
            uint comboCount = numCombos;

            // Loop thru all the combinations for this n choose k case if # of combos <= 1000.
            // Otherwise, just do the lowest ranked 500 and the highest ranked 500.
            if (numCombos > 1000)
            {
                comboCount = 500;
            }
            // Loop thru all the combinations for this n choose k case.
            for (rankLoop = 1; rankLoop < comboCount; rankLoop++)
            {
                // Get the k-indexes for this combination.
                bc.GetCombFromRank(rankLoop, kIndexes);
                // Verify that the Kindexes returned can be used to retrieve the rank of the combination.
                rank2 = bc.GetRank(true, kIndexes, out overflow);
                if (rank2 != rankLoop)
                {
                    s = $"TestRankCombo: {n} choose {k}: GetRank or GetCombFromRank failed - value of {rank2} != rank at {rankLoop}";
                    Console.WriteLine(s);
                    return(false);
                }
                // If not doing all the combinations, then process the high ranks.
                if (numCombos > comboCount)
                {
                    rankHigh = numCombos - rankLoop - 1;
                    // Get the k-indexes for this combination.
                    bc.GetCombFromRank(rankHigh, kIndexes);
                    // Verify that the Kindexes returned can be used to retrieve the rank of the combination.
                    rank2 = bc.GetRank(true, kIndexes, out overflow);
                    if (rank2 != rankHigh)
                    {
                        s = $"TestRankCombo: {n} choose {k}: GetRank or GetCombFromRank failed - value of {rank2} != rankHigh at {rankLoop}";
                        Console.WriteLine(s);
                        return(false);
                    }
                }
            }
            s = $"TestRankCombo: {n} choose {k} completed successfully.";
            Console.WriteLine(s);
            return(true);
        }