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