Description of RandomObjects.
示例#1
0
        public static CBORObject RandomNumberOrRational(RandomGenerator rand)
        {
            switch (rand.UniformInt(7))
            {
            case 0:
                return(CBORObject.FromObject(
                           RandomObjects.RandomDouble(
                               rand,
                               Int32.MaxValue)));

            case 1:
                return(CBORObject.FromObject(
                           RandomObjects.RandomSingle(
                               rand,
                               Int32.MaxValue)));

            case 2:
                return(CBORObject.FromObject(RandomObjects.RandomEInteger(rand)));

            case 3:
                return(CBORObject.FromObject(RandomObjects.RandomEFloat(rand)));

            case 4:
                return
                    (CBORObject.FromObject(RandomObjects.RandomEDecimal(rand)));

            case 5:
                return(CBORObject.FromObject(RandomObjects.RandomInt64(rand)));

            case 6:
                return(CBORObject.FromObject(RandomObjects.RandomERational(rand)));

            default: throw new ArgumentException();
            }
        }
示例#2
0
        public void TestCompareTo()
        {
            var r = new RandomGenerator();

            for (var i = 0; i < 500; ++i)
            {
                ERational bigintA = RandomObjects.RandomERational(r);
                ERational bigintB = RandomObjects.RandomERational(r);
                ERational bigintC = RandomObjects.RandomERational(r);
                TestCommon.CompareTestRelations(bigintA, bigintB, bigintC);
            }
            TestCommon.CompareTestLess(ERational.Zero, ERational.NaN);
            ERational rat, rat2;

            for (var i = 0; i < 100; ++i)
            {
                EInteger num = RandomObjects.RandomEInteger(r);
                if (num.IsZero)
                {
                    // Skip if number is 0; 0/1 and 0/2 are
                    // equal in that case
                    continue;
                }
                num  = num.Abs();
                rat  = ERational.Create(num, EInteger.One);
                rat2 = ERational.Create(num, (EInteger)2);
                TestCommon.CompareTestLess(rat2, rat);
                TestCommon.CompareTestGreater(rat, rat2);
            }
            TestCommon.CompareTestLess(
                ERational.Create(EInteger.One, (EInteger)2),
                ERational.Create((EInteger)4, EInteger.One));
            for (var i = 0; i < 100; ++i)
            {
                EInteger num = RandomObjects.RandomEInteger(r);
                EInteger den = RandomObjects.RandomEInteger(r);
                if (den.IsZero)
                {
                    den = EInteger.One;
                }
                rat = ERational.Create(num, den);
                for (int j = 0; j < 10; ++j)
                {
                    EInteger num2 = num;
                    EInteger den2 = den;
                    EInteger mult = RandomObjects.RandomEInteger(r);
                    if (mult.IsZero || mult.Equals(EInteger.One))
                    {
                        mult = (EInteger)2;
                    }
                    num2 *= (EInteger)mult;
                    den2 *= (EInteger)mult;
                    rat2  = ERational.Create(num2, den2);
                    TestCommon.CompareTestEqual(rat, rat2);
                }
            }
        }
示例#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
 private static EDecimal RandomEDecimalLowExponent(IRandomGenExtended rand)
 {
     while (true)
     {
         EDecimal ef = RandomObjects.RandomEDecimal(rand);
         if (
             ef.Exponent.CompareTo(-20000) >= 0 &&
             ef.Exponent.CompareTo(20000) <= 0)
         {
             return(ef);
         }
     }
 }
示例#5
0
        public void TestCompareToDecimal()
        {
            var fr = new RandomGenerator();

            for (var i = 0; i < 100; ++i)
            {
                ERational er  = RandomObjects.RandomERational(fr);
                int       exp = -100000 + fr.UniformInt(200000);
                EDecimal  ed  = EDecimal.Create(
                    RandomObjects.RandomEInteger(fr),
                    (EInteger)exp);
                ERational er2 = ERational.FromEDecimal(ed);
                int       c2r = er.CompareTo(er2);
                int       c2d = er.CompareToDecimal(ed);
                Assert.AreEqual(c2r, c2d);
            }
        }
示例#6
0
        public static CBORObject RandomNumberOrRational(IRandomGenExtended rand)
        {
            object o = null;

            switch (rand.GetInt32(7))
            {
            case 0:
                o = RandomObjects.RandomDouble(
                    rand,
                    Int32.MaxValue);
                return(CBORObject.FromObject(o));

            case 1:
                o = RandomObjects.RandomSingle(
                    rand,
                    Int32.MaxValue);
                return(CBORObject.FromObject(o));

            case 2:
                return(CBORObject.FromObject(
                           RandomObjects.RandomEInteger(rand)));

            case 3:
                return(CBORObject.FromObject(
                           RandomObjects.RandomEFloat(rand)));

            case 4:
                o = RandomObjects.RandomEDecimal(rand);
                return(CBORObject.FromObject(o));

            case 5:
                o = RandomObjects.RandomInt64(rand);
                return(CBORObject.FromObject(o));

            case 6:
                o = RandomObjects.RandomERational(rand);
                return(CBORObject.FromObject(o));

            default: throw new InvalidOperationException();
            }
        }
示例#7
0
        public static CBORObject RandomNumberOrRational(RandomGenerator rand)
        {
            object o = null;

            switch (rand.UniformInt(7))
            {
            case 0:
                o = RandomObjects.RandomDouble(
                    rand,
                    Int32.MaxValue);
                return(ToObjectTest.TestToFromObjectRoundTrip(o));

            case 1:
                o = RandomObjects.RandomSingle(
                    rand,
                    Int32.MaxValue);
                return(ToObjectTest.TestToFromObjectRoundTrip(o));

            case 2:
                return(ToObjectTest.TestToFromObjectRoundTrip(
                           RandomObjects.RandomEInteger(rand)));

            case 3:
                return(ToObjectTest.TestToFromObjectRoundTrip(
                           RandomObjects.RandomEFloat(rand)));

            case 4:
                o = RandomObjects.RandomEDecimal(rand);
                return(ToObjectTest.TestToFromObjectRoundTrip(o));

            case 5:
                o = RandomObjects.RandomInt64(rand);
                return(ToObjectTest.TestToFromObjectRoundTrip(o));

            case 6:
                o = RandomObjects.RandomERational(rand);
                return(ToObjectTest.TestToFromObjectRoundTrip(o));

            default: throw new InvalidOperationException();
            }
        }
示例#8
0
        public static CBORObject RandomCBORObject(RandomGenerator rand, int
                                                  depth)
        {
            int nextval = rand.UniformInt(11);

            switch (nextval)
            {
            case 0:
            case 1:
            case 2:
            case 3:
                return(RandomNumberOrRational(rand));

            case 4:
                return(rand.UniformInt(2) == 0 ? CBORObject.True : CBORObject.False);

            case 5:
                return(rand.UniformInt(2) == 0 ? CBORObject.Null :
                       CBORObject.Undefined);

            case 6:
                return(ToObjectTest.TestToFromObjectRoundTrip(
                           RandomObjects.RandomTextString(rand)));

            case 7:
                return(ToObjectTest.TestToFromObjectRoundTrip(
                           RandomObjects.RandomByteString(rand)));

            case 8:
                return(RandomCBORArray(rand, depth));

            case 9:
                return(RandomCBORMap(rand, depth));

            case 10:
                return(RandomCBORTaggedObject(rand, depth));

            default: return(RandomNumber(rand));
            }
        }
示例#9
0
        public void TestDecimalString()
        {
            var fr = new RandomGenerator();

            for (var i = 0; i < 1000; ++i)
            {
                EDecimal ed = RandomObjects.RandomEDecimal(fr);
                if (!ed.IsFinite)
                {
                    continue;
                }
                decimal d;
                try {
                    System.Globalization.NumberStyles numstyles =
                        System.Globalization.NumberStyles.AllowExponent |
                        System.Globalization.NumberStyles.Number;
                    d = Decimal.Parse(
                        ed.ToString(),
                        numstyles,
                        System.Globalization.CultureInfo.InvariantCulture);
                    EDecimal ed3 = EDecimal.FromString(
                        ed.ToString(),
                        EContext.CliDecimal);
                    string msg = ed.ToString() + " (expanded: " +
                                 EDecimal.FromString(ed.ToString()) + ")";
                    TestCommon.CompareTestEqual(
                        (EDecimal)d,
                        ed3,
                        msg);
                } catch (OverflowException ex) {
                    EDecimal ed2 = EDecimal.FromString(
                        ed.ToString(),
                        EContext.CliDecimal);
                    Assert.IsTrue(
                        ed2.IsInfinity(),
                        ed.ToString(),
                        ex.ToString());
                }
            }
        }
示例#10
0
        public static CBORObject RandomCBORObject(IRandomGenExtended rand, int
                                                  depth)
        {
            int nextval = rand.GetInt32(11);

            switch (nextval)
            {
            case 0:
            case 1:
            case 2:
            case 3:
                return(RandomNumberOrRational(rand));

            case 4:
                return(rand.GetInt32(2) == 0 ? CBORObject.True : CBORObject.False);

            case 5:
                return(rand.GetInt32(2) == 0 ? CBORObject.Null :
                       CBORObject.Undefined);

            case 6:
                return(CBORObject.FromObject(
                           RandomObjects.RandomTextString(rand)));

            case 7:
                return(CBORObject.FromObject(
                           RandomObjects.RandomByteString(rand)));

            case 8:
                return(RandomCBORArray(rand, depth));

            case 9:
                return(RandomCBORMap(rand, depth));

            case 10:
                return(RandomCBORTaggedObject(rand, depth));

            default: return(RandomNumber(rand));
            }
        }
示例#11
0
        public void TestRemainder()
        {
            var fr = new RandomGenerator();

            for (var i = 0; i < 100; ++i)
            {
                ERational er;
                ERational er2;
                er = ERational.Create(
                    RandomObjects.RandomEInteger(fr),
                    EInteger.One);
                er2 = ERational.Create(
                    RandomObjects.RandomEInteger(fr),
                    EInteger.One);
                if (er2.IsZero || !er2.IsFinite)
                {
                    continue;
                }
                if (er.IsZero || !er.IsFinite)
                {
                    // Code below will divide by "er",
                    // so skip if "er" is zero
                    continue;
                }
                ERational ermult = er.Multiply(er2);
                ERational erdiv  = ermult.Divide(er);
                erdiv = ermult.Remainder(er);
                if (!erdiv.IsZero)
                {
                    Assert.Fail(ermult + "; " + er);
                }
                erdiv = ermult.Remainder(er2);
                if (!erdiv.IsZero)
                {
                    Assert.Fail(er + "; " + er2);
                }
            }
        }
示例#12
0
        public void TestDivide()
        {
            var fr = new RandomGenerator();

            for (var i = 0; i < 500; ++i)
            {
                ERational er  = RandomObjects.RandomERational(fr);
                ERational er2 = RandomObjects.RandomERational(fr);
                if (er2.IsZero || !er2.IsFinite)
                {
                    continue;
                }
                if (er.IsZero || !er.IsFinite)
                {
                    continue;
                }
                ERational ermult = er.Multiply(er2);
                ERational erdiv  = ermult.Divide(er);
                TestCommon.CompareTestEqual(erdiv, er2);
                erdiv = ermult.Divide(er2);
                TestCommon.CompareTestEqual(erdiv, er);
            }
        }
示例#13
0
        public static CBORObject RandomCBORTaggedObject(
            RandomGenerator rand,
            int depth)
        {
            var tag = 0;

            if (rand.UniformInt(2) == 0)
            {
                int[] tagselection =
                {
                    2,  2, 2,  3,  3,  3, 4, 4, 4, 5, 5, 5, 30, 30,
                    30, 0, 1, 25, 26, 27,
                };
                tag = tagselection[rand.UniformInt(tagselection.Length)];
            }
            else
            {
                tag = rand.UniformInt(0x1000000);
            }
            if (tag == 25)
            {
                tag = 0;
            }
            if (tag == 30)
            {
                object o = RandomObjects.RandomByteString(rand);
                return(ToObjectTest.TestToFromObjectRoundTrip(o));
            }
            {
                CBORObject cbor;
                // Console.WriteLine("tag "+tag+" "+i);
                if (tag == 0 || tag == 1 || tag == 28 || tag == 29)
                {
                    tag = 999;
                }
                if (tag == 2 || tag == 3)
                {
                    object o = RandomObjects.RandomByteStringShort(rand);
                    cbor = ToObjectTest.TestToFromObjectRoundTrip(o);
                }
                else if (tag == 4 || tag == 5)
                {
                    cbor = CBORObject.NewArray();
                    object o = RandomObjects.RandomSmallIntegral(rand);
                    cbor.Add(ToObjectTest.TestToFromObjectRoundTrip(o));
                    o = RandomObjects.RandomEInteger(rand);
                    cbor.Add(ToObjectTest.TestToFromObjectRoundTrip(o));
                }
                else if (tag == 30)
                {
                    cbor = CBORObject.NewArray();
                    object o = RandomObjects.RandomSmallIntegral(rand);
                    cbor.Add(ToObjectTest.TestToFromObjectRoundTrip(o));
                    o = RandomObjects.RandomEInteger(rand);
                    cbor.Add(ToObjectTest.TestToFromObjectRoundTrip(o));
                }
                else
                {
                    cbor = RandomCBORObject(rand, depth + 1);
                }
                try {
                    cbor = CBORObject.FromObjectAndTag(cbor, tag);
                    // Console.WriteLine("done");
                    return(cbor);
                } catch (Exception) {
                    return(CBORObject.FromObjectAndTag(cbor, 999));
                }
            }
        }
示例#14
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);
            }
        }
示例#15
0
        public static CBORObject RandomCBORTaggedObject(
            IRandomGenExtended rand,
            int depth)
        {
            var tag = 0;

            if (rand.GetInt32(2) == 0)
            {
                int[] tagselection =
                {
                    2,  2, 2,  3,  3,  3, 4, 4, 4, 5, 5, 5, 30, 30,
                    30, 0, 1, 25, 26, 27,
                };
                tag = tagselection[rand.GetInt32(tagselection.Length)];
            }
            else if (rand.GetInt32(100) < 90)
            {
                return(CBORObject.FromObjectAndTag(
                           RandomCBORObject(rand, depth + 1),
                           rand.GetInt32(0x100000)));
            }
            else
            {
                return(CBORObject.FromObjectAndTag(
                           RandomCBORObject(rand, depth + 1),
                           RandomEIntegerMajorType0(rand)));
            }
            if (tag == 25)
            {
                tag = 0;
            }
            if (tag == 30)
            {
                object o = RandomObjects.RandomByteString(rand);
                return(CBORObject.FromObject(o));
            }
            {
                CBORObject cbor;
                // Console.WriteLine("tag "+tag+" "+i);
                if (tag == 0 || tag == 1 || tag == 28 || tag == 29)
                {
                    tag = 999;
                }
                if (tag == 2 || tag == 3)
                {
                    object o = RandomObjects.RandomByteStringShort(rand);
                    cbor = CBORObject.FromObject(o);
                }
                else if (tag == 4 || tag == 5)
                {
                    cbor = CBORObject.NewArray();
                    object o = RandomObjects.RandomSmallIntegral(rand);
                    cbor.Add(o);
                    o = RandomObjects.RandomEInteger(rand);
                    cbor.Add(o);
                }
                else if (tag == 30)
                {
                    cbor = CBORObject.NewArray();
                    object o = RandomObjects.RandomSmallIntegral(rand);
                    cbor.Add(o);
                    o = RandomObjects.RandomEInteger(rand);
                    cbor.Add(o);
                }
                else
                {
                    cbor = RandomCBORObject(rand, depth + 1);
                }
                return(CBORObject.FromObjectAndTag(cbor, tag));
            }
        }
示例#16
0
        public void TestDecimalString()
        {
            var fr = new RandomGenerator();

            // var sw = new System.Diagnostics.Stopwatch();
            // var sw2 = new System.Diagnostics.Stopwatch();
            // var sw3 = new System.Diagnostics.Stopwatch();
            for (var i = 0; i < 10000; ++i)
            {
                if (i % 100 == 0)
                {
                    // Console.WriteLine(i + " sw=" +
                    // sw.ElapsedMilliseconds + ", " + (sw2.ElapsedMilliseconds) +
                    // ", " + (sw3.ElapsedMilliseconds));
                }
                // sw3.Start();
                EDecimal ed = RandomObjects.RandomEDecimal(fr);
                // sw3.Stop();
                // Reduce to Decimal128. Without this reduction,
                // Decimal.Parse would run significantly more slowly
                // on average for random
                // EDecimals than
                // EDecimal.FromString(CliDecimal) does.
                // Decimal128 covers all numbers representable
                // in a CliDecimal.
                ed = ed.RoundToPrecision(EContext.Decimal128);

                if (!ed.IsFinite)
                {
                    continue;
                }
                string  edString = ed.ToString();
                decimal d;
                try {
                    System.Globalization.NumberStyles numstyles =
                        System.Globalization.NumberStyles.AllowExponent |
                        System.Globalization.NumberStyles.Number;
                    // sw.Start();
                    d = Decimal.Parse(
                        edString,
                        numstyles,
                        System.Globalization.CultureInfo.InvariantCulture);
                    // sw.Stop();
                    // sw2.Start();
                    EDecimal ed3 = EDecimal.FromString(
                        edString,
                        EContext.CliDecimal);
                    // sw2.Stop();
                    var edd = (EDecimal)d;
                    if (!edd.Equals(ed3))
                    {
                        string msg = ed.ToString() + " (expanded: " +
                                     EDecimal.FromString(ed.ToString()) + ")";
                        TestCommon.CompareTestEqual(
                            (EDecimal)d,
                            ed3,
                            msg);
                    }
                    // sw3.Stop();
                } catch (OverflowException ex) {
                    EDecimal ed2 = EDecimal.FromString(
                        edString,
                        EContext.CliDecimal);
                    if (!ed2.IsInfinity())
                    {
                        Assert.Fail(edString + "\n" + ex.ToString());
                    }
                }
            }
        }
示例#17
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);
                        }
                    }
                }
            }
        }
示例#18
0
        public static CBORObject RandomCBORTaggedObject(
            RandomGenerator rand,
            int depth)
        {
            var tag = 0;

            if (rand.UniformInt(2) == 0)
            {
                int[] tagselection = { 2,  2, 2,  3,  3, 3, 4, 4, 4, 5, 5, 5, 30, 30,
                                       30, 0, 1, 25, 26, 27 };
                tag = tagselection[rand.UniformInt(tagselection.Length)];
            }
            else
            {
                tag = rand.UniformInt(0x1000000);
            }
            if (tag == 25)
            {
                tag = 0;
            }
            if (tag == 30)
            {
                return(CBORObject.FromObject(RandomObjects.RandomByteString(rand)));
            }
            for (var i = 0; i < 15; ++i)
            {
                CBORObject o;
                // Console.WriteLine("tag "+tag+" "+i);
                if (tag == 0 || tag == 1 || tag == 28 || tag == 29)
                {
                    tag = 999;
                }
                if (tag == 2 || tag == 3)
                {
                    o = CBORObject.FromObject(RandomObjects.RandomByteStringShort(rand));
                }
                else if (tag == 4 || tag == 5)
                {
                    o = CBORObject.NewArray();
                    o.Add(CBORObject.FromObject(RandomObjects.RandomSmallIntegral(rand)));
                    o.Add(CBORObject.FromObject(RandomObjects.RandomEInteger(rand)));
                }
                else if (tag == 30)
                {
                    o = CBORObject.NewArray();
                    o.Add(CBORObject.FromObject(RandomObjects.RandomSmallIntegral(rand)));
                    o.Add(CBORObject.FromObject(RandomObjects.RandomEInteger(rand)));
                }
                else
                {
                    o = RandomCBORObject(rand, depth + 1);
                }
                try {
                    o = CBORObject.FromObjectAndTag(o, tag);
                    // Console.WriteLine("done");
                    return(o);
                } catch (Exception) {
                    continue;
                }
            }
            // Console.WriteLine("Failed "+tag);
            return(CBORObject.Null);
        }