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])); static_cast<T>(vec[current_idx + 8]));
} }
} }
assert(false);
} }
static void to_msgpack_internal(const basic_json& j, std::vector<uint8_t>& v) 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) else if (j.m_value.number_integer <= UINT64_MAX)
{ {
v.push_back(0xcf); v.push_back(0x1b);
// eight-byte uint64_t // eight-byte uint64_t
add_to_vector(v, 8, j.m_value.number_integer); 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) else if (j.m_value.number_unsigned <= 0xffffffffffffffff)
{ {
v.push_back(0xcf); v.push_back(0x1b);
// eight-byte uint64_t // eight-byte uint64_t
add_to_vector(v, 8, j.m_value.number_unsigned); 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])); static_cast<T>(vec[current_idx + 8]));
} }
} }
assert(false);
} }
static void to_msgpack_internal(const basic_json& j, std::vector<uint8_t>& v) 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) else if (j.m_value.number_integer <= UINT64_MAX)
{ {
v.push_back(0xcf); v.push_back(0x1b);
// eight-byte uint64_t // eight-byte uint64_t
add_to_vector(v, 8, j.m_value.number_integer); 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) else if (j.m_value.number_unsigned <= 0xffffffffffffffff)
{ {
v.push_back(0xcf); v.push_back(0x1b);
// eight-byte uint64_t // eight-byte uint64_t
add_to_vector(v, 8, j.m_value.number_unsigned); add_to_vector(v, 8, j.m_value.number_unsigned);
} }

View file

@ -105,7 +105,7 @@ TEST_CASE("CBOR")
CHECK(result[0] == 0x39); CHECK(result[0] == 0x39);
uint16_t restored = static_cast<uint8_t>(result[1]) * 256 + static_cast<uint8_t>(result[2]); uint16_t restored = static_cast<uint8_t>(result[1]) * 256 + static_cast<uint8_t>(result[2]);
CHECK(restored == positive); CHECK(restored == positive);
CHECK(-1-restored == i); CHECK(-1 - restored == i);
// roundtrip // roundtrip
CHECK(json::from_cbor(result) == j); CHECK(json::from_cbor(result) == j);
@ -286,9 +286,9 @@ TEST_CASE("CBOR")
SECTION("65536..4294967295") SECTION("65536..4294967295")
{ {
for (uint32_t i : for (uint32_t i :
{ {
65536u, 77777u, 1048576u 65536u, 77777u, 1048576u
}) })
{ {
CAPTURE(i); CAPTURE(i);
@ -315,9 +315,59 @@ TEST_CASE("CBOR")
// check individual bytes // check individual bytes
CHECK(result[0] == 0x1a); CHECK(result[0] == 0x1a);
uint32_t restored = static_cast<uint32_t>((static_cast<uint32_t>(result[1]) << 030) + 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[2]) << 020) +
(static_cast<uint32_t>(result[3]) << 010) + (static_cast<uint32_t>(result[3]) << 010) +
static_cast<uint32_t>(result[4])); 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); CHECK(restored == i);
// roundtrip // roundtrip
@ -497,6 +547,55 @@ TEST_CASE("CBOR")
CHECK(json::from_msgpack(result) == j); 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") SECTION("float")