1
0
mirror of https://github.com/nlohmann/json synced 2024-11-30 15:40:07 +00:00

more test cases

This commit is contained in:
Niels Lohmann 2016-11-29 09:49:05 +01:00
parent c46b4ea990
commit ba3c5e1a25
3 changed files with 130 additions and 35 deletions

View File

@ -6204,8 +6204,6 @@ class basic_json
static_cast<T>(vec[current_idx + 8]));
}
}
assert(false);
}
static void to_msgpack_internal(const basic_json& j, std::vector<uint8_t>& 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);
}

View File

@ -6204,8 +6204,6 @@ class basic_json
static_cast<T>(vec[current_idx + 8]));
}
}
assert(false);
}
static void to_msgpack_internal(const basic_json& j, std::vector<uint8_t>& 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);
}

View File

@ -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<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(0x39));
uint16_t positive = -1 - i;
expected.push_back(static_cast<uint8_t>((positive >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(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<uint8_t>(result[1]) * 256 + static_cast<uint8_t>(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<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
// check type
CHECK(j.is_number_integer());
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(0x19));
expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(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<uint8_t>(result[1]) * 256 + static_cast<uint8_t>(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<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
// check type
CHECK(j.is_number_integer());
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(0x1a);
@ -306,20 +306,70 @@ TEST_CASE("CBOR")
expected.push_back(static_cast<uint8_t>((i >> 16) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(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<uint32_t>((static_cast<uint32_t>(result[1]) << 030) +
(static_cast<uint32_t>(result[2]) << 020) +
(static_cast<uint32_t>(result[3]) << 010) +
static_cast<uint32_t>(result[4]));
(static_cast<uint32_t>(result[2]) << 020) +
(static_cast<uint32_t>(result[3]) << 010) +
static_cast<uint32_t>(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<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
// check type
CHECK(j.is_number_integer());
// create expected byte vector
std::vector<uint8_t> expected;
expected.push_back(0x1b);
expected.push_back(static_cast<uint8_t>((i >> 070) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 060) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 050) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 040) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 030) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 020) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 010) & 0xff));
expected.push_back(static_cast<uint8_t>(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<uint64_t>((static_cast<uint64_t>(result[1]) << 070) +
(static_cast<uint64_t>(result[2]) << 060) +
(static_cast<uint64_t>(result[3]) << 050) +
(static_cast<uint64_t>(result[4]) << 040) +
(static_cast<uint64_t>(result[5]) << 030) +
(static_cast<uint64_t>(result[6]) << 020) +
(static_cast<uint64_t>(result[7]) << 010) +
static_cast<uint64_t>(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<uint8_t> expected;
expected.push_back(0x1b);
expected.push_back(static_cast<uint8_t>((i >> 070) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 060) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 050) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 040) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 030) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 020) & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 010) & 0xff));
expected.push_back(static_cast<uint8_t>(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<uint64_t>((static_cast<uint64_t>(result[1]) << 070) +
(static_cast<uint64_t>(result[2]) << 060) +
(static_cast<uint64_t>(result[3]) << 050) +
(static_cast<uint64_t>(result[4]) << 040) +
(static_cast<uint64_t>(result[5]) << 030) +
(static_cast<uint64_t>(result[6]) << 020) +
(static_cast<uint64_t>(result[7]) << 010) +
static_cast<uint64_t>(result[8]));
CHECK(restored == i);
// roundtrip
CHECK(json::from_cbor(result) == j);
}
}
}
SECTION("float")