Skip to content

Commit d59e1d7

Browse files
committed
update msgpack_basic test for compact double packaging
update pack_double and pack_float to avoid warnings
1 parent d5c837b commit d59e1d7

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

include/msgpack/v1/pack.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,10 +1140,10 @@ inline packer<Stream>& packer<Stream>::pack_float(float d)
11401140
{
11411141
if(d == d) { // check for nan
11421142
// compare d to limits to avoid undefined behaviour
1143-
if(d >= 0 && d <= std::numeric_limits<uint64_t>::max() && d == uint64_t(d)) {
1143+
if(d >= 0 && d <= float(std::numeric_limits<uint64_t>::max()) && d == float(uint64_t(d))) {
11441144
pack_imp_uint64(uint64_t(d));
11451145
return *this;
1146-
} else if(d >= std::numeric_limits<int64_t>::min() && d == int64_t(d)) {
1146+
} else if(d >= float(std::numeric_limits<int64_t>::min()) && d == float(int64_t(d))) {
11471147
pack_imp_int64(int64_t(d));
11481148
return *this;
11491149
}
@@ -1162,10 +1162,10 @@ inline packer<Stream>& packer<Stream>::pack_double(double d)
11621162
{
11631163
if(d == d) { // check for nan
11641164
// compare d to limits to avoid undefined behaviour
1165-
if(d >= 0 && d <= std::numeric_limits<uint64_t>::max() && d == uint64_t(d)) {
1165+
if(d >= 0 && d <= double(std::numeric_limits<uint64_t>::max()) && d == double(uint64_t(d))) {
11661166
pack_imp_uint64(uint64_t(d));
11671167
return *this;
1168-
} else if(d >= std::numeric_limits<int64_t>::min() && d == int64_t(d)) {
1168+
} else if(d >= double(std::numeric_limits<int64_t>::min()) && d == double(int64_t(d))) {
11691169
pack_imp_int64(int64_t(d));
11701170
return *this;
11711171
}

test/msgpack_basic.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ BOOST_AUTO_TEST_CASE(simple_buffer_float)
154154
v.push_back(-0.0);
155155
v.push_back(1.0);
156156
v.push_back(-1.0);
157+
v.push_back(1.1f);
158+
v.push_back(-1.1f);
157159
v.push_back(numeric_limits<float>::min());
158160
v.push_back(numeric_limits<float>::max());
159161
v.push_back(nanf("tag"));
@@ -186,6 +188,12 @@ BOOST_AUTO_TEST_CASE(simple_buffer_float)
186188
BOOST_CHECK(std::isinf(val2));
187189
else
188190
BOOST_CHECK(fabs(val2 - val1) <= kEPS);
191+
192+
// check for compact storing of float
193+
if (val1 == val1 && val1 == float(int64_t(val1)))
194+
BOOST_REQUIRE_EQUAL(sbuf.size(),1);
195+
else
196+
BOOST_REQUIRE_EQUAL(sbuf.data()[0],char(0xca));
189197
}
190198
}
191199

@@ -236,6 +244,8 @@ BOOST_AUTO_TEST_CASE(simple_buffer_double)
236244
v.push_back(-0.0);
237245
v.push_back(1.0);
238246
v.push_back(-1.0);
247+
v.push_back(1.1);
248+
v.push_back(-1.1);
239249
v.push_back(numeric_limits<double>::min());
240250
v.push_back(numeric_limits<double>::max());
241251
v.push_back(nanf("tag"));
@@ -272,6 +282,12 @@ BOOST_AUTO_TEST_CASE(simple_buffer_double)
272282
BOOST_CHECK(std::isinf(val2));
273283
else
274284
BOOST_CHECK(fabs(val2 - val1) <= kEPS);
285+
286+
// check for compact storing of double
287+
if (val1 == val1 && val1 == double(int64_t(val1)))
288+
BOOST_REQUIRE_EQUAL(sbuf.size(),1);
289+
else
290+
BOOST_REQUIRE_EQUAL(uint8_t(sbuf.data()[0]),uint8_t(0xcb));
275291
}
276292
}
277293

0 commit comments

Comments
 (0)