コード例 #1
0
        private static double ValidateHeron(ISqrRt heronCalculator, ISqrRt standardCalculator, double error)
        {
            // A function to validate the accuracy of the Heron calculation vs. the internal Math.Sqrt function
            double        resultHeron;
            double        resultStandard;
            int           resultTrue = 0;
            List <bool>   resultList = new List <bool>();
            List <double> errorList  = new List <double>();
            // Create random sample data to use for testing square root methods
            Random     r           = new Random();
            List <int> listNumbers = new List <int>();

            for (int i = 1; i < 10001; i++)
            {
                listNumbers.Add(r.Next(1, 100000));
            }
            // Run both square root methods on each of the random sample data numbers
            foreach (var num in listNumbers)
            {
                resultHeron    = heronCalculator.GetSquareRoot(num);
                resultStandard = standardCalculator.GetSquareRoot(num);
                if (Math.Abs(resultHeron - resultStandard) < error)
                {
                    resultList.Add(true);
                    errorList.Add(Math.Abs(resultHeron - resultStandard));
                    resultTrue++;
                }
                else
                {
                    resultList.Add(false);
                    throw new Exception("The difference between the two methods is not within the accepted error rate.");
                }
            }
            return(resultTrue);
        }
コード例 #2
0
        public void ValidateAndCompareCalculationsWithinErrorLimits(ISqrRt heronCalculator, ISqrRt standardCalculator, double error)
        {
            // Arrange
            double resultHeron;
            double resultStandard;
            bool   resultComparison = false;
            // Create random sample data to use for testing square root methods
            Random     r           = new Random();
            List <int> listNumbers = new List <int>();

            for (int i = 1; i < 10001; i++)
            {
                listNumbers.Add(r.Next(1, 100000));
            }
            // Act
            // Run both square root methods on each of the random sample data numbers
            foreach (var num in listNumbers)
            {
                resultHeron      = heronCalculator.GetSquareRoot(num);
                resultStandard   = standardCalculator.GetSquareRoot(num);
                resultComparison = CompareRoots(resultHeron, resultStandard, error);
            }
            // Assert
            Assert.IsTrue(resultComparison);
        }