Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
dota17 committed May 15, 2020
1 parent e175150 commit 5722544
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 3 deletions.
3 changes: 2 additions & 1 deletion include/nlohmann/detail/output/binary_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ class binary_writer
}
else
{
if (j.m_value.number_float < std::numeric_limits<float>::max() and
if (j.m_value.number_float > -std::numeric_limits<float>::min() and
j.m_value.number_float < std::numeric_limits<float>::max() and
static_cast<double>(static_cast<float>(j.m_value.number_float)) == j.m_value.number_float)
{
oa->write_character(get_cbor_float_prefix(static_cast<float>(j.m_value.number_float)));
Expand Down
3 changes: 2 additions & 1 deletion single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12143,7 +12143,8 @@ class binary_writer
}
else
{
if (j.m_value.number_float < std::numeric_limits<float>::max() and
if (j.m_value.number_float > -std::numeric_limits<float>::min() and
j.m_value.number_float < std::numeric_limits<float>::max() and
static_cast<double>(static_cast<float>(j.m_value.number_float)) == j.m_value.number_float)
{
oa->write_character(get_cbor_float_prefix(static_cast<float>(j.m_value.number_float)));
Expand Down
83 changes: 83 additions & 0 deletions test.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
diff --git a/test/src/unit-cbor.cpp b/test/src/unit-cbor.cpp
index 40fe109a..531f56d7 100644
--- a/test/src/unit-cbor.cpp
+++ b/test/src/unit-cbor.cpp
@@ -834,7 +834,7 @@ TEST_CASE("CBOR")
}
}

- SECTION("float")
+ SECTION("double-precision float")
{
SECTION("3.1415925")
{
@@ -853,6 +853,10 @@ TEST_CASE("CBOR")

CHECK(json::from_cbor(result, true, false) == j);
}
+ }
+
+ SECTION("single-precision float")
+ {
SECTION("0.5")
{
double v = 0.5;
@@ -867,6 +871,58 @@ TEST_CASE("CBOR")
CHECK(json::from_cbor(result) == j);
CHECK(json::from_cbor(result) == v);
}
+ SECTION("0.0")
+ {
+ double v = 0.0;
+ json j = v;
+ // its double-precision binary value is:
+ // {0xfb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
+ std::vector<uint8_t> expected = {0xfa, 0x00, 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("-0.0")
+ {
+ 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)")
58 changes: 57 additions & 1 deletion test/src/unit-cbor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ TEST_CASE("CBOR")
}
}

SECTION("float")
SECTION("double-precision float")
{
SECTION("3.1415925")
{
Expand All @@ -853,6 +853,10 @@ TEST_CASE("CBOR")

CHECK(json::from_cbor(result, true, false) == j);
}
}

SECTION("single-precision float")
{
SECTION("0.5")
{
double v = 0.5;
Expand All @@ -867,6 +871,58 @@ TEST_CASE("CBOR")
CHECK(json::from_cbor(result) == j);
CHECK(json::from_cbor(result) == v);
}
SECTION("0.0")
{
double v = 0.0;
json j = v;
// its double-precision binary value is:
// {0xfb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
std::vector<uint8_t> expected = {0xfa, 0x00, 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("-0.0")
{
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)")
Expand Down

0 comments on commit 5722544

Please sign in to comment.