home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_07 / filetest.c < prev    next >
Text File  |  1993-01-07  |  3KB  |  130 lines

  1. /*** File Classes Test Harness: filetest.c ***/
  2.  
  3. #undef NDEBUG
  4.  
  5. #include "file.h"
  6. #include "stdfile.h"
  7. #include "unixfile.h"
  8.  
  9.  
  10. #define TEST_SIZE 32767
  11.  
  12. void init(const char *iname)
  13. {
  14.     puts("generate test file with standard io");
  15.     {   FILE *f = fopen(iname,"wb");
  16.         long i;
  17.         for (i=0; i < TEST_SIZE; i++)
  18.             fputc(i,f);
  19.         fclose(f);
  20.     }
  21.  
  22.     puts("traverse test file with standard io");
  23.     {   FILE *f = fopen(iname,"rb");
  24.         long i;
  25.         char c;
  26.         for (i=0; i < TEST_SIZE; i++) {
  27.             assert(fread(&c,1,1,f) == 1);
  28.             assert(c == (char)i);
  29.         }
  30.         fclose(f);
  31.     }
  32.  
  33. }
  34.  
  35. void test(File *input, File *output)
  36. {   long i, nb;
  37.     char cin, cout;
  38.  
  39.     puts("  bytewise copy");
  40.     assert(FileSeek(input,0,SEEK_SET) == 0);
  41.     for (i = 0; i < TEST_SIZE; i++) {
  42.         assert(FileRead(input,&cin,1) == 1);
  43.         assert(FileWrite(output,&cin,1) == 1);
  44.     }
  45.  
  46.     puts("  random recopy");
  47.     for (i = 0; i < TEST_SIZE; i += nb) {
  48.         char buf[512];
  49.         int n = rand();
  50.         nb = rand() % sizeof buf;
  51.         if (!nb || n + nb > TEST_SIZE)
  52.             continue;
  53.         i += nb;
  54.         assert(FileSeek(input,n,SEEK_SET) == n);
  55.         assert(FileRead(input,&buf,nb) == nb);
  56.         assert(FileSeek(output,n,SEEK_SET) == n);
  57.         assert(FileWrite(output,&buf,nb) == nb);
  58.     }
  59.  
  60.     puts("  bytewise comparison");
  61.     assert(FileSeek(input,0,SEEK_SET) == 0);
  62.     assert(FileSeek(output,0,SEEK_SET) == 0);
  63.     for (i = 0; i < TEST_SIZE; i++) {
  64.         assert(FileRead(input,&cin,1) == 1);
  65.         assert(FileRead(output,&cout,1) == 1);
  66.         assert(cin == cout);
  67.     }
  68.     assert(FileSeek(input,0,SEEK_CUR) == TEST_SIZE );
  69.     assert(FileSeek(output,0,SEEK_CUR) == TEST_SIZE);
  70. }
  71.  
  72. main()
  73. {
  74.     char iname[L_tmpnam];
  75.     const char *oname;
  76.     tmpnam(iname);
  77.  
  78.     puts("initialize classes");
  79.     USE(File);
  80.     USE(StdFile);
  81.     USE(UnixFile);
  82.  
  83.     init(iname);
  84.  
  85.     puts("test with input as StdFile, output as StdFile");
  86.     oname = tmpnam(0);
  87.     {   PUSH(fin,StdFile,(iname,"rb"));
  88.         PUSH(fout,StdFile,(oname,"w+b"));
  89.         test(fin,fout);
  90.         POP(fout);
  91.         POP(fin);
  92.     }
  93.     remove(oname);
  94.  
  95.     puts("test with input as UnixFile, output as UnixFile");
  96.     oname = tmpnam(0);
  97.     {   PUSH(fin,UnixFile,(iname,O_RDWR|O_BINARY,0));
  98.         PUSH(fout,UnixFile,(oname,O_RDWR|O_BINARY|O_CREAT,
  99.                                   S_IREAD|S_IWRITE));
  100.         test(fin,fout);
  101.         POP(fout);
  102.         POP(fin);
  103.     }
  104.     remove(oname);
  105.  
  106.     puts("test with input as StdFile, output as UnixFile");
  107.     oname = tmpnam(0);
  108.     {   PUSH(fin,StdFile,(iname,"rb"));
  109.         PUSH(fout,UnixFile,(oname,O_RDWR|O_BINARY|O_CREAT,
  110.                                   S_IREAD|S_IWRITE));
  111.         test(fin,fout);
  112.         POP(fout);
  113.         POP(fin);
  114.     }
  115.     remove(oname);
  116.  
  117.     puts("test with input as UnixFile, output as StdFile");
  118.     oname = tmpnam(0);
  119.     {   PUSH(fin,UnixFile,(iname,O_RDWR|O_BINARY,0));
  120.         PUSH(fout,StdFile,(oname,"w+b"));
  121.         test(fin,fout);
  122.         POP(fout);
  123.         POP(fin);
  124.     }
  125.     remove(oname);
  126.  
  127.     puts("remove test file");
  128.     remove(iname);
  129. }
  130.