1 #include <boost/test/unit_test.hpp>
7 BOOST_AUTO_TEST_SUITE(bignum_tests)
9 // Unfortunately there's no standard way of preventing a function from being
10 // inlined, so we define a macro for it.
12 // You should use it like this:
13 // NOINLINE void function() {...}
15 // This also works and will be defined for any compiler implementing GCC
16 // extensions, such as Clang and ICC.
17 #define NOINLINE __attribute__((noinline))
18 #elif defined(_MSC_VER)
19 #define NOINLINE __declspec(noinline)
21 // We give out a warning because it impacts the correctness of one bignum test.
22 #warning You should define NOINLINE for your compiler.
26 // For the following test case, it is useful to use additional tools.
28 // The simplest one to use is the compiler flag -ftrapv, which detects integer
29 // overflows and similar errors. However, due to optimizations and compilers
30 // taking advantage of undefined behavior sometimes it may not actually detect
33 // You can also use compiler-based stack protection to possibly detect possible
34 // stack buffer overruns.
36 // For more accurate diagnostics, you can use an undefined arithmetic operation
37 // detector such as the clang-based tool:
39 // "IOC: An Integer Overflow Checker for C/C++"
41 // Available at: http://embed.cs.utah.edu/ioc/
43 // It might also be useful to use Google's AddressSanitizer to detect
44 // stack buffer overruns, which valgrind can't currently detect.
46 // Let's force this code not to be inlined, in order to actually
47 // test a generic version of the function. This increases the chance
48 // that -ftrapv will detect overflows.
49 NOINLINE void mysetint64(CBigNum& num, int64 n)
54 // For each number, we do 2 tests: one with inline code, then we reset the
55 // value to 0, then the second one with a non-inlined function.
56 BOOST_AUTO_TEST_CASE(bignum_setint64)
63 BOOST_CHECK(num.ToString() == "0");
65 BOOST_CHECK(num.ToString() == "0");
67 BOOST_CHECK(num.ToString() == "0");
72 BOOST_CHECK(num.ToString() == "1");
74 BOOST_CHECK(num.ToString() == "0");
76 BOOST_CHECK(num.ToString() == "1");
81 BOOST_CHECK(num.ToString() == "-1");
83 BOOST_CHECK(num.ToString() == "0");
85 BOOST_CHECK(num.ToString() == "-1");
90 BOOST_CHECK(num.ToString() == "5");
92 BOOST_CHECK(num.ToString() == "0");
94 BOOST_CHECK(num.ToString() == "5");
99 BOOST_CHECK(num.ToString() == "-5");
101 BOOST_CHECK(num.ToString() == "0");
103 BOOST_CHECK(num.ToString() == "-5");
106 n = std::numeric_limits<int64>::min();
108 BOOST_CHECK(num.ToString() == "-9223372036854775808");
110 BOOST_CHECK(num.ToString() == "0");
112 BOOST_CHECK(num.ToString() == "-9223372036854775808");
115 n = std::numeric_limits<int64>::max();
117 BOOST_CHECK(num.ToString() == "9223372036854775807");
119 BOOST_CHECK(num.ToString() == "0");
121 BOOST_CHECK(num.ToString() == "9223372036854775807");
125 BOOST_AUTO_TEST_SUITE_END()