Example #1
0
        public static void TestRationalGetMismatches()
        {
            var jsonNumber = new JsonNumber("3.14");

            // Getting integer types should fail:
            Assert.False(jsonNumber.TryGetByte(out byte byteResult));
            Assert.Throws <FormatException>(() => jsonNumber.GetByte());

            Assert.False(jsonNumber.TryGetInt16(out short shortResult));
            Assert.Throws <FormatException>(() => jsonNumber.GetInt16());

            Assert.False(jsonNumber.TryGetInt32(out int intResult));
            Assert.Throws <FormatException>(() => jsonNumber.GetInt32());

            Assert.False(jsonNumber.TryGetInt64(out long longResult));
            Assert.Throws <FormatException>(() => jsonNumber.GetInt64());

            Assert.False(jsonNumber.TryGetSByte(out sbyte sbyteResult));
            Assert.Throws <FormatException>(() => jsonNumber.GetSByte());

            Assert.False(jsonNumber.TryGetUInt16(out ushort ushortResult));
            Assert.Throws <FormatException>(() => jsonNumber.GetUInt16());

            Assert.False(jsonNumber.TryGetUInt32(out uint uintResult));
            Assert.Throws <FormatException>(() => jsonNumber.GetUInt32());

            Assert.False(jsonNumber.TryGetUInt64(out ulong ulongResult));
            Assert.Throws <FormatException>(() => jsonNumber.GetUInt64());

            jsonNumber = new JsonNumber(double.MaxValue);

            if (PlatformDetection.IsNetFramework)
            {
                // .NET Framework throws for overflow rather than returning Infinity
                // This was fixed for .NET Core 3.0 in order to be IEEE 754 compliant
                Assert.Throws <OverflowException>(() => jsonNumber.GetSingle());
                // Getting double fails as well
                Assert.Throws <OverflowException>(() => jsonNumber.GetDouble());
            }
            else
            {
                Assert.Equal(float.PositiveInfinity, jsonNumber.GetSingle());
            }
            Assert.Throws <OverflowException>(() => jsonNumber.GetDecimal());

            jsonNumber = new JsonNumber("5e500");

            if (PlatformDetection.IsNetFramework)
            {
                Assert.Throws <OverflowException>(() => jsonNumber.GetSingle());
                Assert.Throws <OverflowException>(() => jsonNumber.GetDouble());
            }
            else
            {
                Assert.Equal(float.PositiveInfinity, jsonNumber.GetSingle());
                Assert.Equal(double.PositiveInfinity, jsonNumber.GetDouble());
            }

            Assert.Throws <OverflowException>(() => jsonNumber.GetDecimal());
        }
Example #2
0
        public static void TestDefaultCtor()
        {
            var jsonNumber = new JsonNumber();

            Assert.Equal(0, jsonNumber.GetByte());
            Assert.Equal(0, jsonNumber.GetInt16());
            Assert.Equal(0, jsonNumber.GetInt32());
            Assert.Equal(0, jsonNumber.GetInt64());
            Assert.Equal(0, jsonNumber.GetSingle());
            Assert.Equal(0, jsonNumber.GetDouble());
            Assert.Equal(0, jsonNumber.GetSByte());
            Assert.Equal((ushort)0, jsonNumber.GetUInt16());
            Assert.Equal((uint)0, jsonNumber.GetUInt32());
            Assert.Equal((ulong)0, jsonNumber.GetUInt64());
            Assert.Equal(0, jsonNumber.GetDecimal());
        }
Example #3
0
        public static void TestUnsignedGetMismatches()
        {
            var jsonNumber = new JsonNumber("-1");

            // Getting unsigned types should fail:
            Assert.False(jsonNumber.TryGetByte(out byte byteResult));
            Assert.Throws <OverflowException>(() => jsonNumber.GetByte());

            Assert.False(jsonNumber.TryGetUInt16(out ushort ushortResult));
            Assert.Throws <OverflowException>(() => jsonNumber.GetUInt16());

            Assert.False(jsonNumber.TryGetUInt32(out uint uintResult));
            Assert.Throws <OverflowException>(() => jsonNumber.GetUInt32());

            Assert.False(jsonNumber.TryGetUInt64(out ulong ulongResult));
            Assert.Throws <OverflowException>(() => jsonNumber.GetUInt64());
        }
Example #4
0
        public static void TestIntegerGetMismatches()
        {
            var jsonNumber = new JsonNumber(long.MaxValue);

            // Getting smaller types should fail:
            Assert.False(jsonNumber.TryGetByte(out byte byteResult));
            Assert.Throws <OverflowException>(() => jsonNumber.GetByte());

            Assert.False(jsonNumber.TryGetInt16(out short shortResult));
            Assert.Throws <OverflowException>(() => jsonNumber.GetInt16());

            Assert.False(jsonNumber.TryGetInt32(out int intResult));
            Assert.Throws <OverflowException>(() => jsonNumber.GetInt32());

            Assert.False(jsonNumber.TryGetSByte(out sbyte sbyteResult));
            Assert.Throws <OverflowException>(() => jsonNumber.GetSByte());

            Assert.False(jsonNumber.TryGetUInt16(out ushort ushortResult));
            Assert.Throws <OverflowException>(() => jsonNumber.GetUInt16());

            Assert.False(jsonNumber.TryGetUInt32(out uint uintResult));
            Assert.Throws <OverflowException>(() => jsonNumber.GetUInt32());
        }
Example #5
0
        public static void TestChangingTypes()
        {
            var jsonNumber = new JsonNumber(5);

            Assert.Equal(5, jsonNumber.GetInt32());

            jsonNumber.SetDouble(3.14);
            Assert.Equal(3.14, jsonNumber.GetDouble());

            jsonNumber.SetByte(17);
            Assert.Equal(17, jsonNumber.GetByte());

            jsonNumber.SetInt64(long.MaxValue);
            Assert.Equal(long.MaxValue, jsonNumber.GetInt64());

            jsonNumber.SetUInt16(ushort.MaxValue);
            Assert.Equal(ushort.MaxValue, jsonNumber.GetUInt16());

            jsonNumber.SetSingle(-1.1f);
            Assert.Equal(-1.1f, jsonNumber.GetSingle());

            jsonNumber.SetSByte(4);
            Assert.Equal(4, jsonNumber.GetSByte());

            jsonNumber.SetUInt32(127);
            Assert.Equal((uint)127, jsonNumber.GetUInt32());

            jsonNumber.SetFormattedValue("1e400");
            Assert.Equal("1e400", jsonNumber.ToString());

            jsonNumber.SetUInt64(ulong.MaxValue);
            Assert.Equal(ulong.MaxValue, jsonNumber.GetUInt64());

            jsonNumber.SetDecimal(decimal.MaxValue);
            Assert.Equal(decimal.MaxValue, jsonNumber.GetDecimal());
        }