Debian Codegraph @

src/external/catch2/tests/SelfTest/UsageTests/Compilation.tests.cpp

raw
1
2 // Copyright Catch2 Authors
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE.txt or copy at
5 // https://www.boost.org/LICENSE_1_0.txt)
6
7 // SPDX-License-Identifier: BSL-1.0
8
9 #include <helpers/type_with_lit_0_comparisons.hpp>
10
11 #include <array>
12 #include <type_traits>
13
14 // Setup for #1403 -- look for global overloads of operator << for classes
15 // in a different namespace.
16 #include <ostream>
17
18 namespace foo {
19 struct helper_1403 {
20 bool operator==(helper_1403) const { return true; }
21 };
22 }
23
24 namespace bar {
25 template <typename... Ts>
26 struct TypeList {};
27 }
28
29 #ifdef __GNUC__
30 #pragma GCC diagnostic ignored "-Wmissing-declarations"
31 #endif
32 static std::ostream& operator<<(std::ostream& out, foo::helper_1403 const&) {
33 return out << "[1403 helper]";
34 }
35 ///////////////////////////////
36
37 #include <catch2/catch_test_macros.hpp>
38 #include <catch2/generators/catch_generators_range.hpp>
39 #include <catch2/matchers/catch_matchers_string.hpp>
40
41 #include <cstring>
42
43 // Comparison operators can return non-booleans.
44 // This is unusual, but should be supported.
45 struct logic_t {
46 logic_t operator< (logic_t) const { return {}; }
47 logic_t operator<=(logic_t) const { return {}; }
48 logic_t operator> (logic_t) const { return {}; }
49 logic_t operator>=(logic_t) const { return {}; }
50 logic_t operator==(logic_t) const { return {}; }
51 logic_t operator!=(logic_t) const { return {}; }
52 explicit operator bool() const { return true; }
53 };
54
55
56 static void throws_int(bool b) {
57 if (b) {
58 throw 1;
59 }
60 }
61
62 template<typename T>
63 bool templated_tests(T t) {
64 int a = 3;
65 REQUIRE(a == t);
66 CHECK(a == t);
67 REQUIRE_THROWS(throws_int(true));
68 CHECK_THROWS_AS(throws_int(true), int);
69 REQUIRE_NOTHROW(throws_int(false));
70 REQUIRE_THAT("aaa", Catch::Matchers::EndsWith("aaa"));
71 return true;
72 }
73
74 struct A {};
75
76 static std::ostream &operator<<(std::ostream &o, const A &) { return o << 0; }
77
78 struct B : private A {
79 bool operator==(int) const { return true; }
80 };
81
82 #ifdef __clang__
83 #pragma clang diagnostic push
84 #pragma clang diagnostic ignored "-Wunused-function"
85 #endif
86 #ifdef __GNUC__
87 // Note that because -~GCC~-, this warning cannot be silenced temporarily, by pushing diagnostic stack...
88 // Luckily it is firing in test files and thus can be silenced for the whole file, without losing much.
89 #pragma GCC diagnostic ignored "-Wunused-function"
90 #endif
91
92 B f();
93
94 std::ostream g();
95
96 #ifdef __clang__
97 #pragma clang diagnostic pop
98 #endif
99
100 template <typename, typename>
101 struct Fixture_1245 {};
102
103 // This is a minimal example for an issue we have found in 1.7.0
104 struct dummy_809 {
105 int i;
106 };
107
108 template<typename T>
109 bool operator==(const T& val, dummy_809 f) {
110 return val == f.i;
111 }
112
113 TEST_CASE("#809") {
114 dummy_809 f;
115 f.i = 42;
116 REQUIRE(42 == f);
117 }
118
119
120 // ------------------------------------------------------------------
121 // Changes to REQUIRE_THROWS_AS made it stop working in a template in
122 // an unfixable way (as long as C++03 compatibility is being kept).
123 // To prevent these from happening in the future, this needs to compile
124
125 TEST_CASE("#833") {
126 REQUIRE(templated_tests<int>(3));
127 }
128
129
130 // Test containing example where original stream insertable check breaks compilation
131 TEST_CASE("#872") {
132 A dummy;
133 CAPTURE(dummy);
134 B x;
135 REQUIRE (x == 4);
136 }
137
138 TEST_CASE("#1027: Bitfields can be captured") {
139 struct Y {
140 uint32_t v : 1;
141 };
142 Y y{ 0 };
143 REQUIRE(y.v == 0);
144 REQUIRE(0 == y.v);
145 }
146
147 TEST_CASE( "#3001: Enum-based bitfields can be captured" ) {
148 enum E {
149 ZERO = 0,
150 ONE = 1,
151 TWO = 2,
152 };
153
154 struct BF {
155 E e : 2;
156 };
157
158 BF bf{};
159 bf.e = ONE;
160 REQUIRE( bf.e == 1 );
161 REQUIRE( 1 == bf.e );
162 }
163
164 // Comparison operators can return non-booleans.
165 // This is unusual, but should be supported.
166 TEST_CASE("#1147") {
167 logic_t t1, t2;
168 REQUIRE(t1 == t2);
169 REQUIRE(t1 != t2);
170 REQUIRE(t1 < t2);
171 REQUIRE(t1 > t2);
172 REQUIRE(t1 <= t2);
173 REQUIRE(t1 >= t2);
174 }
175
176 // unsigned array
177 TEST_CASE("#1238") {
178 unsigned char uarr[] = "123";
179 CAPTURE(uarr);
180 signed char sarr[] = "456";
181 CAPTURE(sarr);
182
183 REQUIRE(std::memcmp(uarr, "123", sizeof(uarr)) == 0);
184 REQUIRE(std::memcmp(sarr, "456", sizeof(sarr)) == 0);
185 }
186
187 TEST_CASE_METHOD((Fixture_1245<int, int>), "#1245", "[compilation]") {
188 SUCCEED();
189 }
190
191 TEST_CASE("#1403", "[compilation]") {
192 ::foo::helper_1403 h1, h2;
193 REQUIRE(h1 == h2);
194 }
195
196 TEST_CASE("Optionally static assertions", "[compilation]") {
197 STATIC_REQUIRE( std::is_void<void>::value );
198 STATIC_REQUIRE_FALSE( std::is_void<int>::value );
199 STATIC_CHECK( std::is_void<void>::value );
200 STATIC_CHECK_FALSE( std::is_void<int>::value );
201 }
202
203 TEST_CASE("#1548", "[compilation]") {
204 using namespace bar;
205 REQUIRE(std::is_same<TypeList<int>, TypeList<int>>::value);
206 }
207
208 // #925
209 using signal_t = void (*) (void*);
210
211 struct TestClass {
212 signal_t testMethod_uponComplete_arg = nullptr;
213 };
214
215 namespace utility {
216 inline static void synchronizing_callback( void * ) { }
217 }
218
219 #if defined (_MSC_VER)
220 #pragma warning(push)
221 // The function pointer comparison below triggers warning because of
222 // calling conventions
223 #pragma warning(disable:4244)
224 #endif
225 TEST_CASE("#925: comparing function pointer to function address failed to compile", "[!nonportable]" ) {
226 TestClass test;
227 REQUIRE(utility::synchronizing_callback != test.testMethod_uponComplete_arg);
228 }
229 #if defined (_MSC_VER)
230 #pragma warning(pop)
231 #endif
232
233 TEST_CASE( "#1319: Sections can have description (even if it is not saved",
234 "[compilation]" ) {
235 SECTION( "SectionName", "This is a long form section description" ) {
236 SUCCEED();
237 }
238 }
239
240 TEST_CASE("Lambdas in assertions") {
241 REQUIRE([]() { return true; }());
242 }
243
244 namespace {
245 struct HasBitOperators {
246 int value;
247
248 friend HasBitOperators operator| (HasBitOperators lhs, HasBitOperators rhs) {
249 return { lhs.value | rhs.value };
250 }
251 friend HasBitOperators operator& (HasBitOperators lhs, HasBitOperators rhs) {
252 return { lhs.value & rhs.value };
253 }
254 friend HasBitOperators operator^ (HasBitOperators lhs, HasBitOperators rhs) {
255 return { lhs.value ^ rhs.value };
256 }
257 explicit operator bool() const {
258 return !!value;
259 }
260
261 friend std::ostream& operator<<(std::ostream& out, HasBitOperators val) {
262 out << "Val: " << val.value;
263 return out;
264 }
265 };
266 }
267
268 TEST_CASE("Assertion macros support bit operators and bool conversions", "[compilation][bitops]") {
269 HasBitOperators lhs{ 1 }, rhs{ 2 };
270 REQUIRE(lhs | rhs);
271 REQUIRE_FALSE(lhs & rhs);
272 REQUIRE(HasBitOperators{ 1 } & HasBitOperators{ 1 });
273 REQUIRE(lhs ^ rhs);
274 REQUIRE_FALSE(lhs ^ lhs);
275 }
276
277 namespace {
278 struct ImmovableType {
279 ImmovableType() = default;
280
281 ImmovableType(ImmovableType const&) = delete;
282 ImmovableType& operator=(ImmovableType const&) = delete;
283 ImmovableType(ImmovableType&&) = delete;
284 ImmovableType& operator=(ImmovableType&&) = delete;
285
286 friend bool operator==(ImmovableType const&, ImmovableType const&) {
287 return true;
288 }
289 };
290 }
291
292 TEST_CASE("Immovable types are supported in basic assertions", "[compilation][.approvals]") {
293 REQUIRE(ImmovableType{} == ImmovableType{});
294 }
295
296 namespace adl {
297
298 struct always_true {
299 explicit operator bool() const { return true; }
300 };
301
302 #define COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(op) \
303 template <class T, class U> \
304 auto operator op (T&&, U&&) { \
305 return always_true{}; \
306 }
307
308 COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(==)
309 COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(!=)
310 COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(<)
311 COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(>)
312 COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(<=)
313 COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(>=)
314 COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(|)
315 COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(&)
316 COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(^)
317
318 #undef COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR
319
320 }
321
322 TEST_CASE("ADL universal operators don't hijack expression deconstruction", "[compilation][.approvals]") {
323 REQUIRE(adl::always_true{});
324 REQUIRE(0 == adl::always_true{});
325 REQUIRE(0 != adl::always_true{});
326 REQUIRE(0 < adl::always_true{});
327 REQUIRE(0 > adl::always_true{});
328 REQUIRE(0 <= adl::always_true{});
329 REQUIRE(0 >= adl::always_true{});
330 REQUIRE(0 | adl::always_true{});
331 REQUIRE(0 & adl::always_true{});
332 REQUIRE(0 ^ adl::always_true{});
333 }
334
335 TEST_CASE( "#2555 - types that can only be compared with 0 literal implemented as pointer conversion are supported",
336 "[compilation][approvals]" ) {
337 REQUIRE( TypeWithLit0Comparisons{} < 0 );
338 REQUIRE_FALSE( 0 < TypeWithLit0Comparisons{} );
339 REQUIRE( TypeWithLit0Comparisons{} <= 0 );
340 REQUIRE_FALSE( 0 <= TypeWithLit0Comparisons{} );
341
342 REQUIRE( TypeWithLit0Comparisons{} > 0 );
343 REQUIRE_FALSE( 0 > TypeWithLit0Comparisons{} );
344 REQUIRE( TypeWithLit0Comparisons{} >= 0 );
345 REQUIRE_FALSE( 0 >= TypeWithLit0Comparisons{} );
346
347 REQUIRE( TypeWithLit0Comparisons{} == 0 );
348 REQUIRE_FALSE( 0 == TypeWithLit0Comparisons{} );
349 REQUIRE( TypeWithLit0Comparisons{} != 0 );
350 REQUIRE_FALSE( 0 != TypeWithLit0Comparisons{} );
351 }
352
353 // These tests require `consteval` to propagate through `constexpr` calls
354 // which is a late DR against C++20.
355 #if defined( CATCH_CPP20_OR_GREATER ) && defined( __cpp_consteval ) && \
356 __cpp_consteval >= 202211L
357 // Can't have internal linkage to avoid warnings
358 void ZeroLiteralErrorFunc();
359 namespace {
360 struct ZeroLiteralConsteval {
361 template <class T, std::enable_if_t<std::is_same_v<T, int>, int> = 0>
362 consteval ZeroLiteralConsteval( T zero ) noexcept {
363 if ( zero != 0 ) { ZeroLiteralErrorFunc(); }
364 }
365 };
366
367 // Should only be constructible from literal 0. Uses the propagating
368 // consteval constructor trick (currently used by MSVC, might be used
369 // by libc++ in the future as well).
370 struct TypeWithConstevalLit0Comparison {
371 # define DEFINE_COMP_OP( op ) \
372 constexpr friend bool operator op( TypeWithConstevalLit0Comparison, \
373 ZeroLiteralConsteval ) { \
374 return true; \
375 } \
376 constexpr friend bool operator op( ZeroLiteralConsteval, \
377 TypeWithConstevalLit0Comparison ) { \
378 return false; \
379 } \
380 /* std::orderings only have these for ==, but we add them for all \
381 operators so we can test all overloads for decomposer */ \
382 constexpr friend bool operator op( TypeWithConstevalLit0Comparison, \
383 TypeWithConstevalLit0Comparison ) { \
384 return true; \
385 }
386
387 DEFINE_COMP_OP( < )
388 DEFINE_COMP_OP( <= )
389 DEFINE_COMP_OP( > )
390 DEFINE_COMP_OP( >= )
391 DEFINE_COMP_OP( == )
392 DEFINE_COMP_OP( != )
393
394 #undef DEFINE_COMP_OP
395 };
396
397 } // namespace
398
399 namespace Catch {
400 template <>
401 struct capture_by_value<TypeWithConstevalLit0Comparison> : std::true_type {};
402 }
403
404 TEST_CASE( "#2555 - types that can only be compared with 0 literal implemented as consteval check are supported",
405 "[compilation][approvals]" ) {
406 REQUIRE( TypeWithConstevalLit0Comparison{} < 0 );
407 REQUIRE_FALSE( 0 < TypeWithConstevalLit0Comparison{} );
408 REQUIRE( TypeWithConstevalLit0Comparison{} <= 0 );
409 REQUIRE_FALSE( 0 <= TypeWithConstevalLit0Comparison{} );
410
411 REQUIRE( TypeWithConstevalLit0Comparison{} > 0 );
412 REQUIRE_FALSE( 0 > TypeWithConstevalLit0Comparison{} );
413 REQUIRE( TypeWithConstevalLit0Comparison{} >= 0 );
414 REQUIRE_FALSE( 0 >= TypeWithConstevalLit0Comparison{} );
415
416 REQUIRE( TypeWithConstevalLit0Comparison{} == 0 );
417 REQUIRE_FALSE( 0 == TypeWithConstevalLit0Comparison{} );
418 REQUIRE( TypeWithConstevalLit0Comparison{} != 0 );
419 REQUIRE_FALSE( 0 != TypeWithConstevalLit0Comparison{} );
420 }
421
422 // We check all comparison ops to test, even though orderings, the primary
423 // motivation for this functionality, only have self-comparison (and thus
424 // have the ambiguity issue) for `==` and `!=`.
425 TEST_CASE( "Comparing const instances of type registered with capture_by_value",
426 "[regression][approvals][compilation]" ) {
427 SECTION("Type with consteval-int constructor") {
428 auto const const_Lit0Type_1 = TypeWithConstevalLit0Comparison{};
429 auto const const_Lit0Type_2 = TypeWithConstevalLit0Comparison{};
430 REQUIRE( const_Lit0Type_1 == const_Lit0Type_2 );
431 REQUIRE( const_Lit0Type_1 <= const_Lit0Type_2 );
432 REQUIRE( const_Lit0Type_1 < const_Lit0Type_2 );
433 REQUIRE( const_Lit0Type_1 >= const_Lit0Type_2 );
434 REQUIRE( const_Lit0Type_1 > const_Lit0Type_2 );
435 REQUIRE( const_Lit0Type_1 != const_Lit0Type_2 );
436 }
437 SECTION("Type with constexpr-int constructor") {
438 auto const const_Lit0Type_1 = TypeWithLit0Comparisons{};
439 auto const const_Lit0Type_2 = TypeWithLit0Comparisons{};
440 REQUIRE( const_Lit0Type_1 == const_Lit0Type_2 );
441 REQUIRE( const_Lit0Type_1 <= const_Lit0Type_2 );
442 REQUIRE( const_Lit0Type_1 < const_Lit0Type_2 );
443 REQUIRE( const_Lit0Type_1 >= const_Lit0Type_2 );
444 REQUIRE( const_Lit0Type_1 > const_Lit0Type_2 );
445 REQUIRE( const_Lit0Type_1 != const_Lit0Type_2 );
446 }
447 }
448
449 #endif // C++20 consteval
450
451
452 namespace {
453 struct MultipleImplicitConstructors {
454 MultipleImplicitConstructors( double ) {}
455 MultipleImplicitConstructors( int64_t ) {}
456 bool operator==( MultipleImplicitConstructors ) const { return true; }
457 bool operator!=( MultipleImplicitConstructors ) const { return true; }
458 bool operator<( MultipleImplicitConstructors ) const { return true; }
459 bool operator<=( MultipleImplicitConstructors ) const { return true; }
460 bool operator>( MultipleImplicitConstructors ) const { return true; }
461 bool operator>=( MultipleImplicitConstructors ) const { return true; }
462 };
463 }
464 TEST_CASE("#2571 - tests compile types that have multiple implicit constructors from lit 0",
465 "[compilation][approvals]") {
466 MultipleImplicitConstructors mic1( 0.0 );
467 MultipleImplicitConstructors mic2( 0.0 );
468 REQUIRE( mic1 == mic2 );
469 REQUIRE( mic1 != mic2 );
470 REQUIRE( mic1 < mic2 );
471 REQUIRE( mic1 <= mic2 );
472 REQUIRE( mic1 > mic2 );
473 REQUIRE( mic1 >= mic2 );
474 }
475
476 #if defined( CATCH_CONFIG_CPP20_COMPARE_OVERLOADS )
477 // This test does not test all the related codepaths, but it is the original
478 // reproducer
479 TEST_CASE( "Comparing const std::weak_ordering instances must compile",
480 "[compilation][approvals][regression]" ) {
481 auto const const_ordering_1 = std::weak_ordering::less;
482 auto const const_ordering_2 = std::weak_ordering::less;
483 auto plain_ordering_1 = std::weak_ordering::less;
484 REQUIRE( const_ordering_1 == plain_ordering_1 );
485 REQUIRE( const_ordering_1 == const_ordering_2 );
486 REQUIRE( plain_ordering_1 == const_ordering_1 );
487 }
488 #endif
489
490 // Reproduce issue with yaml-cpp iterators, where the `const_iterator`
491 // for Node type has `const T` as the value_type. This is wrong for
492 // multitude of reasons, but there might be other libraries in the wild
493 // that share this issue, and the workaround needed to support
494 // `from_range(iter, iter)` helper with those libraries is easy enough.
495 class HasBadIterator {
496 std::array<int, 10> m_arr{};
497
498 public:
499 class iterator {
500 const int* m_ptr = nullptr;
501
502 public:
503 iterator( const int* ptr ): m_ptr( ptr ) {}
504
505 using difference_type = std::ptrdiff_t;
506 using value_type = const int;
507 using pointer = const int*;
508 using reference = const int&;
509 using iterator_category = std::input_iterator_tag;
510
511 iterator& operator++() {
512 ++m_ptr;
513 return *this;
514 }
515
516 iterator operator++( int ) {
517 auto ret( *this );
518 ++( *this );
519 return ret;
520 }
521
522 friend bool operator==( iterator lhs, iterator rhs ) {
523 return lhs.m_ptr == rhs.m_ptr;
524 }
525 friend bool operator!=( iterator lhs, iterator rhs ) {
526 return !( lhs == rhs );
527 }
528
529 int operator*() const { return *m_ptr; }
530 };
531
532 iterator cbegin() const { return { m_arr.data() }; }
533 iterator cend() const { return { m_arr.data() + m_arr.size() }; }
534 };
535
536 TEST_CASE("from_range(iter, iter) supports const_iterators", "[generators][from-range][approvals]") {
537 using namespace Catch::Generators;
538
539 HasBadIterator data;
540 auto gen = from_range(data.cbegin(), data.cend());
541 (void)gen;
542 }