ORCA-C/Tests/Conformance/C8.7.0.1.CC
Stephen Heumann 91d33b586d Fix various C99+ conformance issues and bugs in test cases.
The main changes made to most tests are:

*Declarations always include explicit types, not relying on implicit int. The declaration of main in most test programs is changed to be "int main (void) {...}", adding an explicit return type and a prototype. (There are still some non-prototyped functions, though.)

*Functions are always declared before use, either by including a header or by providing a declaration for the specific function. The latter approach is usually used for printf, to avoid requiring ORCA/C to process stdio.h when compiling every test case (which might make test runs noticeably slower).

*Make all return statements in non-void functions (e.g. main) return a value.

*Avoid some instances of undefined behavior and type errors in printf and scanf calls.

Several miscellaneous bugs are also fixed.

There are still a couple test cases that intentionally rely on the C89 behavior, to ensure it still works.
2022-10-17 20:17:24 -05:00

153 lines
3.2 KiB
C++

/* Conformance Test 8.7.0.1: Verification of switch statement */
#include <stdio.h>
/****************************************************************************/
int F1 (int i)
{
switch (i)
{
case 1: return 9;
case 2: return 99;
case 3: return 999;
default: return 9999;
}
}
/****************************************************************************/
int main (void)
{
char ch = 'd';
int i = 3;
short s = 5;
long L = 0;
unsigned int ui = 0x7F;
enum Colors { red, black, gold, green } color = gold;
switch (i * s) /* test "fall through" of case labels */
{
case 15: ++L;
case 1: ++L;
case 2: ++L;
default: ++L;
}
if (L != 4)
goto Fail;
switch (ch >> i) /* test break out with goto statement */
{
case 1: L = 1;
case 2: L = 2;
case 12: L = 12;
goto Out;
default: L = 0;
}
Out:
if (L != 12)
goto Fail;
switch (s) /* test break out with break statement */
{
case 1: L = 1;
case 2: L = 2;
case 3: L = 3;
case 4: L = 4;
case 5: L = 5;
break;
default: L = 0;
}
if (L != 5)
goto Fail;
switch (F1 (i)) /* test break out with return statement */
{
case 999: L = 999;
break;
default: L = 0;
}
if (L != 999)
goto Fail;
switch (2) /* test no case labels equal switch */
{ /* expr and no default given */
case 0: L = 0;
case 1: L = 1;
}
if (L != 999)
goto Fail;
switch (ui) /* multiple case labels + default on */
{ /* same statement */
case 0x7c: L = 0x7c;
break;
case 0x7e: case 0x7F: case 0x80: default:
L = 0x80;
break;
case 0x7D: L = 0x7d;
break;
}
if (L != 128)
goto Fail;
{
enum Colors { red, black, gold, green } color = gold;
switch (color) /* test nested switch statements */
{
case red: switch (ui && i)
{
case 1: L = 1;
break;
case 0: L = 0;
break;
}
break;
case black: switch (--i)
{
case 2: L = 2;
break;
}
break;
case gold: switch (color)
{
case gold: L = (long) (color);
}
break;
}
if (L != 2)
goto Fail;
}
printf ("Passed Conformance Test 8.7.0.1\n");
return 0;
Fail:
printf ("Failed Conformance Test 8.7.0.1\n");
}