overflow.pass.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // <fstream>
  10. // int_type overflow(int_type c = traits::eof());
  11. // This test is not entirely portable
  12. #include <fstream>
  13. #include <cassert>
  14. #include "../../../../platform_support.h" // locale name macros
  15. template <class CharT>
  16. struct test_buf
  17. : public std::basic_filebuf<CharT>
  18. {
  19. typedef std::basic_filebuf<CharT> base;
  20. typedef typename base::char_type char_type;
  21. typedef typename base::int_type int_type;
  22. typedef typename base::traits_type traits_type;
  23. char_type* pbase() const {return base::pbase();}
  24. char_type* pptr() const {return base::pptr();}
  25. char_type* epptr() const {return base::epptr();}
  26. void gbump(int n) {base::gbump(n);}
  27. virtual int_type overflow(int_type c = traits_type::eof()) {return base::overflow(c);}
  28. };
  29. int main()
  30. {
  31. {
  32. test_buf<char> f;
  33. assert(f.open("overflow.dat", std::ios_base::out) != 0);
  34. assert(f.is_open());
  35. assert(f.pbase() == 0);
  36. assert(f.pptr() == 0);
  37. assert(f.epptr() == 0);
  38. assert(f.overflow('a') == 'a');
  39. assert(f.pbase() != 0);
  40. assert(f.pptr() == f.pbase());
  41. assert(f.epptr() - f.pbase() == 4095);
  42. }
  43. {
  44. test_buf<char> f;
  45. assert(f.open("overflow.dat", std::ios_base::in) != 0);
  46. assert(f.is_open());
  47. assert(f.sgetc() == 'a');
  48. }
  49. std::remove("overflow.dat");
  50. {
  51. test_buf<char> f;
  52. f.pubsetbuf(0, 0);
  53. assert(f.open("overflow.dat", std::ios_base::out) != 0);
  54. assert(f.is_open());
  55. assert(f.pbase() == 0);
  56. assert(f.pptr() == 0);
  57. assert(f.epptr() == 0);
  58. assert(f.overflow('a') == 'a');
  59. assert(f.pbase() == 0);
  60. assert(f.pptr() == 0);
  61. assert(f.epptr() == 0);
  62. }
  63. {
  64. test_buf<char> f;
  65. assert(f.open("overflow.dat", std::ios_base::in) != 0);
  66. assert(f.is_open());
  67. assert(f.sgetc() == 'a');
  68. }
  69. std::remove("overflow.dat");
  70. {
  71. test_buf<wchar_t> f;
  72. assert(f.open("overflow.dat", std::ios_base::out) != 0);
  73. assert(f.is_open());
  74. assert(f.pbase() == 0);
  75. assert(f.pptr() == 0);
  76. assert(f.epptr() == 0);
  77. assert(f.overflow(L'a') == L'a');
  78. assert(f.pbase() != 0);
  79. assert(f.pptr() == f.pbase());
  80. assert(f.epptr() - f.pbase() == 4095);
  81. }
  82. {
  83. test_buf<wchar_t> f;
  84. assert(f.open("overflow.dat", std::ios_base::in) != 0);
  85. assert(f.is_open());
  86. assert(f.sgetc() == L'a');
  87. }
  88. std::remove("overflow.dat");
  89. {
  90. test_buf<wchar_t> f;
  91. f.pubsetbuf(0, 0);
  92. assert(f.open("overflow.dat", std::ios_base::out) != 0);
  93. assert(f.is_open());
  94. assert(f.pbase() == 0);
  95. assert(f.pptr() == 0);
  96. assert(f.epptr() == 0);
  97. assert(f.overflow(L'a') == L'a');
  98. assert(f.pbase() == 0);
  99. assert(f.pptr() == 0);
  100. assert(f.epptr() == 0);
  101. }
  102. {
  103. test_buf<wchar_t> f;
  104. assert(f.open("overflow.dat", std::ios_base::in) != 0);
  105. assert(f.is_open());
  106. assert(f.sgetc() == L'a');
  107. }
  108. std::remove("overflow.dat");
  109. {
  110. test_buf<wchar_t> f;
  111. f.pubimbue(std::locale(LOCALE_en_US_UTF_8));
  112. assert(f.open("overflow.dat", std::ios_base::out) != 0);
  113. assert(f.sputc(0x4E51) == 0x4E51);
  114. assert(f.sputc(0x4E52) == 0x4E52);
  115. assert(f.sputc(0x4E53) == 0x4E53);
  116. }
  117. {
  118. test_buf<char> f;
  119. assert(f.open("overflow.dat", std::ios_base::in) != 0);
  120. assert(f.is_open());
  121. assert(f.sbumpc() == 0xE4);
  122. assert(f.sbumpc() == 0xB9);
  123. assert(f.sbumpc() == 0x91);
  124. assert(f.sbumpc() == 0xE4);
  125. assert(f.sbumpc() == 0xB9);
  126. assert(f.sbumpc() == 0x92);
  127. assert(f.sbumpc() == 0xE4);
  128. assert(f.sbumpc() == 0xB9);
  129. assert(f.sbumpc() == 0x93);
  130. assert(f.sbumpc() == -1);
  131. }
  132. std::remove("overflow.dat");
  133. }