home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 275 / DPCS0111DVD.ISO / Toolkit / Audio-Visual / VirtualDub / Source / VirtualDub-1.9.10-src.7z / src / test / source / TestBufferedStream.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2009-09-14  |  1.4 KB  |  65 lines

  1. #include <vd2/system/filesys.h>
  2. #include <vd2/system/vdstl.h>
  3. #include <vd2/system/file.h>
  4. #include "test.h"
  5.  
  6. DEFINE_TEST(BufferedStream) {
  7.     typedef vdfastvector<uint8> Data;
  8.     Data tempstream(1000000);
  9.  
  10.     Data::iterator it(tempstream.begin()), itEnd(tempstream.end());
  11.     for(; it!=itEnd; ++it) {
  12.         *it = (uint8)rand();
  13.     }
  14.  
  15.     VDMemoryStream ms(tempstream.data(), tempstream.size());
  16.     VDBufferedStream bs(&ms, 256);
  17.  
  18.     char tmpbuf[1024];
  19.  
  20.     // test for 1.7.4 bug
  21.     bs.Seek(0);
  22.     bs.Read(tmpbuf, 256);
  23.     TEST_ASSERT(!memcmp(tmpbuf, tempstream.data() + 0, 256));
  24.     bs.Read(tmpbuf, 256);
  25.     TEST_ASSERT(!memcmp(tmpbuf, tempstream.data() + 256, 256));
  26.     bs.Seek(0);
  27.     bs.Read(tmpbuf, 256);
  28.     TEST_ASSERT(!memcmp(tmpbuf, tempstream.data() + 0, 256));
  29.  
  30.     // random test
  31.     uint32 pos = (uint32)bs.Pos();
  32.     for(uint32 i=0; i<20; ++i) {
  33.         for(uint32 j=0; j<50000; ++j) {
  34.             uint32 len = rand() & 1023;
  35.  
  36.             if (1000000 - pos < len || (rand() & 1)) {
  37.                 do {
  38.                     pos = (rand() ^ (rand() << 14)) % 1000000;
  39.                 } while(pos + len > 1000000);
  40.  
  41.                 bs.Seek(pos);
  42.             }
  43.  
  44.             bs.Read(tmpbuf, len);
  45.             TEST_ASSERT(!memcmp(tmpbuf, tempstream.data() + pos, len));
  46.             pos += len;
  47.         }
  48.  
  49.         tmpbuf[0] = '[';
  50.         for(uint32 j=0; j<=i; ++j)
  51.             tmpbuf[j+1] = '*';
  52.         for(uint32 j=i+1; j<20; ++j)
  53.             tmpbuf[j+1] = ' ';
  54.         tmpbuf[21] = ']';
  55.         tmpbuf[22] = '\r';
  56.         tmpbuf[23] = 0;
  57.         fputs(tmpbuf, stderr);
  58.         fflush(stderr);
  59.     }
  60.     fputc('\n', stderr);
  61.  
  62.     return 0;
  63. }
  64.  
  65.