public void CompareNotEqualsTest()
        {
            //Arrange
            var firstFraction  = new SimpleFraction(1, 2);
            var secondFraction = new SimpleFraction(1, 4);
            var expectedResult = 1;

            //Act
            var result = firstFraction.CompareTo(secondFraction);

            //Assert
            Assert.AreEqual(expectedResult, result);
        }
        public void AddTest()
        {
            //Arrange
            var fraction       = new SimpleFraction(1, 2);
            var addFraction    = new SimpleFraction(1, 2);
            var expectedResult = new SimpleFraction(1, 1);

            //Act
            var result = fraction.Add(addFraction);

            //Assert
            Assert.AreEqual(expectedResult, result);
        }
        public void SumTest()
        {
            //Arrange
            var left           = new SimpleFraction(1, 2);
            var right          = new SimpleFraction(1, 2);
            var expectedResult = new SimpleFraction(1, 1);

            //Act
            var result = left + right;

            //Assert
            Assert.AreEqual(expectedResult, result);
        }
        public void MultiplicationTest()
        {
            //Arrange
            var left           = new SimpleFraction(1, 2);
            var right          = new SimpleFraction(1, 2);
            var expectedResult = new SimpleFraction(1, 4);

            //Act
            var result = left * right;

            //Assert
            Assert.AreEqual(expectedResult, result);
        }
Пример #5
0
        public void DivisionSimpleFractionWithZeroDenominator()
        {
            SimpleFraction simpleFraction1 = new SimpleFraction(100500, 1);
            SimpleFraction simpleFraction2 = new SimpleFraction(0, 2);

            SimpleFraction result;

            string expectedExceptionMessage = $"Заданный аргумент находится вне диапазона допустимых значений.\r\nИмя параметра: Знаминатель не может быть меньше или равен нулю.";

            var factException = Assert.Throws <ArgumentOutOfRangeException>(() => result = simpleFraction1 / simpleFraction2);

            Assert.That(factException.Message, Is.EqualTo(expectedExceptionMessage));
        }
        public void DifferenceSympleFractions()
        {
            SimpleFraction simpleFraction1 = new SimpleFraction(2, 4);
            SimpleFraction simpleFraction2 = new SimpleFraction(2, 4);

            SimpleFraction result = (simpleFraction1 - simpleFraction2);

            int expectedNumerator   = 0;
            int expectedDenominator = 16;

            Assert.AreEqual(expectedNumerator, result.Numerator, $"Фактический числитель {result.Numerator} отличается от ожидаемго {expectedNumerator}");

            Assert.AreEqual(expectedDenominator, result.Denominator, $"Фактический знаменатель {result.Denominator} отличается от ожидаемго {expectedDenominator}");
        }
Пример #7
0
        public void SummarySimpleFractionWithNegativeNumerators()
        {
            SimpleFraction simpleFraction1 = new SimpleFraction(-4, 4);
            SimpleFraction simpleFraction2 = new SimpleFraction(-2, 4);

            SimpleFraction result = (simpleFraction1 + simpleFraction2);

            int expectedNumerator   = -24;
            int expectedDenominator = 16;

            Assert.AreEqual(expectedNumerator, result.Numerator, $"Фактический числитель {result.Numerator} отличается от ожидаемго {expectedNumerator}");

            Assert.AreEqual(expectedDenominator, result.Denominator, $"Фактический знаменатель {result.Denominator} отличается от ожидаемго {expectedDenominator}");
        }
        public void DifferenceimpleFractionBigSize()
        {
            SimpleFraction simpleFraction1 = new SimpleFraction(1, 2);
            SimpleFraction simpleFraction2 = new SimpleFraction(100500, 100500);

            SimpleFraction result = (simpleFraction1 - simpleFraction2);

            int expectedNumerator   = -100500;
            int expectedDenominator = 201000;

            Assert.AreEqual(expectedNumerator, result.Numerator, $"Фактический числитель {result.Numerator} отличается от ожидаемго {expectedNumerator}");

            Assert.AreEqual(expectedDenominator, result.Denominator, $"Фактический знаменатель {result.Denominator} отличается от ожидаемго {expectedDenominator}");
        }
        public void PositiveTestGetQuotient()
        {
            var fraction1 = new SimpleFraction(15, 7);
            var fraction2 = new SimpleFraction(5, 49);
            var result1   = SimpleFraction.GetQuotient(fraction1, fraction2);
            var fraction3 = new SimpleFraction(3, 11);
            var fraction4 = new SimpleFraction(4, 5);
            var result2   = SimpleFraction.GetQuotient(fraction3, fraction4);

            Assert.AreEqual(21, result1.Numerator);
            Assert.AreEqual(1, result1.Denominator);
            Assert.AreEqual(15, result2.Numerator);
            Assert.AreEqual(44, result2.Denominator);
        }
        public void TestGetComposition()
        {
            var fraction1 = new SimpleFraction(7, 15);
            var fraction2 = new SimpleFraction(5, 49);
            var result1   = SimpleFraction.GetComposition(fraction1, fraction2);
            var fraction3 = new SimpleFraction(3, 11);
            var fraction4 = new SimpleFraction(4, 5);
            var result2   = SimpleFraction.GetComposition(fraction3, fraction4);

            Assert.AreEqual(1, result1.Numerator);
            Assert.AreEqual(21, result1.Denominator);
            Assert.AreEqual(12, result2.Numerator);
            Assert.AreEqual(55, result2.Denominator);
        }
        public void TestGetDifference()
        {
            var fraction1 = new SimpleFraction(7, 15);
            var fraction2 = new SimpleFraction(-5, 15);
            var result1   = SimpleFraction.GetDifference(fraction1, fraction2);
            var fraction3 = new SimpleFraction(3, 8);
            var fraction4 = new SimpleFraction(4, 5);
            var result2   = SimpleFraction.GetDifference(fraction3, fraction4);

            Assert.AreEqual(4, result1.Numerator);
            Assert.AreEqual(5, result1.Denominator);
            Assert.AreEqual(-17, result2.Numerator);
            Assert.AreEqual(40, result2.Denominator);
        }
        public void PositiveTestDoubleArgumentsConstructor()
        {
            var fraction1 = new SimpleFraction(7, 15);
            var fraction2 = new SimpleFraction(5, 15);
            var fraction3 = new SimpleFraction(-45, 25);
            var fraction4 = new SimpleFraction(1, -2);

            Assert.AreEqual(7, fraction1.Numerator);
            Assert.AreEqual(15, fraction1.Denominator);
            Assert.AreEqual(1, fraction2.Numerator);
            Assert.AreEqual(3, fraction2.Denominator);
            Assert.AreEqual(-9, fraction3.Numerator);
            Assert.AreEqual(5, fraction3.Denominator);
            Assert.AreEqual(-1, fraction4.Numerator);
            Assert.AreEqual(2, fraction4.Denominator);
        }
Пример #13
0
        public void FractionConstructor_Num2Den3()
        {
            SimpleFraction fr = new SimpleFraction(2, 3);

            Assert.NotNull(fr);
        }
Пример #14
0
        public String Format(string num)
        {
            double doubleValue = 0;

            double.TryParse(num, out doubleValue);

            bool   isNeg          = (doubleValue < 0.0f) ? true : false;
            double absDoubleValue = Math.Abs(doubleValue);

            double wholePart = Math.Floor(absDoubleValue);
            double decPart   = absDoubleValue - wholePart;

            if (wholePart + decPart == 0)
            {
                return("0");
            }

            //if the absolute value is smaller than 1 over the exact or maxDenom
            //you can stop here and return "0"
            if (absDoubleValue < (1 / Math.Max(exactDenom, maxDenom)))
            {
                return("0");
            }

            //this is necessary to prevent overflow in the maxDenom calculation
            //stink1
            if (wholePart + (int)decPart == wholePart + decPart)
            {
                StringBuilder sb = new StringBuilder();
                if (isNeg)
                {
                    sb.Append("-");
                }
                sb.Append((int)wholePart);
                return(sb.ToString());
            }

            SimpleFraction fract = null;

            try
            {
                //this should be the case because of the constructor
                if (exactDenom > 0)
                {
                    fract = SimpleFraction.BuildFractionExactDenominator(decPart, exactDenom);
                }
                else
                {
                    fract = SimpleFraction.BuildFractionMaxDenominator(decPart, maxDenom);
                }
            }
            catch (SimpleFractionException e)
            {
                return(doubleValue.ToString());
            }

            StringBuilder sb1 = new StringBuilder();

            //now format the results
            if (isNeg)
            {
                sb1.Append("-");
            }

            //if whole part has to go into the numerator
            if ("".Equals(wholePartFormatString))
            {
                int trueNum = (fract.Denominator * (int)wholePart) + fract.Numerator;
                sb1.Append(trueNum).Append("/").Append(fract.Denominator);
                return(sb1.ToString());
            }


            //short circuit if fraction is 0 or 1
            if (fract.Numerator == 0)
            {
                sb1.Append((int)wholePart);
                return(sb1.ToString());
            }
            else if (fract.Numerator == fract.Denominator)
            {
                sb1.Append((int)wholePart + 1);
                return(sb1.ToString());
            }
            //as mentioned above, this ignores the exact space formatting in Excel
            if (wholePart > 0)
            {
                sb1.Append((int)wholePart).Append(" ");
            }
            sb1.Append(fract.Numerator).Append("/").Append(fract.Denominator);
            return(sb1.ToString());
        }
 public void NegativeTestGetQuotient()
 {
     var fraction1 = new SimpleFraction(15, 7);
     var fraction2 = new SimpleFraction(0, 49);
     var result1   = SimpleFraction.GetQuotient(fraction1, fraction2);
 }
        public void TestToString()
        {
            var fraction1 = new SimpleFraction(3, 8);

            Assert.AreEqual("3/8", fraction1.ToString());
        }
 public void NegativeTestSingleArgumentConstructor()
 {
     var fraction = new SimpleFraction(0);
 }
 public void NegativeTestDoubleArgumentsConstructor()
 {
     var fraction = new SimpleFraction(25, 0);
 }