MCPcopy Create free account
hub / github.com/ElementsProject/elements / BOOST_AUTO_TEST_CASE

Function BOOST_AUTO_TEST_CASE

src/test/serfloat_tests.cpp:38–99  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

36} // namespace
37
38BOOST_AUTO_TEST_CASE(double_serfloat_tests) {
39 BOOST_CHECK_EQUAL(TestDouble(0.0), 0U);
40 BOOST_CHECK_EQUAL(TestDouble(-0.0), 0x8000000000000000);
41 BOOST_CHECK_EQUAL(TestDouble(std::numeric_limits<double>::infinity()), 0x7ff0000000000000U);
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);
48
49 // Roundtrip test on IEC559-compatible systems
50 if (std::numeric_limits<double>::is_iec559) {
51 BOOST_CHECK_EQUAL(sizeof(double), 8U);
52 BOOST_CHECK_EQUAL(sizeof(uint64_t), 8U);
53 // Test extreme values
54 TestDouble(std::numeric_limits<double>::min());
55 TestDouble(-std::numeric_limits<double>::min());
56 TestDouble(std::numeric_limits<double>::max());
57 TestDouble(-std::numeric_limits<double>::max());
58 TestDouble(std::numeric_limits<double>::lowest());
59 TestDouble(-std::numeric_limits<double>::lowest());
60 TestDouble(std::numeric_limits<double>::quiet_NaN());
61 TestDouble(-std::numeric_limits<double>::quiet_NaN());
62 TestDouble(std::numeric_limits<double>::signaling_NaN());
63 TestDouble(-std::numeric_limits<double>::signaling_NaN());
64 TestDouble(std::numeric_limits<double>::denorm_min());
65 TestDouble(-std::numeric_limits<double>::denorm_min());
66 // Test exact encoding: on currently supported platforms, EncodeDouble
67 // should produce exactly the same as the in-memory representation for non-NaN.
68 for (int j = 0; j < 1000; ++j) {
69 // Iterate over 9 specific bits exhaustively; the others are chosen randomly.
70 // These specific bits are the sign bit, and the 2 top and bottom bits of
71 // exponent and mantissa in the IEEE754 binary64 format.
72 for (int x = 0; x < 512; ++x) {
73 uint64_t v = InsecureRandBits(64);
74 v &= ~(uint64_t{1} << 0);
75 if (x & 1) v |= (uint64_t{1} << 0);
76 v &= ~(uint64_t{1} << 1);
77 if (x & 2) v |= (uint64_t{1} << 1);
78 v &= ~(uint64_t{1} << 50);
79 if (x & 4) v |= (uint64_t{1} << 50);
80 v &= ~(uint64_t{1} << 51);
81 if (x & 8) v |= (uint64_t{1} << 51);
82 v &= ~(uint64_t{1} << 52);
83 if (x & 16) v |= (uint64_t{1} << 52);
84 v &= ~(uint64_t{1} << 53);
85 if (x & 32) v |= (uint64_t{1} << 53);
86 v &= ~(uint64_t{1} << 61);
87 if (x & 64) v |= (uint64_t{1} << 61);
88 v &= ~(uint64_t{1} << 62);
89 if (x & 128) v |= (uint64_t{1} << 62);
90 v &= ~(uint64_t{1} << 63);
91 if (x & 256) v |= (uint64_t{1} << 63);
92 double f;
93 memcpy(&f, &v, 8);
94 uint64_t v2 = TestDouble(f);
95 if (!std::isnan(f)) BOOST_CHECK_EQUAL(v, v2);

Callers

nothing calls this directly

Calls 6

TestDoubleFunction · 0.85
InsecureRandBitsFunction · 0.85
EncodeDoubleFunction · 0.85
uint256SFunction · 0.85
DecodeDoubleFunction · 0.85
HashFunction · 0.50

Tested by

no test coverage detected