stdalign-tests: new module
[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 typedef long double longdouble;
29 typedef struct { char a[1]; } struct1;
30 typedef struct { char a[2]; } struct2;
31 typedef struct { char a[3]; } struct3;
32 typedef struct { char a[4]; } struct4;
33
34 verify (__alignof_is_defined == 1);
35 #ifndef alignof
36 # error "alignof is not a macro"
37 #endif
38
39 #if __alignas_is_defined
40 verify (__alignas_is_defined == 1);
41 # ifndef alignas
42 #  error "alignas is not a macro"
43 # endif
44 # define CHECK_ALIGNAS(type) \
45     type alignas (1 << 3) type##_alignas; \
46     type _Alignas (1 << 3) type##_Alignas;
47 #else
48 # define CHECK_ALIGNAS(type)
49 #endif
50
51 #define CHECK(type) \
52   typedef struct { char slot1; type slot2; } type##_helper; \
53   verify (alignof (type) == offsetof (type##_helper, slot2)); \
54   verify (_Alignof (type) == alignof (type)); \
55   const int type##_alignment = alignof (type); \
56   CHECK_ALIGNAS(type)
57
58 CHECK (char)
59 CHECK (short)
60 CHECK (int)
61 CHECK (long)
62 CHECK (float)
63 CHECK (double)
64 CHECK (longdouble)
65 #ifdef INT64_MAX
66 CHECK (int64_t)
67 #endif
68 CHECK (struct1)
69 CHECK (struct2)
70 CHECK (struct3)
71 CHECK (struct4)
72
73 int
74 main ()
75 {
76   return 0;
77 }