From d27d042bf811327a829729037c5e7f4fd71fdb9e Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Wed, 19 Dec 2018 11:27:51 -0500 Subject: Address a couple of Coverity false positives Add tests for is_power_of_2 --- src/tests/test_utils.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) (limited to 'src/tests/test_utils.cpp') diff --git a/src/tests/test_utils.cpp b/src/tests/test_utils.cpp index 6708524b4..485d72a2a 100644 --- a/src/tests/test_utils.cpp +++ b/src/tests/test_utils.cpp @@ -1,5 +1,5 @@ /* -* (C) 2015 Jack Lloyd +* (C) 2015,2018 Jack Lloyd * (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity * (C) 2017 René Korthaus, Rohde & Schwarz Cybersecurity * @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -236,6 +237,51 @@ class CT_Mask_Tests final : public Test BOTAN_REGISTER_TEST("ct_utils", CT_Mask_Tests); +class BitOps_Tests final : public Test + { + public: + std::vector run() override + { + std::vector results; + + results.push_back(test_power_of_2()); + + return results; + } + private: + template + 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(val), expected); + } + + Test::Result test_power_of_2() + { + Test::Result result("is_power_of_2"); + + test_power_of_2(result, 0, false); + test_power_of_2(result, 1, false); + test_power_of_2(result, 2, true); + test_power_of_2(result, 3, false); + test_power_of_2(result, 0x8000, true); + test_power_of_2(result, 0x8001, false); + test_power_of_2(result, 0x8000000, true); + + test_power_of_2(result, 0, false); + test_power_of_2(result, 1, false); + test_power_of_2(result, 2, true); + test_power_of_2(result, 3, false); + test_power_of_2(result, 0x8000, true); + test_power_of_2(result, 0x8001, false); + test_power_of_2(result, 0x8000000, true); + test_power_of_2(result, 0x100000000000, true); + + return result; + } + }; + +BOTAN_REGISTER_TEST("bit_ops", BitOps_Tests); + #if defined(BOTAN_HAS_POLY_DBL) class Poly_Double_Tests final : public Text_Based_Test -- cgit v1.2.3