add fgets/fgetc test

This commit is contained in:
Colin Leroy-Mira 2024-01-11 18:30:13 +01:00
parent ebf43ddb05
commit f7388cfb79
2 changed files with 64 additions and 0 deletions

View File

@ -86,6 +86,10 @@ extern FILE* stderr;
# define FILENAME_MAX (80+1)
#elif defined(__TELESTRAT__)
# define FILENAME_MAX (50+1)
#elif defined(__SIM6502__)
# define FILENAME_MAX (1024+1)
#elif defined(__SIM65C02__)
# define FILENAME_MAX (1024+1)
#else
# define FILENAME_MAX (16+1)
#endif

60
test/ref/test_fgets.c Normal file
View File

@ -0,0 +1,60 @@
/*
!!DESCRIPTION!! fgets test
!!LICENCE!! Public domain
*/
#include "common.h"
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE *in, *out;
char buf[32];
#define INFILE "cf.in"
int main(int argc,char **argv)
{
static char outfile_path[FILENAME_MAX+1];
sprintf(outfile_path, "%s.test.out", argv[0]);
out = fopen(outfile_path, "wb");
if (out == NULL) {
return EXIT_FAILURE;
}
if (fgets(buf, sizeof(buf), out) != NULL) {
printf("Error, could fgets with write-only file\n");
return 1;
}
if (!ferror(out)) {
printf("Error: file pointer should be in error state\n");
}
fclose(out);
in = fopen(INFILE, "rb");
if (in == NULL) {
return EXIT_FAILURE;
}
if (fgets(NULL, 0, in) != NULL) {
printf("Error, could fgets with zero size\n");
return 1;
}
while (fgets(buf, sizeof(buf), in) != NULL)
{
printf("%s",buf);
}
if (!feof(in))
{
printf("We should have EOF!\n");
}
fclose(in);
return 0;
}