From ee0f23fdc6b8346cd6e48782bd8287a3a39b563b Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Mon, 28 Nov 2016 23:20:03 +0100 Subject: [PATCH] :construction: bug fixes and more tests --- src/json.hpp | 2 +- src/json.hpp.re2c | 2 +- test/Makefile | 2 +- test/src/unit-cbor.cpp | 160 ++++++++++++++++++++++++----------------- 4 files changed, 98 insertions(+), 68 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index 7a04c958..420685cb 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -6490,8 +6490,8 @@ class basic_json v.push_back(0x3b); add_to_vector(v, 8, positive_number); } - break; } + break; } case value_t::number_unsigned: diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 1f831ede..8b08242e 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -6490,8 +6490,8 @@ class basic_json v.push_back(0x3b); add_to_vector(v, 8, positive_number); } - break; } + break; } case value_t::number_unsigned: diff --git a/test/Makefile b/test/Makefile index d6120aa4..488cec89 100644 --- a/test/Makefile +++ b/test/Makefile @@ -77,4 +77,4 @@ test-%: src/unit-%.cpp ../src/json.hpp src/catch.hpp TEST_PATTERN = "*" TEST_PREFIX = "" check: $(TESTCASES) - @cd .. ; for testcase in $(TESTCASES); do echo "Executing $$testcase..."; $(TEST_PREFIX)test/$$testcase $(TEST_PATTERN); done + @cd .. ; for testcase in $(TESTCASES); do echo "Executing $$testcase..."; $(TEST_PREFIX)test/$$testcase $(TEST_PATTERN) || exit 1; done diff --git a/test/src/unit-cbor.cpp b/test/src/unit-cbor.cpp index 6b7d2d2b..16836e88 100644 --- a/test/src/unit-cbor.cpp +++ b/test/src/unit-cbor.cpp @@ -77,67 +77,21 @@ TEST_CASE("CBOR") { SECTION("signed") { - SECTION("-24..-1") + SECTION("-9263 (int 16)") { - for (auto i = -24; i <= -1; ++i) - { - CAPTURE(i); + json j = -9263; + std::vector expected = {0x39, 0x24, 0x2e}; - // create JSON value with integer number - json j = i; + const auto result = json::to_cbor(j); + CHECK(result == expected); - // check type - CHECK(j.is_number_integer()); + int16_t restored = -1 - ((result[1] << 8) + result[2]); + CHECK(restored == -9263); - // create expected byte vector - std::vector expected; - expected.push_back(0x20 - 1 - static_cast(i)); - - // compare result + size - const auto result = json::to_cbor(j); - CHECK(result == expected); - CHECK(result.size() == 1); - - // check individual bytes - CHECK(static_cast(0x20 - 1 - result[0]) == i); - - // roundtrip - CHECK(json::from_cbor(result) == j); - } + // roundtrip + CHECK(json::from_cbor(result) == j); } - - /* - SECTION("0..127 (positive fixnum)") - { - for (size_t i = 0; i <= 255; ++i) - { - CAPTURE(i); - - // create JSON value with integer number - json j = -1; - j.get_ref() = static_cast(i); - - // check type - CHECK(j.is_number_integer()); - - // create expected byte vector - std::vector expected; - expected.push_back(static_cast(i)); - - // compare result + size - const auto result = json::to_msgpack(j); - CHECK(result == expected); - CHECK(result.size() == 1); - - // check individual bytes - CHECK(result[0] == i); - - // roundtrip - CHECK(json::from_msgpack(result) == j); - } - } - */ - + SECTION("-256..-24") { for (auto i = -256; i < -24; ++i) @@ -169,19 +123,95 @@ TEST_CASE("CBOR") } } - SECTION("-9263 (int 16)") + SECTION("-24..-1") { - json j = -9263; - std::vector expected = {0x39, 0x24, 0x2e}; + for (auto i = -24; i <= -1; ++i) + { + CAPTURE(i); - const auto result = json::to_cbor(j); - CHECK(result == expected); + // create JSON value with integer number + json j = i; - int16_t restored = -1 - ((result[1] << 8) + result[2]); - CHECK(restored == -9263); + // check type + CHECK(j.is_number_integer()); - // roundtrip - CHECK(json::from_cbor(result) == j); + // create expected byte vector + std::vector expected; + expected.push_back(0x20 - 1 - static_cast(i)); + + // compare result + size + const auto result = json::to_cbor(j); + CHECK(result == expected); + CHECK(result.size() == 1); + + // check individual bytes + CHECK(static_cast(0x20 - 1 - result[0]) == i); + + // roundtrip + CHECK(json::from_cbor(result) == j); + } + } + + SECTION("0..22") + { + for (size_t i = 0; i <= 22; ++i) + { + CAPTURE(i); + + // create JSON value with integer number + json j = -1; + j.get_ref() = static_cast(i); + + // check type + CHECK(j.is_number_integer()); + + // create expected byte vector + std::vector expected; + expected.push_back(static_cast(i)); + + // compare result + size + const auto result = json::to_cbor(j); + CHECK(result == expected); + CHECK(result.size() == 1); + + // check individual bytes + CHECK(result[0] == i); + + // roundtrip + CHECK(json::from_cbor(result) == j); + } + } + + SECTION("23..255") + { + for (size_t i = 23; i <= 255; ++i) + { + CAPTURE(i); + + // create JSON value with integer number + json j = -1; + j.get_ref() = static_cast(i); + + // check type + CHECK(j.is_number_integer()); + + // create expected byte vector + std::vector expected; + expected.push_back(static_cast(0x18)); + expected.push_back(static_cast(i)); + + // compare result + size + const auto result = json::to_cbor(j); + CHECK(result == expected); + CHECK(result.size() == 2); + + // check individual bytes + CHECK(result[0] == 0x18); + CHECK(result[1] == i); + + // roundtrip + CHECK(json::from_cbor(result) == j); + } } /*