public void FloatDeserializationNegative() { List <string> testCases = new List <string>() { "{\"Float\":1.7976931348623157E+309}", // Larger double.MaxValue "{\"Float\":-1.7976931348623157E+309}", // Larger double.MinValue }; // Need to ensure that the type is registered as a table to force the id property check DefaultSerializer.SerializerSettings.ContractResolver.ResolveTableName(typeof(FloatType)); foreach (var testCase in testCases) { var input = testCase; FloatType actual = new FloatType(); Exception actualError = null; try { DefaultSerializer.Deserialize(input, actual); } catch (Exception e) { actualError = e; } Assert.AreEqual(actualError.Message, "Value was either too large or too small for a Double."); } }
public void FloatDeserializationNegative() { List<string> testCases = new List<string>() { "{\"Float\":1.7976931348623157E+309}", // Larger double.MaxValue "{\"Float\":-1.7976931348623157E+309}", // Larger double.MinValue }; foreach (var testCase in testCases) { var input = testCase; FloatType actual = new FloatType(); Exception actualError = null; try { // Need to ensure that the type is registered as a table to force the id property check DefaultSerializer.SerializerSettings.ContractResolver.ResolveTableName(typeof(FloatType)); DefaultSerializer.Deserialize(input, actual); } catch (Exception e) { actualError = e; } Assert.Contains(actualError.Message, "is not a valid number. Path 'Float'"); } }
public void FloatDeserialization() { List<Tuple<FloatType, string>> testCases = new List<Tuple<FloatType, string>>() { new Tuple<FloatType, string>(new FloatType() { Float = 0 }, "{}"), new Tuple<FloatType, string>(new FloatType() { Float = 0 }, "{\"Float\":null}"), new Tuple<FloatType, string>(new FloatType() { Float = 0 }, "{\"Float\":0}"), new Tuple<FloatType, string>(new FloatType() { Float = 0 }, "{\"Float\":false}"), new Tuple<FloatType, string>(new FloatType() { Float = 1 }, "{\"Float\":true}"), new Tuple<FloatType, string>(new FloatType() { Float = 5.8f }, "{\"Float\":\"5.8\"}"), new Tuple<FloatType, string>(new FloatType() { Float = -1 }, "{\"Float\":-1}"), new Tuple<FloatType, string>(new FloatType() { Float = 1 }, "{\"Float\":1}"), new Tuple<FloatType, string>(new FloatType() { Float = 0.0F }, "{\"Float\":4.94065645841247E-325}"), new Tuple<FloatType, string>(new FloatType() { Float = 0.0F }, "{\"Float\":-4.94065645841247E-325}"), new Tuple<FloatType, string>(new FloatType() { Float = float.Epsilon }, "{\"Float\":1.401298E-45}"), new Tuple<FloatType, string>(new FloatType() { Float = -float.Epsilon }, "{\"Float\":-1.401298E-45}"), new Tuple<FloatType, string>(new FloatType() { Float = float.Epsilon }, "{\"Float\":1.4012984643248171E-45}"), new Tuple<FloatType, string>(new FloatType() { Float = -float.Epsilon }, "{\"Float\":-1.4012984643248171E-45}"), new Tuple<FloatType, string>(new FloatType() { Float = float.Epsilon }, "{\"Float\":1.4012984643248171E-46}"), new Tuple<FloatType, string>(new FloatType() { Float = -float.Epsilon }, "{\"Float\":-1.4012984643248171E-46}"), new Tuple<FloatType, string>(new FloatType() { Float = float.MaxValue }, "{\"Float\":3.40282347E+38}"), new Tuple<FloatType, string>(new FloatType() { Float = float.MinValue }, "{\"Float\":-3.40282347E+38}"), new Tuple<FloatType, string>(new FloatType() { Float = float.MaxValue }, "{\"Float\":3.4028234663852886E+38}"), new Tuple<FloatType, string>(new FloatType() { Float = float.MinValue }, "{\"Float\":-3.4028234663852886E+38}"), new Tuple<FloatType, string>(new FloatType() { Float = float.MinValue }, "{\"Float\":\"-3.4028234663852886E+38\"}"), new Tuple<FloatType, string>(new FloatType() { Float = float.MaxValue }, "{\"Float\":3.40282346638528865E+38}"), new Tuple<FloatType, string>(new FloatType() { Float = float.MinValue }, "{\"Float\":-3.40282346638528865E+38}"), new Tuple<FloatType, string>(new FloatType() { Float = float.PositiveInfinity }, "{\"Float\":3.4028234663852887E+39}"), new Tuple<FloatType, string>(new FloatType() { Float = float.NegativeInfinity }, "{\"Float\":-3.4028234663852887E+39}"), }; // Need to ensure that the type is registered as a table to force the id property check DefaultSerializer.SerializerSettings.ContractResolver.ResolveTableName(typeof(FloatType)); foreach (var testCase in testCases) { var input = JToken.Parse(testCase.Item2); var expected = testCase.Item1; FloatType actual = new FloatType(); DefaultSerializer.Deserialize(input, actual); if (float.IsPositiveInfinity(expected.Float)) { Assert.IsTrue(float.IsPositiveInfinity(actual.Float)); } else if (float.IsNegativeInfinity(expected.Float)) { Assert.IsTrue(float.IsNegativeInfinity(actual.Float)); } else { Assert.AreEqual(actual.Float, expected.Float); } if (testCase.Item2 != "{}") { actual = new FloatType(); actual.Float = 34.6F; DefaultSerializer.Deserialize(input, actual); if (float.IsPositiveInfinity(expected.Float)) { Assert.IsTrue(float.IsPositiveInfinity(actual.Float)); } else if (float.IsNegativeInfinity(expected.Float)) { Assert.IsTrue(float.IsNegativeInfinity(actual.Float)); } else { Assert.AreEqual(actual.Float, expected.Float); } } JArray json = JToken.Parse("[" + testCase.Item2 + "]") as JArray; actual = DefaultSerializer.Deserialize<FloatType>(json).FirstOrDefault(); if (float.IsPositiveInfinity(expected.Float)) { Assert.IsTrue(float.IsPositiveInfinity(actual.Float)); } else if (float.IsNegativeInfinity(expected.Float)) { Assert.IsTrue(float.IsNegativeInfinity(actual.Float)); } else { Assert.AreEqual(actual.Float, expected.Float); } actual = DefaultSerializer.Deserialize<FloatType>(input); if (float.IsPositiveInfinity(expected.Float)) { Assert.IsTrue(float.IsPositiveInfinity(actual.Float)); } else if (float.IsNegativeInfinity(expected.Float)) { Assert.IsTrue(float.IsNegativeInfinity(actual.Float)); } else { Assert.AreEqual(actual.Float, expected.Float); } } }