diff options
author | Jack Lloyd <[email protected]> | 2015-07-23 23:53:28 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2015-07-23 23:53:28 -0400 |
commit | c3b243d95e3c200af7fe4533bec6ab8d9f8a27d9 (patch) | |
tree | fb51c52bdf2e1fe0e5bc1fd47b52b3a3ecf16846 /src/tests | |
parent | aaf7af95d54dd015a6649f5bdf32e3746a6ac1c2 (diff) |
BigInt::to_u32bit failed on 32-bit integers. GH #220
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/test_bigint.cpp | 48 |
1 files changed, 47 insertions, 1 deletions
diff --git a/src/tests/test_bigint.cpp b/src/tests/test_bigint.cpp index f6ae2a654..2bd7df2a7 100644 --- a/src/tests/test_bigint.cpp +++ b/src/tests/test_bigint.cpp @@ -17,7 +17,7 @@ #include <cstdlib> #include <iterator> - +#include <sstream> #include <botan/bigint.h> #include <botan/exceptn.h> #include <botan/numthry.h> @@ -26,6 +26,50 @@ using namespace Botan; namespace { +void test_failure(const char* function, const char* file, int line, + const std::string& what_failed) + { + std::cout << "FAIL " << function << " " << file << ":" << line << " " + << what_failed << std::endl; + } + +#define BOTAN_TEST(lhs, rhs, msg) try { \ + ++tests_run; \ + const auto lhs_val = lhs; \ + const auto rhs_val = rhs; \ + const bool cmp = lhs_val == rhs_val; \ + if(!cmp) \ + { \ + std::ostringstream fmt; \ + fmt << "expr '" << #lhs << " == " << #rhs << "' false, " \ + << "actually " << lhs_val << " " << rhs_val \ + << " (" << msg << ")"; \ + test_failure(BOTAN_CURRENT_FUNCTION, __FILE__, __LINE__, fmt.str()); \ + ++tests_failed; \ + } \ + } \ + catch(std::exception& e) \ + { \ + std::ostringstream fmt; \ + fmt << "exception '" << e.what() << "' (" << msg << ")"; \ + test_failure(BOTAN_CURRENT_FUNCTION, __FILE__, __LINE__, fmt.str()); \ + ++tests_failed; \ + } + +size_t test_bigint_to_u32bit() + { + size_t tests_run = 0, tests_failed = 0; + + for(size_t i = 0; i != 32; ++i) + { + const u32bit in = 1 << i; + BOTAN_TEST(in, BigInt(in).to_u32bit(), "In range round trips"); + } + + test_report("BigInt to_u32bit", tests_run, tests_failed); + return tests_failed; + } + void strip_comments(std::string& line) { if(line.find('#') != std::string::npos) @@ -303,6 +347,8 @@ size_t test_bigint() bool first = true; size_t counter = 0; + total_errors += test_bigint_to_u32bit(); + auto& rng = test_rng(); while(!test_data.eof()) |