summaryrefslogtreecommitdiffstats
path: root/gnulib-tests/test-stdbool.c
blob: 3574a9e72fc063c0d281db4605a45fc90ff3914c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* Test of <stdbool.h> substitute.
   Copyright (C) 2002-2007, 2009-2022 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

/* Written by Bruno Haible <bruno@clisp.org>, 2007.  */

/* Define ADDRESS_CHECK_OKAY if it is OK to assign an address to a 'bool'
   and this does not generate a warning (because we want this test to succeed
   even when using gcc's -Werror).  */
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) \
    || (__clang_major__ >= 4)
/* We can silence the warning.  */
# pragma GCC diagnostic ignored "-Waddress"
# define ADDRESS_CHECK_OKAY
#elif defined __GNUC__ || defined __clang__
/* There may be a warning.  */
#else
/* Ignore warnings from other compilers.  */
# define ADDRESS_CHECK_OKAY
#endif

#include <config.h>

#include <stdbool.h>

#ifndef bool
 "error: bool is not defined"
#endif
#ifndef false
 "error: false is not defined"
#endif
#if false
 "error: false is not 0"
#endif
#ifndef true
 "error: true is not defined"
#endif
#if true != 1
 "error: true is not 1"
#endif
#ifndef __bool_true_false_are_defined
 "error: __bool_true_false_are_defined is not defined"
#endif

/* Several tests cannot be guaranteed with gnulib's <stdbool.h>, at
   least, not for all compilers and compiler options.  */
#if HAVE_STDBOOL_H || 3 <= __GNUC__ || 4 <= __clang_major__
struct s { _Bool s: 1; _Bool t; } s;
#endif

char a[true == 1 ? 1 : -1];
char b[false == 0 ? 1 : -1];
char c[__bool_true_false_are_defined == 1 ? 1 : -1];
#if HAVE_STDBOOL_H || 3 <= __GNUC__ || 4 <= __clang_major__ /* See above.  */
char d[(bool) 0.5 == true ? 1 : -1];
# ifdef ADDRESS_CHECK_OKAY /* Avoid gcc warning.  */
/* C99 may plausibly be interpreted as not requiring support for a cast from
   a variable's address to bool in a static initializer.  So treat it like a
   GCC extension.  */
#  if defined __GNUC__ || defined __clang__
bool e = &s;
#  endif
# endif
char f[(_Bool) 0.0 == false ? 1 : -1];
#endif
char g[true];
char h[sizeof (_Bool)];
#if HAVE_STDBOOL_H || 3 <= __GNUC__ || 4 <= __clang_major__ /* See above.  */
char i[sizeof s.t];
#endif
enum { j = false, k = true, l = false * true, m = true * 256 };
_Bool n[m];
char o[sizeof n == m * sizeof n[0] ? 1 : -1];
char p[-1 - (_Bool) 0 < 0 && -1 - (bool) 0 < 0 ? 1 : -1];
/* Catch a bug in an HP-UX C compiler.  See
   https://gcc.gnu.org/ml/gcc-patches/2003-12/msg02303.html
   https://lists.gnu.org/r/bug-coreutils/2005-11/msg00161.html
 */
_Bool q = true;
_Bool *pq = &q;

int
main ()
{
  int error = 0;

#if HAVE_STDBOOL_H || 3 <= __GNUC_ || 4 <= __clang_major___ /* See above.  */
# ifdef ADDRESS_CHECK_OKAY /* Avoid gcc warning.  */
  /* A cast from a variable's address to bool is valid in expressions.  */
  {
    bool e1 = &s;
    if (!e1)
      error = 1;
  }
# endif
#endif

  /* Catch a bug in IBM AIX xlc compiler version 6.0.0.0
     reported by James Lemley on 2005-10-05; see
     https://lists.gnu.org/r/bug-coreutils/2005-10/msg00086.html
     This is a runtime test, since a corresponding compile-time
     test would rely on initializer extensions.  */
  {
    char digs[] = "0123456789";
    if (&(digs + 5)[-2 + (bool) 1] != &digs[4])
      error = 1;
  }

  return error;
}