* device/include/stdio.h: added NULL, size_t, typedef pfn_outputchar,
[fw/sdcc] / support / regression / tests / zeropad.c
1 /** Zeropad tests.
2
3     storage: idata, xdata, code,
4 */
5 #ifndef STORAGE
6 #define STORAGE {storage}
7 #endif
8
9 #if defined __GNUC__
10   #define FLEXARRAY (__GNUC__ >= 3)
11   //since g fails on GCC 2.95.4 on alpha and I don't know how to detect alpha...
12   #define TEST_G    (__GNUC__ >= 3)
13 #else
14   #define FLEXARRAY 1
15   #define TEST_G    1
16 #endif
17
18 #include <testfwk.h>
19
20 typedef unsigned int size_t;
21 #define offsetof(s,m)   (size_t)&(((s *)0)->m)
22
23 #if defined(PORT_HOST) || defined(SDCC_z80) || defined(SDCC_gbz80)
24 # define idata
25 # define xdata
26 # define code
27 #endif
28
29 #if defined(SDCC_hc08)
30 # define idata data
31 #endif
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 //array will be truncated but warning will be suppressed
39 //if truncation is incorrect, other ASSERTs will fail with high probability
40 char STORAGE trunc[2] = {'a', 'b', 'c'};
41 #endif
42
43 char STORAGE array[5] = {'a', 'b', 'c'};
44
45 #if TEST_G
46   struct w {
47     char a;
48     int  b;
49   } STORAGE g[3] = {
50     {'x', 1},
51     {'y'},
52     {'z', 3}
53   };
54 #endif
55
56 struct x {
57   short a;
58   char  b[10];
59 };
60
61 struct x STORAGE teststruct[5] = {
62   { 10, {  1, 2, 3, 4, 5} },
63   { 20, { 11 } },
64   { 30, {  6, 7, 8} }
65 };
66
67 #if FLEXARRAY
68   struct y {
69     short a;
70     char  b[];
71   };
72
73 struct y STORAGE incompletestruct = {
74   10, {1, 2, 3, 4, 5}
75 };
76 #endif
77
78 void
79 testZeropad(void)
80 {
81   ASSERT(string1[1] == '\x01');
82   ASSERT(string2[1] == '\x01');
83
84   ASSERT(array[2] == 'c');
85   ASSERT(array[4] == 0);
86
87 #if TEST_G
88   ASSERT(g[1].a == 'y');
89   ASSERT(g[1].b == 0);
90   ASSERT(g[2].a == 'z');
91   ASSERT(g[2].b == 3);
92 #endif
93
94   ASSERT(teststruct[0].b[1] ==  2);
95   ASSERT(teststruct[0].b[5] ==  0);
96   ASSERT(teststruct[1].b[0] == 11);
97   ASSERT(teststruct[4].b[9] ==  0);
98
99   ASSERT(sizeof(teststruct[2].a) ==  2);
100   ASSERT(sizeof(teststruct[1].b) == 10);
101   ASSERT(sizeof(teststruct[1])   == 12);
102   ASSERT(sizeof(teststruct)      == 60);
103
104 #if FLEXARRAY
105   ASSERT(incompletestruct.a    == 10);
106   ASSERT(incompletestruct.b[0] ==  1);
107   ASSERT(incompletestruct.b[4] ==  5);
108
109   ASSERT(sizeof(incompletestruct) == sizeof(struct y));
110   ASSERT(sizeof(incompletestruct) == offsetof(struct y, b));
111   ASSERT(sizeof(incompletestruct) == offsetof(struct x, b));
112 #endif
113 }