home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stlpt453.zip / STLport-4.5.3 / test / regression / seek.cpp < prev    next >
C/C++ Source or Header  |  2000-12-07  |  4KB  |  139 lines

  1.  
  2.  
  3. // Regression test for STL seekg
  4.  
  5. #include <cstdio>       // I really like formatted output.
  6. #include <cstdarg>
  7. #include <fstream>
  8. #include <vector>
  9.  
  10. #ifndef cstd
  11. #ifdef _MSC_VER
  12. #define cstd
  13. #else
  14. #define cstd std
  15. #endif
  16. #endif
  17.  
  18. #ifndef USING_STLPORT
  19. #define vsnprintf _vsnprintf
  20. #endif
  21.  
  22. class Error : public std::exception
  23. {
  24.  public:
  25.     Error(const char *pszfmt, ...);
  26.     const char *what() const;
  27.  private:
  28.     char mMessage[256];
  29. };
  30.  
  31. #define countof(x) (sizeof(x)/sizeof((x)[0]))
  32.  
  33. Error::Error(const char *pszfmt, ...)
  34. {
  35.     cstd::va_list ap;
  36.     va_start(ap, pszfmt);
  37.     cstd::vsnprintf(mMessage, countof(mMessage), pszfmt, ap);
  38.     va_end(ap);
  39. }
  40.  
  41. const char *Error::what() const
  42. {
  43.     return mMessage;
  44. }
  45.  
  46. // usage: testseek filename
  47. int main(int argc, char **argv)
  48. {
  49.     try
  50.     {
  51.         if (argc != 2) {
  52.             throw Error("Wrong number of arguments (Usage: testseek filename)");
  53.         }
  54.         std::ifstream ifs(argv[1]);
  55.         if (!ifs) {
  56.             throw Error("Failed to open file \"%s\"", argv[1]);
  57.         }
  58.  
  59.         ifs.exceptions(std::ios::badbit);
  60.         // First pass: read contents of file into character vector
  61.         std::vector<char> charvec;
  62.         cstd::printf("First pass:\n");
  63.         for(;;)
  64.         {
  65.             char ch;
  66.             if (!ifs.get(ch)) {
  67.                 break;
  68.             }
  69.             cstd::printf("%c", ch);
  70.             charvec.push_back(ch);
  71.         }
  72.         printf("First pass done\n");
  73.  
  74.         // Second pass: get positions into pos vector, checking chars
  75.         std::vector<std::ios::pos_type> posvec;
  76.         ifs.clear();    // necessary?
  77.         ifs.seekg(0);
  78.         std::vector<char>::const_iterator char_it;
  79.         printf("Second pass:\n");
  80.         for(char_it = charvec.begin(); char_it != charvec.end(); ++char_it)
  81.         {
  82.             std::ios::pos_type pos = ifs.tellg();
  83.             posvec.push_back(pos);
  84.             char ch;
  85.             if (!ifs.get(ch)) {
  86.                 break;
  87.             }
  88.             cstd::printf("%c", ch);
  89.             if (ch != *char_it) {
  90.                 throw Error("Character mismatch: got '%c', expected '%c'",
  91.                             ch, *char_it);
  92.             }
  93.         }
  94.         if (char_it != charvec.end()) {
  95.             throw Error("Unexpected end of file");
  96.         }
  97.  
  98.         printf("Second pass done\n");
  99.  
  100.         // Third pass: seek to each saved position and read next char
  101.         ifs.clear();    // necessary, but I don't like it
  102.         std::vector<std::ios::pos_type>::const_iterator pos_it;
  103.  
  104.         for(pos_it = posvec.begin();
  105.             pos_it != posvec.end(); ++pos_it)
  106.         {
  107.             printf ("%d ", (int)*pos_it);
  108.         }
  109.         printf ("\n");
  110.  
  111.         printf("Third pass:\n");
  112.         for(char_it = charvec.begin(), pos_it = posvec.begin();
  113.             char_it != charvec.end(); ++char_it, ++pos_it)
  114.         {
  115.             ifs.seekg(*pos_it);
  116.             char ch;
  117.             if (!ifs.get(ch)) {
  118.                 throw Error("End of file when expecting '%c'", *char_it);
  119.             }
  120.             cstd::printf("%c", ch);
  121.             if (ch != *char_it) {
  122.                 throw Error("Character mismatch: got '%c', expected '%c'",
  123.                             ch, *char_it);
  124.             }
  125.         }
  126.         printf("Third pass done\n");
  127.         printf("Test passed\n");
  128.         
  129.             
  130.     }
  131.     catch (std::exception &e)
  132.     {
  133.         cstd::printf("\nCaught exception: %s\n", e.what());
  134.         cstd::printf("Test failed!\n");
  135.         return 1;
  136.     }
  137.     return 0;
  138. }
  139.