1
0
mirror of https://github.com/nlohmann/json synced 2024-11-22 20:10:06 +00:00

add test for get_ptr<binary_t*>

This commit is contained in:
Niels Lohmann 2020-05-06 22:13:55 +02:00
parent 1d6f7b0d4e
commit 3cd2a977ae
No known key found for this signature in database
GPG Key ID: 7F3CEA63AE251B69

View File

@ -34,22 +34,6 @@ using nlohmann::json;
TEST_CASE("pointer access") TEST_CASE("pointer access")
{ {
// create a JSON value with different types
json json_types =
{
{"boolean", true},
{
"number", {
{"integer", 42},
{"unsigned", 42u},
{"floating-point", 17.23}
}
},
{"string", "Hello, world!"},
{"array", {1, 2, 3, 4, 5}},
{"null", nullptr}
};
SECTION("pointer access to object_t") SECTION("pointer access to object_t")
{ {
using test_type = json::object_t; using test_type = json::object_t;
@ -61,11 +45,11 @@ TEST_CASE("pointer access")
CHECK(*p1 == value.get<test_type>()); CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>(); const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>()); CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == value.get<test_type>()); CHECK(*p2 == value.get<test_type>());
const test_type* const p3 = value.get_ptr<const test_type* const>(); const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>()); CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == value.get<test_type>()); CHECK(*p3 == value.get<test_type>());
// check if null pointers are returned correctly // check if null pointers are returned correctly
@ -76,6 +60,7 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<json::number_integer_t*>() == nullptr); CHECK(value.get_ptr<json::number_integer_t*>() == nullptr);
CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr); CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr);
CHECK(value.get_ptr<json::number_float_t*>() == nullptr); CHECK(value.get_ptr<json::number_float_t*>() == nullptr);
CHECK(value.get_ptr<json::binary_t*>() == nullptr);
} }
SECTION("pointer access to const object_t") SECTION("pointer access to const object_t")
@ -89,11 +74,11 @@ TEST_CASE("pointer access")
CHECK(*p1 == value.get<test_type>()); CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>(); const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>()); CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == value.get<test_type>()); CHECK(*p2 == value.get<test_type>());
const test_type* const p3 = value.get_ptr<const test_type* const>(); const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>()); CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == value.get<test_type>()); CHECK(*p3 == value.get<test_type>());
// check if null pointers are returned correctly // check if null pointers are returned correctly
@ -104,6 +89,7 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<const json::number_integer_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_unsigned_t*>() == nullptr);
CHECK(value.get_ptr<const json::number_float_t*>() == nullptr); CHECK(value.get_ptr<const json::number_float_t*>() == nullptr);
CHECK(value.get_ptr<const json::binary_t*>() == nullptr);
} }
SECTION("pointer access to array_t") SECTION("pointer access to array_t")
@ -117,11 +103,11 @@ TEST_CASE("pointer access")
CHECK(*p1 == value.get<test_type>()); CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>(); const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>()); CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == value.get<test_type>()); CHECK(*p2 == value.get<test_type>());
const test_type* const p3 = value.get_ptr<const test_type* const>(); const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>()); CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == value.get<test_type>()); CHECK(*p3 == value.get<test_type>());
// check if null pointers are returned correctly // check if null pointers are returned correctly
@ -132,6 +118,8 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<json::number_integer_t*>() == nullptr); CHECK(value.get_ptr<json::number_integer_t*>() == nullptr);
CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr); CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr);
CHECK(value.get_ptr<json::number_float_t*>() == nullptr); CHECK(value.get_ptr<json::number_float_t*>() == nullptr);
CHECK(value.get_ptr<json::binary_t*>() == nullptr);
} }
SECTION("pointer access to const array_t") SECTION("pointer access to const array_t")
@ -145,11 +133,11 @@ TEST_CASE("pointer access")
CHECK(*p1 == value.get<test_type>()); CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>(); const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>()); CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == value.get<test_type>()); CHECK(*p2 == value.get<test_type>());
const test_type* const p3 = value.get_ptr<const test_type* const>(); const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>()); CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == value.get<test_type>()); CHECK(*p3 == value.get<test_type>());
// check if null pointers are returned correctly // check if null pointers are returned correctly
@ -160,6 +148,8 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<const json::number_integer_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_unsigned_t*>() == nullptr);
CHECK(value.get_ptr<const json::number_float_t*>() == nullptr); CHECK(value.get_ptr<const json::number_float_t*>() == nullptr);
CHECK(value.get_ptr<const json::binary_t*>() == nullptr);
} }
SECTION("pointer access to string_t") SECTION("pointer access to string_t")
@ -173,11 +163,11 @@ TEST_CASE("pointer access")
CHECK(*p1 == value.get<test_type>()); CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>(); const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>()); CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == value.get<test_type>()); CHECK(*p2 == value.get<test_type>());
const test_type* const p3 = value.get_ptr<const test_type* const>(); const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>()); CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == value.get<test_type>()); CHECK(*p3 == value.get<test_type>());
// check if null pointers are returned correctly // check if null pointers are returned correctly
@ -188,6 +178,7 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<json::number_integer_t*>() == nullptr); CHECK(value.get_ptr<json::number_integer_t*>() == nullptr);
CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr); CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr);
CHECK(value.get_ptr<json::number_float_t*>() == nullptr); CHECK(value.get_ptr<json::number_float_t*>() == nullptr);
CHECK(value.get_ptr<json::binary_t*>() == nullptr);
} }
SECTION("pointer access to const string_t") SECTION("pointer access to const string_t")
@ -201,11 +192,11 @@ TEST_CASE("pointer access")
CHECK(*p1 == value.get<test_type>()); CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>(); const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>()); CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == value.get<test_type>()); CHECK(*p2 == value.get<test_type>());
const test_type* const p3 = value.get_ptr<const test_type* const>(); const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>()); CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == value.get<test_type>()); CHECK(*p3 == value.get<test_type>());
// check if null pointers are returned correctly // check if null pointers are returned correctly
@ -216,6 +207,7 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<const json::number_integer_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_unsigned_t*>() == nullptr);
CHECK(value.get_ptr<const json::number_float_t*>() == nullptr); CHECK(value.get_ptr<const json::number_float_t*>() == nullptr);
CHECK(value.get_ptr<const json::binary_t*>() == nullptr);
} }
SECTION("pointer access to boolean_t") SECTION("pointer access to boolean_t")
@ -229,11 +221,11 @@ TEST_CASE("pointer access")
CHECK(*p1 == value.get<test_type>()); CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>(); const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>()); CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == value.get<test_type>()); CHECK(*p2 == value.get<test_type>());
const test_type* const p3 = value.get_ptr<const test_type* const>(); const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>()); CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == value.get<test_type>()); CHECK(*p3 == value.get<test_type>());
// check if null pointers are returned correctly // check if null pointers are returned correctly
@ -244,6 +236,7 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<json::number_integer_t*>() == nullptr); CHECK(value.get_ptr<json::number_integer_t*>() == nullptr);
CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr); CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr);
CHECK(value.get_ptr<json::number_float_t*>() == nullptr); CHECK(value.get_ptr<json::number_float_t*>() == nullptr);
CHECK(value.get_ptr<json::binary_t*>() == nullptr);
} }
SECTION("pointer access to const boolean_t") SECTION("pointer access to const boolean_t")
@ -257,11 +250,11 @@ TEST_CASE("pointer access")
//CHECK(*p1 == value.get<test_type>()); //CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>(); const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>()); CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == value.get<test_type>()); CHECK(*p2 == value.get<test_type>());
const test_type* const p3 = value.get_ptr<const test_type* const>(); const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>()); CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == value.get<test_type>()); CHECK(*p3 == value.get<test_type>());
// check if null pointers are returned correctly // check if null pointers are returned correctly
@ -272,6 +265,8 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<const json::number_integer_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_unsigned_t*>() == nullptr);
CHECK(value.get_ptr<const json::number_float_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::binary_t*>() == nullptr);
} }
SECTION("pointer access to number_integer_t") SECTION("pointer access to number_integer_t")
@ -285,11 +280,11 @@ TEST_CASE("pointer access")
CHECK(*p1 == value.get<test_type>()); CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>(); const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>()); CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == value.get<test_type>()); CHECK(*p2 == value.get<test_type>());
const test_type* const p3 = value.get_ptr<const test_type* const>(); const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>()); CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == value.get<test_type>()); CHECK(*p3 == value.get<test_type>());
// check if null pointers are returned correctly // check if null pointers are returned correctly
@ -300,6 +295,7 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<json::number_integer_t*>() != nullptr); CHECK(value.get_ptr<json::number_integer_t*>() != nullptr);
CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr); CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr);
CHECK(value.get_ptr<json::number_float_t*>() == nullptr); CHECK(value.get_ptr<json::number_float_t*>() == nullptr);
CHECK(value.get_ptr<json::binary_t*>() == nullptr);
} }
SECTION("pointer access to const number_integer_t") SECTION("pointer access to const number_integer_t")
@ -313,11 +309,11 @@ TEST_CASE("pointer access")
CHECK(*p1 == value.get<test_type>()); CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>(); const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>()); CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == value.get<test_type>()); CHECK(*p2 == value.get<test_type>());
const test_type* const p3 = value.get_ptr<const test_type* const>(); const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>()); CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == value.get<test_type>()); CHECK(*p3 == value.get<test_type>());
// check if null pointers are returned correctly // check if null pointers are returned correctly
@ -328,6 +324,7 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<const json::number_integer_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_unsigned_t*>() == nullptr);
CHECK(value.get_ptr<const json::number_float_t*>() == nullptr); CHECK(value.get_ptr<const json::number_float_t*>() == nullptr);
CHECK(value.get_ptr<const json::binary_t*>() == nullptr);
} }
SECTION("pointer access to number_unsigned_t") SECTION("pointer access to number_unsigned_t")
@ -341,11 +338,11 @@ TEST_CASE("pointer access")
CHECK(*p1 == value.get<test_type>()); CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>(); const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>()); CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == value.get<test_type>()); CHECK(*p2 == value.get<test_type>());
const test_type* const p3 = value.get_ptr<const test_type* const>(); const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>()); CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == value.get<test_type>()); CHECK(*p3 == value.get<test_type>());
// check if null pointers are returned correctly // check if null pointers are returned correctly
@ -356,6 +353,7 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<json::number_integer_t*>() != nullptr); CHECK(value.get_ptr<json::number_integer_t*>() != nullptr);
CHECK(value.get_ptr<json::number_unsigned_t*>() != nullptr); CHECK(value.get_ptr<json::number_unsigned_t*>() != nullptr);
CHECK(value.get_ptr<json::number_float_t*>() == nullptr); CHECK(value.get_ptr<json::number_float_t*>() == nullptr);
CHECK(value.get_ptr<json::binary_t*>() == nullptr);
} }
SECTION("pointer access to const number_unsigned_t") SECTION("pointer access to const number_unsigned_t")
@ -369,11 +367,11 @@ TEST_CASE("pointer access")
CHECK(*p1 == value.get<test_type>()); CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>(); const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>()); CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == value.get<test_type>()); CHECK(*p2 == value.get<test_type>());
const test_type* const p3 = value.get_ptr<const test_type* const>(); const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>()); CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == value.get<test_type>()); CHECK(*p3 == value.get<test_type>());
// check if null pointers are returned correctly // check if null pointers are returned correctly
@ -384,6 +382,7 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<const json::number_integer_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_unsigned_t*>() != nullptr);
CHECK(value.get_ptr<const json::number_float_t*>() == nullptr); CHECK(value.get_ptr<const json::number_float_t*>() == nullptr);
CHECK(value.get_ptr<const json::binary_t*>() == nullptr);
} }
SECTION("pointer access to number_float_t") SECTION("pointer access to number_float_t")
@ -397,11 +396,11 @@ TEST_CASE("pointer access")
CHECK(*p1 == Approx(value.get<test_type>())); CHECK(*p1 == Approx(value.get<test_type>()));
const test_type* p2 = value.get_ptr<const test_type*>(); const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>()); CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == Approx(value.get<test_type>())); CHECK(*p2 == Approx(value.get<test_type>()));
const test_type* const p3 = value.get_ptr<const test_type* const>(); const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>()); CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == Approx(value.get<test_type>())); CHECK(*p3 == Approx(value.get<test_type>()));
// check if null pointers are returned correctly // check if null pointers are returned correctly
@ -412,6 +411,7 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<json::number_integer_t*>() == nullptr); CHECK(value.get_ptr<json::number_integer_t*>() == nullptr);
CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr); CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr);
CHECK(value.get_ptr<json::number_float_t*>() != nullptr); CHECK(value.get_ptr<json::number_float_t*>() != nullptr);
CHECK(value.get_ptr<json::binary_t*>() == nullptr);
} }
SECTION("pointer access to const number_float_t") SECTION("pointer access to const number_float_t")
@ -425,11 +425,11 @@ TEST_CASE("pointer access")
CHECK(*p1 == Approx(value.get<test_type>())); CHECK(*p1 == Approx(value.get<test_type>()));
const test_type* p2 = value.get_ptr<const test_type*>(); const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>()); CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == Approx(value.get<test_type>())); CHECK(*p2 == Approx(value.get<test_type>()));
const test_type* const p3 = value.get_ptr<const test_type* const>(); const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>()); CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == Approx(value.get<test_type>())); CHECK(*p3 == Approx(value.get<test_type>()));
// check if null pointers are returned correctly // check if null pointers are returned correctly
@ -440,5 +440,6 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<const json::number_integer_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_unsigned_t*>() == nullptr);
CHECK(value.get_ptr<const json::number_float_t*>() != nullptr); CHECK(value.get_ptr<const json::number_float_t*>() != nullptr);
CHECK(value.get_ptr<const json::binary_t*>() == nullptr);
} }
} }