openat: don’t close (-1)
[gnulib.git] / tests / test-alignasof.c
blob12f695666431784bca5c757962325731078c49ac
1 /* Test of alignasof module.
2 Copyright 2009-2024 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Paul Eggert, inspired by Bruno Haible's test-alignof.c. */
19 #include <config.h>
21 #include <stddef.h>
22 #include <stdint.h>
24 #include "macros.h"
26 typedef long double longdouble;
27 typedef struct { char a[1]; } struct1;
28 typedef struct { char a[2]; } struct2;
29 typedef struct { char a[3]; } struct3;
30 typedef struct { char a[4]; } struct4;
32 #if (202311 <= __STDC_VERSION__ || __alignas_is_defined \
33 || 201103 <= __cplusplus)
34 /* mingw can go up only to 8. 8 is all that GNU Emacs needs, so let's
35 limit the test to 8 for now. */
36 # define TEST_ALIGNMENT 8
37 #else
38 # undef alignas
39 # define alignas(alignment)
40 # define TEST_ALIGNMENT 1
41 #endif
43 #define CHECK_STATIC(type) \
44 typedef struct { char slot1; type slot2; } type##_helper; \
45 static_assert (alignof (type) == offsetof (type##_helper, slot2)); \
46 const int type##_alignment = alignof (type); \
47 type alignas (TEST_ALIGNMENT) static_##type##_alignas
49 #define CHECK_ALIGNED(var) ASSERT ((uintptr_t) &(var) % TEST_ALIGNMENT == 0)
51 CHECK_STATIC (char);
52 CHECK_STATIC (short);
53 CHECK_STATIC (int);
54 CHECK_STATIC (long);
55 #ifdef INT64_MAX
56 CHECK_STATIC (int64_t);
57 #endif
58 CHECK_STATIC (float);
59 CHECK_STATIC (double);
60 /* CHECK_STATIC (longdouble); */
61 CHECK_STATIC (struct1);
62 CHECK_STATIC (struct2);
63 CHECK_STATIC (struct3);
64 CHECK_STATIC (struct4);
66 int
67 main ()
69 #if defined __SUNPRO_C && __SUNPRO_C < 0x5150
70 /* Avoid a test failure due to Sun Studio Developer Bug Report #2125432. */
71 fputs ("Skipping test: known Sun C compiler bug\n", stderr);
72 return 77;
73 #elif defined __HP_cc && __ia64
74 /* Avoid a test failure due to HP-UX Itanium cc bug; see:
75 https://lists.gnu.org/r/bug-gnulib/2017-03/msg00078.html */
76 fputs ("Skipping test: known HP-UX Itanium cc compiler bug\n", stderr);
77 return 77;
78 #elif defined __clang__ && defined __ibmxl__
79 /* Avoid a test failure with IBM xlc 16.1. It ignores alignas (8),
80 _Alignas (8), and __attribute__ ((__aligned__ (8))). */
81 fputs ("Skipping test: known AIX XL C compiler deficiency\n", stderr);
82 return 77;
83 #else
84 CHECK_ALIGNED (static_char_alignas);
85 CHECK_ALIGNED (static_short_alignas);
86 CHECK_ALIGNED (static_int_alignas);
87 CHECK_ALIGNED (static_long_alignas);
88 # ifdef INT64_MAX
89 CHECK_ALIGNED (static_int64_t_alignas);
90 # endif
91 CHECK_ALIGNED (static_float_alignas);
92 CHECK_ALIGNED (static_double_alignas);
93 /* CHECK_ALIGNED (static_longdouble_alignas); */
94 CHECK_ALIGNED (static_struct1_alignas);
95 CHECK_ALIGNED (static_struct2_alignas);
96 CHECK_ALIGNED (static_struct3_alignas);
97 CHECK_ALIGNED (static_struct4_alignas);
98 return test_exit_status;
99 #endif