summaryrefslogtreecommitdiff
path: root/support/regression/tests/shifts.c
blob: 02aa1920350aaa03ff2135b79d5bdd0673b4faa5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/** Tests covering the shift operators.

    sign: signed, unsigned
    type: char, int, long
    storage: static,
    attr: volatile

    vals: 3

    pending - 1792, 851968, 1560281088, -3, -1792, -851968, -1560000000
*/
#include <testfwk.h>

void
test1ShiftClasses(void)
{
    {attr} {storage} {sign} {type} i, result;

    i = 30;
    ASSERT(i>>3 == 3);
    ASSERT(i<<2 == 120);

    result = i;
    result >>= 2;
    ASSERT(result == 7);

    result = i;
    result <<= 2;
    ASSERT(result == 120);
}

/* This tests for implementation-defined behaviour (right-shifting negative values).
   For sdcc the implementation defined behaviour is that right shift for arithmetic
   types is arithmetic. */
void
test2ShiftRight(void)
{
#ifndef __SDCC_pdk14 // Lack of memory
    {attr} {storage} signed {type} i, result;

    i = -120;
    ASSERT(i>>1 == -60);
    ASSERT(i>>2 == -30);
    ASSERT(i>>3 == -15);
    ASSERT(i>>4 == -8);
    ASSERT(i>>5 == -4);
    ASSERT(i>>6 == -2);
    ASSERT(i>>7 == -1);
    ASSERT(i>>8 == -1);
    result = i;
    result >>= 3;
    ASSERT(result == -15);
#endif
}

void
test3ShiftByteMultiples(void)
{
    {attr} {storage} {type} i;

    i = ({type}){vals};
    ASSERT(i>>8  == ({type})({vals} >> 8));
    ASSERT(i>>16 == ({type})({vals} >> 16));
    ASSERT(i>>24 == ({type})({vals} >> 24));

    i = ({type}){vals};
    ASSERT( ({type})(i<<8)  ==  ({type})({vals} << 8));;
    ASSERT((({type}) i<<16) == (({type}) {vals} << 16));
    ASSERT((({type}) i<<24) == (({type}) {vals} << 24));
}

void
test4ShiftOne(void)
{
#ifndef __SDCC_pdk14 // Lack of memory
    {attr} {storage} {sign} {type} i;
    {sign} {type} result;

    i = ({type}){vals};

    result = i >> 1;
    ASSERT(result == ({type})(({type}){vals} >> 1));

    result = i;
    result >>= 1;
    ASSERT(result == ({type})(({type}){vals} >> 1));

    result = i << 1;
    ASSERT(result == ({type})(({type}){vals} << 1));

    result = i;
    result <<= 1;
    ASSERT(result == ({type})(({type}){vals} << 1));
#endif
}

#ifndef __SDCC_pdk14 // Lack of memory
static {type} ShiftLeftByParam ({type} count)
{
    {attr} {storage} {type} i;
    i = ({type}){vals};
    return (i << count);
}

static {type} ShiftRightByParam ({type} count)
{
    {attr} {storage} {type} i;
    i = ({type}){vals};
    return (i >> count);
}
#endif

void
testShiftByParam(void)
{
#ifndef __SDCC_pdk14 // Lack of memory
    ASSERT(ShiftLeftByParam(2)  == ({type})({vals} << 2));
    ASSERT(ShiftRightByParam(2) == ({type})({vals} >> 2));
#endif
}