Repository: nlohmann/json
File: test/src/unit-cbor.cpp (C++)

Code before:
                {
                    double v = -0.0;
                    json j = v;
                    // its double-precision binary value is:
                    // {0xfb, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
                    std::vector<uint8_t> expected = {0xfa, 0x80, 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("100.0")
                {
                    double v = 100.0;
                    json j = v;
                    // its double-precision binary value is:
                    // {0xfb, 0x40, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
                    std::vector<uint8_t> expected = {0xfa, 0x42, 0xc8, 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("200.0")
                {
                    double v = 200.0;
                    json j = v;
                    // its double-precision binary value is:
                    // {0xfb, 0x40, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
                    std::vector<uint8_t> expected = {0xfa, 0x43, 0x48, 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)")
            {
                SECTION("errors")
                {
                    SECTION("no byte follows")
                    {
                        json _;
                        CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0xf9})), json::parse_error&);
                        CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0xf9})),
                                          "[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR number: unexpected end of input");
                        CHECK(json::from_cbor(std::vector<uint8_t>({0xf9}), true, false).is_discarded());

Review comment (suggestion):
Please add tests for `lowest()`,`max()` and check that they are returned as `float`. Then also check `lowest()-1` and `max()+1` and check that they are returned as `double`.

Code after:
                {
                    double v = -0.0;
                    json j = v;
                    // its double-precision binary value is:
                    // {0xfb, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
                    std::vector<uint8_t> expected = {0xfa, 0x80, 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("100.0")
                {
                    double v = 100.0;
                    json j = v;
                    // its double-precision binary value is:
                    // {0xfb, 0x40, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
                    std::vector<uint8_t> expected = {0xfa, 0x42, 0xc8, 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("200.0")
                {
                    double v = 200.0;
                    json j = v;
                    // its double-precision binary value is:
                    // {0xfb, 0x40, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
                    std::vector<uint8_t> expected = {0xfa, 0x43, 0x48, 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("3.40282e+38(max float)")
                {
                    float v = std::numeric_limits<float>::max();
                    json j = v;
                    std::vector<uint8_t> expected =
                    {
                        0xfa, 0x7f, 0x7f, 0xff, 0xff
                    };
                    const auto result = json::to_cbor(j);
                    CHECK(result == expected);
                    // roundtrip
                    CHECK(json::from_cbor(result) == j);
                    CHECK(json::from_cbor(result) == v);