• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Created by Phil on 29/11/2010.
3  *  Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
4  *
5  *  Distributed under the Boost Software License, Version 1.0. (See accompanying
6  *  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7  */
8 
9 #include "catch.hpp"
10 
11 #ifdef __clang__
12 #   pragma clang diagnostic ignored "-Wc++98-compat"
13 #   pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
14 #endif
15 
16 
17 #include <iostream>
18 #include <cerrno>
19 #include <limits>
20 #include <sstream>
21 
22 namespace { namespace MiscTests {
23 
24 #ifndef MISC_TEST_HELPERS_INCLUDED // Don't compile this more than once per TU
25 #define MISC_TEST_HELPERS_INCLUDED
26 
makeString(bool makeNull)27 inline const char* makeString( bool makeNull ) {
28     return makeNull ? nullptr : "valid string";
29 }
testCheckedIf(bool flag)30 inline bool testCheckedIf( bool flag )  {
31     CHECKED_IF( flag )
32         return true;
33     else
34         return false;
35 }
testCheckedElse(bool flag)36 inline bool testCheckedElse( bool flag ) {
37     CHECKED_ELSE( flag )
38         return false;
39 
40     return true;
41 }
42 
Factorial(unsigned int number)43 inline unsigned int Factorial( unsigned int number )  {
44     return number > 1 ? Factorial(number-1)*number : 1;
45 }
46 
f()47 static int f() {
48     return 1;
49 }
50 
manuallyRegisteredTestFunction()51 inline void manuallyRegisteredTestFunction() {
52     SUCCEED( "was called" );
53 }
54 
55 struct AutoTestReg {
AutoTestReg__anona438b8650111::MiscTests::AutoTestReg56     AutoTestReg() {
57         REGISTER_TEST_CASE( manuallyRegisteredTestFunction, "ManuallyRegistered" );
58     }
59 };
60 CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS
61 static AutoTestReg autoTestReg;
62 CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS
63 
64 template<typename T>
65 struct Foo {
size__anona438b8650111::MiscTests::Foo66     size_t size() { return 0; }
67 };
68 
69 #endif
70 
71 TEST_CASE( "random SECTION tests", "[.][sections][failing]" ) {
72     int a = 1;
73     int b = 2;
74 
75     SECTION( "doesn't equal" ) {
76         REQUIRE( a != b );
77         REQUIRE( b != a );
78     }
79 
80     SECTION( "not equal" ) {
81         REQUIRE( a != b);
82     }
83 }
84 
85 TEST_CASE( "nested SECTION tests", "[.][sections][failing]" ) {
86     int a = 1;
87     int b = 2;
88 
89     SECTION( "doesn't equal" ) {
90         REQUIRE( a != b );
91         REQUIRE( b != a );
92 
93         SECTION( "not equal" ) {
94             REQUIRE( a != b);
95         }
96     }
97 }
98 
99 TEST_CASE( "more nested SECTION tests", "[sections][failing][.]" ) {
100     int a = 1;
101     int b = 2;
102 
103     SECTION( "doesn't equal" ) {
104         SECTION( "equal" ) {
105             REQUIRE( a == b );
106         }
107 
108         SECTION( "not equal" ) {
109             REQUIRE( a != b );
110         }
111         SECTION( "less than" ) {
112             REQUIRE( a < b );
113         }
114     }
115 }
116 
117 TEST_CASE( "even more nested SECTION tests", "[sections]" ) {
118     SECTION( "c" ) {
119         SECTION( "d (leaf)" ) {
120             SUCCEED(); // avoid failing due to no tests
121         }
122 
123         SECTION( "e (leaf)" ) {
124             SUCCEED(); // avoid failing due to no tests
125         }
126     }
127 
128     SECTION( "f (leaf)" ) {
129         SUCCEED(); // avoid failing due to no tests
130     }
131 }
132 
133 TEST_CASE( "looped SECTION tests", "[.][failing][sections]" ) {
134     int a = 1;
135 
136     for( int b = 0; b < 10; ++b ) {
137         DYNAMIC_SECTION( "b is currently: " << b ) {
138             CHECK( b > a );
139         }
140     }
141 }
142 
143 TEST_CASE( "looped tests", "[.][failing]" ) {
144     static const int fib[]  = { 1, 1, 2, 3, 5, 8, 13, 21 };
145 
146     for( std::size_t i=0; i < sizeof(fib)/sizeof(int); ++i ) {
147         INFO( "Testing if fib[" << i << "] (" << fib[i] << ") is even" );
148         CHECK( ( fib[i] % 2 ) == 0 );
149     }
150 }
151 
152 TEST_CASE( "Sends stuff to stdout and stderr", "[.]" ) {
153     std::cout << "A string sent directly to stdout" << std::endl;
154     std::cerr << "A string sent directly to stderr" << std::endl;
155     std::clog << "A string sent to stderr via clog" << std::endl;
156 }
157 
158 TEST_CASE( "null strings" ) {
159     REQUIRE( makeString( false ) != static_cast<char*>(nullptr));
160     REQUIRE( makeString( true ) == static_cast<char*>(nullptr));
161 }
162 
163 TEST_CASE( "checkedIf" ) {
164     REQUIRE( testCheckedIf( true ) );
165 }
166 
167 TEST_CASE( "checkedIf, failing", "[failing][.]" ) {
168     REQUIRE( testCheckedIf( false ) );
169 }
170 
171 TEST_CASE( "checkedElse" ) {
172     REQUIRE( testCheckedElse( true ) );
173 }
174 
175 TEST_CASE( "checkedElse, failing", "[failing][.]" ) {
176     REQUIRE( testCheckedElse( false ) );
177 }
178 
179 TEST_CASE( "xmlentitycheck" ) {
180     SECTION( "embedded xml: <test>it should be possible to embed xml characters, such as <, \" or &, or even whole <xml>documents</xml> within an attribute</test>" ) {
181         SUCCEED(); // We need this here to stop it failing due to no tests
182     }
183     SECTION( "encoded chars: these should all be encoded: &&&\"\"\"<<<&\"<<&\"" ) {
184         SUCCEED(); // We need this here to stop it failing due to no tests
185     }
186 }
187 
188 TEST_CASE( "send a single char to INFO", "[failing][.]" ) {
189     INFO(3);
190     REQUIRE(false);
191 }
192 
193 TEST_CASE( "atomic if", "[failing][0]") {
194     std::size_t x = 0;
195 
196     if( x )
197         REQUIRE(x > 0);
198     else
199         REQUIRE(x == 0);
200 }
201 
202 
203 TEST_CASE( "Factorials are computed", "[factorial]" ) {
204   REQUIRE( Factorial(0) == 1 );
205   REQUIRE( Factorial(1) == 1 );
206   REQUIRE( Factorial(2) == 2 );
207   REQUIRE( Factorial(3) == 6 );
208   REQUIRE( Factorial(10) == 3628800 );
209 }
210 
211 TEST_CASE( "An empty test with no assertions", "[empty]" ) {}
212 
213 TEST_CASE( "Nice descriptive name", "[tag1][tag2][tag3][.]" ) {
214     WARN( "This one ran" );
215 }
216 TEST_CASE( "first tag", "[tag1]" ) {}
217 TEST_CASE( "second tag", "[tag2]" ) {}
218 
219 //
220 //TEST_CASE( "spawn a new process", "[.]" )
221 //{
222 //    // !TBD Work in progress
223 //    char line[200];
224 //    FILE* output = popen("./CatchSelfTest ./failing/matchers/StartsWith", "r");
225 //    while ( fgets(line, 199, output) )
226 //        std::cout << line;
227 //}
228 
229 TEST_CASE( "vectors can be sized and resized", "[vector]" ) {
230 
231     std::vector<int> v( 5 );
232 
233     REQUIRE( v.size() == 5 );
234     REQUIRE( v.capacity() >= 5 );
235 
236     SECTION( "resizing bigger changes size and capacity" ) {
237         v.resize( 10 );
238 
239         REQUIRE( v.size() == 10 );
240         REQUIRE( v.capacity() >= 10 );
241     }
242     SECTION( "resizing smaller changes size but not capacity" ) {
243         v.resize( 0 );
244 
245         REQUIRE( v.size() == 0 );
246         REQUIRE( v.capacity() >= 5 );
247 
248         SECTION( "We can use the 'swap trick' to reset the capacity" ) {
249             std::vector<int> empty;
250             empty.swap( v );
251 
252             REQUIRE( v.capacity() == 0 );
253         }
254     }
255     SECTION( "reserving bigger changes capacity but not size" ) {
256         v.reserve( 10 );
257 
258         REQUIRE( v.size() == 5 );
259         REQUIRE( v.capacity() >= 10 );
260     }
261     SECTION( "reserving smaller does not change size or capacity" ) {
262         v.reserve( 0 );
263 
264         REQUIRE( v.size() == 5 );
265         REQUIRE( v.capacity() >= 5 );
266     }
267 }
268 
269 TEMPLATE_TEST_CASE( "TemplateTest: vectors can be sized and resized", "[vector][template]", int, float, std::string, (std::tuple<int,float>) ) {
270 
271     std::vector<TestType> v( 5 );
272 
273     REQUIRE( v.size() == 5 );
274     REQUIRE( v.capacity() >= 5 );
275 
276     SECTION( "resizing bigger changes size and capacity" ) {
277         v.resize( 10 );
278 
279         REQUIRE( v.size() == 10 );
280         REQUIRE( v.capacity() >= 10 );
281     }
282     SECTION( "resizing smaller changes size but not capacity" ) {
283         v.resize( 0 );
284 
285         REQUIRE( v.size() == 0 );
286         REQUIRE( v.capacity() >= 5 );
287 
288         SECTION( "We can use the 'swap trick' to reset the capacity" ) {
289             std::vector<TestType> empty;
290             empty.swap( v );
291 
292             REQUIRE( v.capacity() == 0 );
293         }
294     }
295     SECTION( "reserving bigger changes capacity but not size" ) {
296         v.reserve( 10 );
297 
298         REQUIRE( v.size() == 5 );
299         REQUIRE( v.capacity() >= 10 );
300     }
301     SECTION( "reserving smaller does not change size or capacity" ) {
302         v.reserve( 0 );
303 
304         REQUIRE( v.size() == 5 );
305         REQUIRE( v.capacity() >= 5 );
306     }
307 }
308 
309 TEMPLATE_PRODUCT_TEST_CASE("A Template product test case", "[template][product]", (std::vector, Foo), (int, float)) {
310     TestType x;
311     REQUIRE(x.size() == 0);
312 }
313 
314 TEMPLATE_PRODUCT_TEST_CASE("Product with differing arities", "[template][product]", std::tuple, (int, (int, double), (int, double, float))) {
315     REQUIRE(std::tuple_size<TestType>::value >= 1);
316 }
317 
318 // https://github.com/philsquared/Catch/issues/166
319 TEST_CASE("A couple of nested sections followed by a failure", "[failing][.]") {
320     SECTION("Outer")
321         SECTION("Inner")
322             SUCCEED("that's not flying - that's failing in style");
323 
324     FAIL("to infinity and beyond");
325 }
326 
327 TEST_CASE("not allowed", "[!throws]") {
328     // This test case should not be included if you run with -e on the command line
329     SUCCEED();
330 }
331 
332 //TEST_CASE( "Is big endian" ) {
333 //    CHECK( Catch::Detail::Endianness::which() == Catch::Detail::Endianness::Little );
334 //}
335 
336 TEST_CASE( "Tabs and newlines show in output", "[.][whitespace][failing]" ) {
337 
338     // Based on issue #242
339     std::string s1 = "if ($b == 10) {\n\t\t$a\t= 20;\n}";
340     std::string s2 = "if ($b == 10) {\n\t$a = 20;\n}\n";
341     CHECK( s1 == s2 );
342 }
343 
344 
345 #ifdef CATCH_CONFIG_WCHAR
346 TEST_CASE( "toString on const wchar_t const pointer returns the string contents", "[toString]" ) {
347         const wchar_t * const s = L"wide load";
348         std::string result = ::Catch::Detail::stringify( s );
349         CHECK( result == "\"wide load\"" );
350 }
351 
352 TEST_CASE( "toString on const wchar_t pointer returns the string contents", "[toString]" ) {
353         const wchar_t * s = L"wide load";
354         std::string result = ::Catch::Detail::stringify( s );
355         CHECK( result == "\"wide load\"" );
356 }
357 
358 TEST_CASE( "toString on wchar_t const pointer returns the string contents", "[toString]" ) {
359         auto const s = const_cast<wchar_t*>( L"wide load" );
360         std::string result = ::Catch::Detail::stringify( s );
361         CHECK( result == "\"wide load\"" );
362 }
363 
364 TEST_CASE( "toString on wchar_t returns the string contents", "[toString]" ) {
365         auto s = const_cast<wchar_t*>( L"wide load" );
366         std::string result = ::Catch::Detail::stringify( s );
367         CHECK( result == "\"wide load\"" );
368 }
369 #endif
370 
371 TEST_CASE( "long long" ) {
372     long long l = std::numeric_limits<long long>::max();
373 
374     REQUIRE( l == std::numeric_limits<long long>::max() );
375 }
376 
377 //TEST_CASE( "Divide by Zero signal handler", "[.][sig]" ) {
378 //    int i = 0;
379 //    int x = 10/i; // This should cause the signal to fire
380 //    CHECK( x == 0 );
381 //}
382 
383 TEST_CASE( "This test 'should' fail but doesn't", "[.][failing][!shouldfail]" ) {
384     SUCCEED( "oops!" );
385 }
386 
387 TEST_CASE( "# A test name that starts with a #" ) {
388     SUCCEED( "yay" );
389 }
390 
391 TEST_CASE( "#835 -- errno should not be touched by Catch", "[.][failing][!shouldfail]" ) {
392     errno = 1;
393     CHECK(f() == 0);
394     REQUIRE(errno == 1); // Check that f() doesn't touch errno.
395 }
396 
397 TEST_CASE( "#961 -- Dynamically created sections should all be reported", "[.]" ) {
398     for (char i = '0'; i < '5'; ++i) {
399         SECTION(std::string("Looped section ") + i) {
400             SUCCEED( "Everything is OK" );
401         }
402     }
403 }
404 
405 TEST_CASE( "#1175 - Hidden Test", "[.]" ) {
406   // Just for checking that hidden test is not listed by default
407   SUCCEED();
408 }
409 
410 }} // namespace MiscTests
411