1
0
mirror of https://github.com/cc65/cc65.git synced 2024-12-23 04:30:10 +00:00
cc65/test/ref/test_fread.c
Colin Leroy-Mira 1fab179cb4 a BIT of 65C02 optimisations
Use BIT immediate instead of AND when reloading A is required
afterwards.
Add an fread unit test as the optimisation touches fread.
Sprinkle a few zero page indexed while we're at it.
2024-02-02 19:45:49 +01:00

71 lines
1.3 KiB
C

/*
!!DESCRIPTION!! fgets test
!!LICENCE!! Public domain
*/
#include "common.h"
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
FILE *in, *out;
char buf[32];
#define INFILE "cf.in"
int main(int argc,char **argv)
{
static char outfile_path[FILENAME_MAX+1];
static int r;
sprintf(outfile_path, "%s.test.out", argv[0]);
out = fopen(outfile_path, "wb");
if (out == NULL) {
return EXIT_FAILURE;
}
if (fread(buf, 1, sizeof(buf), out) != NULL) {
printf("Error, could fread with write-only file\n");
return 1;
}
if (!ferror(out)) {
printf("Error: file pointer should be in error state\n");
}
fclose(out);
unlink(outfile_path);
in = fopen(INFILE, "rb");
if (in == NULL) {
return EXIT_FAILURE;
}
/* Test that ungetc doesn't break fread */
buf[0] = fgetc(in);
ungetc(buf[0], in);
r = fread(buf, 1, sizeof(buf), out);
if (r == 0) {
printf("Error: could not start reading.\n");
}
fwrite(buf, 1, r, stdout);
/* Finish reading file. */
while ((r = fread(buf, 1, sizeof(buf), out)) != 0)
{
fwrite(buf, 1, r, stdout);
}
if (!feof(in))
{
printf("We should have EOF!\n");
}
fclose(in);
return 0;
}