Subversion Repositories Kolibri OS

Rev

Rev 6433 | Go to most recent revision | 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. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <kolibrisys.h>
  21.  
  22. #define fgetc fgetc_dbg  
  23. #define ungetc ungetc_dbg  
  24.  
  25.        
  26. int fgetc_dbg(FILE* file)
  27. {
  28.         int c = 0, rc;
  29.        
  30.         rc = fread(&c, 1, 1, file);
  31.  
  32.         if (rc < 1) return EOF;
  33.  
  34.         return c;
  35. }
  36.  
  37. int ungetc_dbg(int c,FILE* file)
  38. {
  39.         dword res;
  40.  
  41.     if(!file)
  42.     {
  43.         errno = E_INVALIDPTR;
  44.         return EOF;
  45.     }
  46.  
  47.         if ((file->mode & 3!=FILE_OPEN_READ) && (file->mode & FILE_OPEN_PLUS==0))
  48.     {
  49.         errno = E_ACCESS;
  50.         return EOF;
  51.     }
  52.  
  53.         if (file->filepos>file->filesize || file->filepos==0 || c == EOF || file->ungetc_buf != EOF)
  54.         {
  55.             errno = E_EOF;
  56.                 return EOF;
  57.         }
  58.        
  59.         file->ungetc_buf = c;
  60.         file->filepos--;
  61.  
  62.         return c;
  63. }
  64.  
  65. void mark(int n)
  66. {
  67.         n++;
  68. }      
  69.        
  70.  
  71. int main(void)
  72. {
  73.         int i;
  74.         char a[100];
  75.         FILE *f;
  76.  
  77.         TEST(i, !(f = fopen("_tmpfile.tmp","w+")), 0, "failed to create temp file %d!=%d (%s)");
  78.  
  79.         if (!f) return t_status;
  80.  
  81.         TEST(i, fprintf(f, "hello, world\n"), 13, "%d != %d (%m)");
  82.         TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d (%m)");
  83.  
  84.         TEST(i, feof(f), 0, "%d != %d");
  85.         TEST(i, fgetc(f), 'h', "'%c' != '%c'");
  86.         TEST(i, ftell(f), 1, "%d != %d");
  87.         TEST(i, ungetc('x', f), 'x', "%d != %d");
  88.         TEST(i, ftell(f), 0, "%d != %d");
  89.         TEST(i, fscanf(f, "%[h]", a), 0, "got %d fields, expected %d");
  90.         TEST(i, ftell(f), 0, "%d != %d");
  91. mark(0x11);
  92. printf("debug file ungetbuf=%d\n", f->ungetc_buf);
  93.         TEST(i, fgetc(f), 'x', "'%c' != '%c'");  
  94.         TEST(i, ftell(f), 1, "%d != %d");
  95.  
  96.         TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d");
  97.         TEST(i, ungetc('x', f), 'x', "%d != %d");
  98. mark(0x22);
  99.         TEST(i, fread(a, 1, sizeof a, f), 14, "read %d, expected %d");
  100.         a[14] = 0;
  101.         TEST_S(a, "xhello, world\n", "mismatch reading ungot character");
  102.  
  103.         TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d");
  104.         TEST(i, fscanf(f, "%[x]", a), 0, "got %d fields, expected %d");
  105.         TEST(i, ungetc('x', f), 'x', "unget failed after fscanf: %d != %d");
  106.         TEST(i, fgetc(f), 'x', "'%c' != '%c'");
  107. mark(0x33);    
  108.         TEST(i, ftell(f), 1, "%d != %d");
  109.         TEST(i, fgetc(f), 'h', "'%c' != '%c'");
  110.  
  111.         printf("%s finished\n", __FILE__);
  112.         fclose(f);
  113.         return t_status;
  114. }
  115.