Skip to content

Commit afd964d

Browse files
committed
Convert existing float encoding tests
1 parent bda33f9 commit afd964d

File tree

2 files changed

+38
-46
lines changed

2 files changed

+38
-46
lines changed

src/test/serfloat_tests.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5+
#include <hash.h>
56
#include <test/util/setup_common.h>
67
#include <util/serfloat.h>
8+
#include <serialize.h>
9+
#include <streams.h>
710

811
#include <boost/test/unit_test.hpp>
912

@@ -37,8 +40,14 @@ BOOST_AUTO_TEST_CASE(double_serfloat_tests) {
3740
BOOST_CHECK_EQUAL(TestDouble(-0.0), 0x8000000000000000);
3841
BOOST_CHECK_EQUAL(TestDouble(std::numeric_limits<double>::infinity()), 0x7ff0000000000000);
3942
BOOST_CHECK_EQUAL(TestDouble(-std::numeric_limits<double>::infinity()), 0xfff0000000000000);
43+
BOOST_CHECK_EQUAL(TestDouble(0.5), 0x3fe0000000000000ULL);
44+
BOOST_CHECK_EQUAL(TestDouble(1.0), 0x3ff0000000000000ULL);
45+
BOOST_CHECK_EQUAL(TestDouble(2.0), 0x4000000000000000ULL);
46+
BOOST_CHECK_EQUAL(TestDouble(4.0), 0x4010000000000000ULL);
47+
BOOST_CHECK_EQUAL(TestDouble(785.066650390625), 0x4088888880000000ULL);
4048

41-
if (std::numeric_limits<float>::is_iec559) {
49+
// Roundtrip test on IEC559-compatible systems
50+
if (std::numeric_limits<double>::is_iec559) {
4251
BOOST_CHECK_EQUAL(sizeof(double), 8);
4352
BOOST_CHECK_EQUAL(sizeof(uint64_t), 8);
4453
// Test extreme values
@@ -89,4 +98,32 @@ BOOST_AUTO_TEST_CASE(double_serfloat_tests) {
8998
}
9099
}
91100

101+
/*
102+
Python code to generate the below hashes:
103+
104+
def reversed_hex(x):
105+
return binascii.hexlify(''.join(reversed(x)))
106+
def dsha256(x):
107+
return hashlib.sha256(hashlib.sha256(x).digest()).digest()
108+
109+
reversed_hex(dsha256(''.join(struct.pack('<d', x) for x in range(0,1000)))) == '43d0c82591953c4eafe114590d392676a01585d25b25d433557f0d7878b23f96'
110+
*/
111+
BOOST_AUTO_TEST_CASE(doubles)
112+
{
113+
CDataStream ss(SER_DISK, 0);
114+
// encode
115+
for (int i = 0; i < 1000; i++) {
116+
ss << EncodeDouble(i);
117+
}
118+
BOOST_CHECK(Hash(ss) == uint256S("43d0c82591953c4eafe114590d392676a01585d25b25d433557f0d7878b23f96"));
119+
120+
// decode
121+
for (int i = 0; i < 1000; i++) {
122+
uint64_t val;
123+
ss >> val;
124+
double j = DecodeDouble(val);
125+
BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
126+
}
127+
}
128+
92129
BOOST_AUTO_TEST_SUITE_END()

src/test/serialize_tests.cpp

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -88,51 +88,6 @@ BOOST_AUTO_TEST_CASE(sizes)
8888
BOOST_CHECK_EQUAL(GetSerializeSize(bool(0), 0), 1U);
8989
}
9090

91-
BOOST_AUTO_TEST_CASE(doubles_conversion)
92-
{
93-
// Choose values that map unambiguously to binary floating point to avoid
94-
// rounding issues at the compiler side.
95-
BOOST_CHECK_EQUAL(ser_uint64_to_double(0x0000000000000000ULL), 0.0);
96-
BOOST_CHECK_EQUAL(ser_uint64_to_double(0x3fe0000000000000ULL), 0.5);
97-
BOOST_CHECK_EQUAL(ser_uint64_to_double(0x3ff0000000000000ULL), 1.0);
98-
BOOST_CHECK_EQUAL(ser_uint64_to_double(0x4000000000000000ULL), 2.0);
99-
BOOST_CHECK_EQUAL(ser_uint64_to_double(0x4010000000000000ULL), 4.0);
100-
BOOST_CHECK_EQUAL(ser_uint64_to_double(0x4088888880000000ULL), 785.066650390625);
101-
102-
BOOST_CHECK_EQUAL(ser_double_to_uint64(0.0), 0x0000000000000000ULL);
103-
BOOST_CHECK_EQUAL(ser_double_to_uint64(0.5), 0x3fe0000000000000ULL);
104-
BOOST_CHECK_EQUAL(ser_double_to_uint64(1.0), 0x3ff0000000000000ULL);
105-
BOOST_CHECK_EQUAL(ser_double_to_uint64(2.0), 0x4000000000000000ULL);
106-
BOOST_CHECK_EQUAL(ser_double_to_uint64(4.0), 0x4010000000000000ULL);
107-
BOOST_CHECK_EQUAL(ser_double_to_uint64(785.066650390625), 0x4088888880000000ULL);
108-
}
109-
/*
110-
Python code to generate the below hashes:
111-
112-
def reversed_hex(x):
113-
return binascii.hexlify(''.join(reversed(x)))
114-
def dsha256(x):
115-
return hashlib.sha256(hashlib.sha256(x).digest()).digest()
116-
117-
reversed_hex(dsha256(''.join(struct.pack('<d', x) for x in range(0,1000)))) == '43d0c82591953c4eafe114590d392676a01585d25b25d433557f0d7878b23f96'
118-
*/
119-
BOOST_AUTO_TEST_CASE(doubles)
120-
{
121-
CDataStream ss(SER_DISK, 0);
122-
// encode
123-
for (int i = 0; i < 1000; i++) {
124-
ss << double(i);
125-
}
126-
BOOST_CHECK(Hash(ss) == uint256S("43d0c82591953c4eafe114590d392676a01585d25b25d433557f0d7878b23f96"));
127-
128-
// decode
129-
for (int i = 0; i < 1000; i++) {
130-
double j;
131-
ss >> j;
132-
BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
133-
}
134-
}
135-
13691
BOOST_AUTO_TEST_CASE(varints)
13792
{
13893
// encode

0 commit comments

Comments
 (0)