summaryrefslogtreecommitdiff
path: root/support/regression/tests/zeropad.c
blob: 158acbf6ec3594ddec936be8a856aea5b11af9e6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/** Zeropad tests.

    storage: auto, __idata, __pdata, __xdata, __code,
*/
#ifndef STORAGE
#define STORAGE_{storage}
#define STORAGE {storage}
#endif

#if defined (__GNUC__) && defined (__alpha__) && (__GNUC__ < 3)
  /* since g fails on GCC 2.95.4 on alpha... */
  #define FLEXARRAY 0
  #define TEST_G    0
#elif defined (STORAGE_auto)
  /* only static flexible arrays are allowed */
  #define FLEXARRAY 0
  #define TEST_G    1
#else
  #define FLEXARRAY 1
  #define TEST_G    1
#endif

#include <testfwk.h>
#include <stddef.h>

#ifndef __SDCC_pdk14 // Lack of memory

#if defined (STORAGE_auto)
  void Zeropad(void)
  {
#else
  extern char STORAGE bug1470790[3];
         char STORAGE bug1470790[] = {1, };

#endif //STORAGE_auto

const char *string1 = "\x00\x01";
const char string2[] = "\x00\x01";

#ifndef PORT_HOST
#pragma disable_warning 147 //no warning about excess initializers (W_EXCESS_INITIALIZERS)
#pragma disable_warning  85 //no warning about unreferenced variables (W_NO_REFERENCE)
//array will be truncated but warning will be suppressed
//if truncation is incorrect, other ASSERTs will fail with high probability
char STORAGE trunc[2] = {'a', 'b', 'c'};
#endif

char STORAGE array[5] = {'a', 'b', 'c'};

#if TEST_G
  struct w {
    char a;
    int  b;
  } STORAGE g[3] = {
    {'x', 1},
    {'y'},
    {'z', 3}
  };
#endif

struct x {
  short a;
  char  b[10];
};

struct x STORAGE teststruct[5] = {
  { 10, {  1, 2, 3, 4, 5} },
  { 20, { 11 } },
  { 30, {  6, 7, 8} }
};

#if FLEXARRAY
  struct y {
    short a;
    char  b[];
  };

struct y STORAGE incompletestruct = {
  10, {1, 2, 3, 4, 5}
};
#endif

struct z {
  short a;
  void (*fp)(void);
};

//see bug 2881971
struct z STORAGE funcptrstruct = {
  10
};

#if !defined (STORAGE_auto)
void
Zeropad (void)
{
  ASSERT (bug1470790[0] == 1);
  ASSERT (bug1470790[1] == 0);
#endif //STORAGE_auto

  ASSERT (string1[0] == '\x00');
  ASSERT (string1[1] == '\x01');
  ASSERT (string2[0] == '\x00');
  ASSERT (string2[1] == '\x01');

  ASSERT (array[2] == 'c');
  ASSERT (array[4] == 0);

#if TEST_G
  ASSERT (g[1].a == 'y');
  ASSERT (g[1].b == 0);
  ASSERT (g[2].a == 'z');
  ASSERT (g[2].b == 3);
#endif

  ASSERT (teststruct[0].b[1] ==  2);
  ASSERT (teststruct[0].b[5] ==  0);
  ASSERT (teststruct[1].b[0] == 11);
  ASSERT (teststruct[4].b[9] ==  0);

  ASSERT (sizeof(teststruct[2].a) ==  2);
  ASSERT (sizeof(teststruct[1].b) == 10);
  ASSERT (sizeof(teststruct[1])   == 12);
  ASSERT (sizeof(teststruct)      == 60);

#if FLEXARRAY
  ASSERT (incompletestruct.a    == 10);
  ASSERT (incompletestruct.b[0] ==  1);
  ASSERT (incompletestruct.b[4] ==  5);

  ASSERT (sizeof (incompletestruct) == sizeof (struct y));
  ASSERT (sizeof (incompletestruct) == offsetof (struct y, b));
  ASSERT (sizeof (incompletestruct) == offsetof (struct x, b));
#endif

#if defined (STORAGE_auto)
  array[4] = 1;
#if TEST_G
  g[1].b = 1;
#endif
  teststruct[0].b[5] = 1;
  teststruct[4].b[9] = 1;
#endif //STORAGE_auto
}
#endif

void
testZeropad (void)
{
#ifndef __SDCC_pdk14 // Lack of memory
  Zeropad ();

#if defined (STORAGE_auto)
  Zeropad (); //test reinitialization
#endif //STORAGE_auto
#endif
}