From f7388cfb79def6c7fc9c737120553fea7b76b9e0 Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Thu, 11 Jan 2024 18:30:13 +0100 Subject: [PATCH] add fgets/fgetc test --- include/stdio.h | 4 +++ test/ref/test_fgets.c | 60 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 test/ref/test_fgets.c diff --git a/include/stdio.h b/include/stdio.h index 012b8e2ba..35ebd7784 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -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 diff --git a/test/ref/test_fgets.c b/test/ref/test_fgets.c new file mode 100644 index 000000000..70d30a066 --- /dev/null +++ b/test/ref/test_fgets.c @@ -0,0 +1,60 @@ +/* + !!DESCRIPTION!! fgets test + !!LICENCE!! Public domain +*/ + +#include "common.h" + +#include +#include +#include +#include +#include + +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; +}