diff --git a/test/val/bug1675-ub.c b/test/val/bug1675-ub.c new file mode 100644 index 000000000..72e372308 --- /dev/null +++ b/test/val/bug1675-ub.c @@ -0,0 +1,60 @@ +/* #1675 - Some UB cases of bit-shifts */ + +#include + +int unexpected = 0; + +void Test_UB(void) +{ + { + /* UB per standard, lhs expected in cc65: (int)-32768 */ + if (!((0x4000 << 1) < 0)) { + ++unexpected; + printf("Expected: (0x4000 << 1) < 0, got lhs: %ld\n", (long)(0x4000 << 1)); + } + } + + { + /* UB per standard, lhs expected in cc65: (long)-2147483648L */ + if (!((0x40000000 << 1) < 0)) { + ++unexpected; + printf("Expected: (0x40000000 << 1) < 0, got lhs: %ld\n", (long)(0x40000000 << 1)); + } + } + + { + /* UB per standard, lhs expected in cc65: (int)-32768 */ + if (!(((unsigned char)0x80 << 8) < 0)) { + ++unexpected; + printf("Expected: ((unsigned char)0x80 << 8) < 0, got lhs: %ld\n", (long)((unsigned char)0x80 << 8)); + } + } + + { + /* UB per standard, lhs expected in cc65: (int)-32768 */ + if (!(((short)0x4000L << 1) < 0)) { + ++unexpected; + printf("Expected: ((short)0x4000L << 1) < 0, got lhs: %ld\n", (long)((short)0x4000L << 1)); + } + } + + { + const signed short x = 0x4000; + /* UB per standard, lhs expected in cc65: (int)-32768 */ + if (!((x << 1) < 0)) { + ++unexpected; + printf("Expected: (x << 1) < 0, got lhs: %ld\n", (long)(x << 1)); + } + } +} + +int main(void) +{ + Test_UB(); + + if (unexpected != 0) { + printf("Unexpected: %d\n", unexpected); + } + + return unexpected; +} diff --git a/test/val/bug1675.c b/test/val/bug1675.c new file mode 100644 index 000000000..ee24425df --- /dev/null +++ b/test/val/bug1675.c @@ -0,0 +1,101 @@ +/* #1675 - Some corner cases of bit-shifts */ + +#include + +int failures = 0; + +void Test_Defined(void) +{ + { + /* Well-defined per standard, lhs expected in cc65: (int)-256 */ + if (!(((signed char)0x80 << 1) < 0)) { + ++failures; + printf("Expected: ((signed char)0x80 << 1) < 0, got lhs: %ld\n", (long)((signed char)0x80 << 1)); + } + } + + { + /* Implementation-defined per standard, lhs expected in cc65: (int)-128 */ + if (!(((signed char)0x80 >> 1 << 1) < 0)) { + ++failures; + printf("Expected: ((signed char)0x80 >> 1 << 1) < 0, got lhs: %ld\n", (long)((signed char)0x80 >> 1 << 1)); + } + } + + { + int x = 0; + /* Well-defined per standard, lhs expected in cc65: (int)1 */ + if (!((1 << (x++, 0)) == 1)) { + ++failures; + x = 0; + printf("Expected: (1 << (x++, 0)) == 1, got lhs: %ld\n", (long)(1 << (x++, 0))); + } + + /* Well-defined per standard, lhs expected in cc65: (int)1 */ + if (!(x == 1)) { + ++failures; + printf("Expected: (1 << (x++, 0)) == 1 && x == 1, got x: %d\n", x); + } + } + + { + int x = 0, y = 0x100; + /* Well-defined per standard, lhs expected in cc65: (int)128 */ + if (!((y >> (x++, 0) >> 1) == 0x80)) { + ++failures; + x = 0; + printf("Expected: (y >> (x++, 0) >> 1) == 0x80, got lhs: %ld\n", (long)(y >> (x++, 0) >> 1)); + } + + /* Well-defined per standard, lhs expected in cc65: (int)1 */ + if (!(x == 1)) { + ++failures; + printf("Expected: (y >> (x++, 0) >> 1) == 0x80 && x == 1, got x: %d\n", x); + } + } + + { + int x = 0, y = 0x100; + /* Well-defined per standard, lhs expected in cc65: (int)1 */ + if (!((y >> (x++, 8)) == 1)) { + ++failures; + x = 0; + printf("Expected: (y >> (x++, 8)) == 1, got lhs: %ld\n", (long)(y >> (x++, 8))); + } + + /* Well-defined per standard, lhs expected in cc65: (int)1 */ + if (!(x == 1)) { + ++failures; + printf("Expected: (y >> (x++, 8)) == 1 && x == 1, got x: %d\n", x); + } + } + + { + const signed char x = 0x80; + /* Well-defined per standard, lhs expected in cc65: (int)-256 */ + if (!((x << 1) < 0)) { + ++failures; + printf("Expected: (x << 1) < 0, got lhs: %ld\n", (long)(x << 1)); + } + } + + { + const signed char x = 0x40; + /* Well-defined per standard, lhs expected in cc65: (int)128 */ + if (!((x << 1) >= 0)) { + ++failures; + printf("Expected: (x << 1) >= 0, got lhs: %ld\n", (long)(x << 1)); + } + } +} + +int main(void) +{ + Test_Defined(); + + if (failures != 0) { + printf("Failures: %d\n", failures); + } + + return failures; +}