struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / shifts.c.in
blob15cfefa9c19768a46172eb20e1899459d4f13507
1 /** Tests covering the shift operators.
3 sign: signed, unsigned
4 type: char, int, long
5 storage: static,
6 attr: volatile
8 vals: 3
10 pending - 1792, 851968, 1560281088, -3, -1792, -851968, -1560000000
12 #include <testfwk.h>
14 void
15 test1ShiftClasses(void)
17 {attr} {storage} {sign} {type} i, result;
19 i = 30;
20 ASSERT(i>>3 == 3);
21 ASSERT(i<<2 == 120);
23 result = i;
24 result >>= 2;
25 ASSERT(result == 7);
27 result = i;
28 result <<= 2;
29 ASSERT(result == 120);
32 /* This tests for implementation-defined behaviour (right-shifting negative values).
33 For sdcc the implementation defined behaviour is that right shift for arithmetic
34 types is arithmetic. */
35 void
36 test2ShiftRight(void)
38 #ifndef __SDCC_pdk14 // Lack of memory
39 #if !(defined (__SDCC_pdk15) && defined(__SDCC_STACK_AUTO)) // Lack of code memory
40 {attr} {storage} signed {type} i, result;
42 i = -120;
43 ASSERT(i>>1 == -60);
44 ASSERT(i>>2 == -30);
45 ASSERT(i>>3 == -15);
46 ASSERT(i>>4 == -8);
47 ASSERT(i>>5 == -4);
48 ASSERT(i>>6 == -2);
49 ASSERT(i>>7 == -1);
50 ASSERT(i>>8 == -1);
51 result = i;
52 result >>= 3;
53 ASSERT(result == -15);
54 #endif
55 #endif
58 void
59 test3ShiftByteMultiples(void)
61 {attr} {storage} {type} i;
63 i = ({type}){vals};
64 ASSERT(i>>8 == ({type})({vals} >> 8));
65 ASSERT(i>>16 == ({type})({vals} >> 16));
66 ASSERT(i>>24 == ({type})({vals} >> 24));
68 i = ({type}){vals};
69 ASSERT( ({type})(i<<8) == ({type})({vals} << 8));;
70 ASSERT((({type}) i<<16) == (({type}) {vals} << 16));
71 ASSERT((({type}) i<<24) == (({type}) {vals} << 24));
74 void
75 test4ShiftOne(void)
77 #ifndef __SDCC_pdk14 // Lack of memory
78 #if !(defined (__SDCC_pdk15) && defined(__SDCC_STACK_AUTO)) // Lack of code memory
79 {attr} {storage} {sign} {type} i;
80 {sign} {type} result;
82 i = ({type}){vals};
84 result = i >> 1;
85 ASSERT(result == ({type})(({type}){vals} >> 1));
87 result = i;
88 result >>= 1;
89 ASSERT(result == ({type})(({type}){vals} >> 1));
91 result = i << 1;
92 ASSERT(result == ({type})(({type}){vals} << 1));
94 result = i;
95 result <<= 1;
96 ASSERT(result == ({type})(({type}){vals} << 1));
97 #endif
98 #endif
101 #ifndef __SDCC_pdk14 // Lack of memory
102 #if !(defined (__SDCC_pdk15) && defined(__SDCC_STACK_AUTO)) // Lack of code memory
103 static {type} ShiftLeftByParam ({type} count)
105 {attr} {storage} {type} i;
106 i = ({type}){vals};
107 return (i << count);
110 static {type} ShiftRightByParam ({type} count)
112 {attr} {storage} {type} i;
113 i = ({type}){vals};
114 return (i >> count);
116 #endif
117 #endif
119 void
120 testShiftByParam(void)
122 #ifndef __SDCC_pdk14 // Lack of memory
123 #if !(defined (__SDCC_pdk15) && defined(__SDCC_STACK_AUTO)) // Lack of code memory
124 ASSERT(ShiftLeftByParam(2) == ({type})({vals} << 2));
125 ASSERT(ShiftRightByParam(2) == ({type})({vals} >> 2));
126 #endif
127 #endif