1570af302Sopenharmony_ci#include <stdio.h> 2570af302Sopenharmony_ci#include <string.h> 3570af302Sopenharmony_ci#include <errno.h> 4570af302Sopenharmony_ci#include <limits.h> 5570af302Sopenharmony_ci#include <unistd.h> 6570af302Sopenharmony_ci#include "test.h" 7570af302Sopenharmony_ci 8570af302Sopenharmony_ci#define TEST(r, f, x, m) ( \ 9570af302Sopenharmony_ci errno = 0, ((r) = (f)) == (x) || \ 10570af302Sopenharmony_ci (t_error("%s failed (" m ")\n", #f, r, x, strerror(errno)), 0) ) 11570af302Sopenharmony_ci 12570af302Sopenharmony_ci#define TEST_S(s, x, m) ( \ 13570af302Sopenharmony_ci !strcmp((s),(x)) || \ 14570af302Sopenharmony_ci (t_error("[%s] != [%s] (%s)\n", s, x, m), 0) ) 15570af302Sopenharmony_ci 16570af302Sopenharmony_ciint main(void) 17570af302Sopenharmony_ci{ 18570af302Sopenharmony_ci int i; 19570af302Sopenharmony_ci char a[100]; 20570af302Sopenharmony_ci FILE *f; 21570af302Sopenharmony_ci 22570af302Sopenharmony_ci TEST(i, !(f = tmpfile()), 0, "failed to create temp file %d!=%d (%s)"); 23570af302Sopenharmony_ci 24570af302Sopenharmony_ci if (!f) return t_status; 25570af302Sopenharmony_ci 26570af302Sopenharmony_ci TEST(i, fprintf(f, "hello, world\n"), 13, "%d != %d (%m)"); 27570af302Sopenharmony_ci TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d (%m)"); 28570af302Sopenharmony_ci 29570af302Sopenharmony_ci TEST(i, feof(f), 0, "%d != %d"); 30570af302Sopenharmony_ci TEST(i, fgetc(f), 'h', "'%c' != '%c'"); 31570af302Sopenharmony_ci TEST(i, ftell(f), 1, "%d != %d"); 32570af302Sopenharmony_ci TEST(i, ungetc('x', f), 'x', "%d != %d"); 33570af302Sopenharmony_ci TEST(i, ftell(f), 0, "%d != %d"); 34570af302Sopenharmony_ci TEST(i, fscanf(f, "%[h]", a), 0, "got %d fields, expected %d"); 35570af302Sopenharmony_ci TEST(i, ftell(f), 0, "%d != %d"); 36570af302Sopenharmony_ci TEST(i, fgetc(f), 'x', "'%c' != '%c'"); 37570af302Sopenharmony_ci TEST(i, ftell(f), 1, "%d != %d"); 38570af302Sopenharmony_ci 39570af302Sopenharmony_ci TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d"); 40570af302Sopenharmony_ci TEST(i, ungetc('x', f), 'x', "%d != %d"); 41570af302Sopenharmony_ci TEST(i, fread(a, 1, sizeof a, f), 14, "read %d, expected %d"); 42570af302Sopenharmony_ci a[14] = 0; 43570af302Sopenharmony_ci TEST_S(a, "xhello, world\n", "mismatch reading ungot character"); 44570af302Sopenharmony_ci 45570af302Sopenharmony_ci TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d"); 46570af302Sopenharmony_ci TEST(i, fscanf(f, "%[x]", a), 0, "got %d fields, expected %d"); 47570af302Sopenharmony_ci TEST(i, ungetc('x', f), 'x', "unget failed after fscanf: %d != %d"); 48570af302Sopenharmony_ci TEST(i, fgetc(f), 'x', "'%c' != '%c'"); 49570af302Sopenharmony_ci TEST(i, fgetc(f), 'h', "'%c' != '%c'"); 50570af302Sopenharmony_ci 51570af302Sopenharmony_ci fclose(f); 52570af302Sopenharmony_ci return t_status; 53570af302Sopenharmony_ci} 54