Blame gnulib-tests/test-stdbool.c

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