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

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