示例#1
0
        public static void TestStringEqualRoundTrip(ERational obj)
        {
            string    str    = obj.ToString();
            ERational newobj = ERational.FromString(str);
            string    str2   = newobj.ToString();

            TestCommon.AssertEqualsHashCode(obj, newobj);
            TestCommon.AssertEqualsHashCode(str, str2);
        }
示例#2
0
        public void TestEquals()
        {
            ERational era = ERational.FromString("0/3920");
            ERational erb = ERational.FromString("0/3920");

            TestCommon.CompareTestEqualAndConsistent(
                era,
                erb);
        }
示例#3
0
        public void TestToString()
        {
            var       fr = new RandomGenerator();
            ERational dec;

            for (var i = 0; i < 1000; ++i)
            {
                dec = RandomObjects.RandomERational(fr);
                ExtraTest.TestStringEqualRoundTrip(dec);
            }
            dec = ERational.FromString("-0/500");
            ExtraTest.TestStringEqualRoundTrip(dec);
        }
示例#4
0
        public static void TestStringEqualRoundTrip(ERational obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }
            string    str    = obj.ToString();
            ERational newobj = ERational.FromString(str);

            if (str.Length < 100 || !obj.Equals(newobj))
            {
                TestCommon.AssertEqualsHashCode(obj, newobj);
                string str2 = newobj.ToString();
                TestCommon.AssertEqualsHashCode(str, str2);
            }
        }
示例#5
0
        public void TestConversions()
        {
            var fr = new RandomGenerator();

            for (var i = 0; i < 20000; ++i)
            {
                bool      isNum, isTruncated, isInteger;
                EInteger  eint;
                ERational enumber = RandomObjects.RandomERational(fr);
                if (!enumber.IsFinite)
                {
                    try {
                        enumber.ToByteChecked();
                        Assert.Fail("Should have failed");
                    } catch (OverflowException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    Assert.AreEqual(
                        EInteger.Zero,
                        EInteger.FromByte(enumber.ToByteUnchecked()));
                    try {
                        enumber.ToByteIfExact();
                        Assert.Fail("Should have failed");
                    } catch (OverflowException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    try {
                        enumber.ToInt16Checked();
                        Assert.Fail("Should have failed");
                    } catch (OverflowException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    Assert.AreEqual(
                        EInteger.Zero,
                        EInteger.FromInt16(enumber.ToInt16Unchecked()));
                    try {
                        enumber.ToInt16IfExact();
                        Assert.Fail("Should have failed");
                    } catch (OverflowException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    try {
                        enumber.ToInt32Checked();
                        Assert.Fail("Should have failed");
                    } catch (OverflowException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    Assert.AreEqual(
                        EInteger.Zero,
                        EInteger.FromInt32(enumber.ToInt32Unchecked()));
                    try {
                        enumber.ToInt32IfExact();
                        Assert.Fail("Should have failed");
                    } catch (OverflowException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    try {
                        enumber.ToInt64Checked();
                        Assert.Fail("Should have failed");
                    } catch (OverflowException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    Assert.AreEqual(
                        EInteger.Zero,
                        EInteger.FromInt64(enumber.ToInt64Unchecked()));
                    try {
                        enumber.ToInt64IfExact();
                        Assert.Fail("Should have failed");
                    } catch (OverflowException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    continue;
                }
                ERational enumberInteger = ERational.FromEInteger(enumber.ToEInteger());
                isInteger = enumberInteger.CompareTo(enumber) == 0;
                eint      = enumber.ToEInteger();
                isNum     = enumber.CompareTo(
                    ERational.FromString("0")) >= 0 && enumber.CompareTo(
                    ERational.FromString("255")) <= 0;
                isTruncated = enumber.ToEInteger().CompareTo(
                    EInteger.FromString("0")) >= 0 && enumber.ToEInteger().CompareTo(
                    EInteger.FromString("255")) <= 0;
                if (isNum)
                {
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromByte(enumber.ToByteChecked()));
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromByte(enumber.ToByteUnchecked()));
                    if (isInteger)
                    {
                        TestCommon.AssertEquals(
                            eint,
                            EInteger.FromByte(enumber.ToByteIfExact()));
                    }
                    else
                    {
                        try {
                            enumber.ToByteIfExact();
                            Assert.Fail("Should have failed");
                        } catch (ArithmeticException) {
                            new Object();
                        } catch (Exception ex) {
                            Assert.Fail(ex.ToString());
                            throw new InvalidOperationException(String.Empty, ex);
                        }
                    }
                }
                else if (isTruncated)
                {
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromByte(enumber.ToByteChecked()));
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromByte(enumber.ToByteUnchecked()));
                    try {
                        enumber.ToByteIfExact();
                        Assert.Fail("Should have failed");
                    } catch (ArithmeticException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                }
                else
                {
                    try {
                        enumber.ToByteChecked();
                        Assert.Fail("Should have failed");
                    } catch (OverflowException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    try {
                        enumber.ToByteUnchecked();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    if (isInteger)
                    {
                        try {
                            enumber.ToByteIfExact();
                            Assert.Fail("Should have failed");
                        } catch (OverflowException) {
                            new Object();
                        } catch (Exception ex) {
                            Assert.Fail(ex.ToString());
                            throw new InvalidOperationException(String.Empty, ex);
                        }
                    }
                    else
                    {
                        try {
                            enumber.ToByteIfExact();
                            Assert.Fail("Should have failed");
                        } catch (ArithmeticException) {
                            new Object();
                        } catch (Exception ex) {
                            Assert.Fail(ex.ToString());
                            throw new InvalidOperationException(String.Empty, ex);
                        }
                    }
                }
                isNum = enumber.CompareTo(
                    ERational.FromString("-32768")) >= 0 && enumber.CompareTo(
                    ERational.FromString("32767")) <= 0;
                isTruncated = enumber.ToEInteger().CompareTo(
                    EInteger.FromString("-32768")) >= 0 && enumber.ToEInteger().CompareTo(
                    EInteger.FromString("32767")) <= 0;
                if (isNum)
                {
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromInt16(enumber.ToInt16Checked()));
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromInt16(enumber.ToInt16Unchecked()));
                    if (isInteger)
                    {
                        TestCommon.AssertEquals(
                            eint,
                            EInteger.FromInt16(enumber.ToInt16IfExact()));
                    }
                    else
                    {
                        try {
                            enumber.ToInt16IfExact();
                            Assert.Fail("Should have failed");
                        } catch (ArithmeticException) {
                            new Object();
                        } catch (Exception ex) {
                            Assert.Fail(ex.ToString());
                            throw new InvalidOperationException(String.Empty, ex);
                        }
                    }
                }
                else if (isTruncated)
                {
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromInt16(enumber.ToInt16Checked()));
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromInt16(enumber.ToInt16Unchecked()));
                    try {
                        enumber.ToInt16IfExact();
                        Assert.Fail("Should have failed");
                    } catch (ArithmeticException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                }
                else
                {
                    try {
                        enumber.ToInt16Checked();
                        Assert.Fail("Should have failed");
                    } catch (OverflowException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    try {
                        enumber.ToInt16Unchecked();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    if (isInteger)
                    {
                        try {
                            enumber.ToInt16IfExact();
                            Assert.Fail("Should have failed");
                        } catch (OverflowException) {
                            new Object();
                        } catch (Exception ex) {
                            Assert.Fail(ex.ToString());
                            throw new InvalidOperationException(String.Empty, ex);
                        }
                    }
                    else
                    {
                        try {
                            enumber.ToInt16IfExact();
                            Assert.Fail("Should have failed");
                        } catch (ArithmeticException) {
                            new Object();
                        } catch (Exception ex) {
                            Assert.Fail(ex.ToString());
                            throw new InvalidOperationException(String.Empty, ex);
                        }
                    }
                }
                isNum = enumber.CompareTo(
                    ERational.FromString("-2147483648")) >= 0 && enumber.CompareTo(
                    ERational.FromString("2147483647")) <= 0;
                isTruncated = enumber.ToEInteger().CompareTo(
                    EInteger.FromString("-2147483648")) >= 0 &&
                              enumber.ToEInteger().CompareTo(
                    EInteger.FromString("2147483647")) <= 0;
                if (isNum)
                {
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromInt32(enumber.ToInt32Checked()));
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromInt32(enumber.ToInt32Unchecked()));
                    if (isInteger)
                    {
                        TestCommon.AssertEquals(
                            eint,
                            EInteger.FromInt32(enumber.ToInt32IfExact()));
                    }
                    else
                    {
                        try {
                            enumber.ToInt32IfExact();
                            Assert.Fail("Should have failed");
                        } catch (ArithmeticException) {
                            new Object();
                        } catch (Exception ex) {
                            Assert.Fail(ex.ToString());
                            throw new InvalidOperationException(String.Empty, ex);
                        }
                    }
                }
                else if (isTruncated)
                {
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromInt32(enumber.ToInt32Checked()));
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromInt32(enumber.ToInt32Unchecked()));
                    try {
                        enumber.ToInt32IfExact();
                        Assert.Fail("Should have failed");
                    } catch (ArithmeticException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                }
                else
                {
                    try {
                        enumber.ToInt32Checked();
                        Assert.Fail("Should have failed");
                    } catch (OverflowException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    try {
                        enumber.ToInt32Unchecked();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    if (isInteger)
                    {
                        try {
                            enumber.ToInt32IfExact();
                            Assert.Fail("Should have failed");
                        } catch (OverflowException) {
                            new Object();
                        } catch (Exception ex) {
                            Assert.Fail(ex.ToString());
                            throw new InvalidOperationException(String.Empty, ex);
                        }
                    }
                    else
                    {
                        try {
                            enumber.ToInt32IfExact();
                            Assert.Fail("Should have failed");
                        } catch (ArithmeticException) {
                            new Object();
                        } catch (Exception ex) {
                            Assert.Fail(ex.ToString());
                            throw new InvalidOperationException(String.Empty, ex);
                        }
                    }
                }
                isNum = enumber.CompareTo(
                    ERational.FromString("-9223372036854775808")) >= 0 && enumber.CompareTo(
                    ERational.FromString("9223372036854775807")) <= 0;
                isTruncated = enumber.ToEInteger().CompareTo(
                    EInteger.FromString("-9223372036854775808")) >= 0 &&
                              enumber.ToEInteger().CompareTo(
                    EInteger.FromString("9223372036854775807")) <= 0;
                if (isNum)
                {
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromInt64(enumber.ToInt64Checked()));
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromInt64(enumber.ToInt64Unchecked()));
                    if (isInteger)
                    {
                        TestCommon.AssertEquals(
                            eint,
                            EInteger.FromInt64(enumber.ToInt64IfExact()));
                    }
                    else
                    {
                        try {
                            enumber.ToInt64IfExact();
                            Assert.Fail("Should have failed");
                        } catch (ArithmeticException) {
                            new Object();
                        } catch (Exception ex) {
                            Assert.Fail(ex.ToString());
                            throw new InvalidOperationException(String.Empty, ex);
                        }
                    }
                }
                else if (isTruncated)
                {
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromInt64(enumber.ToInt64Checked()));
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromInt64(enumber.ToInt64Unchecked()));
                    try {
                        enumber.ToInt64IfExact();
                        Assert.Fail("Should have failed");
                    } catch (ArithmeticException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                }
                else
                {
                    try {
                        enumber.ToInt64Checked();
                        Assert.Fail("Should have failed");
                    } catch (OverflowException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    try {
                        enumber.ToInt64Unchecked();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    if (isInteger)
                    {
                        try {
                            enumber.ToInt64IfExact();
                            Assert.Fail("Should have failed");
                        } catch (OverflowException) {
                            new Object();
                        } catch (Exception ex) {
                            Assert.Fail(ex.ToString());
                            throw new InvalidOperationException(String.Empty, ex);
                        }
                    }
                    else
                    {
                        try {
                            enumber.ToInt64IfExact();
                            Assert.Fail("Should have failed");
                        } catch (ArithmeticException) {
                            new Object();
                        } catch (Exception ex) {
                            Assert.Fail(ex.ToString());
                            throw new InvalidOperationException(String.Empty, ex);
                        }
                    }
                }
            }
        }
示例#6
0
        public void TestFromString()
        {
            ERational er;

            er = ERational.FromString("-2/4");
            Assert.AreEqual(EInteger.FromInt32(-2), er.Numerator);
            Assert.AreEqual(EInteger.FromInt32(4), er.Denominator);
            er = ERational.FromString("2/4");
            Assert.AreEqual(EInteger.FromInt32(2), er.Numerator);
            Assert.AreEqual(EInteger.FromInt32(4), er.Denominator);
            er = ERational.FromString("293939393939/4");
            Assert.AreEqual(EInteger.FromString("293939393939"), er.Numerator);
            Assert.AreEqual(EInteger.FromInt32(4), er.Denominator);
            er = ERational.FromString("-293939393939/4");
            Assert.AreEqual(EInteger.FromString("-293939393939"), er.Numerator);
            Assert.AreEqual(EInteger.FromInt32(4), er.Denominator);
            er = ERational.FromString("-2/293939393939");
            Assert.AreEqual(EInteger.FromInt32(-2), er.Numerator);
            Assert.AreEqual(EInteger.FromString("293939393939"), er.Denominator);
            er = ERational.FromString("-2");
            Assert.AreEqual(EInteger.FromString("-2"), er.Numerator);
            Assert.AreEqual(EInteger.FromInt32(1), er.Denominator);
            er = ERational.FromString("2");
            Assert.AreEqual(EInteger.FromString("2"), er.Numerator);
            Assert.AreEqual(EInteger.FromInt32(1), er.Denominator);
            try {
                ERational.FromString("-2x");
                Assert.Fail("Should have failed");
            } catch (FormatException) {
                new Object();
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
            try {
                ERational.FromString("-2/");
                Assert.Fail("Should have failed");
            } catch (FormatException) {
                new Object();
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
            try {
                ERational.FromString("-2/x");
                Assert.Fail("Should have failed");
            } catch (FormatException) {
                new Object();
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
            try {
                ERational.FromString("-2/2x");
                Assert.Fail("Should have failed");
            } catch (FormatException) {
                new Object();
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
            var fr = new RandomGenerator();

            for (var i = 0; i < 1000; ++i)
            {
                EInteger ei1 = RandomObjects.RandomEInteger(fr);
                EInteger ei2 = RandomObjects.RandomEInteger(fr).Abs();
                if (ei2.IsZero)
                {
                    ei2 = EInteger.One;
                }
                er = ERational.FromString(ei1 + "/" + ei2);
                Assert.AreEqual(ei1, er.Numerator);
                Assert.AreEqual(ei2, er.Denominator);
            }
        }