mirror of
https://github.com/byteworksinc/ORCA-C.git
synced 2025-01-01 13:29:32 +00:00
91d33b586d
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.
113 lines
2.5 KiB
C++
113 lines
2.5 KiB
C++
/* Conformance Test 17.5.0.1: Verification of fseek function */
|
|
|
|
#include <stdio.h>
|
|
|
|
int main (void)
|
|
{
|
|
FILE *f1; /* file pointer */
|
|
int i, j, k, m;
|
|
|
|
f1 = fopen ("3/tmp", "w+b"); /* create binary file to work on */
|
|
if (f1 == NULL)
|
|
goto Fail1;
|
|
for (i = 0; i < 100; i++) /* write 100 integers to the file */
|
|
{
|
|
j = fprintf (f1, "%2d ", i);
|
|
if (j == EOF)
|
|
goto Fail2;
|
|
}
|
|
|
|
i = fseek (f1, 50, SEEK_END); /* extend file by extra 50 bytes */
|
|
if (i)
|
|
goto Fail;
|
|
rewind (f1); /* position at beginning of file */
|
|
i = 0;
|
|
while ((j = fgetc (f1)) != EOF)
|
|
i += 1;
|
|
if (! (j = feof (f1)) )
|
|
goto Fail3;
|
|
if (i != 450) /* check size of file in bytes */
|
|
goto Fail;
|
|
|
|
/* Test seek from beginning of file. */
|
|
|
|
for (k = 0, i = 0; i < 100; i++)
|
|
{
|
|
j = fseek (f1, k, SEEK_SET);
|
|
if (j)
|
|
goto Fail;
|
|
j = fscanf (f1, "%d", &m);
|
|
if (j == EOF)
|
|
goto Fail3;
|
|
if (m != i)
|
|
goto Fail;
|
|
k += 4;
|
|
}
|
|
|
|
/* Test seek from end of file. */
|
|
|
|
j = fseek (f1, -54, SEEK_END); /* start 54 bytes from end of file */
|
|
if (j)
|
|
goto Fail;
|
|
for (k = -54, i = 99; i > 0; i--)
|
|
{
|
|
j = fscanf (f1, "%d", &m);
|
|
if (j == EOF)
|
|
goto Fail3;
|
|
if (m != i)
|
|
goto Fail;
|
|
k -= 4;
|
|
j = fseek (f1, k, SEEK_END);
|
|
if (j)
|
|
goto Fail;
|
|
}
|
|
|
|
/* Test seek from current position in file. */
|
|
j = fseek (f1, 12, SEEK_CUR);
|
|
if (j)
|
|
goto Fail;
|
|
j = fscanf (f1, "%d", &m);
|
|
if (j == EOF)
|
|
goto Fail3;
|
|
if (m != 3)
|
|
goto Fail;
|
|
|
|
j = fseek (f1, 12, SEEK_CUR);
|
|
if (j)
|
|
goto Fail;
|
|
j = fscanf (f1, "%d", &m);
|
|
if (j == EOF)
|
|
goto Fail3;
|
|
if (m != 7)
|
|
goto Fail;
|
|
|
|
/* Close the file and quit. */
|
|
|
|
j = fclose (f1);
|
|
if (j == EOF)
|
|
goto Fail4;
|
|
|
|
printf ("Passed Conformance Test 17.5.0.1\n");
|
|
return 0;
|
|
|
|
Fail:
|
|
printf ("Failed Conformance Test 17.5.0.1\n");
|
|
return 0;
|
|
|
|
Fail1:
|
|
printf ("Could not open tmp file for Conformance Test 17.5.0.1\n");
|
|
return 0;
|
|
|
|
Fail2:
|
|
printf ("Could not write to file for Conformance Test 17.5.0.1\n");
|
|
return 0;
|
|
|
|
Fail3:
|
|
printf ("Error while reading file for Conformance Test 17.5.0.1\n");
|
|
return 0;
|
|
|
|
Fail4:
|
|
printf ("Could not close file for Conformance Test 17.5.0.1\n");
|
|
return 0;
|
|
}
|