635f46dbb29fb31441093fe22db26517cd86bd8b
[fw/sdcc] / support / regression / tests / logic.c
1 /** Tests the basic logical operations.
2
3     type: char, int, long
4     storage: static, 
5     attr: volatile
6     values: 5, 350, 31734
7  */
8 #include <testfwk.h>
9
10 static {type}
11 alwaysTrue(void)
12 {
13     return ({type}){values};
14 }
15
16 static {type}
17 alwaysFalse(void)
18 {
19     return 0;
20 }
21
22 static {type}
23 neverGetHere(void)
24 {
25     FAILM("Shouldn't get here");
26     return 0;
27 }
28
29 static int hit;
30
31 static void
32 resetGetHere(void)
33 {
34     hit = 0;
35 }
36
37 static {type}
38 alwaysGetHere(void)
39 {
40     hit++;
41     return 1;
42 }
43
44 static void
45 testLogicalAnd(void)
46 {
47     {type} true = alwaysTrue();
48     {type} false = alwaysFalse();
49
50     ASSERT(true && true && true);
51     ASSERT(true && !false);
52     ASSERT(!false && true);
53
54     /* Test that the evaluation is aborted on the first false. */
55     if (true && false && neverGetHere()) {
56         /* Tested using neverGetHere() */
57     }
58
59     resetGetHere();
60     /* Test that the evaluation is done left to right. */
61     if (alwaysGetHere() && true && false) {
62         ASSERT(hit == 1);
63     }
64 }
65
66 static void
67 testLogicalOr(void)
68 {
69     {type} true = alwaysTrue();
70     {type} false = alwaysFalse();
71
72     ASSERT(false || false || true);
73     ASSERT(!true || !false);
74     ASSERT(false || true);
75
76     /* Test that the evaluation is aborted on the first hit. */
77     if (false || true || neverGetHere()) {
78         /* Tested using neverGetHere() */
79     }
80
81     resetGetHere();
82     /* Test that the evaluation is done left to right. */
83     if (alwaysGetHere() || true || false) {
84         ASSERT(hit == 1);
85     }
86 }
87
88 static void
89 testNot(void)
90 {
91     {type} true = alwaysTrue();
92     {type} false = alwaysFalse();
93
94     ASSERT(!false);
95     ASSERT(!!true);
96     ASSERT(!!!false);
97 }
98
99 static void
100 testFlagToVariable(void)
101 {
102     {type} true = alwaysTrue();
103     {type} false = alwaysFalse();
104     {type} val = !true;
105
106     ASSERT(!val);
107     val = !!false;
108     ASSERT(!false);
109 }