run.c 1.6 KB

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