summaryrefslogtreecommitdiff
path: root/support/regression/tests/logic.c
blob: 2597ddac97eba1ca2f31c2585f09f9100e806df8 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/** Tests the basic logical operations.

    type: char, int, long
    storage: static, 
    attr: volatile
    values: 5, 350, 31734
 */
#include <testfwk.h>

static {type}
alwaysTrue(void)
{
    return ({type}){values};
}

static {type}
alwaysFalse(void)
{
    return 0;
}

static {type}
neverGetHere1(void)
{
    FAILM("Shouldn't get here 1");
    return 0;
}

static {type}
neverGetHere2(void)
{
    FAILM("Shouldn't get here 2");
    return 0;
}

static {type}
neverGetHere3(void)
{
    FAILM("Shouldn't get here 3");
    return 0;
}

static int hit;

static void
resetGetHere(void)
{
    hit = 0;
}

static {type}
alwaysGetHere(void)
{
    hit++;
    return 1;
}

static void
testLogicalAnd(void)
{
#if !defined(__SDCC_pdk14) // Lack of memory
    {type} true = alwaysTrue();
    {type} false = alwaysFalse();

    ASSERT(true);
    ASSERT(!false);
    ASSERT(true && true && true);
    ASSERT(true && !false);
    ASSERT(!false && true);

    /* Test that the evaluation is aborted on the first false. */
    if (true && false && neverGetHere1()) {
        /* Tested using neverGetHere1() */
    }

    /* Alternate that is similar. */
    if (true && false) {
        neverGetHere2();
        /* Tested using neverGetHere2() */
    }

    resetGetHere();
    /* Test that the evaluation is done left to right. */
    if (alwaysGetHere() && true && false) {
        ASSERT(hit == 1);
    }
#endif
}

static void
testLogicalOr(void)
{
#if !defined(__SDCC_pdk14) // Lack of memory
    {type} true = alwaysTrue();
    {type} false = alwaysFalse();

    ASSERT(true);
    ASSERT(!false);
    ASSERT(false || false || true);
    ASSERT(!true || !false);
    ASSERT(false || true);

    /* Test that the evaluation is aborted on the first hit. */
    if (false || true || neverGetHere3()) {
        /* Tested using neverGetHere3() */
    }

    resetGetHere();
    /* Test that the evaluation is done left to right. */
    if (alwaysGetHere() || true || false) {
        ASSERT(hit == 1);
    }
#endif
}

static void
testNot(void)
{
    {type} true = alwaysTrue();
    {type} false = alwaysFalse();

    ASSERT(!false);
    ASSERT(!!true);
    ASSERT(!!!false);
}

static void
testFlagToVariable(void)
{
    {type} true = alwaysTrue();
    {type} false = alwaysFalse();
    {type} val = !true;

    ASSERT(!val);
    val = !!false;
    ASSERT(!false);
}