diff options
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/test_utils.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/tests/test_utils.cpp b/src/tests/test_utils.cpp index 485d72a2a..de9db6683 100644 --- a/src/tests/test_utils.cpp +++ b/src/tests/test_utils.cpp @@ -245,11 +245,61 @@ class BitOps_Tests final : public Test std::vector<Test::Result> results; results.push_back(test_power_of_2()); + results.push_back(test_ctz()); + results.push_back(test_sig_bytes()); return results; } private: template<typename T> + void test_ctz(Test::Result& result, T val, size_t expected) + { + result.test_eq("ctz(" + std::to_string(val) + ")", Botan::ctz<T>(val), expected); + } + + Test::Result test_ctz() + { + Test::Result result("ctz"); + test_ctz<uint32_t>(result, 0, 32); + test_ctz<uint32_t>(result, 1, 0); + test_ctz<uint32_t>(result, 0x80, 7); + test_ctz<uint32_t>(result, 0x8000000, 27); + test_ctz<uint32_t>(result, 0x8100000, 20); + test_ctz<uint32_t>(result, 0x80000000, 31); + + return result; + } + + template<typename T> + void test_sig_bytes(Test::Result& result, T val, size_t expected) + { + result.test_eq("significant_bytes(" + std::to_string(val) + ")", + Botan::significant_bytes<T>(val), expected); + } + + Test::Result test_sig_bytes() + { + Test::Result result("significant_bytes"); + test_sig_bytes<uint32_t>(result, 0, 0); + test_sig_bytes<uint32_t>(result, 1, 1); + test_sig_bytes<uint32_t>(result, 0x80, 1); + test_sig_bytes<uint32_t>(result, 255, 1); + test_sig_bytes<uint32_t>(result, 256, 2); + test_sig_bytes<uint32_t>(result, 65535, 2); + test_sig_bytes<uint32_t>(result, 65536, 3); + test_sig_bytes<uint32_t>(result, 0x80000000, 4); + + test_sig_bytes<uint64_t>(result, 0, 0); + test_sig_bytes<uint64_t>(result, 1, 1); + test_sig_bytes<uint64_t>(result, 0x80, 1); + test_sig_bytes<uint64_t>(result, 256, 2); + test_sig_bytes<uint64_t>(result, 0x80000000, 4); + test_sig_bytes<uint64_t>(result, 0x100000000, 5); + + return result; + } + + template<typename T> void test_power_of_2(Test::Result& result, T val, bool expected) { result.test_eq("power_of_2(" + std::to_string(val) + ")", Botan::is_power_of_2<T>(val), expected); |