✅ more test cases
This commit is contained in:
parent
c46b4ea990
commit
ba3c5e1a25
3 changed files with 130 additions and 35 deletions
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -105,7 +105,7 @@ TEST_CASE("CBOR")
|
|||
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);
|
||||
|
@ -325,6 +325,56 @@ TEST_CASE("CBOR")
|
|||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
SECTION("-32768..-129 (int 16)")
|
||||
{
|
||||
|
@ -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")
|
||||
|
|
Loading…
Reference in a new issue