|
2 | 2 | // Distributed under the MIT software license, see the accompanying
|
3 | 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
4 | 4 |
|
| 5 | +#include <hash.h> |
5 | 6 | #include <test/util/setup_common.h>
|
6 | 7 | #include <util/serfloat.h>
|
| 8 | +#include <serialize.h> |
| 9 | +#include <streams.h> |
7 | 10 |
|
8 | 11 | #include <boost/test/unit_test.hpp>
|
9 | 12 |
|
@@ -37,8 +40,14 @@ BOOST_AUTO_TEST_CASE(double_serfloat_tests) {
|
37 | 40 | BOOST_CHECK_EQUAL(TestDouble(-0.0), 0x8000000000000000);
|
38 | 41 | BOOST_CHECK_EQUAL(TestDouble(std::numeric_limits<double>::infinity()), 0x7ff0000000000000);
|
39 | 42 | 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); |
40 | 48 |
|
41 |
| - if (std::numeric_limits<float>::is_iec559) { |
| 49 | + // Roundtrip test on IEC559-compatible systems |
| 50 | + if (std::numeric_limits<double>::is_iec559) { |
42 | 51 | BOOST_CHECK_EQUAL(sizeof(double), 8);
|
43 | 52 | BOOST_CHECK_EQUAL(sizeof(uint64_t), 8);
|
44 | 53 | // Test extreme values
|
@@ -89,4 +98,32 @@ BOOST_AUTO_TEST_CASE(double_serfloat_tests) {
|
89 | 98 | }
|
90 | 99 | }
|
91 | 100 |
|
| 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 | + |
92 | 129 | BOOST_AUTO_TEST_SUITE_END()
|
0 commit comments