From d5aaeb4cce173a108a81c247c98de1714e02b833 Mon Sep 17 00:00:00 2001 From: Michael Gmelin Date: Sun, 29 Jul 2018 01:29:50 +0200 Subject: [PATCH 1/2] Make section names unique in loops, as catch doesn't support duplicate sections, see also https://github.com/catchorg/Catch2/issues/816#issuecomment-278268122 As a result, when built with gcc, loop iterations were skipped. When built with clang, the test aborted with an assertion in catch.hpp line 6222. This also addresses the issues discussed here: https://github.com/nlohmann/json/issues/1032#issuecomment-378707696 and here: https://github.com/catchorg/Catch2/issues/1241 Please note that this introduces new problems, as some of the unit tests fail now - the library stores keys in lexographical order, while the cbor/msgpack/ubjson examples store them in original order. --- test/src/unit-cbor.cpp | 10 +++++----- test/src/unit-msgpack.cpp | 10 +++++----- test/src/unit-ubjson.cpp | 10 +++++----- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/test/src/unit-cbor.cpp b/test/src/unit-cbor.cpp index 21235b0d..291f893a 100644 --- a/test/src/unit-cbor.cpp +++ b/test/src/unit-cbor.cpp @@ -1815,7 +1815,7 @@ TEST_CASE("CBOR roundtrips", "[hide]") std::ifstream f_json(filename); json j1 = json::parse(f_json); - SECTION("std::vector") + SECTION(filename + ": std::vector") { // parse CBOR file std::ifstream f_cbor(filename + ".cbor", std::ios::binary); @@ -1829,7 +1829,7 @@ TEST_CASE("CBOR roundtrips", "[hide]") CHECK(j1 == j2); } - SECTION("std::ifstream") + SECTION(filename + ": std::ifstream") { // parse CBOR file std::ifstream f_cbor(filename + ".cbor", std::ios::binary); @@ -1840,7 +1840,7 @@ TEST_CASE("CBOR roundtrips", "[hide]") CHECK(j1 == j2); } - SECTION("uint8_t* and size") + SECTION(filename + ": uint8_t* and size") { // parse CBOR file std::ifstream f_cbor(filename + ".cbor", std::ios::binary); @@ -1854,7 +1854,7 @@ TEST_CASE("CBOR roundtrips", "[hide]") CHECK(j1 == j2); } - SECTION("output to output adapters") + SECTION(filename + ": output to output adapters") { // parse CBOR file std::ifstream f_cbor(filename + ".cbor", std::ios::binary); @@ -1862,7 +1862,7 @@ TEST_CASE("CBOR roundtrips", "[hide]") (std::istreambuf_iterator(f_cbor)), std::istreambuf_iterator()); - SECTION("std::vector") + SECTION(filename + ": output adapters: std::vector") { std::vector vec; json::to_cbor(j1, vec); diff --git a/test/src/unit-msgpack.cpp b/test/src/unit-msgpack.cpp index b10afbf4..37e30664 100644 --- a/test/src/unit-msgpack.cpp +++ b/test/src/unit-msgpack.cpp @@ -1504,7 +1504,7 @@ TEST_CASE("MessagePack roundtrips", "[hide]") std::ifstream f_json(filename); json j1 = json::parse(f_json); - SECTION("std::vector") + SECTION(filename + ": std::vector") { // parse MessagePack file std::ifstream f_msgpack(filename + ".msgpack", std::ios::binary); @@ -1518,7 +1518,7 @@ TEST_CASE("MessagePack roundtrips", "[hide]") CHECK(j1 == j2); } - SECTION("std::ifstream") + SECTION(filename + ": std::ifstream") { // parse MessagePack file std::ifstream f_msgpack(filename + ".msgpack", std::ios::binary); @@ -1529,7 +1529,7 @@ TEST_CASE("MessagePack roundtrips", "[hide]") CHECK(j1 == j2); } - SECTION("uint8_t* and size") + SECTION(filename + ": uint8_t* and size") { // parse MessagePack file std::ifstream f_msgpack(filename + ".msgpack", std::ios::binary); @@ -1543,7 +1543,7 @@ TEST_CASE("MessagePack roundtrips", "[hide]") CHECK(j1 == j2); } - SECTION("output to output adapters") + SECTION(filename + ": output to output adapters") { // parse MessagePack file std::ifstream f_msgpack(filename + ".msgpack", std::ios::binary); @@ -1551,7 +1551,7 @@ TEST_CASE("MessagePack roundtrips", "[hide]") (std::istreambuf_iterator(f_msgpack)), std::istreambuf_iterator()); - SECTION("std::vector") + SECTION(filename + ": output adapters: std::vector") { std::vector vec; json::to_msgpack(j1, vec); diff --git a/test/src/unit-ubjson.cpp b/test/src/unit-ubjson.cpp index 0e15c164..0c3f4559 100644 --- a/test/src/unit-ubjson.cpp +++ b/test/src/unit-ubjson.cpp @@ -2203,7 +2203,7 @@ TEST_CASE("UBJSON roundtrips", "[hide]") std::ifstream f_json(filename); json j1 = json::parse(f_json); - SECTION("std::vector") + SECTION(filename + ": std::vector") { // parse MessagePack file std::ifstream f_ubjson(filename + ".ubjson", std::ios::binary); @@ -2217,7 +2217,7 @@ TEST_CASE("UBJSON roundtrips", "[hide]") CHECK(j1 == j2); } - SECTION("std::ifstream") + SECTION(filename + ": std::ifstream") { // parse MessagePack file std::ifstream f_ubjson(filename + ".ubjson", std::ios::binary); @@ -2228,7 +2228,7 @@ TEST_CASE("UBJSON roundtrips", "[hide]") CHECK(j1 == j2); } - SECTION("uint8_t* and size") + SECTION(filename + ": uint8_t* and size") { // parse MessagePack file std::ifstream f_ubjson(filename + ".ubjson", std::ios::binary); @@ -2242,7 +2242,7 @@ TEST_CASE("UBJSON roundtrips", "[hide]") CHECK(j1 == j2); } - SECTION("output to output adapters") + SECTION(filename + ": output to output adapters") { // parse MessagePack file std::ifstream f_ubjson(filename + ".ubjson", std::ios::binary); @@ -2250,7 +2250,7 @@ TEST_CASE("UBJSON roundtrips", "[hide]") (std::istreambuf_iterator(f_ubjson)), std::istreambuf_iterator()); - SECTION("std::vector") + SECTION(filename + ": output adapters: std::vector") { std::vector vec; json::to_ubjson(j1, vec); From 05b27e83b788b1921251d1b84c17ef5392634411 Mon Sep 17 00:00:00 2001 From: Michael Gmelin Date: Sun, 29 Jul 2018 11:57:56 +0200 Subject: [PATCH 2/2] Exclude bytewise comparison in certain tests. These tests never worked - they weren't run before d5aaeb4. Note that these tests would fail because of this library ordering dictionary keys (which is legal). So changing the input files (or modifying stored cbor/msgpack/ubjson files) would make the tests work and they could get removed from "exclude_packaged". Also move parsing of files in these unit tests to within the inner sections, so that they're only parsed number_of_files * number_of_sections instead of number_of_files * number_of_files * number_of_sections (so, instead of close to 100k parses about 700). --- test/src/unit-cbor.cpp | 46 +++++++++++++++++++++++++++++------- test/src/unit-msgpack.cpp | 49 ++++++++++++++++++++++++++++++++------- test/src/unit-ubjson.cpp | 20 ++++++++++++---- 3 files changed, 95 insertions(+), 20 deletions(-) diff --git a/test/src/unit-cbor.cpp b/test/src/unit-cbor.cpp index 291f893a..b8ba0690 100644 --- a/test/src/unit-cbor.cpp +++ b/test/src/unit-cbor.cpp @@ -1660,6 +1660,21 @@ TEST_CASE("CBOR roundtrips", "[hide]") { SECTION("input from flynn") { + // most of these are exluded due to differences in key order (not a real problem) + auto exclude_packed = std::set{ + "test/data/json.org/1.json", + "test/data/json.org/2.json", + "test/data/json.org/3.json", + "test/data/json.org/4.json", + "test/data/json.org/5.json", + "test/data/json_testsuite/sample.json", // kills AppVeyor + "test/data/json_tests/pass1.json", + "test/data/regression/working_file.json", + "test/data/nst_json_testsuite/test_parsing/y_object.json", + "test/data/nst_json_testsuite/test_parsing/y_object_duplicated_key.json", + "test/data/nst_json_testsuite/test_parsing/y_object_long_strings.json", + }; + for (std::string filename : { "test/data/json_nlohmann_tests/all_unicode.json", @@ -1811,12 +1826,12 @@ TEST_CASE("CBOR roundtrips", "[hide]") { CAPTURE(filename); - // parse JSON file - std::ifstream f_json(filename); - json j1 = json::parse(f_json); - SECTION(filename + ": std::vector") { + // parse JSON file + std::ifstream f_json(filename); + json j1 = json::parse(f_json); + // parse CBOR file std::ifstream f_cbor(filename + ".cbor", std::ios::binary); std::vector packed( @@ -1831,6 +1846,10 @@ TEST_CASE("CBOR roundtrips", "[hide]") SECTION(filename + ": std::ifstream") { + // parse JSON file + std::ifstream f_json(filename); + json j1 = json::parse(f_json); + // parse CBOR file std::ifstream f_cbor(filename + ".cbor", std::ios::binary); json j2; @@ -1842,6 +1861,10 @@ TEST_CASE("CBOR roundtrips", "[hide]") SECTION(filename + ": uint8_t* and size") { + // parse JSON file + std::ifstream f_json(filename); + json j1 = json::parse(f_json); + // parse CBOR file std::ifstream f_cbor(filename + ".cbor", std::ios::binary); std::vector packed( @@ -1856,17 +1879,24 @@ TEST_CASE("CBOR roundtrips", "[hide]") SECTION(filename + ": output to output adapters") { + // parse JSON file + std::ifstream f_json(filename); + json j1 = json::parse(f_json); + // parse CBOR file std::ifstream f_cbor(filename + ".cbor", std::ios::binary); std::vector packed( (std::istreambuf_iterator(f_cbor)), std::istreambuf_iterator()); - SECTION(filename + ": output adapters: std::vector") + if (!exclude_packed.count(filename)) { - std::vector vec; - json::to_cbor(j1, vec); - CHECK(vec == packed); + SECTION(filename + ": output adapters: std::vector") + { + std::vector vec; + json::to_cbor(j1, vec); + CHECK(vec == packed); + } } } } diff --git a/test/src/unit-msgpack.cpp b/test/src/unit-msgpack.cpp index 37e30664..f0c4a38c 100644 --- a/test/src/unit-msgpack.cpp +++ b/test/src/unit-msgpack.cpp @@ -1349,6 +1349,24 @@ TEST_CASE("MessagePack roundtrips", "[hide]") { SECTION("input from msgpack-python") { + // most of these are exluded due to differences in key order (not a real problem) + auto exclude_packed = std::set{ + "test/data/json.org/1.json", + "test/data/json.org/2.json", + "test/data/json.org/3.json", + "test/data/json.org/4.json", + "test/data/json.org/5.json", + "test/data/json_testsuite/sample.json", // kills AppVeyor + "test/data/json_tests/pass1.json", + "test/data/regression/working_file.json", + "test/data/nst_json_testsuite/test_parsing/y_object.json", + "test/data/nst_json_testsuite/test_parsing/y_object_basic.json", + "test/data/nst_json_testsuite/test_parsing/y_object_duplicated_key.json", + "test/data/nst_json_testsuite/test_parsing/y_object_long_strings.json", + "test/data/nst_json_testsuite/test_parsing/y_object_simple.json", + "test/data/nst_json_testsuite/test_parsing/y_object_string_unicode.json", + }; + for (std::string filename : { "test/data/json_nlohmann_tests/all_unicode.json", @@ -1500,12 +1518,12 @@ TEST_CASE("MessagePack roundtrips", "[hide]") { CAPTURE(filename); - // parse JSON file - std::ifstream f_json(filename); - json j1 = json::parse(f_json); - SECTION(filename + ": std::vector") { + // parse JSON file + std::ifstream f_json(filename); + json j1 = json::parse(f_json); + // parse MessagePack file std::ifstream f_msgpack(filename + ".msgpack", std::ios::binary); std::vector packed( @@ -1520,6 +1538,10 @@ TEST_CASE("MessagePack roundtrips", "[hide]") SECTION(filename + ": std::ifstream") { + // parse JSON file + std::ifstream f_json(filename); + json j1 = json::parse(f_json); + // parse MessagePack file std::ifstream f_msgpack(filename + ".msgpack", std::ios::binary); json j2; @@ -1531,6 +1553,10 @@ TEST_CASE("MessagePack roundtrips", "[hide]") SECTION(filename + ": uint8_t* and size") { + // parse JSON file + std::ifstream f_json(filename); + json j1 = json::parse(f_json); + // parse MessagePack file std::ifstream f_msgpack(filename + ".msgpack", std::ios::binary); std::vector packed( @@ -1545,17 +1571,24 @@ TEST_CASE("MessagePack roundtrips", "[hide]") SECTION(filename + ": output to output adapters") { + // parse JSON file + std::ifstream f_json(filename); + json j1 = json::parse(f_json); + // parse MessagePack file std::ifstream f_msgpack(filename + ".msgpack", std::ios::binary); std::vector packed( (std::istreambuf_iterator(f_msgpack)), std::istreambuf_iterator()); - SECTION(filename + ": output adapters: std::vector") + if (!exclude_packed.count(filename)) { - std::vector vec; - json::to_msgpack(j1, vec); - CHECK(vec == packed); + SECTION(filename + ": output adapters: std::vector") + { + std::vector vec; + json::to_msgpack(j1, vec); + CHECK(vec == packed); + } } } } diff --git a/test/src/unit-ubjson.cpp b/test/src/unit-ubjson.cpp index 0c3f4559..73a91868 100644 --- a/test/src/unit-ubjson.cpp +++ b/test/src/unit-ubjson.cpp @@ -2199,12 +2199,12 @@ TEST_CASE("UBJSON roundtrips", "[hide]") { CAPTURE(filename); - // parse JSON file - std::ifstream f_json(filename); - json j1 = json::parse(f_json); - SECTION(filename + ": std::vector") { + // parse JSON file + std::ifstream f_json(filename); + json j1 = json::parse(f_json); + // parse MessagePack file std::ifstream f_ubjson(filename + ".ubjson", std::ios::binary); std::vector packed( @@ -2219,6 +2219,10 @@ TEST_CASE("UBJSON roundtrips", "[hide]") SECTION(filename + ": std::ifstream") { + // parse JSON file + std::ifstream f_json(filename); + json j1 = json::parse(f_json); + // parse MessagePack file std::ifstream f_ubjson(filename + ".ubjson", std::ios::binary); json j2; @@ -2230,6 +2234,10 @@ TEST_CASE("UBJSON roundtrips", "[hide]") SECTION(filename + ": uint8_t* and size") { + // parse JSON file + std::ifstream f_json(filename); + json j1 = json::parse(f_json); + // parse MessagePack file std::ifstream f_ubjson(filename + ".ubjson", std::ios::binary); std::vector packed( @@ -2244,6 +2252,10 @@ TEST_CASE("UBJSON roundtrips", "[hide]") SECTION(filename + ": output to output adapters") { + // parse JSON file + std::ifstream f_json(filename); + json j1 = json::parse(f_json); + // parse MessagePack file std::ifstream f_ubjson(filename + ".ubjson", std::ios::binary); std::vector packed(