diff --git a/test/src/unit-cbor.cpp b/test/src/unit-cbor.cpp index 16836e887..d13c23b64 100644 --- a/test/src/unit-cbor.cpp +++ b/test/src/unit-cbor.cpp @@ -77,6 +77,41 @@ TEST_CASE("CBOR") { SECTION("signed") { + SECTION("-65535..-257") + { + 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 expected; + expected.push_back(static_cast(0x39)); + uint16_t positive = -1 - i; + expected.push_back(static_cast((positive >> 8) & 0xff)); + expected.push_back(static_cast(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(result[1]) * 256 + static_cast(result[2]); + CHECK(restored == positive); + CHECK(-1-restored == i); + + // roundtrip + CHECK(json::from_cbor(result) == j); + } + } + SECTION("-9263 (int 16)") { json j = -9263; @@ -214,6 +249,82 @@ TEST_CASE("CBOR") } } + SECTION("256..65535") + { + for (size_t i = 256; i <= 65535; ++i) + { + CAPTURE(i); + + // create JSON value with integer number + json j = -1; + j.get_ref() = static_cast(i); + + // check type + CHECK(j.is_number_integer()); + + // create expected byte vector + std::vector expected; + expected.push_back(static_cast(0x19)); + expected.push_back(static_cast((i >> 8) & 0xff)); + expected.push_back(static_cast(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(result[1]) * 256 + static_cast(result[2]); + CHECK(restored == i); + + // roundtrip + CHECK(json::from_cbor(result) == j); + } + } + + SECTION("65536..4294967295") + { + for (uint32_t i : + { + 65536u, 77777u, 1048576u + }) + { + CAPTURE(i); + + // create JSON value with integer number + json j = -1; + j.get_ref() = static_cast(i); + + // check type + CHECK(j.is_number_integer()); + + // create expected byte vector + std::vector expected; + expected.push_back(0x1a); + expected.push_back(static_cast((i >> 24) & 0xff)); + expected.push_back(static_cast((i >> 16) & 0xff)); + expected.push_back(static_cast((i >> 8) & 0xff)); + expected.push_back(static_cast(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((static_cast(result[1]) << 030) + + (static_cast(result[2]) << 020) + + (static_cast(result[3]) << 010) + + static_cast(result[4])); + CHECK(restored == i); + + // roundtrip + CHECK(json::from_cbor(result) == j); + } + } + /* SECTION("-32768..-129 (int 16)") {