Ejemplo n.º 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());
        }
Ejemplo n.º 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());
        }
Ejemplo n.º 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());
        }
Ejemplo n.º 4
0
        public static void TestUpcasts()
        {
            byte value      = 17;
            var  jsonNumber = new JsonNumber(value);

            // Getting other types should also succeed:
            Assert.Equal(value, jsonNumber.GetInt16());
            Assert.True(jsonNumber.TryGetInt16(out short shortResult));
            Assert.Equal(value, shortResult);

            Assert.Equal(value, jsonNumber.GetInt32());
            Assert.True(jsonNumber.TryGetInt32(out int intResult));
            Assert.Equal(value, intResult);

            Assert.Equal(value, jsonNumber.GetInt64());
            Assert.True(jsonNumber.TryGetInt64(out long longResult));
            Assert.Equal(value, longResult);

            Assert.Equal(value, jsonNumber.GetSingle());
            Assert.True(jsonNumber.TryGetSingle(out float floatResult));
            Assert.Equal(value, floatResult);

            Assert.Equal(value, jsonNumber.GetDouble());
            Assert.True(jsonNumber.TryGetDouble(out double doubleResult));
            Assert.Equal(value, doubleResult);

            Assert.Equal(value, jsonNumber.GetDecimal());
            Assert.True(jsonNumber.TryGetDecimal(out decimal decimalResult));
            Assert.Equal(value, decimalResult);

            Assert.Equal(value, (byte)jsonNumber.GetSByte());
            Assert.True(jsonNumber.TryGetSByte(out sbyte sbyteResult));
            Assert.Equal(value, (byte)sbyteResult);

            Assert.Equal(value, jsonNumber.GetUInt16());
            Assert.True(jsonNumber.TryGetUInt16(out ushort ushortResult));
            Assert.Equal(value, ushortResult);

            Assert.Equal(value, jsonNumber.GetUInt32());
            Assert.True(jsonNumber.TryGetUInt32(out uint uintResult));
            Assert.Equal(value, uintResult);

            Assert.Equal(value, jsonNumber.GetUInt64());
            Assert.True(jsonNumber.TryGetUInt64(out ulong ulongResult));
            Assert.Equal(value, ulongResult);
        }
Ejemplo n.º 5
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());
        }
Ejemplo n.º 6
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());
        }