Ejemplo n.º 1
0
 public void TestAsEFloat()
 {
     {
         object objectTemp  = CBORTestCommon.FloatPosInf;
         object objectTemp2 =
             ToObjectTest.TestToFromObjectRoundTrip(Single.PositiveInfinity)
             .AsNumber().ToEFloat();
         Assert.AreEqual(objectTemp, objectTemp2);
     }
     {
         object objectTemp  = CBORTestCommon.FloatNegInf;
         object objectTemp2 =
             ToObjectTest.TestToFromObjectRoundTrip(Single.NegativeInfinity)
             .AsNumber().ToEFloat();
         Assert.AreEqual(objectTemp, objectTemp2);
     }
     Assert.IsTrue(ToObjectTest.TestToFromObjectRoundTrip(Single.NaN)
                   .AsNumber().IsNaN());
     {
         object objectTemp  = CBORTestCommon.FloatPosInf;
         object objectTemp2 =
             ToObjectTest.TestToFromObjectRoundTrip(Double.PositiveInfinity)
             .AsNumber().ToEFloat();
         Assert.AreEqual(objectTemp, objectTemp2);
     }
     {
         object objectTemp  = CBORTestCommon.FloatNegInf;
         object objectTemp2 =
             ToObjectTest.TestToFromObjectRoundTrip(Double.NegativeInfinity)
             .AsNumber().ToEFloat();
         Assert.AreEqual(objectTemp, objectTemp2);
     }
     Assert.IsTrue(ToObjectTest.TestToFromObjectRoundTrip(Double.NaN)
                   .AsNumber().IsNaN());
 }
Ejemplo n.º 2
0
        public void TestUUID()
        {
            CBORObject obj =
                ToObjectTest.TestToFromObjectRoundTrip(new Guid(
                                                           "00112233-4455-6677-8899-AABBCCDDEEFF"));

            Assert.AreEqual(CBORType.ByteString, obj.Type);
            Assert.AreEqual(EInteger.FromString("37"), obj.MostInnerTag);
            byte[] bytes = obj.GetByteString();
            Assert.AreEqual(16, bytes.Length);
            Assert.AreEqual(0x00, bytes[0]);
            Assert.AreEqual(0x11, bytes[1]);
            Assert.AreEqual(0x22, bytes[2]);
            Assert.AreEqual(0x33, bytes[3]);
            Assert.AreEqual(0x44, bytes[4]);
            Assert.AreEqual(0x55, bytes[5]);
            Assert.AreEqual(0x66, bytes[6]);
            Assert.AreEqual(0x77, bytes[7]);
            Assert.AreEqual((byte)0x88, bytes[8]);
            Assert.AreEqual((byte)0x99, bytes[9]);
            Assert.AreEqual((byte)0xaa, bytes[10]);
            Assert.AreEqual((byte)0xbb, bytes[11]);
            Assert.AreEqual((byte)0xcc, bytes[12]);
            Assert.AreEqual((byte)0xdd, bytes[13]);
            Assert.AreEqual((byte)0xee, bytes[14]);
            Assert.AreEqual((byte)0xff, bytes[15]);
        }
Ejemplo n.º 3
0
 public void TestIsNaN()
 {
     Assert.IsFalse(CBORObject.FromObject(0).AsNumber().IsNaN());
     Assert.IsFalse(CBORObject.FromObject(99).AsNumber().IsNaN());
     Assert.IsFalse(CBORObject.PositiveInfinity.AsNumber().IsNaN());
     Assert.IsFalse(CBORObject.NegativeInfinity.AsNumber().IsNaN());
     Assert.IsTrue(CBORObject.NaN.AsNumber().IsNaN());
     Assert.IsTrue(ToObjectTest.TestToFromObjectRoundTrip(Double.NaN)
                   .AsNumber().IsNaN());
 }
Ejemplo n.º 4
0
        public void TestCBORObjectArgumentValidation()
        {
            Assert.AreEqual(
                CBORObject.Null,
                ToObjectTest.TestToFromObjectRoundTrip((byte[])null));
            Assert.AreEqual(
                CBORObject.Null,
                ToObjectTest.TestToFromObjectRoundTrip((CBORObject[])null));
            Assert.AreEqual(
                CBORObject.True,
                ToObjectTest.TestToFromObjectRoundTrip(true));
            Assert.AreEqual(
                CBORObject.False,
                ToObjectTest.TestToFromObjectRoundTrip(false));
            Assert.AreEqual(
                ToObjectTest.TestToFromObjectRoundTrip(8),
                ToObjectTest.TestToFromObjectRoundTrip((byte)8));

            try {
                CBORObject.True.ToObject(typeof(ERational));
                Assert.Fail("Should have failed");
            } catch (InvalidOperationException) {
                // NOTE: Intentionally empty
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
            try {
                CBORObject.False.ToObject(typeof(ERational));
                Assert.Fail("Should have failed");
            } catch (InvalidOperationException) {
                // NOTE: Intentionally empty
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
            try {
                CBORObject.NewArray().ToObject(typeof(ERational));
                Assert.Fail("Should have failed");
            } catch (InvalidOperationException) {
                // NOTE: Intentionally empty
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
            try {
                CBORObject.NewMap().ToObject(typeof(ERational));
                Assert.Fail("Should have failed");
            } catch (InvalidOperationException) {
                // NOTE: Intentionally empty
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
        }
Ejemplo n.º 5
0
        public void TestCBOREInteger()
        {
            EInteger bi = EInteger.FromString("9223372036854775808");

            try {
                ToObjectTest.TestToFromObjectRoundTrip(bi).ToObject(typeof(long));
                Assert.Fail("Should have failed");
            } catch (OverflowException) {
                // NOTE: Intentionally empty
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
            try {
                ToObjectTest.TestToFromObjectRoundTrip(bi).ToObject(typeof(int));
                Assert.Fail("Should have failed");
            } catch (OverflowException) {
                // NOTE: Intentionally empty
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
            bi = EInteger.FromString("-9223372036854775809");
            try {
                ToObjectTest.TestToFromObjectRoundTrip(bi).ToObject(typeof(long));
                Assert.Fail("Should have failed");
            } catch (OverflowException) {
                // NOTE: Intentionally empty
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
            try {
                ToObjectTest.TestToFromObjectRoundTrip(bi).ToObject(typeof(int));
                Assert.Fail("Should have failed");
            } catch (OverflowException) {
                // NOTE: Intentionally empty
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
            bi = EInteger.FromString("-9223372036854775808");
            try {
                ToObjectTest.TestToFromObjectRoundTrip(bi).ToObject(typeof(int));
                Assert.Fail("Should have failed");
            } catch (OverflowException) {
                // NOTE: Intentionally empty
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
        }
Ejemplo n.º 6
0
        public void TestEquivalentInfinities()
        {
            CBORObject co, co2;

            co  = ToObjectTest.TestToFromObjectRoundTrip(CBORTestCommon.DecPosInf);
            co2 = ToObjectTest.TestToFromObjectRoundTrip(Double.PositiveInfinity);
            {
                object objectTemp  = co.IsNegative && co.IsInfinity();
                object objectTemp2 = co2.IsNegative &&
                                     co2.IsInfinity();
                Assert.AreEqual(objectTemp, objectTemp2);
            }
        }
Ejemplo n.º 7
0
        public void TestParseJSONNumber()
        {
            foreach (var str in BadJsonNumbers)
            {
                if (CBORDataUtilities.ParseJSONNumber(str) != null)
                {
                    Assert.Fail(str);
                }
                if (CBORDataUtilities.ParseJSONNumber(str, false, false, true) !=
                    null)
                {
                    Assert.Fail(str);
                }
                if (CBORDataUtilities.ParseJSONNumber(str, false, false, false) !=
                    null)
                {
                    Assert.Fail(str);
                }
            }
            CBORObject cbor = CBORDataUtilities.ParseJSONNumber("2e-2147483648");

            CBORTestCommon.AssertJSONSer(cbor, "2E-2147483648");
            foreach (var str in GoodJsonNumbers)
            {
                if (CBORDataUtilities.ParseJSONNumber(str) == null)
                {
                    Assert.Fail(str);
                }
                if (CBORDataUtilities.ParseJSONNumber(str, false, false, true) ==
                    null)
                {
                    Assert.Fail(str);
                }
                if (CBORDataUtilities.ParseJSONNumber(str, false, false, false) ==
                    null)
                {
                    Assert.Fail(str);
                }
            }
            TestCommon.CompareTestEqual(
                ToObjectTest.TestToFromObjectRoundTrip(230).AsNumber(),
                CBORDataUtilities.ParseJSONNumber("23.0e01").AsNumber());
            TestCommon.CompareTestEqual(
                ToObjectTest.TestToFromObjectRoundTrip(23).AsNumber(),
                CBORDataUtilities.ParseJSONNumber("23.0e00").AsNumber());
            cbor = CBORDataUtilities.ParseJSONNumber(
                "1e+99999999999999999999999999");
            Assert.IsTrue(cbor != null);
            Assert.IsFalse(cbor.AsNumber().CanFitInDouble());
            CBORTestCommon.AssertRoundTrip(cbor);
        }
Ejemplo n.º 8
0
        public void TestEquivalentInfinities()
        {
            CBORObject co, co2;

            co  = ToObjectTest.TestToFromObjectRoundTrip(CBORTestCommon.DecPosInf);
            co2 = ToObjectTest.TestToFromObjectRoundTrip(Double.PositiveInfinity);
            {
                bool boolTemp = co.AsNumber().IsNegative() &&
                                co.AsNumber().IsInfinity();
                bool boolTemp2 = co2.AsNumber().IsNegative() &&
                                 co2.AsNumber().IsInfinity();
                Assert.AreEqual(boolTemp, boolTemp2);
            }
        }
Ejemplo n.º 9
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();
            }
        }
Ejemplo n.º 10
0
        public void TestDictionary()
        {
            CBORObject beo = CBORObject.NewMap();

            beo["zero"]  = ToObjectTest.TestToFromObjectRoundTrip(1);
            beo["one"]   = ToObjectTest.TestToFromObjectRoundTrip("two");
            beo["two"]   = ToObjectTest.TestToFromObjectRoundTrip(3);
            beo["three"] = ToObjectTest.TestToFromObjectRoundTrip("four");
            Assert.AreEqual(4, beo.Count);
            Assert.AreEqual(1, beo["zero"].AsNumber().ToInt64Checked());
            {
                string stringTemp = beo["one"].AsString();
                Assert.AreEqual(
                    "two",
                    stringTemp);
            }
            Assert.AreEqual(3, beo["two"].AsNumber().ToInt64Checked());
            {
                string stringTemp = beo["three"].AsString();
                Assert.AreEqual(
                    "four",
                    stringTemp);
            }
            byte[] b = EncodingToBytes(beo);
            beo = EncodingFromBytes(b);
            Assert.AreEqual(4, beo.Count);
            Assert.AreEqual(1, beo["zero"].AsNumber().ToInt64Checked());
            {
                string stringTemp = beo["one"].AsString();
                Assert.AreEqual(
                    "two",
                    stringTemp);
            }
            Assert.AreEqual(3, beo["two"].AsNumber().ToInt64Checked());
            {
                string stringTemp = beo["three"].AsString();
                Assert.AreEqual(
                    "four",
                    stringTemp);
            }
        }
Ejemplo n.º 11
0
        public void TestList()
        {
            CBORObject beo = CBORObject.NewArray();

            beo.Add(ToObjectTest.TestToFromObjectRoundTrip(1));
            beo.Add(ToObjectTest.TestToFromObjectRoundTrip("two"));
            beo.Add(ToObjectTest.TestToFromObjectRoundTrip(3));
            beo.Add(ToObjectTest.TestToFromObjectRoundTrip("four"));
            Assert.AreEqual(4, beo.Count);
            Assert.AreEqual(1, beo[0].AsNumber().ToInt64Checked());
            {
                string stringTemp = beo[1].AsString();
                Assert.AreEqual(
                    "two",
                    stringTemp);
            }
            Assert.AreEqual(3, beo[2].AsNumber().ToInt64Checked());
            {
                string stringTemp = beo[3].AsString();
                Assert.AreEqual(
                    "four",
                    stringTemp);
            }
            byte[] b = EncodingToBytes(beo);
            beo = EncodingFromBytes(b);
            Assert.AreEqual(4, beo.Count);
            Assert.AreEqual(1, beo[0].AsNumber().ToInt64Checked());
            {
                string stringTemp = beo[1].AsString();
                Assert.AreEqual(
                    "two",
                    stringTemp);
            }
            Assert.AreEqual(3, beo[2].AsNumber().ToInt64Checked());
            {
                string stringTemp = beo[3].AsString();
                Assert.AreEqual(
                    "four",
                    stringTemp);
            }
        }
Ejemplo n.º 12
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));
            }
        }
Ejemplo n.º 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 if (rand.UniformInt(100) < 90)
            {
                return(CBORObject.FromObjectAndTag(
                           RandomCBORObject(rand, depth + 1),
                           rand.UniformInt(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(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);
                }
                return(CBORObject.FromObjectAndTag(cbor, tag));
            }
        }
Ejemplo n.º 14
0
        public void TestParseJSONNumber()
        {
            if (CBORDataUtilities.ParseJSONNumber("100.", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-100.", false, false) != null)
            {
                Assert.Fail();
            }
            if (
                CBORDataUtilities.ParseJSONNumber(
                    "100.e+20",
                    false,
                    false) != null)
            {
                Assert.Fail();
            }
            if (
                CBORDataUtilities.ParseJSONNumber(
                    "-100.e20",
                    false,
                    false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("100.e20", false, false) != null)
            {
                Assert.Fail();
            }

            if (CBORDataUtilities.ParseJSONNumber("+0.1", false, false) != null)
            {
                Assert.Fail();
            }

            if (CBORDataUtilities.ParseJSONNumber("0.", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-0.", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0g.1", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0.e+20", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-0.e20", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0.e20", false, false) != null)
            {
                Assert.Fail();
            }

            if (CBORDataUtilities.ParseJSONNumber(null, false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber(
                    String.Empty,
                    false,
                    false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("xyz", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("true", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber(".1", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0..1", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0xyz", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0.1xyz", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0.xyz", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0.5exyz", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0.5q+88", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0.5ee88", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-5e") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-5e-2x") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-5e+2x") != null)
            {
                Assert.Fail();
            }
            CBORObject cbor = CBORDataUtilities.ParseJSONNumber("2e-2147483648");

            CBORTestCommon.AssertJSONSer(cbor, "2E-2147483648");
            if (CBORDataUtilities.ParseJSONNumber(
                    "0.5e+xyz",
                    false,
                    false) != null)
            {
                Assert.Fail();
            }
            if (
                CBORDataUtilities.ParseJSONNumber(
                    "0.5e+88xyz",
                    false,
                    false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0000") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0x1") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0xf") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0x20") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0x01") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber(".2") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber(".05") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-.2") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-.05") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("23.") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("23.e0") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("23.e1") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0.") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("5.2", true, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("5e+1", true, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-5.2", true, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-5e+1", true, false) != null)
            {
                Assert.Fail();
            }

            TestCommon.CompareTestEqual(
                ToObjectTest.TestToFromObjectRoundTrip(230).AsNumber(),
                CBORDataUtilities.ParseJSONNumber("23.0e01").AsNumber());
            TestCommon.CompareTestEqual(
                ToObjectTest.TestToFromObjectRoundTrip(23).AsNumber(),
                CBORDataUtilities.ParseJSONNumber("23.0e00").AsNumber());
            cbor = CBORDataUtilities.ParseJSONNumber(
                "1e+99999999999999999999999999",
                false,
                false);
            Assert.IsTrue(cbor != null);
            Assert.IsFalse(cbor.CanFitInDouble());
            CBORTestCommon.AssertRoundTrip(cbor);
        }
Ejemplo n.º 15
0
 private static CBORNumber ToCN(object o)
 {
     return(ToObjectTest.TestToFromObjectRoundTrip(o).AsNumber());
 }
Ejemplo n.º 16
0
 public void TestAsEDecimal()
 {
     {
         object objectTemp  = CBORTestCommon.DecPosInf;
         object objectTemp2 =
             ToObjectTest.TestToFromObjectRoundTrip(Single.PositiveInfinity)
             .AsNumber().ToEDecimal();
         Assert.AreEqual(objectTemp, objectTemp2);
     }
     {
         object objectTemp  = CBORTestCommon.DecNegInf;
         object objectTemp2 =
             ToObjectTest.TestToFromObjectRoundTrip(Single.NegativeInfinity)
             .AsNumber().ToEDecimal();
         Assert.AreEqual(objectTemp, objectTemp2);
     }
     {
         string stringTemp = ToObjectTest.TestToFromObjectRoundTrip(
             Single.NaN).AsNumber().ToEDecimal().ToString();
         Assert.AreEqual(
             "NaN",
             stringTemp);
     }
     {
         object objectTemp  = CBORTestCommon.DecPosInf;
         object objectTemp2 =
             ToObjectTest.TestToFromObjectRoundTrip(Double.PositiveInfinity)
             .AsNumber().ToEDecimal();
         Assert.AreEqual(objectTemp, objectTemp2);
     }
     {
         object objectTemp  = CBORTestCommon.DecNegInf;
         object objectTemp2 =
             ToObjectTest.TestToFromObjectRoundTrip(Double.NegativeInfinity)
             .AsNumber().ToEDecimal();
         Assert.AreEqual(objectTemp, objectTemp2);
     }
     {
         object objectTemp  = "NaN";
         object objectTemp2 =
             ToObjectTest.TestToFromObjectRoundTrip(
                 Double.NaN).AsNumber().ToEDecimal()
             .ToString();
         Assert.AreEqual(objectTemp, objectTemp2);
     }
     try {
         CBORObject.NewArray().AsNumber().ToEDecimal();
         Assert.Fail("Should have failed");
     } catch (InvalidOperationException) {
         // NOTE: Intentionally empty
     } catch (Exception ex) {
         Assert.Fail(ex.ToString());
         throw new InvalidOperationException(String.Empty, ex);
     }
     try {
         CBORObject.NewMap().AsNumber().ToEDecimal();
         Assert.Fail("Should have failed");
     } catch (InvalidOperationException) {
         // NOTE: Intentionally empty
     } catch (Exception ex) {
         Assert.Fail(ex.ToString());
         throw new InvalidOperationException(String.Empty, ex);
     }
     try {
         CBORObject.True.AsNumber().ToEDecimal();
         Assert.Fail("Should have failed");
     } catch (InvalidOperationException) {
         // NOTE: Intentionally empty
     } catch (Exception ex) {
         Assert.Fail(ex.ToString());
         throw new InvalidOperationException(String.Empty, ex);
     }
     try {
         CBORObject.False.AsNumber().ToEDecimal();
         Assert.Fail("Should have failed");
     } catch (InvalidOperationException) {
         // NOTE: Intentionally empty
     } catch (Exception ex) {
         Assert.Fail(ex.ToString());
         throw new InvalidOperationException(String.Empty, ex);
     }
     try {
         CBORObject.Undefined.AsNumber().ToEDecimal();
         Assert.Fail("Should have failed");
     } catch (InvalidOperationException) {
         // NOTE: Intentionally empty
     } catch (Exception ex) {
         Assert.Fail(ex.ToString());
         throw new InvalidOperationException(String.Empty, ex);
     }
     try {
         ToObjectTest.TestToFromObjectRoundTrip(
             String.Empty).AsNumber().ToEDecimal();
         Assert.Fail("Should have failed");
     } catch (InvalidOperationException) {
         // NOTE: Intentionally empty
     } catch (Exception ex) {
         Assert.Fail(ex.ToString());
         throw new InvalidOperationException(String.Empty, ex);
     }
 }
Ejemplo n.º 17
0
        public void TestAsEInteger()
        {
            try {
                ToObjectTest.TestToFromObjectRoundTrip(
                    (object)null).AsNumber().ToEInteger();
                Assert.Fail("Should have failed");
            } catch (InvalidOperationException) {
                // NOTE: Intentionally empty
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
            try {
                CBORObject.Null.AsNumber().ToEInteger();
                Assert.Fail("Should have failed");
            } catch (InvalidOperationException) {
                // NOTE: Intentionally empty
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
            try {
                CBORObject.True.AsNumber().ToEInteger();
                Assert.Fail("Should have failed");
            } catch (InvalidOperationException) {
                // NOTE: Intentionally empty
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
            try {
                CBORObject.False.AsNumber().ToEInteger();
                Assert.Fail("Should have failed");
            } catch (InvalidOperationException) {
                // NOTE: Intentionally empty
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
            try {
                CBORObject.Undefined.AsNumber().ToEInteger();
                Assert.Fail("Should have failed");
            } catch (InvalidOperationException) {
                // NOTE: Intentionally empty
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
            try {
                CBORObject.NewArray().AsNumber().ToEInteger();
                Assert.Fail("Should have failed");
            } catch (InvalidOperationException) {
                // NOTE: Intentionally empty
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
            try {
                CBORObject.NewMap().AsNumber().ToEInteger();
                Assert.Fail("Should have failed");
            } catch (InvalidOperationException) {
                // NOTE: Intentionally empty
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
            CBORObject numbers = CBORObjectTest.GetNumberData();

            for (int i = 0; i < numbers.Count; ++i)
            {
                CBORObject numberinfo   = numbers[i];
                string     numberString = numberinfo["number"].AsString();
                CBORObject cbornumber   =
                    ToObjectTest.TestToFromObjectRoundTrip(EDecimal.FromString(
                                                               numberString));
                if (!numberinfo["integer"].Equals(CBORObject.Null))
                {
                    Assert.AreEqual(
                        numberinfo["integer"].AsString(),
                        cbornumber.AsNumber().ToEInteger().ToString());
                }
                else
                {
                    try {
                        cbornumber.AsNumber().ToEInteger();
                        Assert.Fail("Should have failed");
                    } catch (OverflowException) {
                        // NOTE: Intentionally empty
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                }
            }

            {
                string stringTemp =
                    ToObjectTest.TestToFromObjectRoundTrip(0.75f).AsNumber().ToEInteger()
                    .ToString();
                Assert.AreEqual(
                    "0",
                    stringTemp);
            }
            {
                string stringTemp =
                    ToObjectTest.TestToFromObjectRoundTrip(0.99f).AsNumber().ToEInteger()
                    .ToString();
                Assert.AreEqual(
                    "0",
                    stringTemp);
            }
            {
                string stringTemp =
                    ToObjectTest.TestToFromObjectRoundTrip(0.0000000000000001f)
                    .AsNumber().ToEInteger().ToString();
                Assert.AreEqual(
                    "0",
                    stringTemp);
            }
            {
                string stringTemp =
                    ToObjectTest.TestToFromObjectRoundTrip(0.5f).AsNumber().ToEInteger()
                    .ToString();
                Assert.AreEqual(
                    "0",
                    stringTemp);
            }
            {
                string stringTemp =
                    ToObjectTest.TestToFromObjectRoundTrip(1.5f).AsNumber().ToEInteger()
                    .ToString();
                Assert.AreEqual(
                    "1",
                    stringTemp);
            }
            {
                string stringTemp =
                    ToObjectTest.TestToFromObjectRoundTrip(2.5f).AsNumber().ToEInteger()
                    .ToString();
                Assert.AreEqual(
                    "2",
                    stringTemp);
            }
            {
                string stringTemp =

                    ToObjectTest.TestToFromObjectRoundTrip(
                        (float)328323f).AsNumber().ToEInteger().ToString();
                Assert.AreEqual(
                    "328323",
                    stringTemp);
            }
            {
                string stringTemp =
                    ToObjectTest.TestToFromObjectRoundTrip(
                        (double)0.75).AsNumber().ToEInteger()
                    .ToString();
                Assert.AreEqual(
                    "0",
                    stringTemp);
            }
            {
                string stringTemp = ToObjectTest.TestToFromObjectRoundTrip(
                    (double)0.99).AsNumber().ToEInteger().ToString();
                Assert.AreEqual(
                    "0",
                    stringTemp);
            }
            {
                string stringTemp =
                    ToObjectTest.TestToFromObjectRoundTrip((double)0.0000000000000001)
                    .AsNumber().ToEInteger().ToString();
                Assert.AreEqual(
                    "0",
                    stringTemp);
            }
            {
                string stringTemp = ToObjectTest.TestToFromObjectRoundTrip(
                    (double)0.5).AsNumber().ToEInteger().ToString();
                Assert.AreEqual(
                    "0",
                    stringTemp);
            }
            {
                string stringTemp =
                    ToObjectTest.TestToFromObjectRoundTrip(
                        (double)1.5).AsNumber().ToEInteger()
                    .ToString();
                Assert.AreEqual(
                    "1",
                    stringTemp);
            }
            {
                string stringTemp = ToObjectTest.TestToFromObjectRoundTrip(
                    (double)2.5).AsNumber().ToEInteger().ToString();
                Assert.AreEqual(
                    "2",
                    stringTemp);
            }
            {
                double dbl        = 328323;
                string stringTemp = ToObjectTest.TestToFromObjectRoundTrip(dbl)
                                    .AsNumber().ToEInteger().ToString();
                Assert.AreEqual(
                    "328323",
                    stringTemp);
            }
            try {
                ToObjectTest.TestToFromObjectRoundTrip(Single.PositiveInfinity)
                .AsNumber().ToEInteger();
                Assert.Fail("Should have failed");
            } catch (OverflowException) {
                // NOTE: Intentionally empty
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
            try {
                ToObjectTest.TestToFromObjectRoundTrip(Single.NegativeInfinity)
                .AsNumber().ToEInteger();
                Assert.Fail("Should have failed");
            } catch (OverflowException) {
                // NOTE: Intentionally empty
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
            try {
                ToObjectTest.TestToFromObjectRoundTrip(
                    Single.NaN).AsNumber().ToEInteger();
                Assert.Fail("Should have failed");
            } catch (OverflowException) {
                // NOTE: Intentionally empty
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
            try {
                ToObjectTest.TestToFromObjectRoundTrip(Double.PositiveInfinity)
                .AsNumber().ToEInteger();
                Assert.Fail("Should have failed");
            } catch (OverflowException) {
                // NOTE: Intentionally empty
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
            try {
                ToObjectTest.TestToFromObjectRoundTrip(Double.NegativeInfinity)
                .AsNumber().ToEInteger();
                Assert.Fail("Should have failed");
            } catch (OverflowException) {
                // NOTE: Intentionally empty
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
            try {
                ToObjectTest.TestToFromObjectRoundTrip(
                    Double.NaN).AsNumber().ToEInteger();
                Assert.Fail("Should have failed");
            } catch (OverflowException) {
                // NOTE: Intentionally empty
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
        }