summaryrefslogtreecommitdiff
path: root/ccan/alignof/test/run.c
diff options
context:
space:
mode:
authorWilliam Casarin <jb55@jb55.com>2018-07-09 12:10:32 -0700
committerWilliam Casarin <jb55@jb55.com>2018-07-09 12:10:32 -0700
commit1b8fbbd843ddeb5fc81c9303db9c590a436d499b (patch)
treea7227dfe8e4fbaee7b1e0b58b24994dce8078f3f /ccan/alignof/test/run.c
parent37a9cdd2e80386f2c94e14e4f511284ae14c745a (diff)
progress
Diffstat (limited to 'ccan/alignof/test/run.c')
-rw-r--r--ccan/alignof/test/run.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/ccan/alignof/test/run.c b/ccan/alignof/test/run.c
new file mode 100644
index 0000000..5e3216f
--- /dev/null
+++ b/ccan/alignof/test/run.c
@@ -0,0 +1,61 @@
+#include <ccan/alignof/alignof.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <ccan/tap/tap.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(void)
+{
+ 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 = c2 = c3 = c4 = 0;
+
+ 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());
+}