xref: /minix3/external/bsd/atf/dist/atf-c++/macros_hpp_test.cpp (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc //
2*11be35a1SLionel Sambuc // Automated Testing Framework (atf)
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Copyright (c) 2008 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc // All rights reserved.
6*11be35a1SLionel Sambuc //
7*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc // are met:
10*11be35a1SLionel Sambuc // 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc //    notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc // 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc //    notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc //    documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc //
16*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*11be35a1SLionel Sambuc // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*11be35a1SLionel Sambuc // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*11be35a1SLionel Sambuc // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*11be35a1SLionel Sambuc // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*11be35a1SLionel Sambuc // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*11be35a1SLionel Sambuc // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*11be35a1SLionel Sambuc // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*11be35a1SLionel Sambuc // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*11be35a1SLionel Sambuc // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*11be35a1SLionel Sambuc // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*11be35a1SLionel Sambuc // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc //
29*11be35a1SLionel Sambuc 
30*11be35a1SLionel Sambuc #include <stdexcept>
31*11be35a1SLionel Sambuc 
32*11be35a1SLionel Sambuc #include <atf-c++/macros.hpp>
33*11be35a1SLionel Sambuc 
34*11be35a1SLionel Sambuc void
atf_check_errno_semicolons(void)35*11be35a1SLionel Sambuc atf_check_errno_semicolons(void)
36*11be35a1SLionel Sambuc {
37*11be35a1SLionel Sambuc     // Check that ATF_CHECK_ERRNO does not contain a semicolon that would
38*11be35a1SLionel Sambuc     // cause an empty-statement that confuses some compilers.
39*11be35a1SLionel Sambuc     ATF_CHECK_ERRNO(1, 1 == 1);
40*11be35a1SLionel Sambuc     ATF_CHECK_ERRNO(2, 2 == 2);
41*11be35a1SLionel Sambuc }
42*11be35a1SLionel Sambuc 
43*11be35a1SLionel Sambuc void
atf_require_inside_if(void)44*11be35a1SLionel Sambuc atf_require_inside_if(void)
45*11be35a1SLionel Sambuc {
46*11be35a1SLionel Sambuc     // Make sure that ATF_REQUIRE can be used inside an if statement that
47*11be35a1SLionel Sambuc     // does not have braces.  Earlier versions of it generated an error
48*11be35a1SLionel Sambuc     // if there was an else clause because they confused the compiler
49*11be35a1SLionel Sambuc     // by defining an unprotected nested if.
50*11be35a1SLionel Sambuc     if (true)
51*11be35a1SLionel Sambuc         ATF_REQUIRE(true);
52*11be35a1SLionel Sambuc     else
53*11be35a1SLionel Sambuc         ATF_REQUIRE(true);
54*11be35a1SLionel Sambuc }
55*11be35a1SLionel Sambuc 
56*11be35a1SLionel Sambuc void
atf_require_eq_inside_if(void)57*11be35a1SLionel Sambuc atf_require_eq_inside_if(void)
58*11be35a1SLionel Sambuc {
59*11be35a1SLionel Sambuc     // Make sure that ATF_REQUIRE_EQ can be used inside an if statement
60*11be35a1SLionel Sambuc     // that does not have braces.  Earlier versions of it generated an
61*11be35a1SLionel Sambuc     // error if there was an else clause because they confused the
62*11be35a1SLionel Sambuc     // compiler by defining an unprotected nested if.
63*11be35a1SLionel Sambuc     if (true)
64*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(true, true);
65*11be35a1SLionel Sambuc     else
66*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(true, true);
67*11be35a1SLionel Sambuc }
68*11be35a1SLionel Sambuc 
69*11be35a1SLionel Sambuc void
atf_require_throw_runtime_error(void)70*11be35a1SLionel Sambuc atf_require_throw_runtime_error(void)
71*11be35a1SLionel Sambuc {
72*11be35a1SLionel Sambuc     // Check that we can pass std::runtime_error to ATF_REQUIRE_THROW.
73*11be35a1SLionel Sambuc     // Earlier versions generated a warning because the macro's code also
74*11be35a1SLionel Sambuc     // attempted to capture this exception, and thus we had a duplicate
75*11be35a1SLionel Sambuc     // catch clause.
76*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW(std::runtime_error, (void)0);
77*11be35a1SLionel Sambuc }
78*11be35a1SLionel Sambuc 
79*11be35a1SLionel Sambuc void
atf_require_throw_inside_if(void)80*11be35a1SLionel Sambuc atf_require_throw_inside_if(void)
81*11be35a1SLionel Sambuc {
82*11be35a1SLionel Sambuc     // Make sure that ATF_REQUIRE_THROW can be used inside an if statement
83*11be35a1SLionel Sambuc     // that does not have braces.  Earlier versions of it generated an
84*11be35a1SLionel Sambuc     // error because a trailing ; after a catch block was not allowed.
85*11be35a1SLionel Sambuc     if (true)
86*11be35a1SLionel Sambuc         ATF_REQUIRE_THROW(std::runtime_error, (void)0);
87*11be35a1SLionel Sambuc     else
88*11be35a1SLionel Sambuc         ATF_REQUIRE_THROW(std::runtime_error, (void)1);
89*11be35a1SLionel Sambuc }
90*11be35a1SLionel Sambuc 
91*11be35a1SLionel Sambuc void
atf_require_errno_semicolons(void)92*11be35a1SLionel Sambuc atf_require_errno_semicolons(void)
93*11be35a1SLionel Sambuc {
94*11be35a1SLionel Sambuc     // Check that ATF_REQUIRE_ERRNO does not contain a semicolon that would
95*11be35a1SLionel Sambuc     // cause an empty-statement that confuses some compilers.
96*11be35a1SLionel Sambuc     ATF_REQUIRE_ERRNO(1, 1 == 1);
97*11be35a1SLionel Sambuc     ATF_REQUIRE_ERRNO(2, 2 == 2);
98*11be35a1SLionel Sambuc }
99*11be35a1SLionel Sambuc 
100*11be35a1SLionel Sambuc // Test case names should not be expanded during instatiation so that they
101*11be35a1SLionel Sambuc // can have the exact same name as macros.
102*11be35a1SLionel Sambuc #define TEST_MACRO_1 invalid + name
103*11be35a1SLionel Sambuc #define TEST_MACRO_2 invalid + name
104*11be35a1SLionel Sambuc #define TEST_MACRO_3 invalid + name
105*11be35a1SLionel Sambuc ATF_TEST_CASE(TEST_MACRO_1);
ATF_TEST_CASE_HEAD(TEST_MACRO_1)106*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(TEST_MACRO_1) { }
ATF_TEST_CASE_BODY(TEST_MACRO_1)107*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(TEST_MACRO_1) { }
instantiate_1(void)108*11be35a1SLionel Sambuc void instantiate_1(void) {
109*11be35a1SLionel Sambuc     ATF_TEST_CASE_USE(TEST_MACRO_1);
110*11be35a1SLionel Sambuc     atf::tests::tc* the_test = new ATF_TEST_CASE_NAME(TEST_MACRO_1)();
111*11be35a1SLionel Sambuc     delete the_test;
112*11be35a1SLionel Sambuc }
113*11be35a1SLionel Sambuc ATF_TEST_CASE_WITH_CLEANUP(TEST_MACRO_2);
ATF_TEST_CASE_HEAD(TEST_MACRO_2)114*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(TEST_MACRO_2) { }
ATF_TEST_CASE_BODY(TEST_MACRO_2)115*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(TEST_MACRO_2) { }
ATF_TEST_CASE_CLEANUP(TEST_MACRO_2)116*11be35a1SLionel Sambuc ATF_TEST_CASE_CLEANUP(TEST_MACRO_2) { }
instatiate_2(void)117*11be35a1SLionel Sambuc void instatiate_2(void) {
118*11be35a1SLionel Sambuc     ATF_TEST_CASE_USE(TEST_MACRO_2);
119*11be35a1SLionel Sambuc     atf::tests::tc* the_test = new ATF_TEST_CASE_NAME(TEST_MACRO_2)();
120*11be35a1SLionel Sambuc     delete the_test;
121*11be35a1SLionel Sambuc }
122*11be35a1SLionel Sambuc ATF_TEST_CASE_WITH_CLEANUP(TEST_MACRO_3);
ATF_TEST_CASE_HEAD(TEST_MACRO_3)123*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(TEST_MACRO_3) { }
ATF_TEST_CASE_BODY(TEST_MACRO_3)124*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(TEST_MACRO_3) { }
ATF_TEST_CASE_CLEANUP(TEST_MACRO_3)125*11be35a1SLionel Sambuc ATF_TEST_CASE_CLEANUP(TEST_MACRO_3) { }
instatiate_3(void)126*11be35a1SLionel Sambuc void instatiate_3(void) {
127*11be35a1SLionel Sambuc     ATF_TEST_CASE_USE(TEST_MACRO_3);
128*11be35a1SLionel Sambuc     atf::tests::tc* the_test = new ATF_TEST_CASE_NAME(TEST_MACRO_3)();
129*11be35a1SLionel Sambuc     delete the_test;
130*11be35a1SLionel Sambuc }
131