]> git.sur5r.net Git - cc65/blob - test/misc/cc65141011.c
cfg/atari-xex.cfg: fix typo in comment
[cc65] / test / misc / cc65141011.c
1 /*
2   !!DESCRIPTION!! equality problem
3   !!ORIGIN!!      Testsuite
4   !!LICENCE!!     Public Domain
5 */
6
7 /*
8     Different results, depending on whether constant is on left or right side.
9
10     The optimizer sometimes makes code that executes the right-side expression
11     as eight bits; but then, tests it against the left-side zero as 16 bits.
12     The high-byte is garbage; therefore, that test might, or might not, work.
13     It depends on the platform and the amount of optimization.
14
15     http://www.cc65.org/mailarchive/2014-10/11680.html
16     http://www.cc65.org/mailarchive/2014-10/11682.html
17     http://www.cc65.org/mailarchive/2014-10/11683.html
18 */
19
20 #include <stdio.h>
21
22 static unsigned char fails = 4;
23 static unsigned char bad[3], good[3];
24
25 int main(void)
26 {
27     unsigned char joy_state = 0x7e;
28     unsigned a, b;
29
30     /* NOTE: It fails in only the printf() statements, the other stuff
31              below works! */
32     printf("bad: %u, ", 0 == (joy_state & 1) );
33     printf("good: %u\n", (joy_state & 1) == 0 );
34
35     sprintf(bad, "%u", 0 == (joy_state & 1) );
36     sprintf(good, "%u", (joy_state & 1) == 0 );
37
38     printf("bad: %u, ", bad[0] - '0' );
39     printf("good: %u\n", good[0] - '0' );
40
41     fails -= bad[0] - '0';
42     fails -= good[0] - '0';
43
44     if (0 == (joy_state & 1)) fails--;
45     if ((joy_state & 1) == 0) fails--;
46
47     printf("failures: %u\n", fails );
48
49     /* The above printf() returns a value with a zero high-byte.
50     ** Therefore, the next (broken) statement works (by accident).
51     */
52     a = 0 == (joy_state & 1);
53     b = (joy_state & 1) == 0;
54
55     printf("a: %u, ", a );
56     printf("b: %u\n", b );
57
58     return fails;
59 }