improved test coverage

This commit is contained in:
Niels Lohmann 2018-01-14 20:07:38 +01:00
parent f85f4967fe
commit 9d6b3731b9
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
4 changed files with 54 additions and 27 deletions

View file

@ -79,7 +79,7 @@ coverage:
mkdir build_coverage mkdir build_coverage
cd build_coverage ; CXX=g++-5 cmake .. -GNinja -DJSON_Coverage=ON cd build_coverage ; CXX=g++-5 cmake .. -GNinja -DJSON_Coverage=ON
cd build_coverage ; ninja cd build_coverage ; ninja
cd build_coverage ; ctest cd build_coverage ; ctest -j10
cd build_coverage ; ninja lcov_html cd build_coverage ; ninja lcov_html
open build_coverage/test/html/index.html open build_coverage/test/html/index.html

View file

@ -813,7 +813,16 @@ class binary_writer
} }
} }
char ubjson_prefix(const BasicJsonType& j) /*!
@brief determine the type prefix of container values
@note This function does not need to be 100% accurate when it comes to
integer limits. In case a number exceeds the limits of int64_t,
this will be detected by a later call to function
write_number_with_ubjson_prefix. Therefore, we return 'L' for any
value that does not fit the previous limits.
*/
char ubjson_prefix(const BasicJsonType& j) const noexcept
{ {
switch (j.type()) switch (j.type())
{ {
@ -841,11 +850,10 @@ class binary_writer
{ {
return 'l'; return 'l';
} }
else if ((std::numeric_limits<int64_t>::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits<int64_t>::max)()) else // no check and assume int64_t (see note above)
{ {
return 'L'; return 'L';
} }
break;
} }
case value_t::number_unsigned: case value_t::number_unsigned:
@ -866,11 +874,10 @@ class binary_writer
{ {
return 'l'; return 'l';
} }
else if (j.m_value.number_unsigned <= (std::numeric_limits<int64_t>::max)()) else // no check and assume int64_t (see note above)
{ {
return 'L'; return 'L';
} }
break;
} }
case value_t::number_float: case value_t::number_float:
@ -885,11 +892,9 @@ class binary_writer
case value_t::object: case value_t::object:
return '{'; return '{';
default: default: // discarded values
break; return 'N';
} }
return '\0';
} }
private: private:

View file

@ -6965,7 +6965,16 @@ class binary_writer
} }
} }
char ubjson_prefix(const BasicJsonType& j) /*!
@brief determine the type prefix of container values
@note This function does not need to be 100% accurate when it comes to
integer limits. In case a number exceeds the limits of int64_t,
this will be detected by a later call to function
write_number_with_ubjson_prefix. Therefore, we return 'L' for any
value that does not fit the previous limits.
*/
char ubjson_prefix(const BasicJsonType& j) const noexcept
{ {
switch (j.type()) switch (j.type())
{ {
@ -6993,11 +7002,10 @@ class binary_writer
{ {
return 'l'; return 'l';
} }
else if ((std::numeric_limits<int64_t>::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits<int64_t>::max)()) else // no check and assume int64_t (see note above)
{ {
return 'L'; return 'L';
} }
break;
} }
case value_t::number_unsigned: case value_t::number_unsigned:
@ -7018,11 +7026,10 @@ class binary_writer
{ {
return 'l'; return 'l';
} }
else if (j.m_value.number_unsigned <= (std::numeric_limits<int64_t>::max)()) else // no check and assume int64_t (see note above)
{ {
return 'L'; return 'L';
} }
break;
} }
case value_t::number_float: case value_t::number_float:
@ -7037,11 +7044,9 @@ class binary_writer
case value_t::object: case value_t::object:
return '{'; return '{';
default: default: // discarded values
break; return 'N';
} }
return '\0';
} }
private: private:

View file

@ -1386,8 +1386,8 @@ TEST_CASE("UBJSON")
{ {
SECTION("array of i") SECTION("array of i")
{ {
json j = {1, 2}; json j = {1, -1};
std::vector<uint8_t> expected = {'[', '$', 'i', '#', 'i', 2, 1, 2}; std::vector<uint8_t> expected = {'[', '$', 'i', '#', 'i', 2, 1, 0xff};
CHECK(json::to_ubjson(j, true, true) == expected); CHECK(json::to_ubjson(j, true, true) == expected);
} }
@ -1400,22 +1400,22 @@ TEST_CASE("UBJSON")
SECTION("array of I") SECTION("array of I")
{ {
json j = {30000, 30001}; json j = {30000, -30000};
std::vector<uint8_t> expected = {'[', '$', 'I', '#', 'i', 2, 0x75, 0x30, 0x75, 0x31}; std::vector<uint8_t> expected = {'[', '$', 'I', '#', 'i', 2, 0x75, 0x30, 0x8a, 0xd0};
CHECK(json::to_ubjson(j, true, true) == expected); CHECK(json::to_ubjson(j, true, true) == expected);
} }
SECTION("array of l") SECTION("array of l")
{ {
json j = {70000, 70001}; json j = {70000, -70000};
std::vector<uint8_t> expected = {'[', '$', 'l', '#', 'i', 2, 0x00, 0x01, 0x11, 0x70, 0x00, 0x01, 0x11, 0x71}; std::vector<uint8_t> expected = {'[', '$', 'l', '#', 'i', 2, 0x00, 0x01, 0x11, 0x70, 0xFF, 0xFE, 0xEE, 0x90};
CHECK(json::to_ubjson(j, true, true) == expected); CHECK(json::to_ubjson(j, true, true) == expected);
} }
SECTION("array of L") SECTION("array of L")
{ {
json j = {5000000000, 5000000001}; json j = {5000000000, -5000000000};
std::vector<uint8_t> expected = {'[', '$', 'L', '#', 'i', 2, 0x00, 0x00, 0x00, 0x01, 0x2A, 0x05, 0xF2, 0x00, 0x00, 0x00, 0x00, 0x01, 0x2A, 0x05, 0xF2, 0x01}; std::vector<uint8_t> expected = {'[', '$', 'L', '#', 'i', 2, 0x00, 0x00, 0x00, 0x01, 0x2A, 0x05, 0xF2, 0x00, 0xFF, 0xFF, 0xFF, 0xFE, 0xD5, 0xFA, 0x0E, 0x00};
CHECK(json::to_ubjson(j, true, true) == expected); CHECK(json::to_ubjson(j, true, true) == expected);
} }
} }
@ -1426,37 +1426,54 @@ TEST_CASE("UBJSON")
{ {
json j = {1u, 2u}; json j = {1u, 2u};
std::vector<uint8_t> expected = {'[', '$', 'i', '#', 'i', 2, 1, 2}; std::vector<uint8_t> expected = {'[', '$', 'i', '#', 'i', 2, 1, 2};
std::vector<uint8_t> expected_size = {'[', '#', 'i', 2, 'i', 1, 'i', 2};
CHECK(json::to_ubjson(j, true, true) == expected); CHECK(json::to_ubjson(j, true, true) == expected);
CHECK(json::to_ubjson(j, true) == expected_size);
} }
SECTION("array of U") SECTION("array of U")
{ {
json j = {200u, 201u}; json j = {200u, 201u};
std::vector<uint8_t> expected = {'[', '$', 'U', '#', 'i', 2, 0xC8, 0xC9}; std::vector<uint8_t> expected = {'[', '$', 'U', '#', 'i', 2, 0xC8, 0xC9};
std::vector<uint8_t> expected_size = {'[', '#', 'i', 2, 'U', 0xC8, 'U', 0xC9};
CHECK(json::to_ubjson(j, true, true) == expected); CHECK(json::to_ubjson(j, true, true) == expected);
CHECK(json::to_ubjson(j, true) == expected_size);
} }
SECTION("array of I") SECTION("array of I")
{ {
json j = {30000u, 30001u}; json j = {30000u, 30001u};
std::vector<uint8_t> expected = {'[', '$', 'I', '#', 'i', 2, 0x75, 0x30, 0x75, 0x31}; std::vector<uint8_t> expected = {'[', '$', 'I', '#', 'i', 2, 0x75, 0x30, 0x75, 0x31};
std::vector<uint8_t> expected_size = {'[', '#', 'i', 2, 'I', 0x75, 0x30, 'I', 0x75, 0x31};
CHECK(json::to_ubjson(j, true, true) == expected); CHECK(json::to_ubjson(j, true, true) == expected);
CHECK(json::to_ubjson(j, true) == expected_size);
} }
SECTION("array of l") SECTION("array of l")
{ {
json j = {70000u, 70001u}; json j = {70000u, 70001u};
std::vector<uint8_t> expected = {'[', '$', 'l', '#', 'i', 2, 0x00, 0x01, 0x11, 0x70, 0x00, 0x01, 0x11, 0x71}; std::vector<uint8_t> expected = {'[', '$', 'l', '#', 'i', 2, 0x00, 0x01, 0x11, 0x70, 0x00, 0x01, 0x11, 0x71};
std::vector<uint8_t> expected_size = {'[', '#', 'i', 2, 'l', 0x00, 0x01, 0x11, 0x70, 'l', 0x00, 0x01, 0x11, 0x71};
CHECK(json::to_ubjson(j, true, true) == expected); CHECK(json::to_ubjson(j, true, true) == expected);
CHECK(json::to_ubjson(j, true) == expected_size);
} }
SECTION("array of L") SECTION("array of L")
{ {
json j = {5000000000u, 5000000001u}; json j = {5000000000u, 5000000001u};
std::vector<uint8_t> expected = {'[', '$', 'L', '#', 'i', 2, 0x00, 0x00, 0x00, 0x01, 0x2A, 0x05, 0xF2, 0x00, 0x00, 0x00, 0x00, 0x01, 0x2A, 0x05, 0xF2, 0x01}; std::vector<uint8_t> expected = {'[', '$', 'L', '#', 'i', 2, 0x00, 0x00, 0x00, 0x01, 0x2A, 0x05, 0xF2, 0x00, 0x00, 0x00, 0x00, 0x01, 0x2A, 0x05, 0xF2, 0x01};
std::vector<uint8_t> expected_size = {'[', '#', 'i', 2, 'L', 0x00, 0x00, 0x00, 0x01, 0x2A, 0x05, 0xF2, 0x00, 'L', 0x00, 0x00, 0x00, 0x01, 0x2A, 0x05, 0xF2, 0x01};
CHECK(json::to_ubjson(j, true, true) == expected); CHECK(json::to_ubjson(j, true, true) == expected);
CHECK(json::to_ubjson(j, true) == expected_size);
} }
} }
SECTION("discarded")
{
json j = {json::value_t::discarded, json::value_t::discarded};
std::vector<uint8_t> expected = {'[', '$', 'N', '#', 'i', 2};
CHECK(json::to_ubjson(j, true, true) == expected);
}
} }
} }