Blame gnulib-tests/test-stdalign.c

Packit 33f14e
/* Test of <stdalign.h>.
Packit 33f14e
   Copyright 2009-2017 Free Software Foundation, Inc.
Packit 33f14e
Packit 33f14e
   This program is free software: you can redistribute it and/or modify
Packit 33f14e
   it under the terms of the GNU General Public License as published by
Packit 33f14e
   the Free Software Foundation; either version 3 of the License, or
Packit 33f14e
   (at your option) any later version.
Packit 33f14e
Packit 33f14e
   This program is distributed in the hope that it will be useful,
Packit 33f14e
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 33f14e
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 33f14e
   GNU General Public License for more details.
Packit 33f14e
Packit 33f14e
   You should have received a copy of the GNU General Public License
Packit 33f14e
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 33f14e
Packit 33f14e
/* Written by Paul Eggert, inspired by Bruno Haible's test-alignof.c.  */
Packit 33f14e
Packit 33f14e
#include <config.h>
Packit 33f14e
Packit 33f14e
#include <stdalign.h>
Packit 33f14e
Packit 33f14e
#include <stddef.h>
Packit 33f14e
#include <stdint.h>
Packit 33f14e
Packit 33f14e
#include "verify.h"
Packit 33f14e
Packit 33f14e
#include "macros.h"
Packit 33f14e
Packit 33f14e
typedef long double longdouble;
Packit 33f14e
typedef struct { char a[1]; } struct1;
Packit 33f14e
typedef struct { char a[2]; } struct2;
Packit 33f14e
typedef struct { char a[3]; } struct3;
Packit 33f14e
typedef struct { char a[4]; } struct4;
Packit 33f14e
Packit 33f14e
verify (__alignof_is_defined == 1);
Packit 33f14e
#ifndef alignof
Packit 33f14e
# error "alignof is not a macro"
Packit 33f14e
#endif
Packit 33f14e
Packit 33f14e
#if __alignas_is_defined
Packit 33f14e
verify (__alignas_is_defined == 1);
Packit 33f14e
# ifndef alignas
Packit 33f14e
#  error "alignas is not a macro"
Packit 33f14e
# endif
Packit 33f14e
/* mingw can go up only to 8.  8 is all that GNU Emacs needs, so let's
Packit 33f14e
   limit the test to 8 for now.  */
Packit 33f14e
# define TEST_ALIGNMENT 8
Packit 33f14e
#else
Packit 33f14e
# define _Alignas(alignment)
Packit 33f14e
# define alignas(alignment)
Packit 33f14e
# define TEST_ALIGNMENT 1
Packit 33f14e
#endif
Packit 33f14e
Packit 33f14e
#define CHECK_STATIC(type) \
Packit 33f14e
  typedef struct { char slot1; type slot2; } type##_helper; \
Packit 33f14e
  verify (alignof (type) == offsetof (type##_helper, slot2)); \
Packit 33f14e
  verify (_Alignof (type) == alignof (type)); \
Packit 33f14e
  const int type##_alignment = alignof (type); \
Packit 33f14e
  type alignas (TEST_ALIGNMENT) static_##type##_alignas; \
Packit 33f14e
  type _Alignas (TEST_ALIGNMENT) static_##type##_Alignas
Packit 33f14e
Packit 33f14e
#define CHECK_ALIGNED(var) ASSERT ((uintptr_t) &(var) % TEST_ALIGNMENT == 0)
Packit 33f14e
Packit 33f14e
CHECK_STATIC (char);
Packit 33f14e
CHECK_STATIC (short);
Packit 33f14e
CHECK_STATIC (int);
Packit 33f14e
CHECK_STATIC (long);
Packit 33f14e
#ifdef INT64_MAX
Packit 33f14e
CHECK_STATIC (int64_t);
Packit 33f14e
#endif
Packit 33f14e
CHECK_STATIC (float);
Packit 33f14e
CHECK_STATIC (double);
Packit 33f14e
/* CHECK_STATIC (longdouble); */
Packit 33f14e
CHECK_STATIC (struct1);
Packit 33f14e
CHECK_STATIC (struct2);
Packit 33f14e
CHECK_STATIC (struct3);
Packit 33f14e
CHECK_STATIC (struct4);
Packit 33f14e
Packit 33f14e
int
Packit 33f14e
main ()
Packit 33f14e
{
Packit 33f14e
#if defined __SUNPRO_C
Packit 33f14e
  /* Avoid a test failure due to Sun Studio Developer Bug Report #2125432.  */
Packit 33f14e
  fputs ("Skipping test: known Sun C compiler bug\n", stderr);
Packit 33f14e
  return 77;
Packit 33f14e
#elif defined __HP_cc && __ia64
Packit 33f14e
  /* Avoid a test failure due to HP-UX Itanium cc bug; see:
Packit 33f14e
     http://lists.gnu.org/archive/html/bug-gnulib/2017-03/msg00078.html  */
Packit 33f14e
  fputs ("Skipping test: known HP-UX Itanium cc compiler bug\n", stderr);
Packit 33f14e
  return 77;
Packit 33f14e
#else
Packit 33f14e
  CHECK_ALIGNED (static_char_alignas);
Packit 33f14e
  CHECK_ALIGNED (static_char_Alignas);
Packit 33f14e
  CHECK_ALIGNED (static_short_alignas);
Packit 33f14e
  CHECK_ALIGNED (static_short_Alignas);
Packit 33f14e
  CHECK_ALIGNED (static_int_alignas);
Packit 33f14e
  CHECK_ALIGNED (static_int_Alignas);
Packit 33f14e
  CHECK_ALIGNED (static_long_alignas);
Packit 33f14e
  CHECK_ALIGNED (static_long_Alignas);
Packit 33f14e
# ifdef INT64_MAX
Packit 33f14e
  CHECK_ALIGNED (static_int64_t_alignas);
Packit 33f14e
  CHECK_ALIGNED (static_int64_t_Alignas);
Packit 33f14e
# endif
Packit 33f14e
  CHECK_ALIGNED (static_float_alignas);
Packit 33f14e
  CHECK_ALIGNED (static_float_Alignas);
Packit 33f14e
  CHECK_ALIGNED (static_double_alignas);
Packit 33f14e
  CHECK_ALIGNED (static_double_Alignas);
Packit 33f14e
  /* CHECK_ALIGNED (static_longdouble_alignas); */
Packit 33f14e
  /* CHECK_ALIGNED (static_longdouble_Alignas); */
Packit 33f14e
  CHECK_ALIGNED (static_struct1_alignas);
Packit 33f14e
  CHECK_ALIGNED (static_struct1_Alignas);
Packit 33f14e
  CHECK_ALIGNED (static_struct2_alignas);
Packit 33f14e
  CHECK_ALIGNED (static_struct2_Alignas);
Packit 33f14e
  CHECK_ALIGNED (static_struct3_alignas);
Packit 33f14e
  CHECK_ALIGNED (static_struct3_Alignas);
Packit 33f14e
  CHECK_ALIGNED (static_struct4_alignas);
Packit 33f14e
  CHECK_ALIGNED (static_struct4_Alignas);
Packit 33f14e
  return 0;
Packit 33f14e
#endif
Packit 33f14e
}