struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / gcc-torture-execute-bitfld-1.c
blob6d5410eb80202d359e2d9edb716868764eea741c
1 /*
2 bitfld-1-1.c from the execute part of the gcc torture tests.
3 */
5 #include <testfwk.h>
7 #ifdef __SDCC
8 #pragma std_c89
9 #endif
11 /* Copyright 2002 Free Software Foundation, Inc.
13 Tests correct signedness of operations on bitfields; in particular
14 that integer promotions are done correctly, including the case when
15 casts are present.
17 The C front end was eliding the cast of an unsigned bitfield to
18 unsigned as a no-op, when in fact it forces a conversion to a
19 full-width unsigned int. (At the time of writing, the C++ front end
20 has a different bug; it erroneously promotes the uncast unsigned
21 bitfield to an unsigned int).
23 Source: Neil Booth, 25 Jan 2002, based on PR 3325 (and 3326, which
24 is a different manifestation of the same bug).
27 extern void abort ();
29 void
30 testTortureExecute (void)
32 struct x { signed int i : 7; unsigned int u : 7; } bit;
34 unsigned int u;
35 int i;
36 unsigned int unsigned_result = -13U % 61;
37 int signed_result = -13 % 61;
39 bit.u = 61, u = 61;
40 bit.i = -13, i = -13;
42 if (i % u != unsigned_result)
43 ASSERT (0);
44 if (i % (unsigned int) u != unsigned_result)
45 ASSERT (0);
47 /* Somewhat counter-intuitively, bit.u is promoted to an int, making
48 the operands and result an int. */
49 if (i % bit.u != signed_result)
50 ASSERT (0);;
52 if (bit.i % bit.u != signed_result)
53 ASSERT (0);
55 /* But with a cast to unsigned int, the unsigned int is promoted to
56 itself as a no-op, and the operands and result are unsigned. */
57 if (i % (unsigned int) bit.u != unsigned_result)
58 ASSERT (0);
60 if (bit.i % (unsigned int) bit.u != unsigned_result)
61 ASSERT (0);
63 return;