openat: don’t close (-1)
[gnulib.git] / tests / test-stddef.c
blob5bceb18a29150363fd58b4a25f69f67907ce3cae
1 /* Test of <stddef.h> substitute.
2 Copyright (C) 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 Eric Blake <ebb9@byu.net>, 2009. */
19 #include <config.h>
21 #include <stddef.h>
23 /* Check that appropriate types are defined. */
24 wchar_t a = 'c';
25 ptrdiff_t b = 1;
26 size_t c = 2;
27 max_align_t mat;
29 /* Check that NULL can be passed through varargs as a pointer type,
30 per POSIX 2008. */
31 static_assert (sizeof NULL == sizeof (void *));
33 /* Check that offsetof produces integer constants with correct type. */
34 struct d
36 char e;
37 char f;
39 /* Solaris 10 has a bug where offsetof is under-parenthesized, and
40 cannot be used as an arbitrary expression. However, since it is
41 unlikely to bite real code, we ignore that short-coming. */
42 /* static_assert (sizeof offsetof (struct d, e) == sizeof (size_t)); */
43 static_assert (sizeof (offsetof (struct d, e)) == sizeof (size_t));
44 static_assert (offsetof (struct d, f) == 1);
46 /* Check max_align_t's alignment. */
47 static_assert (alignof (double) <= alignof (max_align_t));
48 static_assert (alignof (int) <= alignof (max_align_t));
49 static_assert (alignof (long double) <= alignof (max_align_t));
50 static_assert (alignof (long int) <= alignof (max_align_t));
51 static_assert (alignof (ptrdiff_t) <= alignof (max_align_t));
52 static_assert (alignof (size_t) <= alignof (max_align_t));
53 static_assert (alignof (wchar_t) <= alignof (max_align_t));
54 static_assert (alignof (struct d) <= alignof (max_align_t));
55 #if defined __GNUC__ || defined __clang__ || defined __IBM__ALIGNOF__
56 static_assert (__alignof__ (double) <= __alignof__ (max_align_t));
57 static_assert (__alignof__ (int) <= __alignof__ (max_align_t));
58 static_assert (__alignof__ (long double) <= __alignof__ (max_align_t));
59 static_assert (__alignof__ (long int) <= __alignof__ (max_align_t));
60 static_assert (__alignof__ (ptrdiff_t) <= __alignof__ (max_align_t));
61 static_assert (__alignof__ (size_t) <= __alignof__ (max_align_t));
62 static_assert (__alignof__ (wchar_t) <= __alignof__ (max_align_t));
63 static_assert (__alignof__ (struct d) <= __alignof__ (max_align_t));
64 #endif
66 int test_unreachable_optimization (int x);
67 _Noreturn void test_unreachable_noreturn (void);
69 int
70 test_unreachable_optimization (int x)
72 /* Check that the compiler uses 'unreachable' for optimization.
73 This function, when compiled with optimization, should have code
74 equivalent to
75 return x + 3;
76 Use 'objdump --disassemble test-stddef.o' to verify this. */
77 if (x < 4)
78 unreachable ();
79 return (x > 1 ? x + 3 : 2 * x + 10);
82 _Noreturn void
83 test_unreachable_noreturn (void)
85 /* Check that the compiler's data-flow analysis recognizes 'unreachable ()'.
86 This function should not elicit a warning. */
87 unreachable ();
90 #include <limits.h> /* INT_MAX */
92 /* offsetof promotes to an unsigned integer if and only if sizes do
93 not fit in int. */
94 static_assert ((offsetof (struct d, e) < -1) == (INT_MAX < (size_t) -1));
96 int
97 main (void)
99 return 0;