summaryrefslogtreecommitdiff
path: root/support/regression/tests/offsetof.c
diff options
context:
space:
mode:
authorXavier ASUS <xavi92psx@gmail.com>2019-10-18 00:31:54 +0200
committerXavier ASUS <xavi92psx@gmail.com>2019-10-18 00:31:54 +0200
commit268a53de823a6750d6256ee1fb1e7707b4b45740 (patch)
tree42c1799a9a82b2f7d9790ee9fe181d72a7274751 /support/regression/tests/offsetof.c
downloadsdcc-gas-268a53de823a6750d6256ee1fb1e7707b4b45740.tar.gz
sdcc-3.9.0 fork implementing GNU assembler syntax
This fork aims to provide better support for stm8-binutils
Diffstat (limited to 'support/regression/tests/offsetof.c')
-rw-r--r--support/regression/tests/offsetof.c83
1 files changed, 83 insertions, 0 deletions
diff --git a/support/regression/tests/offsetof.c b/support/regression/tests/offsetof.c
new file mode 100644
index 0000000..e272884
--- /dev/null
+++ b/support/regression/tests/offsetof.c
@@ -0,0 +1,83 @@
+/*
+ * some testcases to check sanity of the offsetof implementation
+ * especially the __builtin_offsetof implementation
+ */
+
+#include <testfwk.h>
+#include <stddef.h>
+
+struct tail {
+ int t;
+};
+
+struct aux {
+ char x;
+ char fil[2];
+ struct tail ta;
+};
+
+union bla {
+ char a;
+ long b;
+ struct aux ax;
+};
+
+struct st {
+ char first;
+ short a[11], b;
+ struct aux s;
+ struct aux arr[7];
+ union bla abla;
+ union bla abla_arr[2];
+};
+
+typedef struct st st_t;
+typedef union bla bla_t;
+
+#define check(type, element) \
+ { int x, y; \
+ union { long bits; type *ptr; } nul; nul.bits = 0; \
+ x = (int) &(nul.ptr->element); \
+ y = offsetof (type, element); \
+ ASSERT (x == y); \
+ }
+
+void
+testOffsetOf(void)
+{
+ int z;
+
+ check (struct st, first);
+#ifndef __SDCC_pdk14 // Lack of memory
+ check (struct st, b);
+ check (struct st, a);
+ check (struct st, a[9]);
+ check (struct st, s);
+ check (struct st, s.x);
+ check (struct st, s.ta.t);
+ check (struct st, s.fil);
+ check (struct st, s.fil[1]);
+ check (struct st, arr);
+ check (struct st, arr[1]);
+ check (struct st, arr[1].x);
+ check (struct st, arr[1].fil);
+ check (struct st, arr[1].fil[1]);
+ check (struct st, abla);
+ check (struct st, abla.b);
+ check (struct st, abla_arr);
+ check (struct st, abla_arr[1]);
+ check (struct st, abla_arr[1].b);
+ check (struct st, abla_arr[1].ax);
+ check (struct st, abla_arr[1].ax.fil);
+ check (struct st, abla_arr[1].ax.fil[1]);
+#endif
+
+ z = 7; check (struct st, a[z*3+1]);
+ z = 3; check (struct st, arr[z].x);
+
+ check (st_t, arr[1].fil[1]);
+ check (bla_t, b);
+
+ ASSERT (0 == offsetof (union bla, b));
+ ASSERT (0 == offsetof (bla_t, b));
+}