C++ Utilities 5.24.8
Useful C++ classes and routines such as argument parser, IO and conversion utilities
Loading...
Searching...
No Matches
traitstests.cpp
Go to the documentation of this file.
1#include "../misc/traits.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
14using namespace std;
15using namespace CppUtilities::Traits;
16
17using namespace CPPUNIT_NS;
18
20struct SomeStruct {
21 string foo;
22 int bar;
23};
24
25struct CountableStruct {
26 int numberOfElements = 42;
27 size_t size() const;
28};
29
30struct TestIncomplete;
32
33static_assert(!Bool<false>::value, "Bool<false>");
34static_assert(Bool<true>::value, "Bool<true>");
35static_assert(!Not<Bool<true>>::value, "Not");
36static_assert(!Any<Bool<false>, Bool<false>>::value, "Any: negative case");
37static_assert(Any<Bool<true>, Bool<false>>::value, "Any: positive case");
38static_assert(!All<Bool<true>, Bool<false>>::value, "All: negative case");
39static_assert(All<Bool<true>, Bool<true>>::value, "All: positive case");
40static_assert(!None<Bool<true>, Bool<false>>::value, "None: negative case");
41static_assert(!None<Bool<true>, Bool<true>>::value, "None: negative case");
42static_assert(None<Bool<false>, Bool<false>>::value, "None: positive case");
43
44static_assert(!IsSpecializationOf<string, basic_stringbuf>::value, "IsSpecializationOf: negative case");
45static_assert(IsSpecializationOf<string, basic_string>::value, "IsSpecializationOf: positive case");
46static_assert(IsSpecializationOf<string &, basic_string>::value, "IsSpecializationOf: positive case");
47static_assert(IsSpecializationOf<const string &, basic_string>::value, "IsSpecializationOf: positive case");
48static_assert(IsSpecializationOf<volatile string, basic_string>::value, "IsSpecializationOf: positive case");
49static_assert(!IsSpecializingAnyOf<string, basic_stringbuf, vector>::value, "IsSpecializingAnyOf: negative case");
50static_assert(!IsSpecializingAnyOf<string, basic_stringbuf, list, vector>::value, "IsSpecializingAnyOf: negative case");
51static_assert(IsSpecializingAnyOf<string, basic_stringbuf, basic_string, vector>::value, "IsSpecializingAnyOf: positive case");
52static_assert(IsSpecializingAnyOf<string, basic_stringbuf, vector, basic_string>::value, "IsSpecializingAnyOf: positive case");
53static_assert(IsSpecializingAnyOf<string, basic_string>::value, "IsSpecializingAnyOf: positive case");
54
55static_assert(IsAnyOf<string, string, int, bool>::value, "IsAnyOf: positive case");
56static_assert(IsAnyOf<int, string, int, bool>::value, "IsAnyOf: positive case");
57static_assert(IsAnyOf<bool, string, int, bool>::value, "IsAnyOf: positive case");
58static_assert(!IsAnyOf<unsigned int, string, int, bool>::value, "IsAnyOf: negative case");
59static_assert(!IsNoneOf<string, string, int, bool>::value, "IsNoneOf: negative case");
60static_assert(!IsNoneOf<int, string, int, bool>::value, "IsNoneOf: negative case");
61static_assert(!IsNoneOf<bool, string, int, bool>::value, "IsNoneOf: negative case");
62static_assert(IsNoneOf<unsigned int, string, int, bool>::value, "IsNoneOf: positive case");
63
64static_assert(!IsDereferencable<string>::value, "IsDereferencable: negative case");
65static_assert(!IsDereferencable<int>::value, "IsDereferencable: negative case");
66static_assert(IsDereferencable<string *>::value, "IsDereferencable: positive case");
67static_assert(IsDereferencable<int *>::value, "IsDereferencable: positive case");
68static_assert(IsDereferencable<unique_ptr<string>>::value, "IsDereferencable: positive case");
69static_assert(IsDereferencable<shared_ptr<string>>::value, "IsDereferencable: positive case");
70static_assert(!IsDereferencable<weak_ptr<string>>::value, "IsDereferencable: positive case");
71
72static_assert(!IsIteratable<int>::value, "IsIterator: negative case");
73static_assert(!IsIteratable<SomeStruct>::value, "IsIterator: negative case");
74static_assert(IsIteratable<string>::value, "IsIterator: positive case");
75static_assert(IsIteratable<vector<int>>::value, "IsIterator: positive case");
76static_assert(IsIteratable<list<string>>::value, "IsIterator: positive case");
77static_assert(IsIteratable<map<string, string>>::value, "IsIterator: positive case");
78static_assert(IsIteratable<initializer_list<double>>::value, "IsIterator: positive case");
79static_assert(!HasSize<SomeStruct>::value, "HasSize: negative case");
80static_assert(!HasSize<forward_list<SomeStruct>>::value, "HasSize: negative case");
81static_assert(HasSize<vector<SomeStruct>>::value, "HasSize: positive case");
82static_assert(HasSize<string>::value, "HasSize: positive case");
83static_assert(HasSize<CountableStruct>::value, "HasSize: positive case");
84static_assert(!IsReservable<list<SomeStruct>>::value, "HasSize: negative case");
85static_assert(IsReservable<vector<SomeStruct>>::value, "HasSize: positive case");
86static_assert(HasOperatorBool<function<void(void)>>::value, "HasOperatorBool: positive case");
87static_assert(!HasOperatorBool<SomeStruct>::value, "HasOperatorBool: negative case");
88
89static_assert(!IsCString<string>::value, "IsCString: negative case");
90static_assert(!IsCString<int[]>::value, "IsCString: negative case");
91static_assert(!IsCString<int *>::value, "IsCString: negative case");
92static_assert(IsCString<char[]>::value, "IsCString: positive case");
93static_assert(IsCString<char *>::value, "IsCString: positive case");
94static_assert(IsCString<const char *>::value, "IsCString: positive case");
95static_assert(!IsString<int *>::value, "IsString: negative case");
96static_assert(!IsString<stringstream>::value, "IsString: negative case");
97static_assert(IsString<const char *>::value, "IsString: positive case");
98static_assert(IsString<string>::value, "IsString: positive case");
99static_assert(IsString<const string>::value, "IsString: positive case (const)");
100static_assert(IsString<volatile string>::value, "IsString: positive case (volatile)");
101static_assert(IsString<u16string>::value, "IsString: positive case");
102
103static_assert(!IsComplete<TestIncomplete>::value, "IsComplete: negative case");
104static_assert(IsComplete<CountableStruct>::value, "IsComplete: positive case");
105
106constexpr int i = 5;
107constexpr CountableStruct someStruct{};
108static_assert(dereferenceMaybe(&i) == 5, "int* dereferenced");
109static_assert(dereferenceMaybe(i) == 5, "int not dereferenced");
110static_assert(dereferenceMaybe(&someStruct).numberOfElements == 42, "CountableStruct* dereferenced");
111static_assert(dereferenceMaybe(someStruct).numberOfElements == 42, "CountableStruct not dereferenced");
112
116class TraitsTest : public TestFixture {
117 CPPUNIT_TEST_SUITE(TraitsTest);
118 CPPUNIT_TEST(testDereferenceMaybe);
119 CPPUNIT_TEST_SUITE_END();
120
121public:
122 void setUp()
123 {
124 }
125 void tearDown()
126 {
127 }
128
130};
131
133
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}
The TraitsTest class tests parts of the Traits namespace which can not be evaluated at compile-time.
void testDereferenceMaybe()
Tests whether a smart pointer to a string can be treated like a normal string through the use of dere...
void tearDown()
Contains traits for conveniently exploiting SFINAE.
Definition traits.h:11
constexpr auto & dereferenceMaybe(T &&value)
Dereferences the specified value if possible; otherwise just returns value itself.
Definition traits.h:143
STL namespace.
Evaluates to Bool<true> if all specified conditions are true; otherwise evaluates to Bool<false>.
Definition traits.h:34
Evaluates to Bool<true> if at least one of the specified conditions is true; otherwise evaluates to B...
Definition traits.h:29
Wraps a static boolean constant.
Definition traits.h:23
Evaluates to Bool<true> if the specified type is any of the specified types; otherwise evaluates to B...
Definition traits.h:71
Evaluates to Bool<true> if the specified type is a C-string (char * or const char *); otherwise evalu...
Definition traits.h:84
Evaluates to Bool<true> if the specified type is complete; if the type is only forward-declared it ev...
Definition traits.h:92
Evaluates to Bool<true> if the specified type is none of the specified types; otherwise evaluates to ...
Definition traits.h:76
Evaluates to Bool<true> if the specified type is based on the specified template; otherwise evaluates...
Definition traits.h:62
Evaluates to Bool<true> if the specified type is based on one of the specified templates; otherwise e...
Definition traits.h:64
Evaluates to Bool<true> if the specified type is a standard string, standard string view or C-string ...
Definition traits.h:89
Evaluates to Bool<true> if none of the specified conditions are true; otherwise evaluates to Bool<fal...
Definition traits.h:39
CPPUNIT_TEST_SUITE_REGISTRATION(TraitsTest)
constexpr int i
constexpr CountableStruct someStruct