Debian Codegraph @

Utilities/cmcppdap/src/traits_test.cpp

raw
1 // Copyright 2021 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "dap/traits.h"
16
17 #include "gmock/gmock.h"
18 #include "gtest/gtest.h"
19
20 namespace dap {
21 namespace traits {
22
23 namespace {
24 struct S {};
25 struct E : S {};
26 void F1(S) {}
27 void F3(int, S, float) {}
28 void E1(E) {}
29 void E3(int, E, float) {}
30 } // namespace
31
32 TEST(ParameterType, Function) {
33 F1({}); // Avoid unused method warning
34 F3(0, {}, 0); // Avoid unused method warning
35 static_assert(std::is_same<ParameterType<decltype(&F1), 0>, S>::value, "");
36 static_assert(std::is_same<ParameterType<decltype(&F3), 0>, int>::value, "");
37 static_assert(std::is_same<ParameterType<decltype(&F3), 1>, S>::value, "");
38 static_assert(std::is_same<ParameterType<decltype(&F3), 2>, float>::value,
39 "");
40 }
41
42 TEST(ParameterType, Method) {
43 class C {
44 public:
45 void F1(S) {}
46 void F3(int, S, float) {}
47 };
48 C().F1({}); // Avoid unused method warning
49 C().F3(0, {}, 0); // Avoid unused method warning
50 static_assert(std::is_same<ParameterType<decltype(&C::F1), 0>, S>::value, "");
51 static_assert(std::is_same<ParameterType<decltype(&C::F3), 0>, int>::value,
52 "");
53 static_assert(std::is_same<ParameterType<decltype(&C::F3), 1>, S>::value, "");
54 static_assert(std::is_same<ParameterType<decltype(&C::F3), 2>, float>::value,
55 "");
56 }
57
58 TEST(ParameterType, ConstMethod) {
59 class C {
60 public:
61 void F1(S) const {}
62 void F3(int, S, float) const {}
63 };
64 C().F1({}); // Avoid unused method warning
65 C().F3(0, {}, 0); // Avoid unused method warning
66 static_assert(std::is_same<ParameterType<decltype(&C::F1), 0>, S>::value, "");
67 static_assert(std::is_same<ParameterType<decltype(&C::F3), 0>, int>::value,
68 "");
69 static_assert(std::is_same<ParameterType<decltype(&C::F3), 1>, S>::value, "");
70 static_assert(std::is_same<ParameterType<decltype(&C::F3), 2>, float>::value,
71 "");
72 }
73
74 TEST(ParameterType, StaticMethod) {
75 class C {
76 public:
77 static void F1(S) {}
78 static void F3(int, S, float) {}
79 };
80 C::F1({}); // Avoid unused method warning
81 C::F3(0, {}, 0); // Avoid unused method warning
82 static_assert(std::is_same<ParameterType<decltype(&C::F1), 0>, S>::value, "");
83 static_assert(std::is_same<ParameterType<decltype(&C::F3), 0>, int>::value,
84 "");
85 static_assert(std::is_same<ParameterType<decltype(&C::F3), 1>, S>::value, "");
86 static_assert(std::is_same<ParameterType<decltype(&C::F3), 2>, float>::value,
87 "");
88 }
89
90 TEST(ParameterType, FunctionLike) {
91 using F1 = std::function<void(S)>;
92 using F3 = std::function<void(int, S, float)>;
93 static_assert(std::is_same<ParameterType<F1, 0>, S>::value, "");
94 static_assert(std::is_same<ParameterType<F3, 0>, int>::value, "");
95 static_assert(std::is_same<ParameterType<F3, 1>, S>::value, "");
96 static_assert(std::is_same<ParameterType<F3, 2>, float>::value, "");
97 }
98
99 TEST(ParameterType, Lambda) {
100 auto l1 = [](S) {};
101 auto l3 = [](int, S, float) {};
102 static_assert(std::is_same<ParameterType<decltype(l1), 0>, S>::value, "");
103 static_assert(std::is_same<ParameterType<decltype(l3), 0>, int>::value, "");
104 static_assert(std::is_same<ParameterType<decltype(l3), 1>, S>::value, "");
105 static_assert(std::is_same<ParameterType<decltype(l3), 2>, float>::value, "");
106 }
107
108 TEST(HasSignature, Function) {
109 F1({}); // Avoid unused method warning
110 F3(0, {}, 0); // Avoid unused method warning
111 static_assert(HasSignature<decltype(&F1), decltype(&F1)>::value, "");
112 static_assert(HasSignature<decltype(&F3), decltype(&F3)>::value, "");
113 static_assert(HasSignature<decltype(&F3), decltype(&F3)>::value, "");
114 static_assert(HasSignature<decltype(&F3), decltype(&F3)>::value, "");
115 static_assert(!HasSignature<decltype(&F1), decltype(&F3)>::value, "");
116 static_assert(!HasSignature<decltype(&F3), decltype(&F1)>::value, "");
117 static_assert(!HasSignature<decltype(&F3), decltype(&F1)>::value, "");
118 static_assert(!HasSignature<decltype(&F3), decltype(&F1)>::value, "");
119 }
120
121 TEST(HasSignature, Method) {
122 class C {
123 public:
124 void F1(S) {}
125 void F3(int, S, float) {}
126 };
127 C().F1({}); // Avoid unused method warning
128 C().F3(0, {}, 0); // Avoid unused method warning
129
130 static_assert(HasSignature<decltype(&C::F1), decltype(&F1)>::value, "");
131 static_assert(HasSignature<decltype(&C::F3), decltype(&F3)>::value, "");
132 static_assert(HasSignature<decltype(&C::F3), decltype(&F3)>::value, "");
133 static_assert(HasSignature<decltype(&C::F3), decltype(&F3)>::value, "");
134 static_assert(!HasSignature<decltype(&C::F1), decltype(&F3)>::value, "");
135 static_assert(!HasSignature<decltype(&C::F3), decltype(&F1)>::value, "");
136 static_assert(!HasSignature<decltype(&C::F3), decltype(&F1)>::value, "");
137 static_assert(!HasSignature<decltype(&C::F3), decltype(&F1)>::value, "");
138 }
139
140 TEST(HasSignature, ConstMethod) {
141 class C {
142 public:
143 void F1(S) const {}
144 void F3(int, S, float) const {}
145 };
146 C().F1({}); // Avoid unused method warning
147 C().F3(0, {}, 0); // Avoid unused method warning
148
149 static_assert(HasSignature<decltype(&C::F1), decltype(&F1)>::value, "");
150 static_assert(HasSignature<decltype(&C::F3), decltype(&F3)>::value, "");
151 static_assert(HasSignature<decltype(&C::F3), decltype(&F3)>::value, "");
152 static_assert(HasSignature<decltype(&C::F3), decltype(&F3)>::value, "");
153 static_assert(!HasSignature<decltype(&C::F1), decltype(&F3)>::value, "");
154 static_assert(!HasSignature<decltype(&C::F3), decltype(&F1)>::value, "");
155 static_assert(!HasSignature<decltype(&C::F3), decltype(&F1)>::value, "");
156 static_assert(!HasSignature<decltype(&C::F3), decltype(&F1)>::value, "");
157 }
158
159 TEST(HasSignature, StaticMethod) {
160 class C {
161 public:
162 static void F1(S) {}
163 static void F3(int, S, float) {}
164 };
165 C::F1({}); // Avoid unused method warning
166 C::F3(0, {}, 0); // Avoid unused method warning
167
168 static_assert(HasSignature<decltype(&C::F1), decltype(&F1)>::value, "");
169 static_assert(HasSignature<decltype(&C::F3), decltype(&F3)>::value, "");
170 static_assert(HasSignature<decltype(&C::F3), decltype(&F3)>::value, "");
171 static_assert(HasSignature<decltype(&C::F3), decltype(&F3)>::value, "");
172 static_assert(!HasSignature<decltype(&C::F1), decltype(&F3)>::value, "");
173 static_assert(!HasSignature<decltype(&C::F3), decltype(&F1)>::value, "");
174 static_assert(!HasSignature<decltype(&C::F3), decltype(&F1)>::value, "");
175 static_assert(!HasSignature<decltype(&C::F3), decltype(&F1)>::value, "");
176 }
177
178 TEST(HasSignature, FunctionLike) {
179 using f1 = std::function<void(S)>;
180 using f3 = std::function<void(int, S, float)>;
181 static_assert(HasSignature<f1, decltype(&F1)>::value, "");
182 static_assert(HasSignature<f3, decltype(&F3)>::value, "");
183 static_assert(HasSignature<f3, decltype(&F3)>::value, "");
184 static_assert(HasSignature<f3, decltype(&F3)>::value, "");
185 static_assert(!HasSignature<f1, decltype(&F3)>::value, "");
186 static_assert(!HasSignature<f3, decltype(&F1)>::value, "");
187 static_assert(!HasSignature<f3, decltype(&F1)>::value, "");
188 static_assert(!HasSignature<f3, decltype(&F1)>::value, "");
189 }
190
191 TEST(HasSignature, Lambda) {
192 auto l1 = [](S) {};
193 auto l3 = [](int, S, float) {};
194 static_assert(HasSignature<decltype(l1), decltype(&F1)>::value, "");
195 static_assert(HasSignature<decltype(l3), decltype(&F3)>::value, "");
196 static_assert(HasSignature<decltype(l3), decltype(&F3)>::value, "");
197 static_assert(HasSignature<decltype(l3), decltype(&F3)>::value, "");
198 static_assert(!HasSignature<decltype(l1), decltype(&F3)>::value, "");
199 static_assert(!HasSignature<decltype(l3), decltype(&F1)>::value, "");
200 static_assert(!HasSignature<decltype(l3), decltype(&F1)>::value, "");
201 static_assert(!HasSignature<decltype(l3), decltype(&F1)>::value, "");
202 }
203
204 ////
205
206 TEST(CompatibleWith, Function) {
207 F1({}); // Avoid unused method warning
208 F3(0, {}, 0); // Avoid unused method warning
209 E1({}); // Avoid unused method warning
210 E3(0, {}, 0); // Avoid unused method warning
211 static_assert(CompatibleWith<decltype(&F1), decltype(&F1)>::value, "");
212 static_assert(CompatibleWith<decltype(&F3), decltype(&F3)>::value, "");
213 static_assert(CompatibleWith<decltype(&F3), decltype(&F3)>::value, "");
214 static_assert(CompatibleWith<decltype(&F3), decltype(&F3)>::value, "");
215
216 static_assert(!CompatibleWith<decltype(&F1), decltype(&F3)>::value, "");
217 static_assert(!CompatibleWith<decltype(&F3), decltype(&F1)>::value, "");
218 static_assert(!CompatibleWith<decltype(&F3), decltype(&F1)>::value, "");
219 static_assert(!CompatibleWith<decltype(&F3), decltype(&F1)>::value, "");
220
221 static_assert(CompatibleWith<decltype(&E1), decltype(&F1)>::value, "");
222 static_assert(CompatibleWith<decltype(&E3), decltype(&F3)>::value, "");
223 static_assert(CompatibleWith<decltype(&E3), decltype(&F3)>::value, "");
224 static_assert(CompatibleWith<decltype(&E3), decltype(&F3)>::value, "");
225
226 static_assert(!CompatibleWith<decltype(&F1), decltype(&E1)>::value, "");
227 static_assert(!CompatibleWith<decltype(&F3), decltype(&E3)>::value, "");
228 static_assert(!CompatibleWith<decltype(&F3), decltype(&E3)>::value, "");
229 static_assert(!CompatibleWith<decltype(&F3), decltype(&E3)>::value, "");
230 }
231
232 TEST(CompatibleWith, Method) {
233 class C {
234 public:
235 void F1(S) {}
236 void F3(int, S, float) {}
237 void E1(E) {}
238 void E3(int, E, float) {}
239 };
240 C().F1({}); // Avoid unused method warning
241 C().F3(0, {}, 0); // Avoid unused method warning
242 C().E1({}); // Avoid unused method warning
243 C().E3(0, {}, 0); // Avoid unused method warning
244
245 static_assert(CompatibleWith<decltype(&C::F1), decltype(&F1)>::value, "");
246 static_assert(CompatibleWith<decltype(&C::F3), decltype(&F3)>::value, "");
247 static_assert(CompatibleWith<decltype(&C::F3), decltype(&F3)>::value, "");
248 static_assert(CompatibleWith<decltype(&C::F3), decltype(&F3)>::value, "");
249
250 static_assert(!CompatibleWith<decltype(&C::F1), decltype(&F3)>::value, "");
251 static_assert(!CompatibleWith<decltype(&C::F3), decltype(&F1)>::value, "");
252 static_assert(!CompatibleWith<decltype(&C::F3), decltype(&F1)>::value, "");
253 static_assert(!CompatibleWith<decltype(&C::F3), decltype(&F1)>::value, "");
254
255 static_assert(CompatibleWith<decltype(&C::E1), decltype(&C::F1)>::value, "");
256 static_assert(CompatibleWith<decltype(&C::E3), decltype(&C::F3)>::value, "");
257 static_assert(CompatibleWith<decltype(&C::E3), decltype(&C::F3)>::value, "");
258 static_assert(CompatibleWith<decltype(&C::E3), decltype(&C::F3)>::value, "");
259
260 static_assert(!CompatibleWith<decltype(&C::F1), decltype(&C::E1)>::value, "");
261 static_assert(!CompatibleWith<decltype(&C::F3), decltype(&C::E3)>::value, "");
262 static_assert(!CompatibleWith<decltype(&C::F3), decltype(&C::E3)>::value, "");
263 static_assert(!CompatibleWith<decltype(&C::F3), decltype(&C::E3)>::value, "");
264 }
265
266 TEST(CompatibleWith, ConstMethod) {
267 class C {
268 public:
269 void F1(S) const {}
270 void F3(int, S, float) const {}
271 void E1(E) const {}
272 void E3(int, E, float) const {}
273 };
274 C().F1({}); // Avoid unused method warning
275 C().F3(0, {}, 0); // Avoid unused method warning
276 C().E1({}); // Avoid unused method warning
277 C().E3(0, {}, 0); // Avoid unused method warning
278
279 static_assert(CompatibleWith<decltype(&C::F1), decltype(&F1)>::value, "");
280 static_assert(CompatibleWith<decltype(&C::F3), decltype(&F3)>::value, "");
281 static_assert(CompatibleWith<decltype(&C::F3), decltype(&F3)>::value, "");
282 static_assert(CompatibleWith<decltype(&C::F3), decltype(&F3)>::value, "");
283
284 static_assert(!CompatibleWith<decltype(&C::F1), decltype(&F3)>::value, "");
285 static_assert(!CompatibleWith<decltype(&C::F3), decltype(&F1)>::value, "");
286 static_assert(!CompatibleWith<decltype(&C::F3), decltype(&F1)>::value, "");
287 static_assert(!CompatibleWith<decltype(&C::F3), decltype(&F1)>::value, "");
288
289 static_assert(CompatibleWith<decltype(&C::E1), decltype(&C::F1)>::value, "");
290 static_assert(CompatibleWith<decltype(&C::E3), decltype(&C::F3)>::value, "");
291 static_assert(CompatibleWith<decltype(&C::E3), decltype(&C::F3)>::value, "");
292 static_assert(CompatibleWith<decltype(&C::E3), decltype(&C::F3)>::value, "");
293
294 static_assert(!CompatibleWith<decltype(&C::F1), decltype(&C::E1)>::value, "");
295 static_assert(!CompatibleWith<decltype(&C::F3), decltype(&C::E3)>::value, "");
296 static_assert(!CompatibleWith<decltype(&C::F3), decltype(&C::E3)>::value, "");
297 static_assert(!CompatibleWith<decltype(&C::F3), decltype(&C::E3)>::value, "");
298 }
299
300 TEST(CompatibleWith, StaticMethod) {
301 class C {
302 public:
303 static void F1(S) {}
304 static void F3(int, S, float) {}
305 static void E1(E) {}
306 static void E3(int, E, float) {}
307 };
308 C::F1({}); // Avoid unused method warning
309 C::F3(0, {}, 0); // Avoid unused method warning
310 C::E1({}); // Avoid unused method warning
311 C::E3(0, {}, 0); // Avoid unused method warning
312
313 static_assert(CompatibleWith<decltype(&C::F1), decltype(&F1)>::value, "");
314 static_assert(CompatibleWith<decltype(&C::F3), decltype(&F3)>::value, "");
315 static_assert(CompatibleWith<decltype(&C::F3), decltype(&F3)>::value, "");
316 static_assert(CompatibleWith<decltype(&C::F3), decltype(&F3)>::value, "");
317
318 static_assert(!CompatibleWith<decltype(&C::F1), decltype(&F3)>::value, "");
319 static_assert(!CompatibleWith<decltype(&C::F3), decltype(&F1)>::value, "");
320 static_assert(!CompatibleWith<decltype(&C::F3), decltype(&F1)>::value, "");
321 static_assert(!CompatibleWith<decltype(&C::F3), decltype(&F1)>::value, "");
322
323 static_assert(CompatibleWith<decltype(&C::E1), decltype(&C::F1)>::value, "");
324 static_assert(CompatibleWith<decltype(&C::E3), decltype(&C::F3)>::value, "");
325 static_assert(CompatibleWith<decltype(&C::E3), decltype(&C::F3)>::value, "");
326 static_assert(CompatibleWith<decltype(&C::E3), decltype(&C::F3)>::value, "");
327
328 static_assert(!CompatibleWith<decltype(&C::F1), decltype(&C::E1)>::value, "");
329 static_assert(!CompatibleWith<decltype(&C::F3), decltype(&C::E3)>::value, "");
330 static_assert(!CompatibleWith<decltype(&C::F3), decltype(&C::E3)>::value, "");
331 static_assert(!CompatibleWith<decltype(&C::F3), decltype(&C::E3)>::value, "");
332 }
333
334 TEST(CompatibleWith, FunctionLike) {
335 using f1 = std::function<void(S)>;
336 using f3 = std::function<void(int, S, float)>;
337 using e1 = std::function<void(E)>;
338 using e3 = std::function<void(int, E, float)>;
339 static_assert(CompatibleWith<f1, decltype(&F1)>::value, "");
340 static_assert(CompatibleWith<f3, decltype(&F3)>::value, "");
341 static_assert(CompatibleWith<f3, decltype(&F3)>::value, "");
342 static_assert(CompatibleWith<f3, decltype(&F3)>::value, "");
343
344 static_assert(!CompatibleWith<f1, decltype(&F3)>::value, "");
345 static_assert(!CompatibleWith<f3, decltype(&F1)>::value, "");
346 static_assert(!CompatibleWith<f3, decltype(&F1)>::value, "");
347 static_assert(!CompatibleWith<f3, decltype(&F1)>::value, "");
348
349 static_assert(CompatibleWith<e1, f1>::value, "");
350 static_assert(CompatibleWith<e3, f3>::value, "");
351 static_assert(CompatibleWith<e3, f3>::value, "");
352 static_assert(CompatibleWith<e3, f3>::value, "");
353
354 static_assert(!CompatibleWith<f1, e1>::value, "");
355 static_assert(!CompatibleWith<f3, e3>::value, "");
356 static_assert(!CompatibleWith<f3, e3>::value, "");
357 static_assert(!CompatibleWith<f3, e3>::value, "");
358 }
359
360 TEST(CompatibleWith, Lambda) {
361 auto f1 = [](S) {};
362 auto f3 = [](int, S, float) {};
363 auto e1 = [](E) {};
364 auto e3 = [](int, E, float) {};
365 static_assert(CompatibleWith<decltype(f1), decltype(&F1)>::value, "");
366 static_assert(CompatibleWith<decltype(f3), decltype(&F3)>::value, "");
367 static_assert(CompatibleWith<decltype(f3), decltype(&F3)>::value, "");
368 static_assert(CompatibleWith<decltype(f3), decltype(&F3)>::value, "");
369
370 static_assert(!CompatibleWith<decltype(f1), decltype(&F3)>::value, "");
371 static_assert(!CompatibleWith<decltype(f3), decltype(&F1)>::value, "");
372 static_assert(!CompatibleWith<decltype(f3), decltype(&F1)>::value, "");
373 static_assert(!CompatibleWith<decltype(f3), decltype(&F1)>::value, "");
374
375 static_assert(CompatibleWith<decltype(e1), decltype(f1)>::value, "");
376 static_assert(CompatibleWith<decltype(e3), decltype(f3)>::value, "");
377 static_assert(CompatibleWith<decltype(e3), decltype(f3)>::value, "");
378 static_assert(CompatibleWith<decltype(e3), decltype(f3)>::value, "");
379
380 static_assert(!CompatibleWith<decltype(f1), decltype(e1)>::value, "");
381 static_assert(!CompatibleWith<decltype(f3), decltype(e3)>::value, "");
382 static_assert(!CompatibleWith<decltype(f3), decltype(e3)>::value, "");
383 static_assert(!CompatibleWith<decltype(f3), decltype(e3)>::value, "");
384 }
385
386 } // namespace traits
387 } // namespace dap