struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / bitfields-bits2.c.in
blob5dcdae6a941ef8f031d2632270e58855761c7d3e
1 /** Operations with 1-bit sized bitfields
2 preBits: 0, 8, 16
3 pattern: 0, 1
4 */
6 #pragma disable_warning 88
8 #include <testfwk.h>
9 #ifdef __sun__
10 #include <inttypes.h>
11 #else
12 #include <stdint.h>
13 #endif
15 #include <stdbool.h>
17 #define PRE_BITS ({preBits})
19 typedef struct
21 #if PRE_BITS > 0
22 unsigned int preBits : PRE_BITS;
23 #endif
24 unsigned int bit0 : 1;
25 unsigned int bit1 : 1;
26 unsigned int bit2 : 1;
27 unsigned int bit3 : 1;
28 unsigned int bit4 : 1;
29 unsigned int bit5 : 1;
30 unsigned int bit6 : 1;
31 unsigned int bit7 : 1;
33 unsigned int postBits : 8;
34 }struct_8bits;
37 #if PATTERN == 0
38 #define PRE_BIT_VALUE 0
39 #define POST_BIT_VALUE 0
40 #else
41 #define PRE_BIT_VALUE ((1UL << PRE_BITS) - 1)
42 #define POST_BIT_VALUE 0xFF
43 #endif
45 bool
46 bits_check_value_no_cast(const struct_8bits * var, const uint8_t value)
48 if((var->bit0 > 0) != ((value & 0x01) > 0)) return false;
49 if((var->bit1 > 0) != ((value & 0x02) > 0)) return false;
50 if((var->bit2 > 0) != ((value & 0x04) > 0)) return false;
51 if((var->bit3 > 0) != ((value & 0x08) > 0)) return false;
53 if((var->bit4 > 0) != ((value & 0x10) > 0)) return false;
54 if((var->bit5 > 0) != ((value & 0x20) > 0)) return false;
55 if((var->bit6 > 0) != ((value & 0x40) > 0)) return false;
56 if((var->bit7 > 0) != ((value & 0x80) > 0)) return false;
58 return true;
62 #define CAST_TO_UINT8(x) ((uint8_t)(x))
64 // Lack of memory.
65 #if !defined(__SDCC_pdk14) && !defined(__SDCC_pdk15)
67 bool
68 bits_check_value(const struct_8bits * const var, const uint8_t value)
70 #if PRE_BITS > 0
71 if(var->preBits != PRE_BIT_VALUE) return false;
72 #endif
74 if(var->postBits != POST_BIT_VALUE) return false;
76 if((var->bit0 > 0) != (CAST_TO_UINT8(value & 0x01) > 0)) return false;
77 if((var->bit1 > 0) != (CAST_TO_UINT8(value & 0x02) > 0)) return false;
78 if((var->bit2 > 0) != (CAST_TO_UINT8(value & 0x04) > 0)) return false;
79 if((var->bit3 > 0) != (CAST_TO_UINT8(value & 0x08) > 0)) return false;
81 if((var->bit4 > 0) != (CAST_TO_UINT8(value & 0x10) > 0)) return false;
82 if((var->bit5 > 0) != (CAST_TO_UINT8(value & 0x20) > 0)) return false;
83 if((var->bit6 > 0) != (CAST_TO_UINT8(value & 0x40) > 0)) return false;
84 if((var->bit7 > 0) != (CAST_TO_UINT8(value & 0x80) > 0)) return false;
86 return bits_check_value_no_cast(var, value);
89 void
90 bits_set_value(struct_8bits * const var, const uint8_t value)
92 #if PRE_BITS > 0
93 var->preBits = PRE_BIT_VALUE;
94 #endif
96 var->postBits = POST_BIT_VALUE;
98 var->bit0 = CAST_TO_UINT8(value & 0x01) > 0;
99 var->bit1 = CAST_TO_UINT8(value & 0x02) > 0;
100 var->bit2 = CAST_TO_UINT8(value & 0x04) > 0;
101 var->bit3 = CAST_TO_UINT8(value & 0x08) > 0;
103 var->bit4 = CAST_TO_UINT8(value & 0x10) > 0;
104 var->bit5 = CAST_TO_UINT8(value & 0x20) > 0;
105 var->bit6 = CAST_TO_UINT8(value & 0x40) > 0;
106 var->bit7 = CAST_TO_UINT8(value & 0x80) > 0;
108 #endif
110 #define BITS_SET_AND_CHECK(value) bits_set_value(&volatileBits, value); ASSERT(bits_check_value(&volatileBits, value))
112 static void
113 testBitfields(void)
115 // Lack of memory.
116 #if !defined(__SDCC_pdk14) && !defined(__SDCC_pdk15)
117 volatile struct_8bits volatileBits;
119 BITS_SET_AND_CHECK(0x00);
120 BITS_SET_AND_CHECK(0xFF);
122 BITS_SET_AND_CHECK(0x0F);
123 BITS_SET_AND_CHECK(0xF0);
125 BITS_SET_AND_CHECK(0x01);
126 BITS_SET_AND_CHECK(0x02);
127 BITS_SET_AND_CHECK(0x04);
128 BITS_SET_AND_CHECK(0x08);
130 BITS_SET_AND_CHECK(0x10);
131 BITS_SET_AND_CHECK(0x20);
132 BITS_SET_AND_CHECK(0x40);
133 BITS_SET_AND_CHECK(0x80);
134 #endif