コード例 #1
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());
        }
コード例 #2
0
        public void CompareToTest()
        {
            //Arrange
            Fraction a = new Fraction(2, 5);
            Fraction a_str = new Fraction(20, 50);
            Fraction b = new Fraction(1, 2);
            Fraction c = b.Clone() as Fraction;
            c.Numerator *= 2;
            c.Denominator *= 2;
            Fraction d = -(b.Clone() as Fraction);

            //Act
            var result1 = b.CompareTo(a);
            var result2 = c.CompareTo(b);
            var result3 = d.CompareTo(a);
            var result4 = a.CompareTo(a_str);

            //Assert
            Assert.AreEqual(1, result1);
            Assert.AreEqual(0, result2);
            Assert.AreEqual(-1, result3);
            Assert.AreEqual(0, result4);
        }
コード例 #3
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));
        }
コード例 #4
0
        public void CloneFractionTest()
        {
            //Arrange
            ulong numerator = 123;
            ulong denominatior = 456;
            Fraction srcFraction = new Fraction(numerator, denominatior);
            Fraction expectedFraction = new Fraction(numerator, denominatior);

            //Act
            Fraction dstFraction = srcFraction.Clone() as Fraction;

            //Assert
            Assert.AreEqual(expectedFraction.Numerator, dstFraction.Numerator);
            Assert.AreEqual(expectedFraction.Denominator, dstFraction.Denominator);
            Assert.AreEqual(expectedFraction.IsNegative, dstFraction.IsNegative);
        }