ORCA-C/Tests/Spec.Conform/SPC4521.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

126 lines
2.5 KiB
C++

/* Separately compiled file needed to run Special Conformance Test 4.5.2.1 */
#include <math.h>
#include <limits.h>
int printf(const char *, ...);
/* Global data */
int *intPtr, i; /* pointers to all the basic types */
long *longPtr, L;
unsigned int *uintPtr, ui;
unsigned long *ulongPtr, ulong;
comp *compPtr, cmp;
char *charPtr, ch;
float *floatPtr, fl;
double *doublePtr, dbl;
extended *extPtr, ext;
/* pointers to conglomerate types */
struct s { int a;
long L; } *structPtr, s;
enum colors { red, black, green } *colorPtr, color;
union longOrShort { int first;
long second; } *unionPtr, un;
void F1 (void)
{
int count = 0;
count += 1;
intPtr = &i;
i = 3;
if (*intPtr != 3)
goto Fail;
count += 1;
longPtr = &L;
L = INT_MAX + 2L;
if (*longPtr != 32769)
goto Fail;
count += 1;
uintPtr = &ui;
ui = UINT_MAX;
if (*uintPtr != 65535u)
goto Fail;
count += 1;
ulongPtr = &ulong;
ulong = ULONG_MAX;
if (*ulongPtr != 4294967295ul)
goto Fail;
count += 1;
compPtr = &cmp;
cmp = ulong + 4;
if (*compPtr != ULONG_MAX + 4)
goto Fail;
count += 1;
charPtr = &ch;
ch = 'A';
if (*charPtr != 'A')
goto Fail;
count += 1;
floatPtr = &fl;
fl = 123.456;
if ( (fabs (*floatPtr - 123.456)) > 0.0001 )
goto Fail;
count += 1;
doublePtr = &dbl;
dbl = 0.0;
if (fabs (*doublePtr - 0.0) > 0.00001)
goto Fail;
count += 1;
extPtr = &ext;
ext = 12.3e20;
if ( (fabs (*extPtr - 123.0E19)) > 0.0001 )
goto Fail;
count += 1;
structPtr = &s;
s.a = INT_MAX;
s.L = LONG_MAX;
if ((structPtr->L != 2147483647l) || (structPtr->a != 32767))
goto Fail;
count += 1;
intPtr = &(s.a);
if (*intPtr != 32767)
goto Fail;
count += 1;
longPtr = &(s.L);
if (*longPtr != LONG_MAX)
goto Fail;
count += 1;
colorPtr = &color;
color = black;
if (*colorPtr != black)
goto Fail;
count += 1;
unionPtr = &un;
un.first = 12;
if (unionPtr->first != 12)
goto Fail;
count += 1;
un.second = 2147483646;
if (unionPtr->second != 2147483646)
goto Fail;
printf ("Passed Special Conformance Test 4.5.2.1\n");
return;
Fail:
printf ("Failed Special Conformance Test 4.5.2.1\n");
printf ("count = %d\n", count);
}