stdalign: port better to MSVC and to Sun C 5.11
[gnulib.git] / tests / test-stdalign.c
1 /* Test of <stdalign.h>.
2    Copyright 2009-2011 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 <http://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
30 typedef long double longdouble;
31 typedef struct { char a[1]; } struct1;
32 typedef struct { char a[2]; } struct2;
33 typedef struct { char a[3]; } struct3;
34 typedef struct { char a[4]; } struct4;
35
36 verify (__alignof_is_defined == 1);
37 #ifndef alignof
38 # error "alignof is not a macro"
39 #endif
40
41 #if __alignas_is_defined
42 verify (__alignas_is_defined == 1);
43 # ifndef alignas
44 #  error "alignas is not a macro"
45 # endif
46 # define TEST_ALIGNMENT 16
47 #else
48 # define _Alignas(alignment)
49 # define alignas(alignment)
50 # define TEST_ALIGNMENT 1
51 #endif
52
53 #define CHECK_STATIC(type) \
54   typedef struct { char slot1; type slot2; } type##_helper; \
55   verify (alignof (type) == offsetof (type##_helper, slot2)); \
56   verify (_Alignof (type) == alignof (type)); \
57   const int type##_alignment = alignof (type); \
58   type alignas (TEST_ALIGNMENT) static_##type##_alignas; \
59   type _Alignas (TEST_ALIGNMENT) static_##type##_Alignas
60
61 #define CHECK_ALIGNED(var) ASSERT ((uintptr_t) &(var) % TEST_ALIGNMENT == 0)
62
63 CHECK_STATIC (char);
64 CHECK_STATIC (short);
65 CHECK_STATIC (int);
66 CHECK_STATIC (long);
67 #ifdef INT64_MAX
68 CHECK_STATIC (int64_t);
69 #endif
70 CHECK_STATIC (float);
71 CHECK_STATIC (double);
72 CHECK_STATIC (longdouble);
73 CHECK_STATIC (struct1);
74 CHECK_STATIC (struct2);
75 CHECK_STATIC (struct3);
76 CHECK_STATIC (struct4);
77
78 int
79 main ()
80 {
81   CHECK_ALIGNED (static_char_alignas);
82   CHECK_ALIGNED (static_char_Alignas);
83   CHECK_ALIGNED (static_short_alignas);
84   CHECK_ALIGNED (static_short_Alignas);
85   CHECK_ALIGNED (static_int_alignas);
86   CHECK_ALIGNED (static_int_Alignas);
87   CHECK_ALIGNED (static_long_alignas);
88   CHECK_ALIGNED (static_long_Alignas);
89 #ifdef INT64_MAX
90   CHECK_ALIGNED (static_int64_t_alignas);
91   CHECK_ALIGNED (static_int64_t_Alignas);
92 #endif
93   CHECK_ALIGNED (static_float_alignas);
94   CHECK_ALIGNED (static_float_Alignas);
95   CHECK_ALIGNED (static_double_alignas);
96   CHECK_ALIGNED (static_double_Alignas);
97   CHECK_ALIGNED (static_longdouble_alignas);
98   CHECK_ALIGNED (static_longdouble_Alignas);
99   CHECK_ALIGNED (static_struct1_alignas);
100   CHECK_ALIGNED (static_struct1_Alignas);
101   CHECK_ALIGNED (static_struct2_alignas);
102   CHECK_ALIGNED (static_struct2_Alignas);
103   CHECK_ALIGNED (static_struct3_alignas);
104   CHECK_ALIGNED (static_struct3_Alignas);
105   CHECK_ALIGNED (static_struct4_alignas);
106   CHECK_ALIGNED (static_struct4_Alignas);
107   return 0;
108 }