ORCA-C/Tests/Conformance/C4.2.4.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

128 lines
3.0 KiB
C++

/* Conformance Test 4.2.4.1: Ensure the same names in different overloading */
/* classes are allowed */
#include <string.h>
int printf(const char *, ...);
struct rect { int h1; /* struct, union, enum type tags */
int v1; /* struct/union components */
int h2;
int v2; };
union longOrShort { int first;
long second; };
enum repeats { h1, v1, h2, v2, first, second }; /* all others: variables, */
/* functions, typedefs, */
/* enumeration constants*/
int main (void)
{
int rect; /* can give variables same */
double longOrShort; /* names as labels, tags,*/
float repeats; /* or components */
int label [10];
union longOrShort first;
enum repeats r;
enum colors { red, black, green };
struct person { char name [20];
char address [50]; };
union floatOrDouble { float red;
double green; };
int Label2 (void); /* can give functions same */
char person (int i); /* names as labels, tags,*/
float name (void); /* or components */
typedef int Label3; /* can give typedefs same */
typedef struct person floatOrDouble; /* names as labels, tags,*/
typedef short colors; /* or components */
typedef float address;
Label3 i;
floatOrDouble x;
colors j;
address z;
rect = 8;
if (rect != 8)
goto label;
longOrShort = 3.5;
if (longOrShort != 3.5)
goto Label3;
repeats = (float) 2;
if (repeats != 2.0)
goto label;
for (i = 0; i < 10; i++)
label [i] = i;
for (i = 9; i >= 0; i--)
if (label [i] != i)
goto label;
first.first = 10;
if (first.first != (9+1))
goto Label2;
r = second;
if (r != 5)
goto Label2;
strcpy (x.name, "Barbara");
if ((strcmp (x.name, "Barbara")) != 0)
goto label;
j = (int) person (6);
if (j != 0x36)
goto Label2;
j = Label2 ();
if (j != 5)
goto Label2;
z = name ();
if (z != 1.0)
goto label;
printf ("Passed Conformance Test 4.2.4.1\n");
return 0;
label: ;
Label2: ;
Label3:
printf ("Failed Conformance Test 4.2.4.1\n");
}
/******************************************************************************/
int Label2 (void)
{
return 5;
}
/******************************************************************************/
char person (int i)
{
return (i + 0x30);
}
/******************************************************************************/
float name (void)
{
return 1.0;
}