#include "../misc/traits.h" #include "../tests/testutils.h" #include #include #include #include #include #include #include #include using namespace std; using namespace CppUtilities::Traits; using namespace CPPUNIT_NS; /// \cond struct SomeStruct { string foo; int bar; }; struct CountableStruct { int numberOfElements = 42; size_t size() const; }; struct TestIncomplete; /// \endcond static_assert(!Bool::value, "Bool"); static_assert(Bool::value, "Bool"); static_assert(!Not>::value, "Not"); static_assert(!Any, Bool>::value, "Any: negative case"); static_assert(Any, Bool>::value, "Any: positive case"); static_assert(!All, Bool>::value, "All: negative case"); static_assert(All, Bool>::value, "All: positive case"); static_assert(!None, Bool>::value, "None: negative case"); static_assert(!None, Bool>::value, "None: negative case"); static_assert(None, Bool>::value, "None: positive case"); static_assert(!IsSpecializationOf::value, "IsSpecializationOf: negative case"); static_assert(IsSpecializationOf::value, "IsSpecializationOf: positive case"); static_assert(IsSpecializationOf::value, "IsSpecializationOf: positive case"); static_assert(IsSpecializationOf::value, "IsSpecializationOf: positive case"); static_assert(IsSpecializationOf::value, "IsSpecializationOf: positive case"); static_assert(!IsSpecializingAnyOf::value, "IsSpecializingAnyOf: negative case"); static_assert(!IsSpecializingAnyOf::value, "IsSpecializingAnyOf: negative case"); static_assert(IsSpecializingAnyOf::value, "IsSpecializingAnyOf: positive case"); static_assert(IsSpecializingAnyOf::value, "IsSpecializingAnyOf: positive case"); static_assert(IsSpecializingAnyOf::value, "IsSpecializingAnyOf: positive case"); static_assert(IsAnyOf::value, "IsAnyOf: positive case"); static_assert(IsAnyOf::value, "IsAnyOf: positive case"); static_assert(IsAnyOf::value, "IsAnyOf: positive case"); static_assert(!IsAnyOf::value, "IsAnyOf: negative case"); static_assert(!IsNoneOf::value, "IsNoneOf: negative case"); static_assert(!IsNoneOf::value, "IsNoneOf: negative case"); static_assert(!IsNoneOf::value, "IsNoneOf: negative case"); static_assert(IsNoneOf::value, "IsNoneOf: positive case"); static_assert(!IsDereferencable::value, "IsDereferencable: negative case"); static_assert(!IsDereferencable::value, "IsDereferencable: negative case"); static_assert(IsDereferencable::value, "IsDereferencable: positive case"); static_assert(IsDereferencable::value, "IsDereferencable: positive case"); static_assert(IsDereferencable>::value, "IsDereferencable: positive case"); static_assert(IsDereferencable>::value, "IsDereferencable: positive case"); static_assert(!IsDereferencable>::value, "IsDereferencable: positive case"); static_assert(!IsIteratable::value, "IsIterator: negative case"); static_assert(!IsIteratable::value, "IsIterator: negative case"); static_assert(IsIteratable::value, "IsIterator: positive case"); static_assert(IsIteratable>::value, "IsIterator: positive case"); static_assert(IsIteratable>::value, "IsIterator: positive case"); static_assert(IsIteratable>::value, "IsIterator: positive case"); static_assert(IsIteratable>::value, "IsIterator: positive case"); static_assert(!HasSize::value, "HasSize: negative case"); static_assert(!HasSize>::value, "HasSize: negative case"); static_assert(HasSize>::value, "HasSize: positive case"); static_assert(HasSize::value, "HasSize: positive case"); static_assert(HasSize::value, "HasSize: positive case"); static_assert(!IsReservable>::value, "HasSize: negative case"); static_assert(IsReservable>::value, "HasSize: positive case"); static_assert(HasOperatorBool>::value, "HasOperatorBool: positive case"); static_assert(!HasOperatorBool::value, "HasOperatorBool: negative case"); static_assert(!IsCString::value, "IsCString: negative case"); static_assert(!IsCString::value, "IsCString: negative case"); static_assert(!IsCString::value, "IsCString: negative case"); static_assert(IsCString::value, "IsCString: positive case"); static_assert(IsCString::value, "IsCString: positive case"); static_assert(IsCString::value, "IsCString: positive case"); static_assert(!IsString::value, "IsString: negative case"); static_assert(!IsString::value, "IsString: negative case"); static_assert(IsString::value, "IsString: positive case"); static_assert(IsString::value, "IsString: positive case"); static_assert(IsString::value, "IsString: positive case (const)"); static_assert(IsString::value, "IsString: positive case (volatile)"); static_assert(IsString::value, "IsString: positive case"); static_assert(!IsComplete::value, "IsComplete: negative case"); static_assert(IsComplete::value, "IsComplete: positive case"); constexpr int i = 5; constexpr CountableStruct someStruct{}; static_assert(dereferenceMaybe(&i) == 5, "int* dereferenced"); static_assert(dereferenceMaybe(i) == 5, "int not dereferenced"); static_assert(dereferenceMaybe(&someStruct).numberOfElements == 42, "CountableStruct* dereferenced"); static_assert(dereferenceMaybe(someStruct).numberOfElements == 42, "CountableStruct not dereferenced"); /*! * \brief The TraitsTest class tests parts of the Traits namespace which can not be evaluated at compile-time. */ class TraitsTest : public TestFixture { CPPUNIT_TEST_SUITE(TraitsTest); CPPUNIT_TEST(testDereferenceMaybe); CPPUNIT_TEST_SUITE_END(); public: void setUp() { } void tearDown() { } void testDereferenceMaybe(); }; CPPUNIT_TEST_SUITE_REGISTRATION(TraitsTest); /*! * \brief Tests whether a smart pointer to a string can be treated like a normal string through the use of dereferenceMaybe(). */ void TraitsTest::testDereferenceMaybe() { auto someString = "foo"s; auto someSmartPointer = make_unique("foo"); CPPUNIT_ASSERT_EQUAL("foo"s, dereferenceMaybe(someString)); CPPUNIT_ASSERT_EQUAL("foo"s, dereferenceMaybe(someSmartPointer)); CPPUNIT_ASSERT_EQUAL("foo"s, dereferenceMaybe(make_unique("foo"))); }