xref: /freebsd/contrib/atf/atf-c/macros_h_test.c (revision fd5e3f3ec6c6248e892c9e7b2f17da3bfe7b6837)
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.h>
27 
28 void atf_require_inside_if(void);
29 void atf_require_equal_inside_if(void);
30 void atf_check_errno_semicolons(void);
31 void atf_require_errno_semicolons(void);
32 
33 void
34 atf_require_inside_if(void)
35 {
36     /* Make sure that ATF_REQUIRE can be used inside an if statement that
37      * does not have braces.  Earlier versions of it generated an error
38      * if there was an else clause because they confused the compiler
39      * by defining an unprotected nested if. */
40     if (true)
41         ATF_REQUIRE(true);
42     else
43         ATF_REQUIRE(true);
44 }
45 
46 void
47 atf_require_equal_inside_if(void)
48 {
49     /* Make sure that ATF_REQUIRE_EQUAL can be used inside an if statement
50      * that does not have braces.  Earlier versions of it generated an
51      * error if there was an else clause because they confused the
52      * compiler by defining an unprotected nested if. */
53     if (true)
54         ATF_REQUIRE_EQ(true, true);
55     else
56         ATF_REQUIRE_EQ(true, true);
57 }
58 
59 void
60 atf_check_errno_semicolons(void)
61 {
62     /* Check that ATF_CHECK_ERRNO does not contain a semicolon that would
63      * cause an empty-statement that confuses some compilers. */
64     ATF_CHECK_ERRNO(1, 1 == 1);
65     ATF_CHECK_ERRNO(2, 2 == 2);
66 }
67 
68 void
69 atf_require_errno_semicolons(void)
70 {
71     /* Check that ATF_REQUIRE_ERRNO does not contain a semicolon that would
72      * cause an empty-statement that confuses some compilers. */
73     ATF_REQUIRE_ERRNO(1, 1 == 1);
74     ATF_REQUIRE_ERRNO(2, 2 == 2);
75 }
76 
77 /* Test case names should not be expanded during instatiation so that they
78  * can have the exact same name as macros. */
79 #define TEST_MACRO_1 invalid + name
80 #define TEST_MACRO_2 invalid + name
81 #define TEST_MACRO_3 invalid + name
82 ATF_TC(TEST_MACRO_1);
83 ATF_TC_HEAD(TEST_MACRO_1, tc) { if (tc != NULL) {} }
84 ATF_TC_BODY(TEST_MACRO_1, tc) { if (tc != NULL) {} }
85 atf_tc_t *test_name_1 = &ATF_TC_NAME(TEST_MACRO_1);
86 atf_tc_pack_t *test_pack_1 = &ATF_TC_PACK_NAME(TEST_MACRO_1);
87 void (*head_1)(atf_tc_t *) = ATF_TC_HEAD_NAME(TEST_MACRO_1);
88 void (*body_1)(const atf_tc_t *) = ATF_TC_BODY_NAME(TEST_MACRO_1);
89 ATF_TC_WITH_CLEANUP(TEST_MACRO_2);
90 ATF_TC_HEAD(TEST_MACRO_2, tc) { if (tc != NULL) {} }
91 ATF_TC_BODY(TEST_MACRO_2, tc) { if (tc != NULL) {} }
92 ATF_TC_CLEANUP(TEST_MACRO_2, tc) { if (tc != NULL) {} }
93 atf_tc_t *test_name_2 = &ATF_TC_NAME(TEST_MACRO_2);
94 atf_tc_pack_t *test_pack_2 = &ATF_TC_PACK_NAME(TEST_MACRO_2);
95 void (*head_2)(atf_tc_t *) = ATF_TC_HEAD_NAME(TEST_MACRO_2);
96 void (*body_2)(const atf_tc_t *) = ATF_TC_BODY_NAME(TEST_MACRO_2);
97 void (*cleanup_2)(const atf_tc_t *) = ATF_TC_CLEANUP_NAME(TEST_MACRO_2);
98 ATF_TC_WITHOUT_HEAD(TEST_MACRO_3);
99 ATF_TC_BODY(TEST_MACRO_3, tc) { if (tc != NULL) {} }
100 atf_tc_t *test_name_3 = &ATF_TC_NAME(TEST_MACRO_3);
101 atf_tc_pack_t *test_pack_3 = &ATF_TC_PACK_NAME(TEST_MACRO_3);
102 void (*body_3)(const atf_tc_t *) = ATF_TC_BODY_NAME(TEST_MACRO_3);
103