Line data Source code
1 : /* Test of <stdbool.h> substitute.
2 : Copyright (C) 2002-2007, 2009-2020 Free Software Foundation, Inc.
3 :
4 : This program is free software: you can redistribute it and/or modify
5 : it under the terms of the GNU General Public License as published by
6 : the Free Software Foundation; either version 3 of the License, or
7 : (at your option) any later version.
8 :
9 : This program is distributed in the hope that it will be useful,
10 : but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : GNU General Public License for more details.
13 :
14 : You should have received a copy of the GNU General Public License
15 : along with this program. If not, see <https://www.gnu.org/licenses/>. */
16 :
17 : /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
18 :
19 : /* We want this test to succeed even when using gcc's -Werror; but to
20 : do that requires a pragma that didn't exist before 4.3.0. */
21 : #ifndef __GNUC__
22 : # define ADDRESS_CHECK_OKAY
23 : #elif __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 3)
24 : /* No way to silence -Waddress. */
25 : #else
26 : # pragma GCC diagnostic ignored "-Waddress"
27 : # define ADDRESS_CHECK_OKAY
28 : #endif
29 :
30 : #include <config.h>
31 :
32 : #include <stdbool.h>
33 :
34 : #ifndef bool
35 : "error: bool is not defined"
36 : #endif
37 : #ifndef false
38 : "error: false is not defined"
39 : #endif
40 : #if false
41 : "error: false is not 0"
42 : #endif
43 : #ifndef true
44 : "error: true is not defined"
45 : #endif
46 : #if true != 1
47 : "error: true is not 1"
48 : #endif
49 : #ifndef __bool_true_false_are_defined
50 : "error: __bool_true_false_are_defined is not defined"
51 : #endif
52 :
53 : /* Several tests cannot be guaranteed with gnulib's <stdbool.h>, at
54 : least, not for all compilers and compiler options. */
55 : #if HAVE_STDBOOL_H || 3 <= __GNUC__
56 : struct s { _Bool s: 1; _Bool t; } s;
57 : #endif
58 :
59 : char a[true == 1 ? 1 : -1];
60 : char b[false == 0 ? 1 : -1];
61 : char c[__bool_true_false_are_defined == 1 ? 1 : -1];
62 : #if HAVE_STDBOOL_H || 3 <= __GNUC__ /* See above. */
63 : char d[(bool) 0.5 == true ? 1 : -1];
64 : # ifdef ADDRESS_CHECK_OKAY /* Avoid gcc warning. */
65 : /* C99 may plausibly be interpreted as not requiring support for a cast from
66 : a variable's address to bool in a static initializer. So treat it like a
67 : GCC extension. */
68 : # ifdef __GNUC__
69 : bool e = &s;
70 : # endif
71 : # endif
72 : char f[(_Bool) 0.0 == false ? 1 : -1];
73 : #endif
74 : char g[true];
75 : char h[sizeof (_Bool)];
76 : #if HAVE_STDBOOL_H || 3 <= __GNUC__ /* See above. */
77 : char i[sizeof s.t];
78 : #endif
79 : enum { j = false, k = true, l = false * true, m = true * 256 };
80 : _Bool n[m];
81 : char o[sizeof n == m * sizeof n[0] ? 1 : -1];
82 : char p[-1 - (_Bool) 0 < 0 && -1 - (bool) 0 < 0 ? 1 : -1];
83 : /* Catch a bug in an HP-UX C compiler. See
84 : https://gcc.gnu.org/ml/gcc-patches/2003-12/msg02303.html
85 : https://lists.gnu.org/r/bug-coreutils/2005-11/msg00161.html
86 : */
87 : _Bool q = true;
88 : _Bool *pq = &q;
89 :
90 : int
91 1 : main ()
92 : {
93 1 : int error = 0;
94 :
95 : #if HAVE_STDBOOL_H || 3 <= __GNUC__ /* See above. */
96 : # ifdef ADDRESS_CHECK_OKAY /* Avoid gcc warning. */
97 : /* A cast from a variable's address to bool is valid in expressions. */
98 : {
99 1 : bool e1 = &s;
100 1 : if (!e1)
101 0 : error = 1;
102 : }
103 : # endif
104 : #endif
105 :
106 : /* Catch a bug in IBM AIX xlc compiler version 6.0.0.0
107 : reported by James Lemley on 2005-10-05; see
108 : https://lists.gnu.org/r/bug-coreutils/2005-10/msg00086.html
109 : This is a runtime test, since a corresponding compile-time
110 : test would rely on initializer extensions. */
111 : {
112 1 : char digs[] = "0123456789";
113 : if (&(digs + 5)[-2 + (bool) 1] != &digs[4])
114 : error = 1;
115 : }
116 :
117 1 : return error;
118 : }
|