Dereference_test.cc
Go to the documentation of this file.
1 /**
2  * @file Dereference_test.cc
3  * @brief Test for Dereference.h utilities.
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date January 23rd, 2015
6  *
7  * A static test is performed to verify that the compilation succeeds with the
8  * types int, int*, unique_ptr<int> and a custom smart pointer to int.
9  */
10 
11 // static tests
12 #include <type_traits>
13 #include <memory>
14 
15 // Boost libraries
16 /*
17  * Boost Magic: define the name of the module;
18  * and do that before the inclusion of Boost unit test headers
19  * because it will change what they provide.
20  * Among the those, there is a main() function and some wrapping catching
21  * unhandled exceptions and considering them test failures, and probably more.
22  * This also makes fairly complicate to receive parameters from the command line
23  * (for example, a random seed).
24  */
25 #define BOOST_TEST_MODULE ( StatCollector_test )
26 #include "boost/test/unit_test.hpp"
27 
28 // library to be tested:
30 
31 
32 //------------------------------------------------------------------------------
33 // custom "smart pointer" type
34 template <typename T>
35 struct MyPtr {
36  using element_type = T;
37  MyPtr(T* p = nullptr): ptr(p) {}
38 
39  T& operator* () { return *ptr; }
40 
41  T* ptr;
42 }; // struct MyPtr<>
43 
44 
45 //******************************************************************************
46 //*** static (compilation-time) tests
47 //***
48 // test details::is_type<>
49 static_assert(lar::util::details::is_type<decltype(*(std::unique_ptr<int>()))>::value,
50  "is_type<*(unique_ptr<int>())> not working");
51 
52 
53 // test details::has_dereference_class
55  "details::has_dereference_class<int> is not working");
57  "details::has_dereference_class<int*> is not working");
59  "details::has_dereference_class<MyPtr<int>> is not working");
60 static_assert(lar::util::details::has_dereference_class<std::unique_ptr<int>>::value,
61  "details::has_dereference_class<unique_ptr<int>> is not working");
62 
63 
64 // test details::dereferenced_type; should always have type = int&
65 static_assert(std::is_same<
67  int
68  >::value,
69  "details::dereferenced_type<int> is not working"
70  );
71 static_assert(std::is_same<
73  int&
74  >::value,
75  "details::dereferenced_type<int*> is not working"
76  );
77 static_assert(std::is_same<
79  int&
80  >::value,
81  "details::dereferenced_type<MyPtr<int>> is not working"
82  );
83 static_assert(std::is_same<
84  typename lar::util::details::dereferenced_type<std::unique_ptr<int>, true>::type,
85  int&
86  >::value,
87  "details::dereferenced_type<unique_ptr<int>> is not working"
88  );
89 
90 
91 // test dereference_class pointer type; should always be int*
92 static_assert(std::is_same<
94  int&
95  >::value,
96  "details::dereference_class<int> not working"
97  );
98 static_assert(std::is_same<
100  int&
101  >::value,
102  "details::dereference_class<int*> not working"
103  );
104 static_assert(std::is_same<
105  lar::util::details::dereference_class<MyPtr<int>, true>::reference_type,
106  int&
107  >::value,
108  "details::dereference_class<MyPtr<int>> not working"
109  );
110 static_assert(std::is_same<
111  lar::util::details::dereference_class<std::unique_ptr<int>, true>::reference_type,
112  int&
113  >::value,
114  "details::dereference_class<unique_ptr<int>> not working"
115  );
116 
117 
118 // test dereference_class pointer type; should always be int*
119 static_assert(std::is_same<
121  int*
122  >::value,
123  "details::make_pointer_class<int> not working"
124  );
125 static_assert(std::is_same<
127  int*
128  >::value,
129  "details::make_pointer_class<int*> not working"
130  );
131 static_assert(std::is_same<
133  int*
134  >::value,
135  "details::make_pointer_class<MyPtr<int>> not working"
136  );
137 static_assert(std::is_same<
138  lar::util::details::make_pointer_class<std::unique_ptr<int>, true>::pointer_type,
139  int*
140  >::value,
141  "details::make_pointer_class<unique_ptr<int>> not working"
142  );
143 
144 
145 // test dereferenced_type type; should always be int
146 static_assert(std::is_same<
148  int
149  >::value,
150  "dereferenced_type<int> not working"
151  );
152 static_assert(std::is_same<
154  int&
155  >::value,
156  "dereferenced_type<int*> not working"
157  );
158 static_assert(std::is_same<
160  int&
161  >::value,
162  "dereferenced_type<MyPtr<int>> not working"
163  );
164 static_assert(std::is_same<
165  typename lar::util::dereferenced_type<std::unique_ptr<int>>::type,
166  int&
167  >::value,
168  "dereferenced_type<unique_ptr<int>> not working"
169  );
170 
171 
172 //******************************************************************************
173 //*** testing starts here;
174 //*** still mostly a compilation test
175 template <typename T>
176 void test() {
177 
178  T value = T(17);
179  T* cptr = &value;
180  MyPtr<T> my_ptr(&value);
181  auto uptr = std::make_unique<T>(value);
182 
183  T* ptr;
184  ptr = lar::util::make_pointer(uptr);
185  BOOST_TEST(*ptr == value);
186  ptr = lar::util::make_pointer(my_ptr);
187  BOOST_TEST(*ptr == value);
188  ptr = lar::util::make_pointer(cptr);
189  BOOST_TEST(*ptr == value);
190  ptr = lar::util::make_pointer(value);
191  BOOST_TEST(*ptr == value);
192 
193  BOOST_TEST(lar::util::dereference(uptr) == value);
194  BOOST_TEST(lar::util::dereference(my_ptr) == value);
195  BOOST_TEST(lar::util::dereference(cptr) == value);
196  BOOST_TEST(lar::util::dereference(value) == value);
197 
198 } // test<>()
199 
200 
201 //******************************************************************************
203  test<int>();
204 }
205 
206 BOOST_AUTO_TEST_CASE(TestConstInt) {
207  test<const int>();
208 }
Functor returning the dereferenced value of the argument.
Definition: Dereference.h:132
BOOST_AUTO_TEST_CASE(TestInt)
details::make_pointer_class< T, details::has_dereference_class< T >::value >::pointer_type make_pointer(T &v)
Returns a pointer to the value of argument, or the argument itself.
Definition: Dereference.h:293
typename std::add_lvalue_reference< typename dereferenced_type< T, CanDereference >::type >::type reference_type
Definition: Dereference.h:135
T & operator*()
Functor returning the pointer to a value in the argument.
Definition: Dereference.h:171
typename std::add_pointer< typename dereferenced_type< T, CanDereference >::type >::type pointer_type
Definition: Dereference.h:174
details::dereference_class< T, details::has_dereference_class< T >::value >::reference_type dereference(T &v)
Returns the value pointed by the argument, or the argument itself.
Definition: Dereference.h:254
p
Definition: test.py:223
Class defining the dereferenced type of the specified type.
Definition: Dereference.h:216
Class holding the type dereferenced from an object of type T.
Definition: Dereference.h:100
MyPtr(T *p=nullptr)
Class compiling only if type T exists (then, it&#39;s std::true_type)
Definition: Dereference.h:24
void test()
Class defining whether the specified type can be dereferenced.
Definition: Dereference.h:51