]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // (C) Copyright Gennadiy Rozental 2001-2015. |
2 | // Distributed under the Boost Software License, Version 1.0. | |
3 | // (See accompanying file LICENSE_1_0.txt or copy at | |
4 | // http://www.boost.org/LICENSE_1_0.txt) | |
5 | // | |
6 | // See http://www.boost.org/libs/test for the library home page. | |
7 | // | |
8 | ||
9 | //[snippet12 | |
10 | #define __BOOST_TEST_MODULE__ MyTest | |
11 | #include <boost/test/unit_test.hpp> | |
12 | ||
13 | int add( int i, int j ) { return i + j; } | |
14 | ||
15 | __BOOST_AUTO_TEST_CASE__(my_test) | |
16 | { | |
17 | // six ways to detect and report the same error: | |
18 | ||
19 | // continues on error | |
20 | __BOOST_TEST__( add(2, 2) == 4 ); /*< | |
21 | This approach uses tool __BOOST_TEST__, which displays an error message (by default on `std::cout`) that includes | |
22 | the expression that failed, as well as the values on the two side of the equation, the source file name, | |
23 | and the source file line number. It also increments the error count. At program termination, | |
24 | the error count will be displayed automatically by the __UTF__.>*/ | |
25 | ||
26 | // throws on error | |
27 | __BOOST_TEST_REQUIRE__( add(2, 2) == 4 ); /*< | |
28 | This approach uses tool __BOOST_TEST_REQUIRE__, is similar to approach #1, except that after displaying the error, | |
29 | an exception is thrown, to be caught by the __UTF__. This approach is suitable when writing an | |
30 | explicit test program, and the error would be so severe as to make further testing impractical. | |
31 | >*/ | |
32 | ||
33 | //continues on error | |
34 | if (add(2, 2) != 4) | |
35 | __BOOST_ERROR__( "Ouch..." ); /*< | |
36 | This approach is similar to approach #1, except that the error detection and error reporting are coded separately. | |
37 | This is most useful when the specific condition being tested requires several independent statements and/or is | |
38 | not indicative of the reason for failure. | |
39 | >*/ | |
40 | ||
41 | // throws on error | |
42 | if (add(2, 2) != 4) | |
43 | __BOOST_FAIL__( "Ouch..." ); /*< | |
44 | This approach is similar to approach #2, except that the error detection and error reporting are coded separately. | |
45 | This is most useful when the specific condition being tested requires several independent statements and/or is | |
46 | not indicative of the reason for failure. | |
47 | >*/ | |
48 | ||
49 | // throws on error | |
50 | if (add(2, 2) != 4) | |
51 | throw "Ouch..."; /*< | |
52 | This approach throws an exception, which will be caught and reported by the __UTF__. The error | |
53 | message displayed when the exception is caught will be most meaningful if the exception is derived from | |
54 | `std::exception`, or is a `char*` or `std::string`. | |
55 | >*/ | |
56 | ||
57 | // continues on error | |
58 | __BOOST_TEST__( add(2, 2) == 4, /*< | |
59 | This approach uses tool __BOOST_TEST__ with additional message argument, is similar to approach #1, | |
60 | except that similar to the approach #3 displays an alternative error message specified as a second argument. | |
61 | >*/ | |
62 | "2 plus 2 is not 4 but " << add(2, 2)); | |
63 | } | |
64 | //] |