proxygen
PropagateConstTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #include <memory>
20 
22 
23 using namespace folly;
24 
26 
27 // force complete template instantiations
28 template class folly::propagate_const<int*>;
31 
32 template <typename T>
33 static bool is_const(T&&) {
34  return std::is_const<_t<std::remove_reference<T>>>::value;
35 }
36 
37 template <typename T>
39 
40 TEST_F(PropagateConstTest, construct_assign) {
41  struct Source {
42  int& operator*();
43  int* get();
44  };
45  struct Implicit {
46  int& operator*();
47  int* get();
48  /* implicit */ Implicit(Source) {}
49  };
50  struct Explicit {
51  int& operator*();
52  int* get();
53  explicit Explicit(Source) {}
54  };
55 
56  EXPECT_TRUE((std::is_constructible<pc<Implicit>, Source>::value));
57  EXPECT_TRUE((std::is_constructible<pc<Explicit>, Source>::value));
58  EXPECT_TRUE((std::is_convertible<Source, pc<Implicit>>::value));
59  EXPECT_FALSE((std::is_convertible<Source, pc<Explicit>>::value));
60  EXPECT_TRUE((std::is_assignable<pc<Implicit>, Source>::value));
61  EXPECT_FALSE((std::is_assignable<pc<Explicit>, Source>::value));
62 
63  EXPECT_TRUE((std::is_constructible<pc<Implicit>, pc<Source>>::value));
64  EXPECT_TRUE((std::is_constructible<pc<Explicit>, pc<Source>>::value));
65  EXPECT_TRUE((std::is_convertible<pc<Source>, pc<Implicit>>::value));
66  EXPECT_FALSE((std::is_convertible<pc<Source>, pc<Explicit>>::value));
67  EXPECT_TRUE((std::is_assignable<pc<Implicit>, pc<Source>>::value));
68  EXPECT_FALSE((std::is_assignable<pc<Explicit>, pc<Source>>::value));
69 }
70 
71 TEST_F(PropagateConstTest, op_assign_move) {
72  auto ptr = pc<std::unique_ptr<int>>{std::make_unique<int>(1)};
73  EXPECT_EQ(*ptr, 1);
74 
75  ptr = std::make_unique<int>(2);
76  EXPECT_EQ(*ptr, 2);
77 }
78 
80  int data[1] = {3};
81  auto a = data + 0;
82  auto pc_a = pc<int*>(a);
83 
84  EXPECT_EQ(a, pc_a.get());
85  EXPECT_EQ(a, as_const(pc_a).get());
86  EXPECT_FALSE(is_const(*pc_a.get()));
87  EXPECT_TRUE(is_const(*as_const(pc_a).get()));
88 }
89 
90 TEST_F(PropagateConstTest, op_indirect) {
91  int data[1] = {3};
92  auto a = data + 0;
93  auto pc_a = pc<int*>(a);
94 
95  EXPECT_EQ(a, &*pc_a);
96  EXPECT_EQ(a, &*as_const(pc_a));
97  EXPECT_FALSE(is_const(*pc_a));
98  EXPECT_TRUE(is_const(*as_const(pc_a)));
99 }
100 
101 TEST_F(PropagateConstTest, op_element_type_ptr) {
102  int data[1] = {3};
103  auto a = data + 0;
104  auto pc_a = pc<int*>(a);
105 
106  EXPECT_EQ(a, static_cast<int*>(pc_a));
107  EXPECT_EQ(a, static_cast<int const*>(as_const(pc_a)));
108 }
109 
111  int data[1] = {3};
112  auto a = data + 0;
113  auto pc_a = pc<int*>(a);
114  auto pc_0 = pc<int*>(nullptr);
115 
116  EXPECT_TRUE(pc_a);
117  EXPECT_FALSE(pc_0);
118 }
119 
121  int data[1] = {3};
122  auto a = data + 0;
123  auto pc_a = pc<int*>(a);
124 
125  EXPECT_EQ(a, get_underlying(pc_a));
130 }
131 
133  int data[2] = {3, 4};
134  auto a = data + 0;
135  auto b = data + 1;
136  auto pc_a = pc<int*>(a);
137  auto pc_b = pc<int*>(b);
138 
139  swap(pc_a, pc_b);
140  EXPECT_EQ(3, *pc_b);
141  EXPECT_EQ(4, *pc_a);
142 
143  swap(pc_a, pc_b);
144  EXPECT_EQ(3, *pc_a);
145  EXPECT_EQ(4, *pc_b);
146 }
147 
148 TEST_F(PropagateConstTest, op_equal_to) {
149  int data[2] = {3, 4};
150  auto a = data + 0;
151  auto b = data + 1;
152  auto pc_a = pc<int*>(a);
153  auto pc_b = pc<int*>(b);
154 
155  auto _ = [](auto&& x, auto&& y) { return x == y; };
156  EXPECT_TRUE(_(pc_a, pc_a));
157  EXPECT_FALSE(_(pc_a, pc_b));
158  EXPECT_FALSE(_(pc_a, nullptr));
159  EXPECT_TRUE(_(pc_a, a));
160  EXPECT_FALSE(_(pc_a, b));
161  EXPECT_TRUE(_(a, pc_a));
162  EXPECT_FALSE(_(b, pc_a));
163 }
164 
165 TEST_F(PropagateConstTest, op_not_equal_to) {
166  int data[2] = {3, 4};
167  auto a = data + 0;
168  auto b = data + 1;
169  auto pc_a = pc<int*>(a);
170  auto pc_b = pc<int*>(b);
171 
172  auto _ = [](auto&& x, auto&& y) { return x != y; };
173  EXPECT_FALSE(_(pc_a, pc_a));
174  EXPECT_TRUE(_(pc_a, pc_b));
175  EXPECT_TRUE(_(pc_a, nullptr));
176  EXPECT_FALSE(_(pc_a, a));
177  EXPECT_TRUE(_(pc_a, b));
178  EXPECT_FALSE(_(a, pc_a));
179  EXPECT_TRUE(_(b, pc_a));
180 }
181 
183  int data[2] = {3, 4};
184  auto a = data + 0;
185  auto b = data + 1;
186  auto pc_a = pc<int*>(a);
187  auto pc_b = pc<int*>(b);
188 
189  auto _ = [](auto&& x, auto&& y) { return x < y; };
190  EXPECT_FALSE(_(pc_a, pc_a));
191  EXPECT_FALSE(_(pc_a, a));
192  EXPECT_FALSE(_(a, pc_a));
193  EXPECT_TRUE(_(pc_a, pc_b));
194  EXPECT_TRUE(_(pc_a, b));
195  EXPECT_TRUE(_(a, pc_b));
196  EXPECT_FALSE(_(pc_b, pc_a));
197  EXPECT_FALSE(_(pc_b, a));
198  EXPECT_FALSE(_(b, pc_a));
199  EXPECT_FALSE(_(pc_b, pc_b));
200  EXPECT_FALSE(_(pc_b, b));
201  EXPECT_FALSE(_(b, pc_b));
202 }
203 
205  int data[2] = {3, 4};
206  auto a = data + 0;
207  auto b = data + 1;
208  auto pc_a = pc<int*>(a);
209  auto pc_b = pc<int*>(b);
210 
211  auto _ = [](auto&& x, auto&& y) { return x > y; };
212  EXPECT_FALSE(_(pc_a, pc_a));
213  EXPECT_FALSE(_(pc_a, a));
214  EXPECT_FALSE(_(a, pc_a));
215  EXPECT_FALSE(_(pc_a, pc_b));
216  EXPECT_FALSE(_(pc_a, b));
217  EXPECT_FALSE(_(a, pc_b));
218  EXPECT_TRUE(_(pc_b, pc_a));
219  EXPECT_TRUE(_(pc_b, a));
220  EXPECT_TRUE(_(b, pc_a));
221  EXPECT_FALSE(_(pc_b, pc_b));
222  EXPECT_FALSE(_(pc_b, b));
223  EXPECT_FALSE(_(b, pc_b));
224 }
225 
226 TEST_F(PropagateConstTest, op_less_equal) {
227  int data[2] = {3, 4};
228  auto a = data + 0;
229  auto b = data + 1;
230  auto pc_a = pc<int*>(a);
231  auto pc_b = pc<int*>(b);
232 
233  auto _ = [](auto&& x, auto&& y) { return x <= y; };
234  EXPECT_TRUE(_(pc_a, pc_a));
235  EXPECT_TRUE(_(pc_a, a));
236  EXPECT_TRUE(_(a, pc_a));
237  EXPECT_TRUE(_(pc_a, pc_b));
238  EXPECT_TRUE(_(pc_a, b));
239  EXPECT_TRUE(_(a, pc_b));
240  EXPECT_FALSE(_(pc_b, pc_a));
241  EXPECT_FALSE(_(pc_b, a));
242  EXPECT_FALSE(_(b, pc_a));
243  EXPECT_TRUE(_(pc_b, pc_b));
244  EXPECT_TRUE(_(pc_b, b));
245  EXPECT_TRUE(_(b, pc_b));
246 }
247 
248 TEST_F(PropagateConstTest, op_greater_equal) {
249  int data[2] = {3, 4};
250  auto a = data + 0;
251  auto b = data + 1;
252  auto pc_a = pc<int*>(a);
253  auto pc_b = pc<int*>(b);
254 
255  auto _ = [](auto&& x, auto&& y) { return x >= y; };
256  EXPECT_TRUE(_(pc_a, pc_a));
257  EXPECT_TRUE(_(pc_a, a));
258  EXPECT_TRUE(_(a, pc_a));
259  EXPECT_FALSE(_(pc_a, pc_b));
260  EXPECT_FALSE(_(pc_a, b));
261  EXPECT_FALSE(_(a, pc_b));
262  EXPECT_TRUE(_(pc_b, pc_a));
263  EXPECT_TRUE(_(pc_b, a));
264  EXPECT_TRUE(_(b, pc_a));
265  EXPECT_TRUE(_(pc_b, pc_b));
266  EXPECT_TRUE(_(pc_b, b));
267  EXPECT_TRUE(_(b, pc_b));
268 }
269 
271  int data[1] = {3};
272  auto a = data + 0;
273  auto pc_a = pc<int*>(a);
274 
275  EXPECT_EQ(std::hash<int*>()(a), std::hash<pc<int*>>()(pc_a));
276 }
277 
279  int data[2] = {3, 4};
280  auto a = data + 0;
281  auto b = data + 1;
282  auto pc_a = pc<int*>(a);
283  auto pc_b = pc<int*>(b);
284 
285  auto _ = std::equal_to<pc<int*>>{};
286  EXPECT_TRUE(_(pc_a, pc_a));
287  EXPECT_FALSE(_(pc_a, pc_b));
288 }
289 
290 TEST_F(PropagateConstTest, not_equal_to) {
291  int data[2] = {3, 4};
292  auto a = data + 0;
293  auto b = data + 1;
294  auto pc_a = pc<int*>(a);
295  auto pc_b = pc<int*>(b);
296 
297  auto _ = std::not_equal_to<pc<int*>>{};
298  EXPECT_FALSE(_(pc_a, pc_a));
299  EXPECT_TRUE(_(pc_a, pc_b));
300 }
301 
303  int data[2] = {3, 4};
304  auto a = data + 0;
305  auto b = data + 1;
306  auto pc_a = pc<int*>(a);
307  auto pc_b = pc<int*>(b);
308 
309  auto _ = std::less<pc<int*>>{};
310  EXPECT_FALSE(_(pc_a, pc_a));
311  EXPECT_TRUE(_(pc_a, pc_b));
312  EXPECT_FALSE(_(pc_b, pc_a));
313  EXPECT_FALSE(_(pc_b, pc_b));
314 }
315 
317  int data[2] = {3, 4};
318  auto a = data + 0;
319  auto b = data + 1;
320  auto pc_a = pc<int*>(a);
321  auto pc_b = pc<int*>(b);
322 
323  auto _ = std::greater<pc<int*>>{};
324  EXPECT_FALSE(_(pc_a, pc_a));
325  EXPECT_FALSE(_(pc_a, pc_b));
326  EXPECT_TRUE(_(pc_b, pc_a));
327  EXPECT_FALSE(_(pc_b, pc_b));
328 }
329 
331  int data[2] = {3, 4};
332  auto a = data + 0;
333  auto b = data + 1;
334  auto pc_a = pc<int*>(a);
335  auto pc_b = pc<int*>(b);
336 
337  auto _ = std::less_equal<pc<int*>>{};
338  EXPECT_TRUE(_(pc_a, pc_a));
339  EXPECT_TRUE(_(pc_a, pc_b));
340  EXPECT_FALSE(_(pc_b, pc_a));
341  EXPECT_TRUE(_(pc_b, pc_b));
342 }
343 
344 TEST_F(PropagateConstTest, greater_equal) {
345  int data[2] = {3, 4};
346  auto a = data + 0;
347  auto b = data + 1;
348  auto pc_a = pc<int*>(a);
349  auto pc_b = pc<int*>(b);
350 
351  auto _ = std::greater_equal<pc<int*>>{};
352  EXPECT_TRUE(_(pc_a, pc_a));
353  EXPECT_FALSE(_(pc_a, pc_b));
354  EXPECT_TRUE(_(pc_b, pc_a));
355  EXPECT_TRUE(_(pc_b, pc_b));
356 }
Definition: InvokeTest.cpp:58
void * ptr
char b
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const int x
folly::std T
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
constexpr T const & as_const(T &t) noexcept
Definition: Utility.h:96
constexpr auto data(C &c) -> decltype(c.data())
Definition: Access.h:71
char a
TEST_F(AsyncSSLSocketWriteTest, write_coalescing1)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
void swap(exception_wrapper &a, exception_wrapper &b) noexcept
constexpr Pointer & get_underlying(propagate_const< Pointer > &obj)
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
Definition: InvokeTest.cpp:65
const internal::AnythingMatcher _
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
static bool is_const(T &&)