testsuite_io.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // -*- C++ -*-
  2. // Testing streambuf/filebuf/stringbuf for the C++ library testsuite.
  3. //
  4. // Copyright (C) 2003-2022 Free Software Foundation, Inc.
  5. //
  6. // This file is part of the GNU ISO C++ Library. This library is free
  7. // software; you can redistribute it and/or modify it under the
  8. // terms of the GNU General Public License as published by the
  9. // Free Software Foundation; either version 3, or (at your option)
  10. // any later version.
  11. //
  12. // This library is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License along
  18. // with this library; see the file COPYING3. If not see
  19. // <http://www.gnu.org/licenses/>.
  20. //
  21. #ifndef _GLIBCXX_TESTSUITE_IO_H
  22. #define _GLIBCXX_TESTSUITE_IO_H
  23. #include <ios>
  24. namespace __gnu_test
  25. {
  26. // Used to verify the constraints/requirements on get and put areas
  27. // as defined in
  28. // 27.5.1 - Stream buffer requirements: get and put areas
  29. // 27.8.1.1 - Template class basic_filebuf p 3
  30. // If the file is not open (ios_base::in) -> input seq. cannot be read
  31. // If the file is not open (ios_base::out) -> output seq. cannot be written
  32. // Joint file position
  33. // 27.8.1.4 - Overridden virtual functions p9
  34. // If unbuffered, pbase == pptr == NULL
  35. // 27.7.1.1 - Basic_stringbuf constructors p 1
  36. // 27.8.1.2 - Basic_filebuf constructors p 1
  37. // ... , initializing the base class with basic_streambuf() 27.5.2.1
  38. template<typename T>
  39. class constraint_buf
  40. : public T
  41. {
  42. public:
  43. bool
  44. write_position()
  45. {
  46. bool one = this->pptr() != 0;
  47. bool two = this->pptr() < this->epptr();
  48. return one && two;
  49. }
  50. bool
  51. read_position()
  52. {
  53. bool one = this->gptr() != 0;
  54. bool two = this->gptr() < this->egptr();
  55. return one && two;
  56. }
  57. bool
  58. unbuffered()
  59. {
  60. bool one = this->pbase() == 0;
  61. bool two = this->pptr() == 0;
  62. return one && two;
  63. }
  64. bool
  65. check_pointers()
  66. {
  67. bool one = this->eback() == 0;
  68. bool two = this->gptr() == 0;
  69. bool three = this->egptr() == 0;
  70. bool four = this->pbase() == 0;
  71. bool five = this->pptr() == 0;
  72. bool six = this->epptr() == 0;
  73. return one && two && three && four && five && six;
  74. }
  75. };
  76. typedef constraint_buf<std::streambuf> constraint_streambuf;
  77. typedef constraint_buf<std::filebuf> constraint_filebuf;
  78. typedef constraint_buf<std::stringbuf> constraint_stringbuf;
  79. #ifdef _GLIBCXX_USE_WCHAR_T
  80. typedef constraint_buf<std::wstreambuf> constraint_wstreambuf;
  81. typedef constraint_buf<std::wfilebuf> constraint_wfilebuf;
  82. typedef constraint_buf<std::wstringbuf> constraint_wstringbuf;
  83. #endif
  84. // Used to check if basic_streambuf::pubsync() has been called.
  85. // This is useful for checking if a function creates [io]stream::sentry
  86. // objects, since the sentry constructors call tie()->flush().
  87. template<typename T>
  88. class sync_buf
  89. : public T
  90. {
  91. private:
  92. bool m_sync_called;
  93. public:
  94. sync_buf()
  95. : m_sync_called(false)
  96. { }
  97. bool sync_called() const
  98. { return m_sync_called; }
  99. protected:
  100. int sync()
  101. {
  102. m_sync_called = true;
  103. return 0;
  104. }
  105. };
  106. typedef sync_buf<std::streambuf> sync_streambuf;
  107. #ifdef _GLIBCXX_USE_WCHAR_T
  108. typedef sync_buf<std::wstreambuf> sync_wstreambuf;
  109. #endif
  110. // Throws on all overflow and underflow calls.
  111. struct underflow_error: std::exception { };
  112. struct overflow_error: std::exception { };
  113. struct positioning_error: std::exception { };
  114. template<typename T>
  115. struct fail_buf
  116. : public T
  117. {
  118. typedef typename T::char_type char_type;
  119. typedef typename T::int_type int_type;
  120. typedef typename T::off_type off_type;
  121. typedef typename T::pos_type pos_type;
  122. private:
  123. char_type p[2];
  124. public:
  125. fail_buf()
  126. {
  127. p[0] = char_type('s');
  128. p[1] = char_type();
  129. this->setg(p, p, p + 1);
  130. }
  131. virtual int_type underflow()
  132. {
  133. throw underflow_error();
  134. return int_type();
  135. }
  136. virtual int_type uflow()
  137. {
  138. throw underflow_error();
  139. return int_type();
  140. }
  141. virtual int_type
  142. overflow(int_type)
  143. {
  144. throw overflow_error();
  145. return int_type();
  146. }
  147. virtual pos_type
  148. seekoff(off_type, std::ios_base::seekdir, std::ios_base::openmode)
  149. {
  150. throw positioning_error();
  151. return pos_type(off_type(-1));
  152. }
  153. virtual pos_type
  154. seekpos(pos_type, std::ios_base::openmode)
  155. {
  156. throw positioning_error();
  157. return pos_type(off_type(-1));
  158. }
  159. virtual int
  160. sync()
  161. {
  162. throw positioning_error();
  163. return 0;
  164. }
  165. };
  166. typedef fail_buf<std::streambuf> fail_streambuf;
  167. #ifdef _GLIBCXX_USE_WCHAR_T
  168. typedef fail_buf<std::wstreambuf> fail_wstreambuf;
  169. #endif
  170. // Facets that throw an exception for every virtual function.
  171. struct facet_error: std::exception { };
  172. template<typename T>
  173. class fail_num_get
  174. : public std::num_get<T>
  175. {
  176. typedef std::ios_base ios_base;
  177. typedef typename std::num_get<T>::iter_type iter_type;
  178. protected:
  179. iter_type
  180. do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, bool&) const
  181. { throw facet_error(); return iter_type(); }
  182. virtual iter_type
  183. do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, long&) const
  184. { throw facet_error(); return iter_type(); }
  185. virtual iter_type
  186. do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
  187. unsigned short&) const
  188. { throw facet_error(); return iter_type(); }
  189. virtual iter_type
  190. do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
  191. unsigned int&) const
  192. { throw facet_error(); return iter_type(); }
  193. virtual iter_type
  194. do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
  195. unsigned long&) const
  196. { throw facet_error(); return iter_type(); }
  197. #ifdef _GLIBCXX_USE_LONG_LONG
  198. virtual iter_type
  199. do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
  200. long long&) const
  201. { throw facet_error(); return iter_type(); }
  202. virtual iter_type
  203. do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
  204. unsigned long long&) const
  205. { throw facet_error(); return iter_type(); }
  206. #endif
  207. virtual iter_type
  208. do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
  209. float&) const
  210. { throw facet_error(); return iter_type(); }
  211. virtual iter_type
  212. do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
  213. double&) const
  214. { throw facet_error(); return iter_type(); }
  215. virtual iter_type
  216. do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
  217. long double&) const
  218. { throw facet_error(); return iter_type(); }
  219. virtual iter_type
  220. do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
  221. void*&) const
  222. { throw facet_error(); return iter_type(); }
  223. };
  224. typedef fail_num_get<char> fail_num_get_char;
  225. #ifdef _GLIBCXX_USE_WCHAR_T
  226. typedef fail_num_get<wchar_t> fail_num_get_wchar_t;
  227. #endif
  228. template<typename T>
  229. class fail_num_put
  230. : public std::num_put<T>
  231. {
  232. typedef std::ios_base ios_base;
  233. typedef typename std::num_put<T>::iter_type iter_type;
  234. typedef typename std::num_put<T>::char_type char_type;
  235. protected:
  236. iter_type
  237. do_put(iter_type, ios_base&, char_type, bool) const
  238. { throw facet_error(); return iter_type(0); }
  239. virtual iter_type
  240. do_put(iter_type, ios_base&, char_type, long) const
  241. { throw facet_error(); return iter_type(0); }
  242. virtual iter_type
  243. do_put(iter_type, ios_base&, char_type, unsigned long) const
  244. { throw facet_error(); return iter_type(0); }
  245. #ifdef _GLIBCXX_USE_LONG_LONG
  246. virtual iter_type
  247. do_put(iter_type, ios_base&, char_type, long long) const
  248. { throw facet_error(); return iter_type(0); }
  249. virtual iter_type
  250. do_put(iter_type, ios_base&, char_type, unsigned long long) const
  251. { throw facet_error(); return iter_type(0); }
  252. #endif
  253. virtual iter_type
  254. do_put(iter_type, ios_base&, char_type, double) const
  255. { throw facet_error(); return iter_type(0); }
  256. virtual iter_type
  257. do_put(iter_type, ios_base&, char_type, long double) const
  258. { throw facet_error(); return iter_type(0); }
  259. virtual iter_type
  260. do_put(iter_type, ios_base&, char_type, const void*) const
  261. { throw facet_error(); return iter_type(0); }
  262. };
  263. typedef fail_num_put<char> fail_num_put_char;
  264. #ifdef _GLIBCXX_USE_WCHAR_T
  265. typedef fail_num_put<wchar_t> fail_num_put_wchar_t;
  266. #endif
  267. } // namespace __gnu_test
  268. #endif // _GLIBCXX_TESTSUITE_IO_H