-
Notifications
You must be signed in to change notification settings - Fork 262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Roundtrip tests are too strict #33
Comments
There are some discussion on #22 as well. The current rountrip test is:
To make it better, what if we relax it in this way:
So that it just make sure that a library will maintain its own format. This is actually need in some situations. |
I would say that the equality of the numerical form is more important than equality of the string form for floathing point values. |
@gregmarr A problem is that, when the parser firstly parses the source JSON, the parsed number may already be incorrect. My proposal above will also have this problem. |
@nlohmann Proposal 2: explicitly check the output to handle |
I just checked the result of @nlohmann 's library:
The problem is not related to These strings are correctly stringified doubles. By "correctness" it means they are convertible back to the exact value of IEEE-754 double, without ambiguity. It is also to be the shortest textual representation. For example, In https://github.com/miloyip/dtoa-benchmark, different conversions routine has been tested.
and
should give "correct" result. It may be argued that, the JSON spec did not say about subnomal double so it should not be checked as "conformance". |
Regarding the setup of the roundtrip tests, as Milo wrote it's currently like this:
Or, using
Which works pretty well, but as noted, this could fail if some library encoded e.g. floating point exponents differently, i.e. in an allowed way that doesn't change the meaning. Milo also suggested the following solution:
This fixes the issue, but let's take a step back. A roundtrip test should check that, when some json input is "round tripped" through a library, the resulting json output is in the same equivalence class as the input, assuming we define two json representations as equivalent if they "encode the same data" (without diving into a formal definition...) In order to check whether two inputs are equivalent, we can use a roundtrip through a reference implementation as a kind of normal-form generator that always yields the same output representation for each equivalence class of json inputs. In other words, we could use the following approach for the roundtrip conformance test:
Unlike the other suggestion, this can't be fooled by a "stupid" or "malicious" library that always serialises to |
There is an issue in the roundtrip tests: The current tests check that the output of a library exactly match the input. However, the JSON specification has several ways to specify a number. For instance, number
1e3
could also be written as1E3
,1e+3
,1E+3
, or1000
. Therefore, any library which, reading1e3
would perform any of these outputs should pass the roundtrip test. Requiring the exact way the number is returned is arbitrary and would be similar to requiring that the order of object keys should remain the same or even requiring whitespace to be preserved.The current tests are in favor of those libraries which (by chance) decide to serialize numbers with letter "e" rather than with letter "E". This affects files
roundtrip24.json
--roundtrip27.json
. When the exponent is changed (e
toE
) or the optional+
is added, the test results differ.More details can be found in the original discussion at nlohmann/json#187.
The text was updated successfully, but these errors were encountered: