6fbee3b9a985288faede643845f902fa29e00ec7
[fw/sdcc] / support / regression / tests / bitvars.c
1 /** Bit vars test.
2
3     type: bool, char, unsigned char, unsigned short, unsigned long
4 */
5
6 #include <testfwk.h>
7 #include <stdbool.h>
8
9 #ifndef PORT_HOST
10 #pragma disable_warning 180 //no warning about using complement on bit/unsigned char
11 #endif
12
13 #if defined (SDCC_STACK_AUTO) || defined (SDCC_hc08) || defined (SDCC_z80)
14 #define NO_BITS
15 #endif
16
17 #if defined (__GNUC__) && defined (__alpha__) && (__GNUC__ < 3)
18 /* since this fails on GCC 2.95.4 on alpha... */
19 #define NO_BITS
20 #endif
21
22 #ifndef NO_BITS
23
24 #define TYPE_{type}
25
26 char foo(bool a, bool b, char c)
27 {
28   return a + b + c;
29 }
30
31 char complement(bool a, bool b)
32 {
33   return (a == b);
34 }
35
36 {type} _0 = 0, _1 = 1, _ff = 0xFF, _ffff = -1;
37
38 #endif
39
40 void
41 testBits(void)
42 {
43 #ifndef NO_BITS
44   bool x = 2;
45   ASSERT (foo(x,3,4) == 6);
46
47   ASSERT (complement (~_0, 1));
48   ASSERT (complement (~_1, 1));
49
50 #if defined TYPE_char
51   ASSERT (complement (~_ff, 0));
52 #else
53   ASSERT (complement (~_ff, 1));
54 #endif
55
56 #if defined TYPE_bool
57   ASSERT (complement (~_ffff, 1));
58 #elif defined TYPE_char
59   ASSERT (complement (~_ffff, 0));
60 #else
61   if (sizeof({type}) < sizeof(int))
62     ASSERT (complement (~_ffff, 1));
63   else
64     ASSERT (complement (~_ffff, 0));
65 #endif
66
67 #endif
68 }