mirror of
https://github.com/nlohmann/json.git
synced 2024-11-23 22:19:02 +08:00
✅ add tests for binary type
This commit is contained in:
parent
fff46ea98c
commit
5c42847011
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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")
|
||||
|
@ -1418,6 +1418,7 @@ TEST_CASE("MessagePack")
|
||||
CHECK_THROWS_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xa5, 0x68, 0x65})), json::parse_error&);
|
||||
CHECK_THROWS_AS(_ = json::from_msgpack(std::vector<uint8_t>({0x92, 0x01})), json::parse_error&);
|
||||
CHECK_THROWS_AS(_ = json::from_msgpack(std::vector<uint8_t>({0x81, 0xa1, 0x61})), json::parse_error&);
|
||||
CHECK_THROWS_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xc4, 0x02})), json::parse_error&);
|
||||
|
||||
CHECK_THROWS_WITH(_ = json::from_msgpack(std::vector<uint8_t>({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<uint8_t>({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<uint8_t>({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<uint8_t>({0x87}), true, false).is_discarded());
|
||||
CHECK(json::from_msgpack(std::vector<uint8_t>({0xcc}), true, false).is_discarded());
|
||||
@ -1477,6 +1480,8 @@ TEST_CASE("MessagePack")
|
||||
CHECK(json::from_msgpack(std::vector<uint8_t>({0xa5, 0x68, 0x65}), true, false).is_discarded());
|
||||
CHECK(json::from_msgpack(std::vector<uint8_t>({0x92, 0x01}), true, false).is_discarded());
|
||||
CHECK(json::from_msgpack(std::vector<uint8_t>({0x81, 0xA1, 0x61}), true, false).is_discarded());
|
||||
CHECK(json::from_msgpack(std::vector<uint8_t>({0xc4, 0x02}), true, false).is_discarded());
|
||||
CHECK(json::from_msgpack(std::vector<uint8_t>({0xc4}), true, false).is_discarded());
|
||||
}
|
||||
|
||||
SECTION("unsupported bytes")
|
||||
|
@ -61,6 +61,7 @@ TEST_CASE("pointer access")
|
||||
CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<json::number_float_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<json::binary_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<json::internal_binary_t*>() == nullptr);
|
||||
}
|
||||
|
||||
SECTION("pointer access to const object_t")
|
||||
@ -90,6 +91,7 @@ TEST_CASE("pointer access")
|
||||
CHECK(value.get_ptr<const json::number_unsigned_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<const json::number_float_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<const json::binary_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<const json::internal_binary_t*>() == nullptr);
|
||||
}
|
||||
|
||||
SECTION("pointer access to array_t")
|
||||
@ -119,7 +121,7 @@ TEST_CASE("pointer access")
|
||||
CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<json::number_float_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<json::binary_t*>() == nullptr);
|
||||
|
||||
CHECK(value.get_ptr<json::internal_binary_t*>() == nullptr);
|
||||
}
|
||||
|
||||
SECTION("pointer access to const array_t")
|
||||
@ -149,7 +151,7 @@ TEST_CASE("pointer access")
|
||||
CHECK(value.get_ptr<const json::number_unsigned_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<const json::number_float_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<const json::binary_t*>() == nullptr);
|
||||
|
||||
CHECK(value.get_ptr<const json::internal_binary_t*>() == nullptr);
|
||||
}
|
||||
|
||||
SECTION("pointer access to string_t")
|
||||
@ -179,6 +181,7 @@ TEST_CASE("pointer access")
|
||||
CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<json::number_float_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<json::binary_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<json::internal_binary_t*>() == nullptr);
|
||||
}
|
||||
|
||||
SECTION("pointer access to const string_t")
|
||||
@ -208,6 +211,7 @@ TEST_CASE("pointer access")
|
||||
CHECK(value.get_ptr<const json::number_unsigned_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<const json::number_float_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<const json::binary_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<const json::internal_binary_t*>() == nullptr);
|
||||
}
|
||||
|
||||
SECTION("pointer access to boolean_t")
|
||||
@ -237,6 +241,7 @@ TEST_CASE("pointer access")
|
||||
CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<json::number_float_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<json::binary_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<json::internal_binary_t*>() == nullptr);
|
||||
}
|
||||
|
||||
SECTION("pointer access to const boolean_t")
|
||||
@ -267,6 +272,7 @@ TEST_CASE("pointer access")
|
||||
CHECK(value.get_ptr<const json::number_float_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<const json::binary_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<const json::binary_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<const json::internal_binary_t*>() == nullptr);
|
||||
}
|
||||
|
||||
SECTION("pointer access to number_integer_t")
|
||||
@ -296,6 +302,7 @@ TEST_CASE("pointer access")
|
||||
CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<json::number_float_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<json::binary_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<json::internal_binary_t*>() == nullptr);
|
||||
}
|
||||
|
||||
SECTION("pointer access to const number_integer_t")
|
||||
@ -325,6 +332,7 @@ TEST_CASE("pointer access")
|
||||
CHECK(value.get_ptr<const json::number_unsigned_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<const json::number_float_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<const json::binary_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<const json::internal_binary_t*>() == nullptr);
|
||||
}
|
||||
|
||||
SECTION("pointer access to number_unsigned_t")
|
||||
@ -354,6 +362,7 @@ TEST_CASE("pointer access")
|
||||
CHECK(value.get_ptr<json::number_unsigned_t*>() != nullptr);
|
||||
CHECK(value.get_ptr<json::number_float_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<json::binary_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<json::internal_binary_t*>() == nullptr);
|
||||
}
|
||||
|
||||
SECTION("pointer access to const number_unsigned_t")
|
||||
@ -383,6 +392,7 @@ TEST_CASE("pointer access")
|
||||
CHECK(value.get_ptr<const json::number_unsigned_t*>() != nullptr);
|
||||
CHECK(value.get_ptr<const json::number_float_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<const json::binary_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<const json::internal_binary_t*>() == nullptr);
|
||||
}
|
||||
|
||||
SECTION("pointer access to number_float_t")
|
||||
@ -412,6 +422,7 @@ TEST_CASE("pointer access")
|
||||
CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<json::number_float_t*>() != nullptr);
|
||||
CHECK(value.get_ptr<json::binary_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<json::internal_binary_t*>() == nullptr);
|
||||
}
|
||||
|
||||
SECTION("pointer access to const number_float_t")
|
||||
@ -471,4 +482,33 @@ TEST_CASE("pointer access")
|
||||
CHECK(value.get_ptr<const json::number_float_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<const json::binary_t*>() != 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<test_type*>();
|
||||
CHECK(p1 == value.get_ptr<test_type*>());
|
||||
//CHECK(*p1 == value.get<test_type>());
|
||||
|
||||
const test_type* p2 = value.get_ptr<const test_type*>();
|
||||
CHECK(p2 == value.get_ptr<const test_type*>());
|
||||
//CHECK(*p2 == value.get<test_type>());
|
||||
|
||||
const test_type* const p3 = value.get_ptr<const test_type* const>();
|
||||
CHECK(p3 == value.get_ptr<const test_type* const>());
|
||||
//CHECK(*p3 == value.get<test_type>());
|
||||
|
||||
// check if null pointers are returned correctly
|
||||
CHECK(value.get_ptr<const json::object_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<const json::array_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<const json::string_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<const json::boolean_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<const json::number_integer_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<const json::number_unsigned_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<const json::number_float_t*>() == nullptr);
|
||||
CHECK(value.get_ptr<const json::internal_binary_t*>() != nullptr);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user