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