Blame gnulib-tests/test-stdbool.c

Packit Service fdd496
/* Test of <stdbool.h> substitute.
Packit Service fdd496
   Copyright (C) 2002-2007, 2009-2017 Free Software Foundation, Inc.
Packit Service fdd496
Packit Service fdd496
   This program is free software: you can redistribute it and/or modify
Packit Service fdd496
   it under the terms of the GNU General Public License as published by
Packit Service fdd496
   the Free Software Foundation; either version 3 of the License, or
Packit Service fdd496
   (at your option) any later version.
Packit Service fdd496
Packit Service fdd496
   This program is distributed in the hope that it will be useful,
Packit Service fdd496
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service fdd496
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service fdd496
   GNU General Public License for more details.
Packit Service fdd496
Packit Service fdd496
   You should have received a copy of the GNU General Public License
Packit Service fdd496
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit Service fdd496
Packit Service fdd496
/* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
Packit Service fdd496
Packit Service fdd496
/* We want this test to succeed even when using gcc's -Werror; but to
Packit Service fdd496
   do that requires a pragma that didn't exist before 4.3.0.  */
Packit Service fdd496
#ifndef __GNUC__
Packit Service fdd496
# define ADDRESS_CHECK_OKAY
Packit Service fdd496
#elif __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 3)
Packit Service fdd496
/* No way to silence -Waddress.  */
Packit Service fdd496
#else
Packit Service fdd496
# pragma GCC diagnostic ignored "-Waddress"
Packit Service fdd496
# define ADDRESS_CHECK_OKAY
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
#include <config.h>
Packit Service fdd496
Packit Service fdd496
#include <stdbool.h>
Packit Service fdd496
Packit Service fdd496
#ifndef bool
Packit Service fdd496
 "error: bool is not defined"
Packit Service fdd496
#endif
Packit Service fdd496
#ifndef false
Packit Service fdd496
 "error: false is not defined"
Packit Service fdd496
#endif
Packit Service fdd496
#if false
Packit Service fdd496
 "error: false is not 0"
Packit Service fdd496
#endif
Packit Service fdd496
#ifndef true
Packit Service fdd496
 "error: true is not defined"
Packit Service fdd496
#endif
Packit Service fdd496
#if true != 1
Packit Service fdd496
 "error: true is not 1"
Packit Service fdd496
#endif
Packit Service fdd496
#ifndef __bool_true_false_are_defined
Packit Service fdd496
 "error: __bool_true_false_are_defined is not defined"
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
/* Several tests cannot be guaranteed with gnulib's <stdbool.h>, at
Packit Service fdd496
   least, not for all compilers and compiler options.  */
Packit Service fdd496
#if HAVE_STDBOOL_H || 3 <= __GNUC__
Packit Service fdd496
struct s { _Bool s: 1; _Bool t; } s;
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
char a[true == 1 ? 1 : -1];
Packit Service fdd496
char b[false == 0 ? 1 : -1];
Packit Service fdd496
char c[__bool_true_false_are_defined == 1 ? 1 : -1];
Packit Service fdd496
#if HAVE_STDBOOL_H || 3 <= __GNUC__ /* See above.  */
Packit Service fdd496
char d[(bool) 0.5 == true ? 1 : -1];
Packit Service fdd496
# ifdef ADDRESS_CHECK_OKAY /* Avoid gcc warning.  */
Packit Service fdd496
/* C99 may plausibly be interpreted as not requiring support for a cast from
Packit Service fdd496
   a variable's address to bool in a static initializer.  So treat it like a
Packit Service fdd496
   GCC extension.  */
Packit Service fdd496
#  ifdef __GNUC__
Packit Service fdd496
bool e = &s;
Packit Service fdd496
#  endif
Packit Service fdd496
# endif
Packit Service fdd496
char f[(_Bool) 0.0 == false ? 1 : -1];
Packit Service fdd496
#endif
Packit Service fdd496
char g[true];
Packit Service fdd496
char h[sizeof (_Bool)];
Packit Service fdd496
#if HAVE_STDBOOL_H || 3 <= __GNUC__ /* See above.  */
Packit Service fdd496
char i[sizeof s.t];
Packit Service fdd496
#endif
Packit Service fdd496
enum { j = false, k = true, l = false * true, m = true * 256 };
Packit Service fdd496
_Bool n[m];
Packit Service fdd496
char o[sizeof n == m * sizeof n[0] ? 1 : -1];
Packit Service fdd496
char p[-1 - (_Bool) 0 < 0 && -1 - (bool) 0 < 0 ? 1 : -1];
Packit Service fdd496
/* Catch a bug in an HP-UX C compiler.  See
Packit Service fdd496
   http://gcc.gnu.org/ml/gcc-patches/2003-12/msg02303.html
Packit Service fdd496
   http://lists.gnu.org/archive/html/bug-coreutils/2005-11/msg00161.html
Packit Service fdd496
 */
Packit Service fdd496
_Bool q = true;
Packit Service fdd496
_Bool *pq = &q;
Packit Service fdd496
Packit Service fdd496
int
Packit Service fdd496
main ()
Packit Service fdd496
{
Packit Service fdd496
  int error = 0;
Packit Service fdd496
Packit Service fdd496
#if HAVE_STDBOOL_H || 3 <= __GNUC__ /* See above.  */
Packit Service fdd496
# ifdef ADDRESS_CHECK_OKAY /* Avoid gcc warning.  */
Packit Service fdd496
  /* A cast from a variable's address to bool is valid in expressions.  */
Packit Service fdd496
  {
Packit Service fdd496
    bool e1 = &s;
Packit Service fdd496
    if (!e1)
Packit Service fdd496
      error = 1;
Packit Service fdd496
  }
Packit Service fdd496
# endif
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
  /* Catch a bug in IBM AIX xlc compiler version 6.0.0.0
Packit Service fdd496
     reported by James Lemley on 2005-10-05; see
Packit Service fdd496
     http://lists.gnu.org/archive/html/bug-coreutils/2005-10/msg00086.html
Packit Service fdd496
     This is a runtime test, since a corresponding compile-time
Packit Service fdd496
     test would rely on initializer extensions.  */
Packit Service fdd496
  {
Packit Service fdd496
    char digs[] = "0123456789";
Packit Service fdd496
    if (&(digs + 5)[-2 + (bool) 1] != &digs[4])
Packit Service fdd496
      error = 1;
Packit Service fdd496
  }
Packit Service fdd496
Packit Service fdd496
  return error;
Packit Service fdd496
}