stdbool: avoid rejecting clang
[gnulib.git] / tests / test-stdbool.c
1 /* Test of <stdbool.h> substitute.
2    Copyright (C) 2002-2007, 2009-2010 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 <http://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 || defined __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 || defined __GNUC__ /* See above.  */
63 char d[(bool) 0.5 == true ? 1 : -1];
64 # ifdef ADDRESS_CHECK_OKAY
65 bool e = &s;
66 # endif
67 char f[(_Bool) 0.0 == false ? 1 : -1];
68 #endif
69 char g[true];
70 char h[sizeof (_Bool)];
71 #if HAVE_STDBOOL_H || defined __GNUC__ /* See above.  */
72 char i[sizeof s.t];
73 #endif
74 enum { j = false, k = true, l = false * true, m = true * 256 };
75 _Bool n[m];
76 char o[sizeof n == m * sizeof n[0] ? 1 : -1];
77 char p[-1 - (_Bool) 0 < 0 && -1 - (bool) 0 < 0 ? 1 : -1];
78 /* Catch a bug in an HP-UX C compiler.  See
79    http://gcc.gnu.org/ml/gcc-patches/2003-12/msg02303.html
80    http://lists.gnu.org/archive/html/bug-coreutils/2005-11/msg00161.html
81  */
82 _Bool q = true;
83 _Bool *pq = &q;
84
85 int
86 main ()
87 {
88   /* Catch a bug in IBM AIX xlc compiler version 6.0.0.0
89      reported by James Lemley on 2005-10-05; see
90      http://lists.gnu.org/archive/html/bug-coreutils/2005-10/msg00086.html
91      This is a runtime test, since a corresponding compile-time
92      test would rely on initializer extensions.  */
93   char digs[] = "0123456789";
94   return &(digs + 5)[-2 + (bool) 1] != &digs[4];
95 }