|
| 1 | +#include <boost/test/unit_test.hpp> |
| 2 | +#include <climits> |
| 3 | + |
| 4 | +#include "bignum.h" |
| 5 | + |
| 6 | +BOOST_AUTO_TEST_SUITE(bignum_tests) |
| 7 | + |
| 8 | + |
| 9 | +// For the following test case, it is useful to use additional tools. |
| 10 | +// |
| 11 | +// The simplest one to use is the compiler flag -ftrapv, which detects integer |
| 12 | +// overflows and similar errors. However, due to optimizations and compilers |
| 13 | +// taking advantage of undefined behavior sometimes it may not actually detect |
| 14 | +// anything. |
| 15 | +// |
| 16 | +// You can also use compiler-based stack protection to possibly detect possible |
| 17 | +// stack buffer overruns. |
| 18 | +// |
| 19 | +// For more accurate diagnostics, you can use an undefined arithmetic operation |
| 20 | +// detector such as the clang-based tool: |
| 21 | +// |
| 22 | +// "IOC: An Integer Overflow Checker for C/C++" |
| 23 | +// |
| 24 | +// Available at: http://embed.cs.utah.edu/ioc/ |
| 25 | +// |
| 26 | +// It might also be useful to use Google's AddressSanitizer to detect |
| 27 | +// stack buffer overruns, which valgrind can't currently detect. |
| 28 | + |
| 29 | +// Let's force this code not to be inlined, in order to actually |
| 30 | +// test a generic version of the function. This increases the chance |
| 31 | +// that -ftrapv will detect overflows. |
| 32 | +void mysetint64(CBigNum& num, int64 n) __attribute__((noinline)); |
| 33 | + |
| 34 | +void mysetint64(CBigNum& num, int64 n) |
| 35 | +{ |
| 36 | + num.setint64(n); |
| 37 | +} |
| 38 | + |
| 39 | +// For each number, we do 2 tests: one with inline code, then we reset the |
| 40 | +// value to 0, then the second one with a non-inlined function. |
| 41 | +BOOST_AUTO_TEST_CASE(bignum_setint64) |
| 42 | +{ |
| 43 | + int64 n; |
| 44 | + |
| 45 | + { |
| 46 | + n = 0; |
| 47 | + CBigNum num(n); |
| 48 | + BOOST_CHECK(num.ToString() == "0"); |
| 49 | + num.setulong(0); |
| 50 | + BOOST_CHECK(num.ToString() == "0"); |
| 51 | + mysetint64(num, n); |
| 52 | + BOOST_CHECK(num.ToString() == "0"); |
| 53 | + } |
| 54 | + { |
| 55 | + n = 1; |
| 56 | + CBigNum num(n); |
| 57 | + BOOST_CHECK(num.ToString() == "1"); |
| 58 | + num.setulong(0); |
| 59 | + BOOST_CHECK(num.ToString() == "0"); |
| 60 | + mysetint64(num, n); |
| 61 | + BOOST_CHECK(num.ToString() == "1"); |
| 62 | + } |
| 63 | + { |
| 64 | + n = -1; |
| 65 | + CBigNum num(n); |
| 66 | + BOOST_CHECK(num.ToString() == "-1"); |
| 67 | + num.setulong(0); |
| 68 | + BOOST_CHECK(num.ToString() == "0"); |
| 69 | + mysetint64(num, n); |
| 70 | + BOOST_CHECK(num.ToString() == "-1"); |
| 71 | + } |
| 72 | + { |
| 73 | + n = 5; |
| 74 | + CBigNum num(n); |
| 75 | + BOOST_CHECK(num.ToString() == "5"); |
| 76 | + num.setulong(0); |
| 77 | + BOOST_CHECK(num.ToString() == "0"); |
| 78 | + mysetint64(num, n); |
| 79 | + BOOST_CHECK(num.ToString() == "5"); |
| 80 | + } |
| 81 | + { |
| 82 | + n = -5; |
| 83 | + CBigNum num(n); |
| 84 | + BOOST_CHECK(num.ToString() == "-5"); |
| 85 | + num.setulong(0); |
| 86 | + BOOST_CHECK(num.ToString() == "0"); |
| 87 | + mysetint64(num, n); |
| 88 | + BOOST_CHECK(num.ToString() == "-5"); |
| 89 | + } |
| 90 | + { |
| 91 | + n = LLONG_MIN; |
| 92 | + CBigNum num(n); |
| 93 | + BOOST_CHECK(num.ToString() == "-9223372036854775808"); |
| 94 | + num.setulong(0); |
| 95 | + BOOST_CHECK(num.ToString() == "0"); |
| 96 | + mysetint64(num, n); |
| 97 | + BOOST_CHECK(num.ToString() == "-9223372036854775808"); |
| 98 | + } |
| 99 | + { |
| 100 | + n = LLONG_MAX; |
| 101 | + CBigNum num(n); |
| 102 | + BOOST_CHECK(num.ToString() == "9223372036854775807"); |
| 103 | + num.setulong(0); |
| 104 | + BOOST_CHECK(num.ToString() == "0"); |
| 105 | + mysetint64(num, n); |
| 106 | + BOOST_CHECK(num.ToString() == "9223372036854775807"); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +BOOST_AUTO_TEST_SUITE_END() |
0 commit comments