コード例 #1
0
        // Vetting the inverse of the results division
        private bool CheckDivision(Fraction.Fraction first, Fraction.Fraction second)
        {
            // Saving the original data
            Fraction.Fraction firstFraction  = first;
            Fraction.Fraction secondFraction = second;
            System.Console.Write("\n" + fractionCalulator.ReducedFraction(firstFraction).GetNumerator().ToString() + '/' + fractionCalulator.ReducedFraction(firstFraction).GetDenominator().ToString()
                                 + " / " + fractionCalulator.ReducedFraction(secondFraction).GetNumerator().ToString() + '/' + fractionCalulator.ReducedFraction(secondFraction).GetDenominator().ToString());

            // Grab the results from the calculator
            // MODIFY: Changing method to something else will produce failed test cases
            //Fraction.Fraction actualResults = fractionCalulator.Addition(first, second);
            Fraction.Fraction actualResults = fractionCalulator.Division(first, second);
            System.Console.Write(" = " + actualResults.GetNumerator().ToString() + '/' + actualResults.GetDenominator().ToString());

            // actual results turns out to be a non-real fraction so pass it
            if (actualResults.GetDenominator() == 0 && second.GetNumerator() == 0)
            {
                return(true);
            }


            // Run the inverse operations of the function to pull the expected results
            Fraction.Fraction expectedResults = new Fraction.Fraction();
            expectedResults.SetDenominator(actualResults.GetDenominator() / second.GetNumerator());
            expectedResults.SetNumerator(actualResults.GetNumerator() / second.GetDenominator());

            // Compare
            if ((expectedResults.GetNumerator() == firstFraction.GetNumerator()) && (expectedResults.GetDenominator() == firstFraction.GetDenominator()))
            {
                return(true);
            }

            // if numerators are both 0, the value is 0
            else if ((expectedResults.GetNumerator() == 0) && (expectedResults.GetNumerator() == 0))
            {
                return(true);
            }

            else
            {
                // try reducing both fractions and check for failing
                expectedResults = fractionCalulator.ReducedFraction(expectedResults);
                firstFraction   = fractionCalulator.ReducedFraction(firstFraction);
                if ((expectedResults.GetNumerator() == firstFraction.GetNumerator()) && (expectedResults.GetDenominator() == firstFraction.GetDenominator()))
                {
                    return(true);
                }
            }
            return(false);
        }
コード例 #2
0
        // Creates a random numerator and denominator for both fractions between minRange (inclusive), and maxRange (exclusive).
        // Requires an (OperationType)operationType to determine the operation to test, ie. Subraction. Requires a length of
        // the amount of tests to run.
        public void RunTests(uint minRange, uint maxRange, OperationType operationType, uint length)
        {
            Random random = new Random();

            for (int i = 0; i < length; ++i)
            {
                // Generate random fractions
                secondFraction.SetDenominator(random.Next((int)minRange, (int)maxRange));
                secondFraction.SetNumerator(random.Next((int)minRange, (int)maxRange));
                firstFraction.SetNumerator(random.Next((int)minRange, (int)maxRange));
                firstFraction.SetDenominator(random.Next((int)minRange, (int)maxRange));

                // Run the operation checks based on the operationType submitted.
                if (operationType == OperationType.ADDITION)
                {
                    if (!CheckAddition(firstFraction, secondFraction))
                    {
                        FailedTestCase("ADDITIVE");
                    }
                }
                else if (operationType == OperationType.SUBTRACTION)
                {
                    if (!CheckSubtraction(firstFraction, secondFraction))
                    {
                        FailedTestCase("SUBTRACTIVE");
                    }
                }
                else if (operationType == OperationType.MULTIPLICATION)
                {
                    if (!CheckMultiplication(firstFraction, secondFraction))
                    {
                        FailedTestCase("MULTIPLICATION");
                    }
                }
                else
                {
                    if (!CheckDivision(firstFraction, secondFraction))
                    {
                        FailedTestCase("DIVISION");
                    }
                }
            }
            System.Console.WriteLine("\nDone!---------------------------");
        }