]> git.ozlabs.org Git - ccan/blob - ccan/alignof/test/run.c
f36113d18fded305b364f98f5ddd94701be5b660
[ccan] / ccan / alignof / test / run.c
1 #include <ccan/alignof/alignof.h>
2 #include <stdlib.h>
3 #include <stddef.h>
4 #include <ccan/tap/tap.h>
5
6 /* Alignment is remarkably difficult to test.  The rules may be more
7  * complex than ALIGNOF() can know: eg. on i386 __alignof__(double) == 8, but
8  * __alignof__(struct containing double) == 4.
9  *
10  * Technically, we can only test that we give *at least* the alignment which
11  * naturally occurs, and that accesses work.
12  *
13  * For the moment, we work around double. */
14 struct lots_of_types
15 {
16         char c;
17         short s;
18         char c2;
19         int i;
20         char c3;
21         float f;
22         char c4;
23         double d;
24         char c5;
25 };
26
27 int main(int argc, char *argv[])
28 {
29         struct lots_of_types lots_of_types, *lp = malloc(sizeof(*lp));
30         char c;
31         short s;
32         char c2;
33         int i;
34         char c3;
35         float f;
36         char c4;
37         double d;
38
39         /* Make sure we use all the variables. */
40         c = c2 = c3 = c4 = 0;
41
42         plan_tests(15);
43         ok1((unsigned long)&c % ALIGNOF(char) == 0);
44         ok1((unsigned long)&s % ALIGNOF(short) == 0);
45         ok1((unsigned long)&i % ALIGNOF(int) == 0);
46         ok1((unsigned long)&f % ALIGNOF(float) == 0);
47         ok1((unsigned long)&d % ALIGNOF(double) == 0);
48
49         ok1((unsigned long)&lots_of_types.c % ALIGNOF(char) == 0);
50         ok1((unsigned long)&lots_of_types.s % ALIGNOF(short) == 0);
51         ok1((unsigned long)&lots_of_types.i % ALIGNOF(int) == 0);
52         ok1((unsigned long)&lots_of_types.f % ALIGNOF(float) == 0);
53         ok1(offsetof(struct lots_of_types, d) % ALIGNOF(double) == 0);
54
55         ok1((unsigned long)&lp->c % ALIGNOF(char) == 0);
56         ok1((unsigned long)&lp->s % ALIGNOF(short) == 0);
57         ok1((unsigned long)&lp->i % ALIGNOF(int) == 0);
58         ok1((unsigned long)&lp->f % ALIGNOF(float) == 0);
59         ok1((unsigned long)&lp->d % ALIGNOF(double) == 0);
60         exit(exit_status());
61 }