mirror of
https://github.com/byteworksinc/ORCA-C.git
synced 2024-11-17 05:06:10 +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.
109 lines
2.8 KiB
C++
109 lines
2.8 KiB
C++
/* Conformance Test 17.6.0.1: Verification of fgetc, getc, and ungetc with */
|
|
/* a text stream */
|
|
|
|
#include <stdio.h>
|
|
|
|
int main (void)
|
|
{
|
|
FILE *f1;
|
|
int i, j;
|
|
char ch;
|
|
|
|
|
|
f1 = fopen ("3/tmp", "w+"); /* create text file to work on */
|
|
if (f1 == NULL)
|
|
goto Fail1;
|
|
for (ch = 'a', i = 0; i < 26; i++)
|
|
{
|
|
j = fputc (ch, f1);
|
|
if ((char) j != ch)
|
|
goto Fail2;
|
|
ch++;
|
|
}
|
|
j = fputc ('\r', f1); /* text files end with return */
|
|
if (j != '\r')
|
|
goto Fail2;
|
|
|
|
rewind (f1); /* check file contents with fgetc */
|
|
for (ch = 'a', i = 0; i < 26; i++)
|
|
{
|
|
j = fgetc (f1);
|
|
if ( (char) j != ch++ )
|
|
goto Fail;
|
|
}
|
|
|
|
i = ungetc ('F', f1); /* test ungetc */
|
|
if (i != 'F')
|
|
goto Fail;
|
|
i = fgetc (f1);
|
|
if (i != 'F')
|
|
goto Fail;
|
|
|
|
i = fgetc (f1);
|
|
if (i != '\n')
|
|
goto Fail;
|
|
i = fgetc (f1);
|
|
if (! (feof (f1)) ) /* ensure end-of-file reached */
|
|
goto Fail;
|
|
j = ungetc (i, f1); /* not an error to try to push */
|
|
if (j != EOF) /* back EOF */
|
|
goto Fail;
|
|
|
|
j = fseek (f1, 0L, SEEK_SET); /* test getc with temp file */
|
|
if (j)
|
|
goto Fail3;
|
|
j = ungetc (i, f1); /* ungetc should return an error */
|
|
if (j != EOF) /* after seeking on the file */
|
|
goto Fail; /* before reading */
|
|
for (ch = 'a', i = 0; i < 26; i++)
|
|
{
|
|
j = getc (f1);
|
|
if ( (char) j != ch++ )
|
|
goto Fail;
|
|
}
|
|
|
|
i = ungetc ('L', f1); /* test ungetc */
|
|
if (i != 'L')
|
|
goto Fail;
|
|
i = fgetc (f1);
|
|
if (i != 'L')
|
|
goto Fail;
|
|
|
|
i = getc (f1);
|
|
if (i != '\n')
|
|
goto Fail;
|
|
i = getc (f1);
|
|
if (! (feof (f1)) ) /* ensure end-of-file reached */
|
|
goto Fail;
|
|
j = ungetc (i, f1); /* not an error to try to push */
|
|
if (j != EOF) /* back EOF */
|
|
goto Fail;
|
|
|
|
i = fclose (f1); /* close the file and quit */
|
|
if (i == EOF)
|
|
goto Fail4;
|
|
|
|
printf ("Passed Conformance Test 17.6.0.1\n");
|
|
return 0;
|
|
|
|
Fail:
|
|
printf ("Failed Conformance Test 17.6.0.1\n");
|
|
return 0;
|
|
|
|
Fail1:
|
|
printf ("Unable to open temp file for Conformance Test 17.6.0.1\n");
|
|
return 0;
|
|
|
|
Fail2:
|
|
printf ("Unable to write to temp file for Conformance Test 17.6.0.1\n");
|
|
return 0;
|
|
|
|
Fail3:
|
|
printf ("Unable to seek to temp file for Conformance Test 17.6.0.1\n");
|
|
return 0;
|
|
|
|
Fail4:
|
|
printf ("Unable to close temp file for Conformance Test 17.6.0.1\n");
|
|
return 0;
|
|
}
|