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

82 lines
2.5 KiB
C++

/* Special Conformance Test 20.2.0.1: Verification of time, asctime, ctime, */
/* localtime, gmtime, mktime, difftime */
/* */
/* The tester needs to verify that the printed time is accurate for the */
/* current clock setting of machine. */
#include <stddef.h>
#include <time.h>
int printf(const char *, ...);
int main (void)
{
time_t secondTime, theTime, *tptr = &theTime;
char *timeString;
struct tm *TM;
double d;
theTime = time (tptr); /* call time to get current date & time */
if (theTime == -1) /* represented as an integral type */
goto Fail;
if (theTime != *tptr)
goto Fail;
/* Call ctime to get current date/time in the form: */
/* Day Mon 99 99:99:99 yyyy\n\0 where Day = day of week; Mon = month; */
/* 99 = day of month; 99:99:99 = current hour:min:sec; yyyy = current year */
timeString = ctime (tptr);
printf ("The current date/time is: %s\n", timeString);
/* Call localtime and gmtime and then echo the fields to the screen. */
TM = localtime (tptr);
if (TM == NULL)
goto Fail;
printf ("localtime is:\n");
printf ("sec = %d min = %d hour = %d day = %d month = %d year = %d\n",
TM->tm_sec, TM->tm_min, TM->tm_hour, TM->tm_mday, TM->tm_mon,
TM->tm_year);
printf ("day of week = %d day of year = %d not daylight savings = %d\n\n",
TM->tm_wday, TM->tm_yday, TM->tm_isdst);
TM = gmtime (tptr);
if (TM == NULL)
goto Fail;
printf ("gmtime is:\n");
printf ("sec = %d min = %d hour = %d day = %d month = %d year = %d\n",
TM->tm_sec, TM->tm_min, TM->tm_hour, TM->tm_mday, TM->tm_mon,
TM->tm_year);
printf ("day of week = %d day of year = %d not daylight savings = %d\n\n",
TM->tm_wday, TM->tm_yday, TM->tm_isdst);
/* Test mktime: should return original time as returned by time. */
secondTime = mktime (TM);
if (secondTime != theTime)
goto Fail;
/* Test asctime: should return same time as returned by ctime. */
timeString = asctime (TM);
printf ("The current date/time is: %s\n", timeString);
/* Test difftime. Value returned should be zero. */
d = difftime (theTime, secondTime);
if (d != 0.0)
goto Fail;
return 0;
Fail:
printf ("Call to a time function in spc20.2.0.1\n");
}