Debian Codegraph @

src/external/catch2/tests/SelfTest/UsageTests/Tricky.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 #ifdef __clang__
10 #pragma clang diagnostic ignored "-Wpadded"
11 #endif
12
13 #ifdef _MSC_VER
14 #pragma warning (disable : 4702) // Disable unreachable code warning for the last test
15 // that is triggered when compiling as Win32|Release
16 #endif
17
18 #include <catch2/catch_test_macros.hpp>
19 #include <catch2/generators/catch_generators.hpp>
20 #include <catch2/generators/catch_generators_range.hpp>
21
22 #include <cstdio>
23 #include <sstream>
24 #include <iostream>
25
26 struct Opaque
27 {
28 int val;
29 bool operator ==( const Opaque& o ) const
30 {
31 return val == o.val;
32 }
33 };
34
35 ///////////////////////////////////////////////////////////////////////////////
36 TEST_CASE
37 (
38 "A failing expression with a non streamable type is still captured",
39 "[Tricky][failing][.]"
40 )
41 {
42
43 Opaque o1, o2;
44 o1.val = 7;
45 o2.val = 8;
46
47 CHECK( &o1 == &o2 );
48 CHECK( o1 == o2 );
49 }
50
51 ///////////////////////////////////////////////////////////////////////////////
52 TEST_CASE
53 (
54 "An expression with side-effects should only be evaluated once",
55 "[Tricky]"
56 )
57 {
58 int i = 7;
59
60 REQUIRE( i++ == 7 );
61 REQUIRE( i++ == 8 );
62
63 }
64
65 namespace A {
66 struct X
67 {
68 X() : a(4), b(2), c(7) {}
69 X(int v) : a(v), b(2), c(7) {}
70 int a;
71 int b;
72 int c;
73 };
74 }
75
76 namespace B {
77 struct Y
78 {
79 Y() : a(4), b(2), c(7) {}
80 Y(int v) : a(v), b(2), c(7) {}
81 int a;
82 int b;
83 int c;
84 };
85 }
86
87 inline bool operator==(const A::X& lhs, const B::Y& rhs)
88 {
89 return (lhs.a == rhs.a);
90 }
91
92 inline bool operator==(const B::Y& lhs, const A::X& rhs)
93 {
94 return (lhs.a == rhs.a);
95 }
96
97
98 ///////////////////////////////////////////////////////////////////////////////
99 /* This, currently, does not compile with LLVM
100 TEST_CASE
101 (
102 "Operators at different namespace levels not hijacked by Koenig lookup"
103 "[Tricky]"
104 )
105 {
106 A::X x;
107 B::Y y;
108 REQUIRE( x == y );
109 }
110 */
111
112 namespace ObjectWithConversions
113 {
114 struct Object
115 {
116 operator unsigned int() const {return 0xc0000000;}
117 };
118
119 ///////////////////////////////////////////////////////////////////////////////
120 TEST_CASE
121 (
122 "Implicit conversions are supported inside assertion macros",
123 "[Tricky][approvals]"
124 )
125 {
126 Object o;
127 REQUIRE(0xc0000000 == o );
128 }
129 }
130
131 namespace EnumBitFieldTests
132 {
133 enum Bits : uint32_t {
134 bit0 = 0x0001,
135 bit1 = 0x0002,
136 bit2 = 0x0004,
137 bit3 = 0x0008,
138 bit1and2 = bit1 | bit2,
139 bit30 = 0x40000000,
140 bit31 = 0x80000000,
141 bit30and31 = bit30 | bit31
142 };
143
144 TEST_CASE( "Test enum bit values", "[Tricky]" )
145 {
146 REQUIRE( 0xc0000000 == bit30and31 );
147 }
148 }
149
150 struct Obj
151 {
152 Obj():prop(&p){}
153
154 int p = 0;
155 int* prop;
156 };
157
158 TEST_CASE("boolean member", "[Tricky]")
159 {
160 Obj obj;
161 REQUIRE( obj.prop != nullptr );
162 }
163
164 // Tests for a problem submitted by Ralph McArdell
165 //
166 // The static bool value should not need to be defined outside the
167 // struct it is declared in - but when evaluating it in a deduced
168 // context it appears to require the extra definition.
169 // The issue was fixed by adding bool overloads to bypass the
170 // templates that were there to deduce it.
171 template <bool B>
172 struct is_true
173 {
174 static const bool value = B;
175 };
176
177 TEST_CASE( "(unimplemented) static bools can be evaluated", "[Tricky]" )
178 {
179 SECTION("compare to true")
180 {
181 REQUIRE( is_true<true>::value == true );
182 REQUIRE( true == is_true<true>::value );
183 }
184 SECTION("compare to false")
185 {
186 REQUIRE( is_true<false>::value == false );
187 REQUIRE( false == is_true<false>::value );
188 }
189
190 SECTION("negation")
191 {
192 REQUIRE( !is_true<false>::value );
193 }
194
195 SECTION("double negation")
196 {
197 REQUIRE( !!is_true<true>::value );
198 }
199
200 SECTION("direct")
201 {
202 REQUIRE( is_true<true>::value );
203 REQUIRE_FALSE( is_true<false>::value );
204 }
205 }
206
207 struct Boolable
208 {
209 explicit Boolable( bool value ) : m_value( value ) {}
210
211 explicit operator bool() const {
212 return m_value;
213 }
214
215 bool m_value;
216 };
217
218 TEST_CASE( "Objects that evaluated in boolean contexts can be checked", "[Tricky][SafeBool]" )
219 {
220 Boolable True( true );
221 Boolable False( false );
222
223 CHECK( True );
224 CHECK( !False );
225 CHECK_FALSE( False );
226 }
227
228 TEST_CASE( "Assertions then sections", "[Tricky]" )
229 {
230 // This was causing a failure due to the way the console reporter was handling
231 // the current section
232
233 REQUIRE( true );
234
235 SECTION( "A section" )
236 {
237 REQUIRE( true );
238
239 SECTION( "Another section" )
240 {
241 REQUIRE( true );
242 }
243 SECTION( "Another other section" )
244 {
245 REQUIRE( true );
246 }
247 }
248 }
249
250 struct Awkward
251 {
252 operator int() const { return 7; }
253 };
254
255 TEST_CASE( "non streamable - with conv. op", "[Tricky]" )
256 {
257 Awkward awkward;
258 std::string s = ::Catch::Detail::stringify( awkward );
259 REQUIRE( s == "7" );
260 }
261
262 inline void foo() {}
263
264 using fooptr_t = void (*)();
265
266 TEST_CASE( "Comparing function pointers", "[Tricky][function pointer]" )
267 {
268 // This was giving a warning in VS2010
269 // #179
270 fooptr_t a = foo;
271
272 REQUIRE( a );
273 REQUIRE( a == &foo );
274 }
275
276 struct S
277 {
278 void f() {}
279 };
280
281
282 TEST_CASE( "Comparing member function pointers", "[Tricky][member function pointer][approvals]" )
283 {
284 using MF = void (S::*)();
285 MF m = &S::f;
286
287 CHECK( m == &S::f );
288 }
289
290 class ClassName {};
291
292 TEST_CASE( "pointer to class", "[Tricky]" )
293 {
294 ClassName *p = 0;
295 REQUIRE( p == 0 );
296 }
297
298 #include <memory>
299
300 TEST_CASE( "null_ptr", "[Tricky]" )
301 {
302 std::unique_ptr<int> ptr;
303 REQUIRE(ptr.get() == nullptr);
304 }
305
306 TEST_CASE( "X/level/0/a", "[Tricky]" ) { SUCCEED(""); }
307 TEST_CASE( "X/level/0/b", "[Tricky][fizz]" ){ SUCCEED(""); }
308 TEST_CASE( "X/level/1/a", "[Tricky]" ) { SUCCEED(""); }
309 TEST_CASE( "X/level/1/b", "[Tricky]" ) { SUCCEED(""); }
310
311 TEST_CASE( "has printf" ) {
312
313 // This can cause problems as, currently, stdout itself is not redirected - only the cout (and cerr) buffer
314 printf( "loose text artifact\n" );
315 }
316
317 namespace {
318 struct constructor_throws {
319 [[noreturn]] constructor_throws() {
320 throw 1;
321 }
322 };
323 }
324
325 TEST_CASE("Commas in various macros are allowed") {
326 REQUIRE_THROWS( std::vector<constructor_throws>{constructor_throws{}, constructor_throws{}} );
327 CHECK_THROWS( std::vector<constructor_throws>{constructor_throws{}, constructor_throws{}} );
328 REQUIRE_NOTHROW( std::vector<int>{1, 2, 3} == std::vector<int>{1, 2, 3} );
329 CHECK_NOTHROW( std::vector<int>{1, 2, 3} == std::vector<int>{1, 2, 3} );
330
331 REQUIRE(std::vector<int>{1, 2} == std::vector<int>{1, 2});
332 CHECK( std::vector<int>{1, 2} == std::vector<int>{1, 2} );
333 REQUIRE_FALSE(std::vector<int>{1, 2} == std::vector<int>{1, 2, 3});
334 CHECK_FALSE( std::vector<int>{1, 2} == std::vector<int>{1, 2, 3} );
335
336 CHECK_NOFAIL( std::vector<int>{1, 2} == std::vector<int>{1, 2} );
337 CHECKED_IF( std::vector<int>{1, 2} == std::vector<int>{1, 2} ) {
338 REQUIRE(true);
339 } CHECKED_ELSE( std::vector<int>{1, 2} == std::vector<int>{1, 2} ) {
340 CHECK(true);
341 }
342 }
343
344 TEST_CASE( "non-copyable objects", "[.][failing]" ) {
345 // Thanks to Agustin Bergé (@k-ballo on the cpplang Slack) for raising this
346 std::type_info const& ti = typeid(int);
347 CHECK( ti == typeid(int) );
348 }
349
350 TEST_CASE("#1514: stderr/stdout is not captured in tests aborted by an exception", "[output-capture][regression][.]") {
351 std::cout << "This would not be caught previously\n" << std::flush;
352 std::clog << "Nor would this\n" << std::flush;
353 // FAIL aborts the test by throwing a Catch exception
354 FAIL("1514");
355 }
356
357
358 TEST_CASE( "#2025: -c shouldn't cause infinite loop", "[sections][generators][regression][.approvals]" ) {
359 SECTION( "Check cursor from buffer offset" ) {
360 auto bufPos = GENERATE_REF( range( 0, 44 ) );
361 WHEN( "Buffer position is " << bufPos ) { REQUIRE( 1 == 1 ); }
362 }
363 }
364
365 TEST_CASE("#2025: original repro", "[sections][generators][regression][.approvals]") {
366 auto fov = GENERATE(true, false);
367 DYNAMIC_SECTION("fov_" << fov) {
368 std::cout << "inside with fov: " << fov << '\n';
369 }
370 }
371
372 TEST_CASE("#2025: same-level sections", "[sections][generators][regression][.approvals]") {
373 SECTION("A") {
374 SUCCEED();
375 }
376 auto i = GENERATE(1, 2, 3);
377 SECTION("B") {
378 REQUIRE(i < 4);
379 }
380 }