Testcase for #1675.

This commit is contained in:
acqn 2022-02-18 15:20:01 +08:00
parent 2bda128ef1
commit 904a77e03c
2 changed files with 161 additions and 0 deletions

60
test/val/bug1675-ub.c Normal file
View File

@ -0,0 +1,60 @@
/* #1675 - Some UB cases of bit-shifts */
#include <stdio.h>
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;
}

101
test/val/bug1675.c Normal file
View File

@ -0,0 +1,101 @@
/* #1675 - Some corner cases of bit-shifts */
#include <stdio.h>
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;
}