struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / constantRange.c
blob28e595a470bf771d01c2583343d90821b50f0fc3
1 /** constant range tests.
3 */
5 #include <testfwk.h>
7 #ifdef __SDCC
8 #pragma std_sdcc99
9 #endif
11 #include <stdbool.h>
12 #ifdef __sun__
13 #include <inttypes.h>
14 #else
15 #include <stdint.h>
16 #endif
18 #if defined (__GNUC__) && defined (__alpha__) && (__GNUC__ < 3)
19 /* bool should be casted to int in order to pass the test. Is this a gcc bug? */
20 #define INT_CAST (int)
21 #else
22 #define INT_CAST
23 #endif
25 int8_t s8;
26 uint8_t u8;
28 int16_t s16;
29 uint16_t u16;
31 int32_t s32;
32 uint32_t u32;
34 void
35 testConstantRange (void)
37 ASSERT (! (INT8_MIN - 1 == s8));
38 ASSERT (! (INT8_MAX + 1 == s8));
39 ASSERT ( (INT8_MIN - 1 != s8));
40 ASSERT ( (INT8_MAX + 1 != s8));
41 ASSERT ( (INT8_MIN - 1 < s8));
42 ASSERT (! (INT8_MAX < s8));
43 ASSERT ( (INT8_MIN <= s8));
44 ASSERT (! (INT8_MAX <= s8));
45 ASSERT (! (INT8_MIN > s8));
46 ASSERT ( (INT8_MAX + 1 > s8));
47 ASSERT (! (INT8_MIN - 1 >= s8));
48 ASSERT ( (INT8_MAX >= s8));
49 #if !defined(__SDCC_pdk14) // Lack of memory
50 ASSERT (! ( 0 - 1 == u8));
51 ASSERT (! (UINT8_MAX + 1 == u8));
52 ASSERT ( ( 0 - 1 != u8));
53 ASSERT ( (UINT8_MAX + 1 != u8));
54 ASSERT ( ( 0 - 1 < u8));
55 ASSERT (! (UINT8_MAX < u8));
56 ASSERT ( ( 0 <= u8));
57 ASSERT (! (UINT8_MAX + 1 <= u8));
58 ASSERT (! ( 0 > u8));
59 ASSERT ( (UINT8_MAX + 1 > u8));
60 ASSERT (! ( 0 - 1 >= u8));
61 ASSERT ( (UINT8_MAX >= u8));
63 /* force extension to long to avoid int (16 bit) overflow */
64 ASSERT (! (INT16_MIN - 1L == s16));
65 ASSERT (! (INT16_MAX + 1L == s16));
66 ASSERT ( (INT16_MIN - 1L != s16));
67 ASSERT ( (INT16_MAX + 1L != s16));
68 ASSERT ( (INT16_MIN - 1L < s16));
69 ASSERT (! (INT16_MAX < s16));
70 ASSERT ( (INT16_MIN <= s16));
71 ASSERT (! (INT16_MAX <= s16));
72 ASSERT (! (INT16_MIN > s16));
73 ASSERT ( (INT16_MAX + 1L > s16));
74 ASSERT (! (INT16_MIN - 1L >= s16));
75 ASSERT ( (INT16_MAX >= s16));
77 ASSERT (! ( 0 - 1 == u16));
78 ASSERT (! (UINT16_MAX + 1L == u16));
79 ASSERT ( ( 0 - 1 != u16));
80 ASSERT ( (UINT16_MAX + 1L != u16));
81 ASSERT (! (UINT16_MAX < u16));
82 ASSERT ( ( 0 <= u16));
83 ASSERT (! (UINT16_MAX + 1L <= u16));
84 ASSERT (! ( 0 > u16));
85 ASSERT ( (UINT16_MAX + 1L > u16));
86 ASSERT ( (UINT16_MAX >= u16));
88 ASSERT (UINTMAX_C(4294967295) <= UINTMAX_MAX);
90 #if defined(PORT_HOST)
91 /* on 32bit host: -1 is presented as 32 bit int, 16 bit unsigned short is promoted to 32 bit int */
92 ASSERT ( ( 0 - 1 < u16)); /* -1 > 0 */
93 ASSERT (! ( 0 - 1 >= u16)); /* !(-1 <= 0) */
94 #else
95 /* on 16bit sdcc: int (-1) is promoted to unsigned int (0xffff) */
96 ASSERT ( ( 0 - 1 > u16)); /* 0xffff > 0 */
97 ASSERT (! ( 0 - 1 <= u16)); /* !(0xffff <= 0) */
98 #endif
100 /* sdcc can't hold a number (INT32_MIN - 1) or (INT32_MAX + 1),
101 there's no 'double' or 'long long' */
102 /* ASSERT (! (INT32_MIN - 1 == s32)); */
103 /* ASSERT (! (INT32_MAX + 1 == s32)); */
104 /* ASSERT ( (INT32_MIN - 1 != s32)); */
105 /* ASSERT ( (INT32_MAX + 1 != s32)); */
106 /* ASSERT ( (INT32_MIN - 1 < s32)); */
107 ASSERT (! (INT32_MAX < s32));
108 ASSERT ( (INT32_MIN <= s32));
109 ASSERT (! (INT32_MAX <= s32));
110 ASSERT (! (INT32_MIN > s32));
111 /* ASSERT ( (INT32_MAX + 1 > s32)); */
112 /* ASSERT (! (INT32_MIN - 1 >= s32)); */
113 ASSERT ( (INT32_MAX >= s32));
115 ASSERT (! ( 0 - 1 == u32));
116 /* ASSERT (! (UINT32_MAX + 1 == u32)); */
117 ASSERT ( ( 0 - 1 != u32));
118 /* ASSERT ( (UINT32_MAX + 1 != u32)); */
119 ASSERT ( ( 0 - 1 > u32)); /* 0xffffffff > 0 */
120 ASSERT (! (UINT32_MAX < u32));
121 ASSERT ( ( 0 <= u32));
122 /* ASSERT (! (UINT32_MAX + 1 <= u32)); */
123 ASSERT (! ( 0 > u32));
124 /* ASSERT ( (UINT32_MAX + 1 > u32)); */
125 ASSERT (! ( 0 - 1 <= u32)); /* !(0xffffffff <= 0) */
126 ASSERT ( (UINT32_MAX >= u32));
127 #endif
130 void
131 testFoo1(void)
133 #if !defined(__SDCC_pdk14) // Lack of memory
134 #if !(defined (__SDCC_pdk15) && defined(__SDCC_STACK_AUTO)) // Lack of code memory
135 #ifdef __bool_true_false_are_defined
136 volatile bool sb, ub;
138 sb = 0;
139 ub = 0;
141 ASSERT (! (-1 == sb));
142 ASSERT ( ( 0 == sb));
143 ASSERT (! ( 1 == sb));
145 ASSERT ( (-1 != sb));
146 ASSERT (! ( 0 != sb));
147 ASSERT ( ( 1 != sb));
149 ASSERT ( (-1 < INT_CAST sb));
150 ASSERT (! ( 0 < sb));
152 ASSERT ( ( 0 <= sb));
153 ASSERT (! ( 1 <= sb));
155 ASSERT (! ( 0 > sb));
156 ASSERT ( ( 1 > sb));
158 ASSERT (! (-1 >= INT_CAST sb));
159 ASSERT ( ( 0 >= sb));
162 ASSERT (! (-1 == ub));
163 ASSERT ( ( 0 == ub));
164 ASSERT (! ( 1 == ub));
166 ASSERT ( (-1 != ub));
167 ASSERT (! ( 0 != ub));
168 ASSERT ( ( 1 != ub));
170 ASSERT ( (-1 < INT_CAST ub));
171 ASSERT (! ( 0 < ub));
173 ASSERT ( ( 0 <= ub));
174 ASSERT (! ( 1 <= ub));
176 ASSERT (! ( 0 > ub));
177 ASSERT ( ( 1 > ub));
179 ASSERT (! (-1 >= INT_CAST ub));
180 ASSERT ( ( 0 >= ub));
181 #endif //__bool_true_false_are_defined
182 #endif
183 #endif
186 void
187 testFoo2(void)
189 #if !defined(__SDCC_pdk14) // Lack of memory
190 #if !(defined (__SDCC_pdk15) && defined(__SDCC_STACK_AUTO)) // Lack of code memory
191 volatile struct {
192 signed sb1:1;
193 signed sb3:3;
194 unsigned ub1:1;
195 unsigned ub3:3;
196 } str;
198 str.sb1 = 0;
199 str.ub1 = 0;
200 str.sb3 = 0;
201 str.ub3 = 0;
203 ASSERT (! (-2 == str.sb1));
204 ASSERT (! (-1 == str.sb1));
205 ASSERT ( ( 0 == str.sb1));
206 ASSERT (! ( 1 == str.sb1));
208 ASSERT (! (-1 == str.ub1));
209 ASSERT ( ( 0 == str.ub1));
210 ASSERT (! ( 1 == str.ub1));
211 ASSERT (! ( 2 == str.ub1));
213 ASSERT (! (-5 == str.sb3));
214 ASSERT (! (-4 == str.sb3));
215 ASSERT ( ( 0 == str.sb3));
216 ASSERT (! ( 3 == str.sb3));
217 ASSERT (! ( 4 == str.sb3));
219 ASSERT (! (-1 == str.ub3));
220 ASSERT ( ( 0 == str.ub3));
221 ASSERT (! ( 7 == str.ub3));
222 ASSERT (! ( 8 == str.ub3));
223 #endif
224 #endif