X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Falignof%2Ftest%2Frun.c;fp=ccan%2Falignof%2Ftest%2Frun.c;h=f76fb6691c73c9692cb1a767723b0f9a63e33a72;hb=650c775ff00cccd03fc84e7789a03c51d9839004;hp=0000000000000000000000000000000000000000;hpb=c8acddea39d222312102952e91c32cfe4dd2cea0;p=ccan diff --git a/ccan/alignof/test/run.c b/ccan/alignof/test/run.c new file mode 100644 index 00000000..f76fb669 --- /dev/null +++ b/ccan/alignof/test/run.c @@ -0,0 +1,62 @@ +#include +#include +#include +#include "alignof/alignof.h" + +/* Alignment is remarkably difficult to test. The rules may be more + * complex than ALIGNOF() can know: eg. on i386 __alignof__(double) == 8, but + * __alignof__(struct containing double) == 4. + * + * Technically, we can only test that we give *at least* the alignment which + * naturally occurs, and that accesses work. + * + * For the moment, we work around double. */ +struct lots_of_types +{ + char c; + short s; + char c2; + int i; + char c3; + float f; + char c4; + double d; + char c5; +}; + +int main(int argc, char *argv[]) +{ + struct lots_of_types lots_of_types, *lp = malloc(sizeof(*lp)); + char c; + short s; + char c2; + int i; + char c3; + float f; + char c4; + double d; + + /* Make sure we use all the variables. */ + c = 0; + c2 = c3 = c4 = c; + + plan_tests(15); + ok1((unsigned long)&c % ALIGNOF(char) == 0); + ok1((unsigned long)&s % ALIGNOF(short) == 0); + ok1((unsigned long)&i % ALIGNOF(int) == 0); + ok1((unsigned long)&f % ALIGNOF(float) == 0); + ok1((unsigned long)&d % ALIGNOF(double) == 0); + + ok1((unsigned long)&lots_of_types.c % ALIGNOF(char) == 0); + ok1((unsigned long)&lots_of_types.s % ALIGNOF(short) == 0); + ok1((unsigned long)&lots_of_types.i % ALIGNOF(int) == 0); + ok1((unsigned long)&lots_of_types.f % ALIGNOF(float) == 0); + ok1(offsetof(struct lots_of_types, d) % ALIGNOF(double) == 0); + + ok1((unsigned long)&lp->c % ALIGNOF(char) == 0); + ok1((unsigned long)&lp->s % ALIGNOF(short) == 0); + ok1((unsigned long)&lp->i % ALIGNOF(int) == 0); + ok1((unsigned long)&lp->f % ALIGNOF(float) == 0); + ok1((unsigned long)&lp->d % ALIGNOF(double) == 0); + exit(exit_status()); +}