From 5c42847011baf6984cd88f54a75533885341a1aa Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 10 May 2020 13:23:18 +0200 Subject: [PATCH] :white_check_mark: add tests for binary type --- .../nlohmann/detail/input/binary_reader.hpp | 12 +---- single_include/nlohmann/json.hpp | 12 +---- test/src/unit-element_access1.cpp | 16 +++++++ test/src/unit-msgpack.cpp | 5 +++ test/src/unit-pointer_access.cpp | 44 ++++++++++++++++++- 5 files changed, 67 insertions(+), 22 deletions(-) diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index a8468ce0..25de9bda 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -1500,11 +1500,6 @@ class binary_reader */ bool get_msgpack_binary(internal_binary_t& result) { - if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::msgpack, "binary"))) - { - return false; - } - switch (current) { case 0xC4: // bin 8 @@ -1582,11 +1577,8 @@ class binary_reader return get_number(input_format_t::msgpack, result.subtype) and get_binary(input_format_t::msgpack, 16, result); } - default: - { - auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack, "expected binary type specification (0xC4-0xC9, 0xD4-0xD8); last byte: 0x" + last_token, "binary"))); - } + default: // LCOV_EXCL_LINE + assert(false); // LCOV_EXCL_LINE } } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 01be0396..abec8d3f 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -7027,11 +7027,6 @@ class binary_reader */ bool get_msgpack_binary(internal_binary_t& result) { - if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::msgpack, "binary"))) - { - return false; - } - switch (current) { case 0xC4: // bin 8 @@ -7109,11 +7104,8 @@ class binary_reader return get_number(input_format_t::msgpack, result.subtype) and get_binary(input_format_t::msgpack, 16, result); } - default: - { - auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack, "expected binary type specification (0xC4-0xC9, 0xD4-0xD8); last byte: 0x" + last_token, "binary"))); - } + default: // LCOV_EXCL_LINE + assert(false); // LCOV_EXCL_LINE } } diff --git a/test/src/unit-element_access1.cpp b/test/src/unit-element_access1.cpp index 069beb50..46638e27 100644 --- a/test/src/unit-element_access1.cpp +++ b/test/src/unit-element_access1.cpp @@ -892,6 +892,22 @@ TEST_CASE("element access 1") CHECK(it == j.end()); } } + + SECTION("binary") + { + { + json j = json::binary_array({1, 2, 3}); + json::iterator it = j.erase(j.begin(), j.end()); + CHECK(j.type() == json::value_t::null); + CHECK(it == j.end()); + } + { + json j = json::binary_array({1, 2, 3}); + json::const_iterator it = j.erase(j.cbegin(), j.cend()); + CHECK(j.type() == json::value_t::null); + CHECK(it == j.end()); + } + } } SECTION("erase with two invalid iterators") diff --git a/test/src/unit-msgpack.cpp b/test/src/unit-msgpack.cpp index cf5b19e7..40b63735 100644 --- a/test/src/unit-msgpack.cpp +++ b/test/src/unit-msgpack.cpp @@ -1418,6 +1418,7 @@ TEST_CASE("MessagePack") CHECK_THROWS_AS(_ = json::from_msgpack(std::vector({0xa5, 0x68, 0x65})), json::parse_error&); CHECK_THROWS_AS(_ = json::from_msgpack(std::vector({0x92, 0x01})), json::parse_error&); CHECK_THROWS_AS(_ = json::from_msgpack(std::vector({0x81, 0xa1, 0x61})), json::parse_error&); + CHECK_THROWS_AS(_ = json::from_msgpack(std::vector({0xc4, 0x02})), json::parse_error&); CHECK_THROWS_WITH(_ = json::from_msgpack(std::vector({0x87})), "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing MessagePack string: unexpected end of input"); @@ -1457,6 +1458,8 @@ TEST_CASE("MessagePack") "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing MessagePack value: unexpected end of input"); CHECK_THROWS_WITH(_ = json::from_msgpack(std::vector({0x81, 0xa1, 0x61})), "[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing MessagePack value: unexpected end of input"); + CHECK_THROWS_WITH(_ = json::from_msgpack(std::vector({0xc4, 0x02})), + "[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing MessagePack binary: unexpected end of input"); CHECK(json::from_msgpack(std::vector({0x87}), true, false).is_discarded()); CHECK(json::from_msgpack(std::vector({0xcc}), true, false).is_discarded()); @@ -1477,6 +1480,8 @@ TEST_CASE("MessagePack") CHECK(json::from_msgpack(std::vector({0xa5, 0x68, 0x65}), true, false).is_discarded()); CHECK(json::from_msgpack(std::vector({0x92, 0x01}), true, false).is_discarded()); CHECK(json::from_msgpack(std::vector({0x81, 0xA1, 0x61}), true, false).is_discarded()); + CHECK(json::from_msgpack(std::vector({0xc4, 0x02}), true, false).is_discarded()); + CHECK(json::from_msgpack(std::vector({0xc4}), true, false).is_discarded()); } SECTION("unsupported bytes") diff --git a/test/src/unit-pointer_access.cpp b/test/src/unit-pointer_access.cpp index f61cfee9..ed245dc6 100644 --- a/test/src/unit-pointer_access.cpp +++ b/test/src/unit-pointer_access.cpp @@ -61,6 +61,7 @@ TEST_CASE("pointer access") CHECK(value.get_ptr() == nullptr); CHECK(value.get_ptr() == nullptr); CHECK(value.get_ptr() == nullptr); + CHECK(value.get_ptr() == nullptr); } SECTION("pointer access to const object_t") @@ -90,6 +91,7 @@ TEST_CASE("pointer access") CHECK(value.get_ptr() == nullptr); CHECK(value.get_ptr() == nullptr); CHECK(value.get_ptr() == nullptr); + CHECK(value.get_ptr() == nullptr); } SECTION("pointer access to array_t") @@ -119,7 +121,7 @@ TEST_CASE("pointer access") CHECK(value.get_ptr() == nullptr); CHECK(value.get_ptr() == nullptr); CHECK(value.get_ptr() == nullptr); - + CHECK(value.get_ptr() == nullptr); } SECTION("pointer access to const array_t") @@ -149,7 +151,7 @@ TEST_CASE("pointer access") CHECK(value.get_ptr() == nullptr); CHECK(value.get_ptr() == nullptr); CHECK(value.get_ptr() == nullptr); - + CHECK(value.get_ptr() == nullptr); } SECTION("pointer access to string_t") @@ -179,6 +181,7 @@ TEST_CASE("pointer access") CHECK(value.get_ptr() == nullptr); CHECK(value.get_ptr() == nullptr); CHECK(value.get_ptr() == nullptr); + CHECK(value.get_ptr() == nullptr); } SECTION("pointer access to const string_t") @@ -208,6 +211,7 @@ TEST_CASE("pointer access") CHECK(value.get_ptr() == nullptr); CHECK(value.get_ptr() == nullptr); CHECK(value.get_ptr() == nullptr); + CHECK(value.get_ptr() == nullptr); } SECTION("pointer access to boolean_t") @@ -237,6 +241,7 @@ TEST_CASE("pointer access") CHECK(value.get_ptr() == nullptr); CHECK(value.get_ptr() == nullptr); CHECK(value.get_ptr() == nullptr); + CHECK(value.get_ptr() == nullptr); } SECTION("pointer access to const boolean_t") @@ -267,6 +272,7 @@ TEST_CASE("pointer access") CHECK(value.get_ptr() == nullptr); CHECK(value.get_ptr() == nullptr); CHECK(value.get_ptr() == nullptr); + CHECK(value.get_ptr() == nullptr); } SECTION("pointer access to number_integer_t") @@ -296,6 +302,7 @@ TEST_CASE("pointer access") CHECK(value.get_ptr() == nullptr); CHECK(value.get_ptr() == nullptr); CHECK(value.get_ptr() == nullptr); + CHECK(value.get_ptr() == nullptr); } SECTION("pointer access to const number_integer_t") @@ -325,6 +332,7 @@ TEST_CASE("pointer access") CHECK(value.get_ptr() == nullptr); CHECK(value.get_ptr() == nullptr); CHECK(value.get_ptr() == nullptr); + CHECK(value.get_ptr() == nullptr); } SECTION("pointer access to number_unsigned_t") @@ -354,6 +362,7 @@ TEST_CASE("pointer access") CHECK(value.get_ptr() != nullptr); CHECK(value.get_ptr() == nullptr); CHECK(value.get_ptr() == nullptr); + CHECK(value.get_ptr() == nullptr); } SECTION("pointer access to const number_unsigned_t") @@ -383,6 +392,7 @@ TEST_CASE("pointer access") CHECK(value.get_ptr() != nullptr); CHECK(value.get_ptr() == nullptr); CHECK(value.get_ptr() == nullptr); + CHECK(value.get_ptr() == nullptr); } SECTION("pointer access to number_float_t") @@ -412,6 +422,7 @@ TEST_CASE("pointer access") CHECK(value.get_ptr() == nullptr); CHECK(value.get_ptr() != nullptr); CHECK(value.get_ptr() == nullptr); + CHECK(value.get_ptr() == nullptr); } SECTION("pointer access to const number_float_t") @@ -471,4 +482,33 @@ TEST_CASE("pointer access") CHECK(value.get_ptr() == nullptr); CHECK(value.get_ptr() != nullptr); } + + SECTION("pointer access to const binary_t") + { + using test_type = const json::internal_binary_t; + const json value = json::binary_array({}); + + // check if pointers are returned correctly + test_type* p1 = value.get_ptr(); + CHECK(p1 == value.get_ptr()); + //CHECK(*p1 == value.get()); + + const test_type* p2 = value.get_ptr(); + CHECK(p2 == value.get_ptr()); + //CHECK(*p2 == value.get()); + + const test_type* const p3 = value.get_ptr(); + CHECK(p3 == value.get_ptr()); + //CHECK(*p3 == value.get()); + + // check if null pointers are returned correctly + CHECK(value.get_ptr() == nullptr); + CHECK(value.get_ptr() == nullptr); + CHECK(value.get_ptr() == nullptr); + CHECK(value.get_ptr() == nullptr); + CHECK(value.get_ptr() == nullptr); + CHECK(value.get_ptr() == nullptr); + CHECK(value.get_ptr() == nullptr); + CHECK(value.get_ptr() != nullptr); + } }