diff options
author | Simon Warta <[email protected]> | 2015-08-07 22:46:21 +0200 |
---|---|---|
committer | Simon Warta <[email protected]> | 2015-08-08 00:49:02 +0200 |
commit | 5a772ce97221e6bf10f9eca7f6d0c9fbf5377be9 (patch) | |
tree | 71ce785b1564a390dc2d160f305766ecc95ba8e6 /src/tests | |
parent | 463f55bf59c77adb4101cbeeccf20b3f15975185 (diff) |
Add matchers for integers and bool
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/catchy/catchy_tests.h | 49 | ||||
-rw-r--r-- | src/tests/catchy/test_bigint.cpp | 50 |
2 files changed, 73 insertions, 26 deletions
diff --git a/src/tests/catchy/catchy_tests.h b/src/tests/catchy/catchy_tests.h index d94cd6c92..f1170fa71 100644 --- a/src/tests/catchy/catchy_tests.h +++ b/src/tests/catchy/catchy_tests.h @@ -12,6 +12,8 @@ // This is basically https://github.com/philsquared/Catch/pull/466 #include <vector> +#include <type_traits> + namespace Catch { namespace Matchers { @@ -36,6 +38,43 @@ namespace Matchers { }; } // namespace StdVector + namespace Boolean { + struct Equals : MatcherImpl<Equals, bool> { + Equals( const bool expected ) : m_expected( expected ){} + Equals( Equals const& other ) : m_expected( other.m_expected ){} + + virtual ~Equals() override {}; + + virtual bool match( bool const& expr ) const { + return m_expected == expr; + } + virtual std::string toString() const { + return " == " + Catch::toString(m_expected); + } + + bool m_expected; + }; + } // Boolean + + namespace Integer { + template<typename T> + struct Equals : MatcherImpl<Equals<T>, T> { + Equals( const T expected ) : m_expected( expected ){} + Equals( Equals const& other ) : m_expected( other.m_expected ){} + + virtual ~Equals() override {}; + + virtual bool match( T const& expr ) const { + return m_expected == expr; + } + virtual std::string toString() const { + return "== " + Catch::toString(m_expected); + } + + T m_expected; + }; + } // namespace Integer + } // namespace Impl // The following functions create the actual matcher objects. @@ -45,6 +84,16 @@ namespace Matchers { return Impl::StdVector::Equals<T, Alloc>( vec ); } + template <typename T, + typename = typename std::enable_if<std::numeric_limits<T>::is_integer, T>::type> + inline Impl::Integer::Equals<T> Equals( T expected ) { + return Impl::Integer::Equals<T>( expected ); + } + + inline Impl::Boolean::Equals Equals( bool expected ) { + return Impl::Boolean::Equals( expected ); + } + } // namespace Matchers } // namespace Catch // END CATCH STD::VECTOR IMPLEMENTATION diff --git a/src/tests/catchy/test_bigint.cpp b/src/tests/catchy/test_bigint.cpp index 7ea2d6370..67821eaf0 100644 --- a/src/tests/catchy/test_bigint.cpp +++ b/src/tests/catchy/test_bigint.cpp @@ -1,9 +1,7 @@ // (C) 2015 Simon Warta (Kullo GmbH) // Botan is released under the Simplified BSD License (see license.txt) -#include "catch.hpp" - -#include <botan/build.h> +#include "catchy_tests.h" #if defined(BOTAN_HAS_BIGINT) @@ -16,57 +14,57 @@ TEST_CASE("Bigint basics", "[bigint]") SECTION("in 0-bit border") { BigInt a(0u); - CHECK(( a.bits() == 0 )); - CHECK(( a.bytes() == 0 )); - CHECK(( a.to_u32bit() == 0 )); + CHECK_THAT(a.bits(), Equals(0)); + CHECK_THAT(a.bytes(), Equals(0)); + CHECK_THAT(a.to_u32bit(), Equals(0)); } SECTION("above 0-bit border") { BigInt a(1u); - CHECK(( a.bits() == 1 )); - CHECK(( a.bytes() == 1 )); - CHECK(( a.to_u32bit() == 1 )); + CHECK_THAT(a.bits(), Equals(1)); + CHECK_THAT(a.bytes(), Equals(1)); + CHECK_THAT(a.to_u32bit(), Equals(1)); } SECTION("in 8-bit border") { BigInt a(255u); - CHECK(( a.bits() == 8 )); - CHECK(( a.bytes() == 1 )); - CHECK(( a.to_u32bit() == 255 )); + CHECK_THAT(a.bits(), Equals(8)); + CHECK_THAT(a.bytes(), Equals(1)); + CHECK_THAT(a.to_u32bit(), Equals(255)); } SECTION("above 8-bit border") { BigInt a(256u); - CHECK(( a.bits() == 9 )); - CHECK(( a.bytes() == 2 )); - CHECK(( a.to_u32bit() == 256 )); + CHECK_THAT(a.bits(), Equals(9)); + CHECK_THAT(a.bytes(), Equals(2)); + CHECK_THAT(a.to_u32bit(), Equals(256)); } SECTION("in 16-bit border") { BigInt a(65535u); - CHECK(( a.bits() == 16 )); - CHECK(( a.bytes() == 2 )); - CHECK(( a.to_u32bit() == 65535 )); + CHECK_THAT(a.bits(), Equals(16)); + CHECK_THAT(a.bytes(), Equals(2)); + CHECK_THAT(a.to_u32bit(), Equals(65535)); } SECTION("above 16-bit border") { BigInt a(65536u); - CHECK(( a.bits() == 17 )); - CHECK(( a.bytes() == 3 )); - CHECK(( a.to_u32bit() == 65536 )); + CHECK_THAT(a.bits(), Equals(17)); + CHECK_THAT(a.bytes(), Equals(3)); + CHECK_THAT(a.to_u32bit(), Equals(65536)); } SECTION("in 32-bit border") { BigInt a(4294967295u); - CHECK(( a.bits() == 32 )); - CHECK(( a.bytes() == 4 )); - CHECK(( a.to_u32bit() == 4294967295u )); + CHECK_THAT(a.bits(), Equals(32)); + CHECK_THAT(a.bytes(), Equals(4)); + CHECK_THAT(a.to_u32bit(), Equals(4294967295u)); } SECTION("above 32-bit border") { BigInt a(4294967296u); - CHECK(( a.bits() == 33 )); - CHECK(( a.bytes() == 5 )); + CHECK_THAT(a.bits(), Equals(33)); + CHECK_THAT(a.bytes(), Equals(5)); CHECK_THROWS( a.to_u32bit() ); } } |