Skip to content

Commit

Permalink
add some testcases about to_cbor()
Browse files Browse the repository at this point in the history
  • Loading branch information
dota17 committed Apr 16, 2020
1 parent 5930420 commit 91c2080
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion test/src/unit-cbor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,24 @@ TEST_CASE("CBOR")
CHECK(result.empty());
}

SECTION("NaN")
{
// NaN value
json j = std::numeric_limits<json::number_float_t>::quiet_NaN();
std::vector<uint8_t> expected = {0xf9, 0x7e, 0x00};
const auto result = json::to_cbor(j);
CHECK(result == expected);
}

SECTION("Infinity")
{
// Infinity value
json j = std::numeric_limits<json::number_float_t>::infinity();
std::vector<uint8_t> expected = {0xf9, 0x7c, 0x00};
const auto result = json::to_cbor(j);
CHECK(result == expected);
}

SECTION("null")
{
json j = nullptr;
Expand Down Expand Up @@ -161,6 +179,24 @@ TEST_CASE("CBOR")
CHECK(json::from_cbor(result, true, false) == j);
}
}
SECTION("use-type parameter")
{
SECTION("type = false")
{
json j = 0.5;
std::vector<uint8_t> expected = {0xfb, 0x3f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
const auto result = json::to_cbor(j, false);
CHECK(result == expected);
}

SECTION("type = true")
{
json j = 0.5;
std::vector<uint8_t> expected = {0xfa, 0x3f, 0x00, 0x00, 0x00};
const auto result = json::to_cbor(j, true);
CHECK(result == expected);
}
}

SECTION("number")
{
Expand Down Expand Up @@ -929,7 +965,7 @@ TEST_CASE("CBOR")

SECTION("NaN")
{
json j = json::from_cbor(std::vector<uint8_t>({0xf9, 0x7c, 0x01}));
json j = json::from_cbor(std::vector<uint8_t>({0xf9, 0x7e, 0x00}));
json::number_float_t d = j;
CHECK(std::isnan(d));
CHECK(j.dump() == "null");
Expand Down

0 comments on commit 91c2080

Please sign in to comment.