コード例 #1
0
        public void AlterConstructorExceptionsTest()
        {
            //Arrange
            int numerator = -1;
            int denominator = 0;

            //Act
            Fraction target = new Fraction(numerator, denominator);
        }
コード例 #2
0
        public void BaseConstructorExceptionsTest()
        {
            //Arrange
            ulong numerator = 1;
            ulong denominator = 0;
            bool isNegative = false;

            //Act
            Fraction target = new Fraction(numerator, denominator, isNegative);
        }
コード例 #3
0
ファイル: Fraction.cs プロジェクト: daveremy/FractionMath
 public static int LowestCommonDenominator(Fraction f1, Fraction f2)
 {
     int lowestDenominator = Math.Min(f1.Denominator, f2.Denominator);
     int highestDenominator = Math.Max(f1.Denominator, f2.Denominator);
     int lowestCommonDenominator = lowestDenominator;
     int multiple = 1;
     while (lowestCommonDenominator % highestDenominator != 0)
     {
         lowestCommonDenominator = lowestDenominator * ++multiple;
     }
     return lowestCommonDenominator;
 }
コード例 #4
0
        public void TestFractionCreation()
        {
            Fraction fraction = new Fraction("1/4");
            Assert.AreEqual(1, fraction.Numerator);
            Assert.AreEqual(4, fraction.Denominator);

            fraction = new Fraction("01/4");
            Assert.AreEqual(1, fraction.Numerator);
            Assert.AreEqual(4, fraction.Denominator);

            fraction = new Fraction(" 1/ 4");
            Assert.AreEqual(1, fraction.Numerator);
            Assert.AreEqual(4, fraction.Denominator);
        }
コード例 #5
0
        public string ConvertStringFromFraction(Fraction value, FractionToStringFormatTypes format = FractionToStringFormatTypes.ToFractional)
        {
            switch (format)
            {
                case FractionToStringFormatTypes.ToDecimal:
                    return value.ToDecimal(null).ToString();

                case FractionToStringFormatTypes.ToFractional:
                    return value.ToString();

                default:
                    throw new FormattingnErrorException("Unknown format");
            }
        }
コード例 #6
0
 public void TestBadFraction()
 {
     try
     {
         Fraction fraction = new Fraction("one/4");
         fraction = new Fraction("1");
         fraction = new Fraction("/");
         fraction = new Fraction("1/fourth");
         fraction = new Fraction(null);
     }
     catch (System.FormatException)
     {
         return;
     }
     Assert.Fail("Expected FormatException");
 }
コード例 #7
0
        public void OperatorSubtractionTest()
        {
            //Arrange
            Fraction a = new Fraction(-1, 2);
            Fraction b = new Fraction(1, 4);
            Fraction expected = new Fraction(-3, 4);

            //Act
            Fraction actual = (a - b);

            //Assert
            Assert.AreEqual(expected, actual);
        }
コード例 #8
0
        public void OperatorNegationTest()
        {
            //Arrange
            Fraction a = new Fraction(1, 2);
            Fraction expected = new Fraction(1, 2, true);

            //Act
            Fraction actual = -a;

            //Assert
            Assert.AreEqual(expected, actual);
        }
コード例 #9
0
        public void DecimalStringParsingTest()
        {
            //Arrange
            string teststring = "1.25";
            Fraction expected = new Fraction(10, 8);

            //Act
            Fraction actual = Fraction.Parse(teststring);

            //Assert
            Assert.AreEqual(expected, actual);
        }
コード例 #10
0
        private void BtnAdd_Click(object sender, EventArgs e)
        {
            // Verify Number will parse our entries. This version will use TryParse in the method VerifyNumbers().
            String badEntry = VerifyNumbers();


            // if VerifyNumbers didn't return null, it found bad character, break out of method and return to main for user to fix.
            if (badEntry != null)
            {
                labelResults.Text = "You entered an invalid number. Please enter a valid integer. Bad entry: " + badEntry;
                return;
            }

            // no bad entries found, continue with fraction creation and math!
            f1 = new MixedFraction(Convert.ToInt32(txtWhole1.Text), Convert.ToInt32(num1.Text),
                                   Convert.ToInt32(den1.Text));
            f2 = new MixedFraction(Convert.ToInt32(txtWhole2.Text), Convert.ToInt32(num2.Text),
                                   Convert.ToInt32(den2.Text));


//            // If I wanted to use Try/Catch: This code would replace above code. It will print out an error message.
//
//            try
//            {
//                f1 = new MixedFraction(Convert.ToInt32(txtWhole1.Text), Convert.ToInt32(num1.Text),
//                    Convert.ToInt32(den1.Text));
//                f2 = new MixedFraction(Convert.ToInt32(txtWhole2.Text), Convert.ToInt32(num2.Text),
//                    Convert.ToInt32(den2.Text));
//            }
//            catch (FormatException exception)
//            {
//                labelResults.Text = "Data entered was not valid. Enter valid integers. Error: " + exception.Message;
//                return;
//            }


            labelFraction1.Text = f1.GetString();
            labelFraction2.Text = f2.GetString();

            answer = new MixedFraction();
            answer.Add(f1, f2);

            // get mixed fractions for first and second entered fractions, print out as string
            MixedFraction mixedFraction1 = new MixedFraction();
            MixedFraction mixedFraction2 = new MixedFraction();

            mixedFraction1.ToMixedFraction(f1);
            mixedFraction2.ToMixedFraction(f2);

            labelFraction1.Text = mixedFraction1.GetMixedString();
            labelFraction2.Text = mixedFraction2.GetMixedString();

            // Convert improper Fraction to mixed fraction
            MixedFraction mixedAnswer = new MixedFraction();

            mixedAnswer.ToMixedFraction(answer);

            lblResult.Text = mixedAnswer.GetMixedString();

            txtWholeResult.Text = Convert.ToString(mixedAnswer.GetWhole());
            numResult.Text      = Convert.ToString(mixedAnswer.GetMixedNumerator());
            denResult.Text      = Convert.ToString(mixedAnswer.GetMixedDen());
            labelResults.Text   = ("All your numbers were valid!");
        }
コード例 #11
0
        public void EqualOperatorTest()
        {
            //Arrange
            Fraction x = new Fraction(6, 8);
            Fraction y = new Fraction(3, 4);
            Fraction z = new Fraction(12, 16);
            Fraction a = new Fraction(11, 16);

            object dec = 3M / 4M; ;

            Fraction k = null;
            Fraction l = null;

            //Assert
            Assert.IsTrue(x == x); //Рефлексивность
            Assert.AreEqual(x == y, y == x); //Симметричность
            Assert.AreEqual(y == z, x == z); //Транзитивность

            Assert.IsTrue(k == null);
            Assert.IsTrue(l == null);
            Assert.IsTrue(null == k);
            Assert.IsTrue(null == l);
            Assert.IsTrue(k == l);

            Assert.IsFalse(a == dec);

            Assert.IsFalse(a == k);
            Assert.IsFalse(z == null);

            Assert.IsTrue(a != k);
            Assert.IsTrue(l != z);
        }
コード例 #12
0
        public void StaticReduceNotCopyFractionTest()
        {
            //Arrange
            Fraction bigFraction = new Fraction(36, 63);
            Fraction expectedSmallFraction = new Fraction(4, 7);

            //Act
            var actualReducedFraction = Fraction.Reduce(bigFraction);

            //Assert
            Assert.IsTrue(System.Object.ReferenceEquals(bigFraction, actualReducedFraction));
            Assert.AreEqual(expectedSmallFraction.Numerator, actualReducedFraction.Numerator);
            Assert.AreEqual(expectedSmallFraction.Denominator, actualReducedFraction.Denominator);
            Assert.AreEqual(expectedSmallFraction.IsNegative, actualReducedFraction.IsNegative);
        }
コード例 #13
0
        public void MathDivisionNullLeftTest()
        {
            //Arrange
            Fraction a = null;
            Fraction b = new Fraction(1, 1); ;

            //Act
            Fraction actual = a / b;
        }
コード例 #14
0
        public void MathMultiplicationNullRightTest()
        {
            //Arrange
            Fraction a = new Fraction(1, 1); ;
            Fraction b = null;

            //Act
            Fraction actual = a * b;
        }
コード例 #15
0
        public void GetTypeCodeTest()
        {
            //Arrange
            Fraction target = new Fraction(1, 3);
            TypeCode expected = TypeCode.Object;

            //Act
            TypeCode actual = target.GetTypeCode();

            //Assert
            Assert.AreEqual(expected, actual);
        }
コード例 #16
0
        public void MathAdditionNullLeftTest()
        {
            //Arrange
            Fraction a = null;
            Fraction b = new Fraction(1, 1);

            //Act
            Fraction actual = a + b;
        }
コード例 #17
0
        public void GetHashCodeTest()
        {
            //Arrange
            var frac1 = new Fraction(0.5);
            var frac2 = new Fraction(1, 2);
            var frac3 = Fraction.Parse("-1/2");

            //Act
            Fraction frac2_alt = (Fraction)frac2.Clone();
            frac2_alt.Numerator += 1;

            //Assert
            Assert.AreEqual(frac1.GetHashCode(), frac2.GetHashCode());
            Assert.AreNotEqual(frac1.GetHashCode(), frac3.GetHashCode());
            Assert.AreNotEqual(frac2.GetHashCode(), frac2_alt.GetHashCode());
        }
コード例 #18
0
        public void FractionStringsParsingTest()
        {
            //Arrange
            string teststringVar1 = "-12/22";
            string teststringVar2 = "-6|11";
            string teststringVar3 = @"-24\44";
            string teststringVar4 = @"-24|44/unused_part";

            Fraction expected = new Fraction(-12, 22);

            //Act
            Fraction actualVar1 = Fraction.Parse(teststringVar1);
            Fraction actualVar2 = Fraction.Parse(teststringVar2);
            Fraction actualVar3 = Fraction.Parse(teststringVar3);
            Fraction actualVar4 = Fraction.Parse(teststringVar4);

            //Assert
            Assert.AreEqual(expected, actualVar1);
            Assert.AreEqual(expected, actualVar2);
            Assert.AreEqual(expected, actualVar3);
            Assert.AreEqual(expected, actualVar4);
        }
コード例 #19
0
        public void EqualsTest()
        {
            //Arrange
            Fraction x = new Fraction(6, 8);
            Fraction y = new Fraction(3, 4);
            Fraction z = new Fraction(1, 1);
            z.Numerator = 12;
            z.Denominator = 16;
            Fraction a = new Fraction(-12, 16);

            object k = x.Clone();
            object m = z.Clone();
            object notFrac = new Double();
            notFrac = 5;
            Fraction l = null;

            //Assert
            Assert.IsTrue(x.Equals(x)); //Рефлексивность
            Assert.AreEqual(x.Equals(y), y.Equals(x)); //Симметричность
            Assert.AreEqual(y.Equals(z), x.Equals(z)); //Транзитивность
            Assert.IsFalse(k.Equals(a));
            Assert.IsFalse(k.Equals(null));
            Assert.IsFalse(k.Equals(notFrac));
            Assert.IsTrue(k.Equals(m));

            Assert.IsFalse(k.Equals(l));
            Assert.IsTrue(y.Equals(k));
            Assert.IsFalse(z.Equals(null));
        }
コード例 #20
0
        public void ReduceFractionTest()
        {
            //Arrange
            Fraction actualFraction = new Fraction(36, 63);
            Fraction expectedFraction = new Fraction(4, 7);

            //Act
            actualFraction.Reduce();

            //Assert
            Assert.AreEqual(expectedFraction.Numerator, actualFraction.Numerator);
            Assert.AreEqual(expectedFraction.Denominator, actualFraction.Denominator);
            Assert.AreEqual(expectedFraction.IsNegative, actualFraction.IsNegative);
        }
コード例 #21
0
        public void MathSubstractionNullRightTest()
        {
            //Arrange
            Fraction a = new Fraction(1, 1); ;
            Fraction b = null;

            //Act
            Fraction actual = a - b;
        }
コード例 #22
0
        public void SortArraysOfFractions()
        {
            //Arrange
            var numberOfSamples = 5;

            Fraction[] fractions = new Fraction[numberOfSamples];
            ulong[] values = new ulong[numberOfSamples];

            for (int i = 0; i < numberOfSamples; i++)
            {
                fractions[i] = new Fraction(1, Convert.ToUInt32(rand.Next(2, 100)), false);
                values[i] = fractions[i].Denominator;
            }

            var orderComparer = new Mock<IComparer>();
            orderComparer.Setup(foo => foo.Compare(It.IsAny<ulong>(), It.IsAny<Fraction>())).Returns((ulong a, Fraction b) => (a.CompareTo(b.Denominator)));

            //Act
            Array.Sort(fractions);
            Array.Sort(values, (a, b) => { return -a.CompareTo(b); });

            //Assert
            CollectionAssert.AreEqual(values, fractions, orderComparer.Object);
        }
コード例 #23
0
 public void TestLowestCommonDenominator()
 {
     Fraction fourNinths = new Fraction("4/9");
     Fraction elevenTwelths = new Fraction("11/12");
     Assert.AreEqual(36, Fraction.LowestCommonDenominator(fourNinths, elevenTwelths));
 }
コード例 #24
0
 public void TestAddFractionSimple()
 {
     Fraction f1 = new Fraction("4/9");
     Fraction f2 = new Fraction("11/12");
     Assert.AreEqual(new Fraction("49/36"), f1 + f2);
 }
コード例 #25
0
ファイル: Fraction.cs プロジェクト: james-shively-iii/ITC255
 public Fraction(Fraction f)
 {
     //this constructor takes a fraction as parameter
     fract = f;
 }
コード例 #26
0
        public void OperatorAdditionTest()
        {
            //Arrange
            Fraction a = new Fraction(1, 2);
            Fraction b = new Fraction(1, 3);
            Fraction expected = new Fraction(5, 6);

            //Act
            Fraction actual = a + b;

            //Assert
            Assert.AreEqual(expected, actual);
        }
コード例 #27
0
 public void DenominatorPropertyExceptionsTest()
 {
     Fraction frac = new Fraction(1, 123);
     frac.Denominator *= 0;
 }
コード例 #28
0
ファイル: Fraction.cs プロジェクト: james-shively-iii/ITC255
        private void GreatestCommonDenominator(Fraction f1, Fraction f2, out Fraction f4, out Fraction f5)
        {
            f1.numerator   *= f2.denominator;
            f2.numerator   *= f1.denominator;
            f1.denominator *= f2.denominator;
            f2.denominator  = f1.denominator;

            f4 = f1;
            f5 = f2;
        }
コード例 #29
0
        public void ConstructionZeroTest()
        {
            //Arrange
            float number = 0;
            Fraction expected = new Fraction(0, 1);

            //Act
            var actual = new Fraction(number);

            //Assert
            Assert.AreEqual<ulong>(expected.Numerator, actual.Numerator);
            Assert.AreEqual<ulong>(expected.Denominator, actual.Denominator);
            Assert.AreEqual<bool>(expected.IsNegative, actual.IsNegative);
        }
コード例 #30
0
        public void OperatorDivisionRightZeroTest()
        {
            //Arrange
            Fraction a = new Fraction(1, 2);
            Fraction b = new Fraction(0, 1234567890);

            //Act
            Fraction actual = a / b;
        }
コード例 #31
0
        public void OperatorMultiplyRightNegativeTest()
        {
            //Arrange
            Fraction a = new Fraction(1, 2);
            Fraction b = new Fraction(-3, 4);
            Fraction expected = new Fraction(-3, 8);

            //Act
            Fraction actual = (a * b);

            //Assert
            Assert.AreEqual(expected, actual);
        }
コード例 #32
0
        public void OperatorDivisionRightNegativeTest()
        {
            //Arrange
            Fraction a = new Fraction(1, 2);
            Fraction b = new Fraction(-1, 3);
            Fraction expected = new Fraction(-3, 2);

            //Act
            Fraction actual = a / b;

            //Assert
            Assert.AreEqual(expected, actual);
        }
コード例 #33
0
        public void DecimalCapacityConstructionTest()
        {
            //Arrange
            var veryLongNumber = 123M / 888M;

            //Act
            Fraction frac = new Fraction(veryLongNumber);
        }