コード例 #1
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);
                        }
                    }
                }
            }
        }