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