From ba3c5e1a25686dfa25d6747df25d1f307aa0722c Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Tue, 29 Nov 2016 09:49:05 +0100 Subject: [PATCH] :white_check_mark: more test cases --- src/json.hpp | 6 +- src/json.hpp.re2c | 6 +- test/src/unit-cbor.cpp | 153 +++++++++++++++++++++++++++++++++-------- 3 files changed, 130 insertions(+), 35 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index 420685cb..dc8939c9 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -6204,8 +6204,6 @@ class basic_json static_cast(vec[current_idx + 8])); } } - - assert(false); } static void to_msgpack_internal(const basic_json& j, std::vector& v) @@ -6452,7 +6450,7 @@ class basic_json } else if (j.m_value.number_integer <= UINT64_MAX) { - v.push_back(0xcf); + v.push_back(0x1b); // eight-byte uint64_t add_to_vector(v, 8, j.m_value.number_integer); } @@ -6520,7 +6518,7 @@ class basic_json } else if (j.m_value.number_unsigned <= 0xffffffffffffffff) { - v.push_back(0xcf); + v.push_back(0x1b); // eight-byte uint64_t add_to_vector(v, 8, j.m_value.number_unsigned); } diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 8b08242e..1e017f4f 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -6204,8 +6204,6 @@ class basic_json static_cast(vec[current_idx + 8])); } } - - assert(false); } static void to_msgpack_internal(const basic_json& j, std::vector& v) @@ -6452,7 +6450,7 @@ class basic_json } else if (j.m_value.number_integer <= UINT64_MAX) { - v.push_back(0xcf); + v.push_back(0x1b); // eight-byte uint64_t add_to_vector(v, 8, j.m_value.number_integer); } @@ -6520,7 +6518,7 @@ class basic_json } else if (j.m_value.number_unsigned <= 0xffffffffffffffff) { - v.push_back(0xcf); + v.push_back(0x1b); // eight-byte uint64_t add_to_vector(v, 8, j.m_value.number_unsigned); } diff --git a/test/src/unit-cbor.cpp b/test/src/unit-cbor.cpp index d13c23b6..31367b1c 100644 --- a/test/src/unit-cbor.cpp +++ b/test/src/unit-cbor.cpp @@ -82,36 +82,36 @@ TEST_CASE("CBOR") for (int16_t i = -65535; i <= -257; ++i) { CAPTURE(i); - + // create JSON value with integer number json j = i; - + // check type CHECK(j.is_number_integer()); - + // create expected byte vector std::vector expected; expected.push_back(static_cast(0x39)); uint16_t positive = -1 - i; expected.push_back(static_cast((positive >> 8) & 0xff)); expected.push_back(static_cast(positive & 0xff)); - + // compare result + size const auto result = json::to_cbor(j); CHECK(result == expected); CHECK(result.size() == 3); - + // check individual bytes CHECK(result[0] == 0x39); uint16_t restored = static_cast(result[1]) * 256 + static_cast(result[2]); CHECK(restored == positive); - CHECK(-1-restored == i); - + CHECK(-1 - restored == i); + // roundtrip CHECK(json::from_cbor(result) == j); } } - + SECTION("-9263 (int 16)") { json j = -9263; @@ -126,7 +126,7 @@ TEST_CASE("CBOR") // roundtrip CHECK(json::from_cbor(result) == j); } - + SECTION("-256..-24") { for (auto i = -256; i < -24; ++i) @@ -254,30 +254,30 @@ TEST_CASE("CBOR") for (size_t i = 256; i <= 65535; ++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(0x19)); expected.push_back(static_cast((i >> 8) & 0xff)); expected.push_back(static_cast(i & 0xff)); - + // compare result + size const auto result = json::to_cbor(j); CHECK(result == expected); CHECK(result.size() == 3); - + // check individual bytes CHECK(result[0] == 0x19); uint16_t restored = static_cast(result[1]) * 256 + static_cast(result[2]); CHECK(restored == i); - + // roundtrip CHECK(json::from_cbor(result) == j); } @@ -286,19 +286,19 @@ TEST_CASE("CBOR") SECTION("65536..4294967295") { for (uint32_t i : - { - 65536u, 77777u, 1048576u - }) + { + 65536u, 77777u, 1048576u + }) { 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(0x1a); @@ -306,20 +306,70 @@ TEST_CASE("CBOR") expected.push_back(static_cast((i >> 16) & 0xff)); expected.push_back(static_cast((i >> 8) & 0xff)); expected.push_back(static_cast(i & 0xff)); - + // compare result + size const auto result = json::to_cbor(j); CHECK(result == expected); CHECK(result.size() == 5); - + // check individual bytes CHECK(result[0] == 0x1a); uint32_t restored = static_cast((static_cast(result[1]) << 030) + - (static_cast(result[2]) << 020) + - (static_cast(result[3]) << 010) + - static_cast(result[4])); + (static_cast(result[2]) << 020) + + (static_cast(result[3]) << 010) + + static_cast(result[4])); CHECK(restored == i); - + + // roundtrip + CHECK(json::from_cbor(result) == j); + } + } + + SECTION("4294967296..4611686018427387903") + { + for (uint64_t i : + { + 4294967296ul, 4611686018427387903ul + }) + { + 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(0x1b); + expected.push_back(static_cast((i >> 070) & 0xff)); + expected.push_back(static_cast((i >> 060) & 0xff)); + expected.push_back(static_cast((i >> 050) & 0xff)); + expected.push_back(static_cast((i >> 040) & 0xff)); + expected.push_back(static_cast((i >> 030) & 0xff)); + expected.push_back(static_cast((i >> 020) & 0xff)); + expected.push_back(static_cast((i >> 010) & 0xff)); + expected.push_back(static_cast(i & 0xff)); + + // compare result + size + const auto result = json::to_cbor(j); + CHECK(result == expected); + CHECK(result.size() == 9); + + // check individual bytes + CHECK(result[0] == 0x1b); + uint64_t restored = static_cast((static_cast(result[1]) << 070) + + (static_cast(result[2]) << 060) + + (static_cast(result[3]) << 050) + + (static_cast(result[4]) << 040) + + (static_cast(result[5]) << 030) + + (static_cast(result[6]) << 020) + + (static_cast(result[7]) << 010) + + static_cast(result[8])); + CHECK(restored == i); + // roundtrip CHECK(json::from_cbor(result) == j); } @@ -497,6 +547,55 @@ TEST_CASE("CBOR") CHECK(json::from_msgpack(result) == j); } } + + SECTION("4294967296..4611686018427387903") + { + for (uint64_t i : + { + 4294967296ul, 4611686018427387903ul + }) + { + CAPTURE(i); + + // create JSON value with integer number + json j = i; + + // check type + CHECK(j.is_number_unsigned()); + + // create expected byte vector + std::vector expected; + expected.push_back(0x1b); + expected.push_back(static_cast((i >> 070) & 0xff)); + expected.push_back(static_cast((i >> 060) & 0xff)); + expected.push_back(static_cast((i >> 050) & 0xff)); + expected.push_back(static_cast((i >> 040) & 0xff)); + expected.push_back(static_cast((i >> 030) & 0xff)); + expected.push_back(static_cast((i >> 020) & 0xff)); + expected.push_back(static_cast((i >> 010) & 0xff)); + expected.push_back(static_cast(i & 0xff)); + + // compare result + size + const auto result = json::to_cbor(j); + CHECK(result == expected); + CHECK(result.size() == 9); + + // check individual bytes + CHECK(result[0] == 0x1b); + uint64_t restored = static_cast((static_cast(result[1]) << 070) + + (static_cast(result[2]) << 060) + + (static_cast(result[3]) << 050) + + (static_cast(result[4]) << 040) + + (static_cast(result[5]) << 030) + + (static_cast(result[6]) << 020) + + (static_cast(result[7]) << 010) + + static_cast(result[8])); + CHECK(restored == i); + + // roundtrip + CHECK(json::from_cbor(result) == j); + } + } } SECTION("float")