Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. //#include <errno.h>
  5. //#include <limits.h>
  6. //#include <unistd.h>
  7. #include "test.h"
  8.  
  9. #define TEST(r, f, x, m) ( \
  10.         errno = 0, ((r) = (f)) == (x) || \
  11.         (t_error("%s failed (" m ")\n", #f, r, x, strerror(errno)), 0) )
  12.  
  13. #define TEST_S(s, x, m) ( \
  14.         !strcmp((s),(x)) || \
  15.         (t_error("[%s] != [%s] (%s)\n", s, x, m), 0) )
  16.  
  17. int main(void)
  18. {
  19.         int i;
  20.         char a[100];
  21.         FILE *f;
  22.  
  23.         TEST(i, !(f = fopen("_tmpfile.tmp","w+")), 0, "failed to create temp file %d!=%d (%s)");
  24.  
  25.         if (!f) return t_status;
  26.  
  27.         TEST(i, fprintf(f, "hello, world\n"), 13, "%d != %d (%m)");
  28.         TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d (%m)");
  29.  
  30.         TEST(i, feof(f), 0, "%d != %d");
  31.         TEST(i, fgetc(f), 'h', "'%c' != '%c'");
  32.         TEST(i, ftell(f), 1, "%d != %d");
  33.         TEST(i, ungetc('x', f), 'x', "%d != %d");
  34.         TEST(i, ftell(f), 0, "%d != %d");
  35.         TEST(i, fscanf(f, "%[h]", a), 0, "got %d fields, expected %d");
  36.         TEST(i, ftell(f), 0, "%d != %d");
  37.         TEST(i, fgetc(f), 'x', "'%c' != '%c'");
  38.         TEST(i, ftell(f), 1, "%d != %d");
  39.  
  40.         TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d");
  41.         TEST(i, ungetc('x', f), 'x', "%d != %d");
  42.         TEST(i, fread(a, 1, sizeof a, f), 14, "read %d, expected %d");
  43.         a[14] = 0;
  44.         TEST_S(a, "xhello, world\n", "mismatch reading ungot character");
  45.  
  46.         TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d");
  47.         TEST(i, fscanf(f, "%[x]", a), 0, "got %d fields, expected %d");
  48.         TEST(i, ungetc('x', f), 'x', "unget failed after fscanf: %d != %d");
  49.         TEST(i, fgetc(f), 'x', "'%c' != '%c'");
  50.         TEST(i, fgetc(f), 'h', "'%c' != '%c'");
  51.  
  52.         printf("%s finished\n", __FILE__);
  53.         fclose(f);
  54.         return t_status;
  55. }
  56.