From 1e300bf768b949498895062b157d71aa3ac9bbc1 Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Thu, 1 Feb 2024 20:13:05 +0100 Subject: [PATCH] Add test case for issue #2395 --- test/todo/bug2395.c | 51 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 test/todo/bug2395.c diff --git a/test/todo/bug2395.c b/test/todo/bug2395.c new file mode 100644 index 000000000..4f4d2a6d0 --- /dev/null +++ b/test/todo/bug2395.c @@ -0,0 +1,51 @@ + +/* bug #2395: Bitwise operators with a boolean expression fail when optimized */ + +#include +#include +#include + +unsigned char a, b; +unsigned char c = 199; +unsigned char d = 100; + +int main(void) { + int fails = 0; + + a = c ^ (d != 0); + b = c ^ 1; + + printf("%u ^ (%u != 0) => %u\n", c, d, a); + if (a != b) { + printf("XOR error: a %d instead of %d\n", a, b); + fails++; + } + + a = c | (d != 0); + b = c | 1; + + printf("%u | (%u != 0) => %u\n", c, d, a); + if (a != b) { + printf("OR error: a %d instead of %d\n", a, b); + fails++; + } + + a = c & (d != 0); + b = c & 1; + + printf("%u & (%u != 0) => %u\n", c, d, a); + if (a != b) { + printf("AND error: a %d instead of %d\n", a, b); + fails++; + } + printf("%d errors\n", fails); + +#ifdef __OPT__ + return fails; +#else + /* Force exit failure on non-optimised version, which works, + * otherwise it breaks the build + */ + return 1; +#endif +}