Regression tests now pass on z80
[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 #if 0
55     /* PENDING: Doesn't work. */
56     /* Test that the evaluation is aborted on the first false. */
57     if (true && false && neverGetHere()) {
58         /* Tested using neverGetHere() */
59     }
60 #else
61     /* Alternate that is similar. */
62     if (true && false) {
63         neverGetHere();
64         /* Tested using neverGetHere() */
65     }
66 #endif
67
68     resetGetHere();
69     /* Test that the evaluation is done left to right. */
70     if (alwaysGetHere() && true && false) {
71         ASSERT(hit == 1);
72     }
73 }
74
75 static void
76 testLogicalOr(void)
77 {
78     {type} true = alwaysTrue();
79     {type} false = alwaysFalse();
80
81     ASSERT(false || false || true);
82     ASSERT(!true || !false);
83     ASSERT(false || true);
84
85 #if 0
86     /* PENDING: Doesn't work in sdcc. */
87     /* Test that the evaluation is aborted on the first hit. */
88     if (false || true || neverGetHere()) {
89         /* Tested using neverGetHere() */
90     }
91 #else
92     /* No equivalent. */
93 #endif
94
95     resetGetHere();
96     /* Test that the evaluation is done left to right. */
97     if (alwaysGetHere() || true || false) {
98         ASSERT(hit == 1);
99     }
100 }
101
102 static void
103 testNot(void)
104 {
105     {type} true = alwaysTrue();
106     {type} false = alwaysFalse();
107
108     ASSERT(!false);
109     ASSERT(!!true);
110     ASSERT(!!!false);
111 }
112
113 static void
114 testFlagToVariable(void)
115 {
116     {type} true = alwaysTrue();
117     {type} false = alwaysFalse();
118     {type} val = !true;
119
120     ASSERT(!val);
121     val = !!false;
122     ASSERT(!false);
123 }