From 47c6570470eb7657a7e65373a93d9f408179e8d1 Mon Sep 17 00:00:00 2001 From: chenguoping Date: Wed, 13 May 2020 20:30:49 +0800 Subject: [PATCH] Add some test cases about to_cbor() --- test/src/unit-cbor.cpp | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/test/src/unit-cbor.cpp b/test/src/unit-cbor.cpp index 710e7af8..40fe109a 100644 --- a/test/src/unit-cbor.cpp +++ b/test/src/unit-cbor.cpp @@ -130,6 +130,24 @@ TEST_CASE("CBOR") CHECK(result.empty()); } + SECTION("NaN") + { + // NaN value + json j = std::numeric_limits::quiet_NaN(); + std::vector expected = {0xf9, 0x7e, 0x00}; + const auto result = json::to_cbor(j); + CHECK(result == expected); + } + + SECTION("Infinity") + { + // Infinity value + json j = std::numeric_limits::infinity(); + std::vector expected = {0xf9, 0x7c, 0x00}; + const auto result = json::to_cbor(j); + CHECK(result == expected); + } + SECTION("null") { json j = nullptr; @@ -835,6 +853,20 @@ TEST_CASE("CBOR") CHECK(json::from_cbor(result, true, false) == j); } + SECTION("0.5") + { + double v = 0.5; + json j = v; + // its double-precision float binary value is + // {0xfb, 0x3f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} + // but to save memory, we can store it as single-precision float. + std::vector expected = {0xfa, 0x3f, 0x00, 0x00, 0x00}; + const auto result = json::to_cbor(j); + CHECK(result == expected); + // roundtrip + CHECK(json::from_cbor(result) == j); + CHECK(json::from_cbor(result) == v); + } } SECTION("half-precision float (edge cases)") @@ -936,7 +968,7 @@ TEST_CASE("CBOR") SECTION("NaN") { - json j = json::from_cbor(std::vector({0xf9, 0x7c, 0x01})); + json j = json::from_cbor(std::vector({0xf9, 0x7e, 0x00})); json::number_float_t d = j; CHECK(std::isnan(d)); CHECK(j.dump() == "null");