1 /*
2 * some testcases to check sanity of the offsetof implementation
3 * especially the __builtin_offsetof implementation
4 */
5
6 #include <testfwk.h>
7 #include <stddef.h>
8
9 struct tail {
10 int t;
11 };
12
13 struct aux {
14 char x;
15 char fil[2];
16 struct tail ta;
17 };
18
19 union bla {
20 char a;
21 long b;
22 struct aux ax;
23 };
24
25 struct st {
26 char first;
27 short a[11], b;
28 struct aux s;
29 struct aux arr[7];
30 union bla abla;
31 union bla abla_arr[2];
32 };
33
34 typedef struct st st_t;
35 typedef union bla bla_t;
36
37 #define check(type, element) \
38 { int x, y; \
39 union { long bits; type *ptr; } nul; nul.bits = 0; \
40 x = (int) &(nul.ptr->element); \
41 y = offsetof (type, element); \
42 ASSERT (x == y); \
43 }
44
45 void
testOffsetOf(void)46 testOffsetOf(void)
47 {
48 int z;
49
50 check (struct st, first);
51 #ifndef __SDCC_pdk14 // Lack of memory
52 check (struct st, b);
53 check (struct st, a);
54 check (struct st, a[9]);
55 check (struct st, s);
56 check (struct st, s.x);
57 check (struct st, s.ta.t);
58 check (struct st, s.fil);
59 check (struct st, s.fil[1]);
60 check (struct st, arr);
61 check (struct st, arr[1]);
62 check (struct st, arr[1].x);
63 check (struct st, arr[1].fil);
64 check (struct st, arr[1].fil[1]);
65 check (struct st, abla);
66 check (struct st, abla.b);
67 check (struct st, abla_arr);
68 check (struct st, abla_arr[1]);
69 check (struct st, abla_arr[1].b);
70 check (struct st, abla_arr[1].ax);
71 check (struct st, abla_arr[1].ax.fil);
72 check (struct st, abla_arr[1].ax.fil[1]);
73 #endif
74
75 z = 7; check (struct st, a[z*3+1]);
76 z = 3; check (struct st, arr[z].x);
77
78 check (st_t, arr[1].fil[1]);
79 check (bla_t, b);
80
81 ASSERT (0 == offsetof (union bla, b));
82 ASSERT (0 == offsetof (bla_t, b));
83 }
84