ORCA-C/Tests/Conformance/C17.5.0.2.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

134 lines
2.9 KiB
C++

/* Conformance Test 17.5.0.2: Verification of fseek, rewind, and ftell */
/* functions for text files */
#include <stdio.h>
#include <string.h>
int main (void)
{
FILE *f1; /* file pointer */
int i, j, m;
long L1;
char s [254], ch1;
char ch [254] = "a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z."
"a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z."
"a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z."
"a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z."
"A.B.C.D.E.F.G.H.I.J.K.L.M.N.O.P.Q.R.S.T.U.V.";
f1 = fopen ("14:tmp", "w+"); /* create temporary file to work on */
if (f1 == NULL)
goto Fail1;
for (i = 0; i < 100; i++) /* write 100 lines to the file */
{
j = fprintf (f1, "%s\n", ch);
if (j == EOF)
goto Fail2;
}
/* Test seek from beginning of file. */
j = fseek (f1, 0L, SEEK_SET);
if (j)
goto Fail;
for (i = 0; i < 100; i++) {
j = fscanf (f1, "%s", s);
if (j == EOF)
goto Fail3;
if (strcmp (s, ch))
goto Fail;
}
/* Test seek from end of file. */
j = fseek (f1, 0L, SEEK_END);
if (j)
goto Fail;
j = fscanf (f1, "%c", (char*)&m);
if (j != EOF)
goto Fail3;
/* Test ftell function. */
L1 = ftell (f1);
if (L1 != 25300)
goto Fail;
rewind (f1);
L1 = ftell (f1);
if (L1 != 0)
goto Fail;
ch1 = fgetc (f1);
if (ch1 == EOF)
goto Fail3;
if (ch1 != 'a')
goto Fail;
ch1 = fgetc (f1);
if (ch1 == EOF)
goto Fail3;
if (ch1 != '.')
goto Fail;
L1 = ftell (f1);
if (L1 != 2)
goto Fail;
ch1 = fgetc (f1);
if (ch1 == EOF)
goto Fail3;
if (ch1 != 'b')
goto Fail;
ch1 = fgetc (f1);
if (ch1 == EOF)
goto Fail3;
if (ch1 != '.')
goto Fail;
ch1 = fgetc (f1);
if (ch1 == EOF)
goto Fail3;
if (ch1 != 'c')
goto Fail;
j = fseek (f1, L1, SEEK_SET); /* verify that ftell-position works */
if (j)
goto Fail;
ch1 = fgetc (f1);
if (ch1 == EOF)
goto Fail3;
if (ch1 != 'b')
goto Fail;
/* Close the file and quit. */
j = fclose (f1);
if (j == EOF)
goto Fail4;
printf ("Passed Conformance Test 17.5.0.2\n");
return 0;
Fail:
printf ("Failed Conformance Test 17.5.0.2\n");
return 0;
Fail1:
printf ("Could not open tmp file for Conformance Test 17.5.0.2\n");
return 0;
Fail2:
printf ("Could not write to file for Conformance Test 17.5.0.2\n");
return 0;
Fail3:
printf ("Error while reading file for Conformance Test 17.5.0.2\n");
return 0;
Fail4:
printf ("Could not close file for Conformance Test 17.5.0.2\n");
return 0;
}