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

123 lines
2.5 KiB
C++

/* Conformance Test 4.2.1.1: Test scoping of identifiers */
/* Global declarations of all different identifier types in C. */
/* Each identifier should be available through the end of the source file. */
#include <math.h>
int printf(const char *, ...);
#define MAC_NAME1 10 /* macro name */
typedef int *intPtr; /* user-defined type */
char F1 (int x, int y); /* function name */
int i; /* variable */
struct ComplexNum { float real; /* type tag name */
float imag; }; /* and struct fields */
union LongOrShort { long longNum; /* type tag name */
int shortNum; }; /* and union fields */
enum flowers { rose, iris, carnation }; /* type tag name */
/* and enum const */
int main (void)
{
char ch;
int m;
intPtr k;
struct ComplexNum c, d;
union LongOrShort z;
enum flowers f;
m = 3;
k = &m;
if (*k != 3)
goto Fail;
c.real = 5.1;
c.imag = 2.0;
d = c;
if ((fabs(d.real - 5.1) > 0.00001) || (fabs(d.imag - 2.0) > 0.00001))
goto Fail;
z.longNum = 5;
if (z.shortNum != 5) /* don't ever do this in real life! */
goto Fail;
f = carnation;
if (f != 2)
goto Fail;
i = MAC_NAME1;
if (i != 10)
goto Fail;
ch = F1 (3, i);
if (ch != '\r')
goto Fail;
printf ("Passed Conformance Test 4.2.1.1\n");
return 0;
printf ("This code can never be reached !!!\n");
Fail:
printf ("Failed Conformance Test 4.2.1.1\n");
return 0;
printf ("Nor can this code ever be reached !!!\n");
}
/****************************************************************************/
char F1 (int x, int y)
{
int m;
intPtr k;
struct ComplexNum c, *d;
union LongOrShort z;
enum flowers f;
m = 5;
k = &m;
if (*k != 5)
goto Err1;
c.real = 18.7;
c.imag = 23.5;
d = &c;
if ((fabs(d->real - 18.7) > 0.00001) || (fabs(d->imag - 23.5) > 0.00001))
goto Err2;
z.longNum = 0x7FE5;
if (z.shortNum != 0x7fe5) /* don't ever do this in real life! */
goto Err3;
#define MAC_NAME2 11
f = iris;
if (f != 1)
goto Fail;
i = MAC_NAME2;
if (i != 11)
goto Fail;
return (x + y);
Err1:
return (1);
Err2:
return (2);
Err3:
return (3);
Fail:
return (4);
}