Imported Upstream version 2.9.0
[debian/cc1111] / support / regression / tests / zeropad.c
1 /** Zeropad tests.
2
3     storage: auto, idata, pdata, xdata, code,
4 */
5 #ifndef STORAGE
6 #define STORAGE_{storage}
7 #define STORAGE {storage}
8 #endif
9
10 #if defined (__GNUC__) && defined (__alpha__) && (__GNUC__ < 3)
11   /* since g fails on GCC 2.95.4 on alpha... */
12   #define FLEXARRAY 0
13   #define TEST_G    0
14 #elif defined (STORAGE_auto)
15   /* only static flexible arrays are allowed */
16   #define FLEXARRAY 0
17   #define TEST_G    1
18 #else
19   #define FLEXARRAY 1
20   #define TEST_G    1
21 #endif
22
23 #include <testfwk.h>
24
25 typedef unsigned int size_t;
26 #define offsetof(s,m)   (size_t)&(((s *)0)->m)
27
28 #if defined (STORAGE_auto)
29   void Zeropad(void)
30   {
31 #endif //STORAGE_auto
32
33 const char *string1 = "\x00\x01";
34 const char string2[] = "\x00\x01";
35
36 #ifndef PORT_HOST
37 #pragma disable_warning 147 //no warning about excess initializers (W_EXCESS_INITIALIZERS)
38 #pragma disable_warning  85 //no warning about unreferenced variables (W_NO_REFERENCE)
39 //array will be truncated but warning will be suppressed
40 //if truncation is incorrect, other ASSERTs will fail with high probability
41 char STORAGE trunc[2] = {'a', 'b', 'c'};
42 #endif
43
44 char STORAGE array[5] = {'a', 'b', 'c'};
45
46 #if TEST_G
47   struct w {
48     char a;
49     int  b;
50   } STORAGE g[3] = {
51     {'x', 1},
52     {'y'},
53     {'z', 3}
54   };
55 #endif
56
57 struct x {
58   short a;
59   char  b[10];
60 };
61
62 struct x STORAGE teststruct[5] = {
63   { 10, {  1, 2, 3, 4, 5} },
64   { 20, { 11 } },
65   { 30, {  6, 7, 8} }
66 };
67
68 #if FLEXARRAY
69   struct y {
70     short a;
71     char  b[];
72   };
73
74 struct y STORAGE incompletestruct = {
75   10, {1, 2, 3, 4, 5}
76 };
77 #endif
78
79 #if !defined (STORAGE_auto)
80 void Zeropad(void)
81 {
82 #endif //STORAGE_auto
83
84   ASSERT(string1[0] == '\x00');
85   ASSERT(string1[1] == '\x01');
86   ASSERT(string2[0] == '\x00');
87   ASSERT(string2[1] == '\x01');
88
89   ASSERT(array[2] == 'c');
90   ASSERT(array[4] == 0);
91
92 #if TEST_G
93   ASSERT(g[1].a == 'y');
94   ASSERT(g[1].b == 0);
95   ASSERT(g[2].a == 'z');
96   ASSERT(g[2].b == 3);
97 #endif
98
99   ASSERT(teststruct[0].b[1] ==  2);
100   ASSERT(teststruct[0].b[5] ==  0);
101   ASSERT(teststruct[1].b[0] == 11);
102   ASSERT(teststruct[4].b[9] ==  0);
103
104   ASSERT(sizeof(teststruct[2].a) ==  2);
105   ASSERT(sizeof(teststruct[1].b) == 10);
106   ASSERT(sizeof(teststruct[1])   == 12);
107   ASSERT(sizeof(teststruct)      == 60);
108
109 #if FLEXARRAY
110   ASSERT(incompletestruct.a    == 10);
111   ASSERT(incompletestruct.b[0] ==  1);
112   ASSERT(incompletestruct.b[4] ==  5);
113
114   ASSERT(sizeof(incompletestruct) == sizeof(struct y));
115   ASSERT(sizeof(incompletestruct) == offsetof(struct y, b));
116   ASSERT(sizeof(incompletestruct) == offsetof(struct x, b));
117 #endif
118
119 #if defined (STORAGE_auto)
120   array[4] = 1;
121 #if TEST_G
122   g[1].b = 1;
123 #endif
124   teststruct[0].b[5] = 1;
125   teststruct[4].b[9] = 1;
126 #endif //STORAGE_auto
127 }
128
129 void
130 testZeropad(void)
131 {
132   Zeropad();
133
134 #if defined (STORAGE_auto)
135   Zeropad(); //test reinitialization
136 #endif //STORAGE_auto
137 }