VirtualBox

source: kBuild/trunk/src/grep/gnulib-tests/test-stdalign.c@ 3532

Last change on this file since 3532 was 3529, checked in by bird, 3 years ago

Imported grep 3.7 from grep-3.7.tar.gz (sha256: c22b0cf2d4f6bbe599c902387e8058990e1eee99aef333a203829e5fd3dbb342), applying minimal auto-props.

  • Property svn:eol-style set to native
File size: 4.0 KB
Line 
1/* Test of <stdalign.h>.
2 Copyright 2009-2021 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17/* Written by Paul Eggert, inspired by Bruno Haible's test-alignof.c. */
18
19#include <config.h>
20
21#include <stdalign.h>
22
23#include <stddef.h>
24#include <stdint.h>
25
26#include "verify.h"
27
28#include "macros.h"
29
30typedef long double longdouble;
31typedef struct { char a[1]; } struct1;
32typedef struct { char a[2]; } struct2;
33typedef struct { char a[3]; } struct3;
34typedef struct { char a[4]; } struct4;
35
36verify (__alignof_is_defined == 1);
37#ifndef alignof
38# error "alignof is not a macro"
39#endif
40
41#if __alignas_is_defined
42verify (__alignas_is_defined == 1);
43# ifndef alignas
44# error "alignas is not a macro"
45# endif
46/* mingw can go up only to 8. 8 is all that GNU Emacs needs, so let's
47 limit the test to 8 for now. */
48# define TEST_ALIGNMENT 8
49#else
50# define _Alignas(alignment)
51# define alignas(alignment)
52# define TEST_ALIGNMENT 1
53#endif
54
55#define CHECK_STATIC(type) \
56 typedef struct { char slot1; type slot2; } type##_helper; \
57 verify (alignof (type) == offsetof (type##_helper, slot2)); \
58 verify (_Alignof (type) == alignof (type)); \
59 const int type##_alignment = alignof (type); \
60 type alignas (TEST_ALIGNMENT) static_##type##_alignas; \
61 type _Alignas (TEST_ALIGNMENT) static_##type##_Alignas
62
63#define CHECK_ALIGNED(var) ASSERT ((uintptr_t) &(var) % TEST_ALIGNMENT == 0)
64
65CHECK_STATIC (char);
66CHECK_STATIC (short);
67CHECK_STATIC (int);
68CHECK_STATIC (long);
69#ifdef INT64_MAX
70CHECK_STATIC (int64_t);
71#endif
72CHECK_STATIC (float);
73CHECK_STATIC (double);
74/* CHECK_STATIC (longdouble); */
75CHECK_STATIC (struct1);
76CHECK_STATIC (struct2);
77CHECK_STATIC (struct3);
78CHECK_STATIC (struct4);
79
80int
81main ()
82{
83#if defined __SUNPRO_C && __SUNPRO_C < 0x5150
84 /* Avoid a test failure due to Sun Studio Developer Bug Report #2125432. */
85 fputs ("Skipping test: known Sun C compiler bug\n", stderr);
86 return 77;
87#elif defined __HP_cc && __ia64
88 /* Avoid a test failure due to HP-UX Itanium cc bug; see:
89 https://lists.gnu.org/r/bug-gnulib/2017-03/msg00078.html */
90 fputs ("Skipping test: known HP-UX Itanium cc compiler bug\n", stderr);
91 return 77;
92#elif defined __clang__ && defined __ibmxl__
93 /* Avoid a test failure with IBM xlc 16.1. It ignores alignas (8),
94 _Alignas (8), and __attribute__ ((__aligned__ (8))). */
95 fputs ("Skipping test: known AIX XL C compiler deficiency\n", stderr);
96 return 77;
97#else
98 CHECK_ALIGNED (static_char_alignas);
99 CHECK_ALIGNED (static_char_Alignas);
100 CHECK_ALIGNED (static_short_alignas);
101 CHECK_ALIGNED (static_short_Alignas);
102 CHECK_ALIGNED (static_int_alignas);
103 CHECK_ALIGNED (static_int_Alignas);
104 CHECK_ALIGNED (static_long_alignas);
105 CHECK_ALIGNED (static_long_Alignas);
106# ifdef INT64_MAX
107 CHECK_ALIGNED (static_int64_t_alignas);
108 CHECK_ALIGNED (static_int64_t_Alignas);
109# endif
110 CHECK_ALIGNED (static_float_alignas);
111 CHECK_ALIGNED (static_float_Alignas);
112 CHECK_ALIGNED (static_double_alignas);
113 CHECK_ALIGNED (static_double_Alignas);
114 /* CHECK_ALIGNED (static_longdouble_alignas); */
115 /* CHECK_ALIGNED (static_longdouble_Alignas); */
116 CHECK_ALIGNED (static_struct1_alignas);
117 CHECK_ALIGNED (static_struct1_Alignas);
118 CHECK_ALIGNED (static_struct2_alignas);
119 CHECK_ALIGNED (static_struct2_Alignas);
120 CHECK_ALIGNED (static_struct3_alignas);
121 CHECK_ALIGNED (static_struct3_Alignas);
122 CHECK_ALIGNED (static_struct4_alignas);
123 CHECK_ALIGNED (static_struct4_Alignas);
124 return 0;
125#endif
126}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette