Debian Codegraph @

tests/traitstests.cpp

raw symbols SCIP
1 #include "../misc/traits.h"
2 #include "../tests/testutils.h"
3
4 #include <cppunit/TestFixture.h>
5 #include <cppunit/extensions/HelperMacros.h>
6
7 #include <forward_list>
8 #include <list>
9 #include <map>
10 #include <memory>
11 #include <string>
12 #include <vector>
13
14 using namespace std;
15 using namespace CppUtilities::Traits;
16
17 using namespace CPPUNIT_NS;
18
19 /// \cond
20 struct SomeStruct {
21 string foo;
22 int bar;
23 };
24
25 struct CountableStruct {
26 int numberOfElements = 42;
27 size_t size() const;
28 };
29
30 struct TestIncomplete;
31 /// \endcond
32
33 static_assert(!Bool<false>::value, "Bool<false>");
34 static_assert(Bool<true>::value, "Bool<true>");
35 static_assert(!Not<Bool<true>>::value, "Not");
36 static_assert(!Any<Bool<false>, Bool<false>>::value, "Any: negative case");
37 static_assert(Any<Bool<true>, Bool<false>>::value, "Any: positive case");
38 static_assert(!All<Bool<true>, Bool<false>>::value, "All: negative case");
39 static_assert(All<Bool<true>, Bool<true>>::value, "All: positive case");
40 static_assert(!None<Bool<true>, Bool<false>>::value, "None: negative case");
41 static_assert(!None<Bool<true>, Bool<true>>::value, "None: negative case");
42 static_assert(None<Bool<false>, Bool<false>>::value, "None: positive case");
43
44 static_assert(!IsSpecializationOf<string, basic_stringbuf>::value, "IsSpecializationOf: negative case");
45 static_assert(IsSpecializationOf<string, basic_string>::value, "IsSpecializationOf: positive case");
46 static_assert(IsSpecializationOf<string &, basic_string>::value, "IsSpecializationOf: positive case");
47 static_assert(IsSpecializationOf<const string &, basic_string>::value, "IsSpecializationOf: positive case");
48 static_assert(IsSpecializationOf<volatile string, basic_string>::value, "IsSpecializationOf: positive case");
49 static_assert(!IsSpecializingAnyOf<string, basic_stringbuf, vector>::value, "IsSpecializingAnyOf: negative case");
50 static_assert(!IsSpecializingAnyOf<string, basic_stringbuf, list, vector>::value, "IsSpecializingAnyOf: negative case");
51 static_assert(IsSpecializingAnyOf<string, basic_stringbuf, basic_string, vector>::value, "IsSpecializingAnyOf: positive case");
52 static_assert(IsSpecializingAnyOf<string, basic_stringbuf, vector, basic_string>::value, "IsSpecializingAnyOf: positive case");
53 static_assert(IsSpecializingAnyOf<string, basic_string>::value, "IsSpecializingAnyOf: positive case");
54
55 static_assert(IsAnyOf<string, string, int, bool>::value, "IsAnyOf: positive case");
56 static_assert(IsAnyOf<int, string, int, bool>::value, "IsAnyOf: positive case");
57 static_assert(IsAnyOf<bool, string, int, bool>::value, "IsAnyOf: positive case");
58 static_assert(!IsAnyOf<unsigned int, string, int, bool>::value, "IsAnyOf: negative case");
59 static_assert(!IsNoneOf<string, string, int, bool>::value, "IsNoneOf: negative case");
60 static_assert(!IsNoneOf<int, string, int, bool>::value, "IsNoneOf: negative case");
61 static_assert(!IsNoneOf<bool, string, int, bool>::value, "IsNoneOf: negative case");
62 static_assert(IsNoneOf<unsigned int, string, int, bool>::value, "IsNoneOf: positive case");
63
64 static_assert(!IsDereferencable<string>::value, "IsDereferencable: negative case");
65 static_assert(!IsDereferencable<int>::value, "IsDereferencable: negative case");
66 static_assert(IsDereferencable<string *>::value, "IsDereferencable: positive case");
67 static_assert(IsDereferencable<int *>::value, "IsDereferencable: positive case");
68 static_assert(IsDereferencable<unique_ptr<string>>::value, "IsDereferencable: positive case");
69 static_assert(IsDereferencable<shared_ptr<string>>::value, "IsDereferencable: positive case");
70 static_assert(!IsDereferencable<weak_ptr<string>>::value, "IsDereferencable: positive case");
71
72 static_assert(!IsIteratable<int>::value, "IsIterator: negative case");
73 static_assert(!IsIteratable<SomeStruct>::value, "IsIterator: negative case");
74 static_assert(IsIteratable<string>::value, "IsIterator: positive case");
75 static_assert(IsIteratable<vector<int>>::value, "IsIterator: positive case");
76 static_assert(IsIteratable<list<string>>::value, "IsIterator: positive case");
77 static_assert(IsIteratable<map<string, string>>::value, "IsIterator: positive case");
78 static_assert(IsIteratable<initializer_list<double>>::value, "IsIterator: positive case");
79 static_assert(!HasSize<SomeStruct>::value, "HasSize: negative case");
80 static_assert(!HasSize<forward_list<SomeStruct>>::value, "HasSize: negative case");
81 static_assert(HasSize<vector<SomeStruct>>::value, "HasSize: positive case");
82 static_assert(HasSize<string>::value, "HasSize: positive case");
83 static_assert(HasSize<CountableStruct>::value, "HasSize: positive case");
84 static_assert(!IsReservable<list<SomeStruct>>::value, "HasSize: negative case");
85 static_assert(IsReservable<vector<SomeStruct>>::value, "HasSize: positive case");
86 static_assert(HasOperatorBool<function<void(void)>>::value, "HasOperatorBool: positive case");
87 static_assert(!HasOperatorBool<SomeStruct>::value, "HasOperatorBool: negative case");
88
89 static_assert(!IsCString<string>::value, "IsCString: negative case");
90 static_assert(!IsCString<int[]>::value, "IsCString: negative case");
91 static_assert(!IsCString<int *>::value, "IsCString: negative case");
92 static_assert(IsCString<char[]>::value, "IsCString: positive case");
93 static_assert(IsCString<char *>::value, "IsCString: positive case");
94 static_assert(IsCString<const char *>::value, "IsCString: positive case");
95 static_assert(!IsString<int *>::value, "IsString: negative case");
96 static_assert(!IsString<stringstream>::value, "IsString: negative case");
97 static_assert(IsString<const char *>::value, "IsString: positive case");
98 static_assert(IsString<string>::value, "IsString: positive case");
99 static_assert(IsString<const string>::value, "IsString: positive case (const)");
100 static_assert(IsString<volatile string>::value, "IsString: positive case (volatile)");
101 static_assert(IsString<u16string>::value, "IsString: positive case");
102
103 static_assert(!IsComplete<TestIncomplete>::value, "IsComplete: negative case");
104 static_assert(IsComplete<CountableStruct>::value, "IsComplete: positive case");
105
106 constexpr int i = 5;
107 constexpr CountableStruct someStruct{};
108 static_assert(dereferenceMaybe(&i) == 5, "int* dereferenced");
109 static_assert(dereferenceMaybe(i) == 5, "int not dereferenced");
110 static_assert(dereferenceMaybe(&someStruct).numberOfElements == 42, "CountableStruct* dereferenced");
111 static_assert(dereferenceMaybe(someStruct).numberOfElements == 42, "CountableStruct not dereferenced");
112
113 /*!
114 * \brief The TraitsTest class tests parts of the Traits namespace which can not be evaluated at compile-time.
115 */
116 class TraitsTest : public TestFixture {
117 CPPUNIT_TEST_SUITE(TraitsTest);
118 CPPUNIT_TEST(testDereferenceMaybe);
119 CPPUNIT_TEST_SUITE_END();
120
121 public:
122 void setUp()
123 {
124 }
125 void tearDown()
126 {
127 }
128
129 void testDereferenceMaybe();
130 };
131
132 CPPUNIT_TEST_SUITE_REGISTRATION(TraitsTest);
133
134 /*!
135 * \brief Tests whether a smart pointer to a string can be treated like a normal string through the use of dereferenceMaybe().
136 */
137 void TraitsTest::testDereferenceMaybe()
138 {
139 auto someString = "foo"s;
140 auto someSmartPointer = make_unique<string>("foo");
141 CPPUNIT_ASSERT_EQUAL("foo"s, dereferenceMaybe(someString));
142 CPPUNIT_ASSERT_EQUAL("foo"s, dereferenceMaybe(someSmartPointer));
143 CPPUNIT_ASSERT_EQUAL("foo"s, dereferenceMaybe(make_unique<string>("foo")));
144 }