Commit be44985c authored by Jan Tattermusch's avatar Jan Tattermusch

Merge pull request #1087 from jskeet/json-tests

Improve number handling in JSON parsing
parents edd60c90 aa431a04
...@@ -39,9 +39,7 @@ using System; ...@@ -39,9 +39,7 @@ using System;
namespace Google.Protobuf namespace Google.Protobuf
{ {
/// <summary> /// <summary>
/// Unit tests for JSON parsing. Some tests are ignored at the moment as the desired behaviour /// Unit tests for JSON parsing.
/// isn't fully known, either in terms of which exceptions should be thrown or whether they should
/// count as valid values.
/// </summary> /// </summary>
public class JsonParserTest public class JsonParserTest
{ {
...@@ -205,6 +203,8 @@ namespace Google.Protobuf ...@@ -205,6 +203,8 @@ namespace Google.Protobuf
[Test] [Test]
[TestCase("+0")] [TestCase("+0")]
[TestCase(" 1")]
[TestCase("1 ")]
[TestCase("00")] [TestCase("00")]
[TestCase("-00")] [TestCase("-00")]
[TestCase("--1")] [TestCase("--1")]
...@@ -318,7 +318,18 @@ namespace Google.Protobuf ...@@ -318,7 +318,18 @@ namespace Google.Protobuf
[TestCase("1.0.0")] [TestCase("1.0.0")]
[TestCase("+1")] [TestCase("+1")]
[TestCase("00")] [TestCase("00")]
[TestCase("01")]
[TestCase("-00")]
[TestCase("-01")]
[TestCase("--1")] [TestCase("--1")]
[TestCase(" Infinity")]
[TestCase(" -Infinity")]
[TestCase("NaN ")]
[TestCase("Infinity ")]
[TestCase("-Infinity ")]
[TestCase(" NaN")]
[TestCase("INFINITY")]
[TestCase("nan")]
[TestCase("\u00BD")] // 1/2 as a single Unicode character. Just sanity checking... [TestCase("\u00BD")] // 1/2 as a single Unicode character. Just sanity checking...
public void StringToDouble_Invalid(string jsonValue) public void StringToDouble_Invalid(string jsonValue)
{ {
...@@ -363,6 +374,10 @@ namespace Google.Protobuf ...@@ -363,6 +374,10 @@ namespace Google.Protobuf
[TestCase("-1", -1)] [TestCase("-1", -1)]
[TestCase("2147483647", 2147483647)] [TestCase("2147483647", 2147483647)]
[TestCase("-2147483648", -2147483648)] [TestCase("-2147483648", -2147483648)]
[TestCase("1e1", 10)]
[TestCase("-1e1", -10)]
[TestCase("10.00", 10)]
[TestCase("-10.00", -10)]
public void NumberToInt32_Valid(string jsonValue, int expectedParsedValue) public void NumberToInt32_Valid(string jsonValue, int expectedParsedValue)
{ {
string json = "{ \"singleInt32\": " + jsonValue + "}"; string json = "{ \"singleInt32\": " + jsonValue + "}";
...@@ -376,7 +391,8 @@ namespace Google.Protobuf ...@@ -376,7 +391,8 @@ namespace Google.Protobuf
[TestCase("-00", typeof(InvalidJsonException))] [TestCase("-00", typeof(InvalidJsonException))]
[TestCase("--1", typeof(InvalidJsonException))] [TestCase("--1", typeof(InvalidJsonException))]
[TestCase("+1", typeof(InvalidJsonException))] [TestCase("+1", typeof(InvalidJsonException))]
[TestCase("1.5", typeof(InvalidProtocolBufferException), Ignore = true, Reason = "Desired behaviour unclear")] [TestCase("1.5", typeof(InvalidProtocolBufferException))]
// Value is out of range
[TestCase("1e10", typeof(InvalidProtocolBufferException))] [TestCase("1e10", typeof(InvalidProtocolBufferException))]
[TestCase("2147483648", typeof(InvalidProtocolBufferException))] [TestCase("2147483648", typeof(InvalidProtocolBufferException))]
[TestCase("-2147483649", typeof(InvalidProtocolBufferException))] [TestCase("-2147483649", typeof(InvalidProtocolBufferException))]
...@@ -411,8 +427,10 @@ namespace Google.Protobuf ...@@ -411,8 +427,10 @@ namespace Google.Protobuf
[TestCase("0", 0L)] [TestCase("0", 0L)]
[TestCase("1", 1L)] [TestCase("1", 1L)]
[TestCase("-1", -1L)] [TestCase("-1", -1L)]
[TestCase("9223372036854775807", 9223372036854775807, Ignore = true, Reason = "Desired behaviour unclear")] // long.MaxValue isn't actually representable as a double. This string value is the highest
[TestCase("-9223372036854775808", -9223372036854775808, Ignore = true, Reason = "Desired behaviour unclear")] // representable value which isn't greater than long.MaxValue.
[TestCase("9223372036854769664", 9223372036854769664)]
[TestCase("-9223372036854775808", -9223372036854775808)]
public void NumberToInt64_Valid(string jsonValue, long expectedParsedValue) public void NumberToInt64_Valid(string jsonValue, long expectedParsedValue)
{ {
string json = "{ \"singleInt64\": " + jsonValue + "}"; string json = "{ \"singleInt64\": " + jsonValue + "}";
...@@ -422,8 +440,11 @@ namespace Google.Protobuf ...@@ -422,8 +440,11 @@ namespace Google.Protobuf
// Assume that anything non-bounds-related is covered in the Int32 case // Assume that anything non-bounds-related is covered in the Int32 case
[Test] [Test]
[TestCase("-9223372036854775809", Ignore = true, Reason = "Desired behaviour unclear")] [TestCase("9223372036854775808")]
[TestCase("9223372036854775808", Ignore = true, Reason = "Desired behaviour unclear")] // Theoretical bound would be -9223372036854775809, but when that is parsed to a double
// we end up with the exact value of long.MinValue due to lack of precision. The value here
// is the "next double down".
[TestCase("-9223372036854780000")]
public void NumberToInt64_Invalid(string jsonValue) public void NumberToInt64_Invalid(string jsonValue)
{ {
string json = "{ \"singleInt64\": " + jsonValue + "}"; string json = "{ \"singleInt64\": " + jsonValue + "}";
...@@ -433,7 +454,9 @@ namespace Google.Protobuf ...@@ -433,7 +454,9 @@ namespace Google.Protobuf
[Test] [Test]
[TestCase("0", 0UL)] [TestCase("0", 0UL)]
[TestCase("1", 1UL)] [TestCase("1", 1UL)]
[TestCase("18446744073709551615", 18446744073709551615, Ignore = true, Reason = "Desired behaviour unclear")] // ulong.MaxValue isn't representable as a double. This value is the largest double within
// the range of ulong.
[TestCase("18446744073709500416", 18446744073709500416UL)]
public void NumberToUInt64_Valid(string jsonValue, ulong expectedParsedValue) public void NumberToUInt64_Valid(string jsonValue, ulong expectedParsedValue)
{ {
string json = "{ \"singleUint64\": " + jsonValue + "}"; string json = "{ \"singleUint64\": " + jsonValue + "}";
...@@ -475,9 +498,9 @@ namespace Google.Protobuf ...@@ -475,9 +498,9 @@ namespace Google.Protobuf
} }
[Test] [Test]
[TestCase("1.7977e308", Ignore = true, Reason = "Desired behaviour unclear")] [TestCase("1.7977e308")]
[TestCase("-1.7977e308", Ignore = true, Reason = "Desired behaviour unclear")] [TestCase("-1.7977e308")]
[TestCase("1e309", Ignore = true, Reason = "Desired behaviour unclear")] [TestCase("1e309")]
[TestCase("1,0")] [TestCase("1,0")]
[TestCase("1.0.0")] [TestCase("1.0.0")]
[TestCase("+1")] [TestCase("+1")]
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment