1 /** Tests covering the shift operators.
2 
3     sign: signed, unsigned
4     type: char, int, long
5     storage: static,
6     attr: volatile
7 
8     vals: 3
9 
10     pending - 1792, 851968, 1560281088, -3, -1792, -851968, -1560000000
11 */
12 #include <testfwk.h>
13 
14 void
test1ShiftClasses(void)15 test1ShiftClasses(void)
16 {
17     {attr} {storage} {sign} {type} i, result;
18 
19     i = 30;
20     ASSERT(i>>3 == 3);
21     ASSERT(i<<2 == 120);
22 
23     result = i;
24     result >>= 2;
25     ASSERT(result == 7);
26 
27     result = i;
28     result <<= 2;
29     ASSERT(result == 120);
30 }
31 
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
test2ShiftRight(void)36 test2ShiftRight(void)
37 {
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;
41 
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
56 }
57 
58 void
test3ShiftByteMultiples(void)59 test3ShiftByteMultiples(void)
60 {
61     {attr} {storage} {type} i;
62 
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));
67 
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));
72 }
73 
74 void
test4ShiftOne(void)75 test4ShiftOne(void)
76 {
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;
81 
82     i = ({type}){vals};
83 
84     result = i >> 1;
85     ASSERT(result == ({type})(({type}){vals} >> 1));
86 
87     result = i;
88     result >>= 1;
89     ASSERT(result == ({type})(({type}){vals} >> 1));
90 
91     result = i << 1;
92     ASSERT(result == ({type})(({type}){vals} << 1));
93 
94     result = i;
95     result <<= 1;
96     ASSERT(result == ({type})(({type}){vals} << 1));
97 #endif
98 #endif
99 }
100 
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)
104 {
105     {attr} {storage} {type} i;
106     i = ({type}){vals};
107     return (i << count);
108 }
109 
110 static {type} ShiftRightByParam ({type} count)
111 {
112     {attr} {storage} {type} i;
113     i = ({type}){vals};
114     return (i >> count);
115 }
116 #endif
117 #endif
118 
119 void
testShiftByParam(void)120 testShiftByParam(void)
121 {
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
128 }
129 
130