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