diff options
author | Jack Lloyd <[email protected]> | 2018-12-23 14:21:48 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-12-23 14:21:48 -0500 |
commit | f10db2ae3d361132202e8e31376374d0d280482a (patch) | |
tree | 9997453e4d28a1a9e4495c44a97f3ac934dc470d /src/tests | |
parent | 935506524f7bcf52d10662a08a4e3e1376cd180a (diff) |
Make significant_words const time also
Only used in one place, where const time doesn't matter, but can't hurt.
Remove low_bit, can be replaced by ctz.
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); |