/gcc-3.4.6/libstdc++-v3/testsuite/27_io/basic_ostream/inserters_other/char/5.cc

https://github.com/MoSync/gcc · C++ · 99 lines · 52 code · 19 blank · 28 comment · 1 complexity · 26875c59d2800b6bb586dc35f813818f MD5 · raw file

  1. // 2003-09-22 Petur Runolfsson <peturr02@ru.is>
  2. // Copyright (C) 2003 Free Software Foundation
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 2, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License along
  14. // with this library; see the file COPYING. If not, write to the Free
  15. // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. // USA.
  17. // 27.6.2.5.3 basic_ostream manipulator inserters
  18. //
  19. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  20. // DR 60. What is a formatted input function?
  21. // Inserters for manipulators do not behave as formatted output functions.
  22. #include <ostream>
  23. #include <stdexcept>
  24. #include <testsuite_hooks.h>
  25. #include <testsuite_io.h>
  26. std::ostream& func1(std::ostream&)
  27. { throw std::runtime_error(""); }
  28. std::ios& func2(std::ios&)
  29. { throw std::runtime_error(""); }
  30. std::ios_base& func3(std::ios_base&)
  31. { throw std::runtime_error(""); }
  32. template<typename T>
  33. void test(T& (*f)(T&))
  34. {
  35. bool test = true;
  36. __gnu_test::sync_streambuf buf;
  37. std::ostream os(&buf);
  38. __gnu_test::sync_streambuf buf_tie;
  39. std::ostream os_tie(&buf_tie);
  40. // No sentry should be constructed so os.tie()->flush() should not be
  41. // called.
  42. os.tie(&os_tie);
  43. try
  44. {
  45. os << f;
  46. // Exceptions thrown by f should not be caught
  47. VERIFY( false );
  48. }
  49. catch (std::runtime_error&)
  50. {
  51. }
  52. // Exceptions thrown by f should not cause badbit to be set
  53. VERIFY( os.good() );
  54. VERIFY( !buf_tie.sync_called() );
  55. // The manipulator should be called even if !os.good().
  56. os.setstate(std::ios_base::eofbit);
  57. try
  58. {
  59. os << f;
  60. // Exceptions thrown by f should not be caught
  61. VERIFY( false );
  62. }
  63. catch (std::runtime_error&)
  64. {
  65. }
  66. // Exceptions thrown by f should not cause badbit to be set
  67. VERIFY( os.rdstate() == std::ios_base::eofbit );
  68. VERIFY( !buf_tie.sync_called() );
  69. }
  70. void test05()
  71. {
  72. test(&func1);
  73. test(&func2);
  74. test(&func3);
  75. }
  76. int main()
  77. {
  78. test05();
  79. return 0;
  80. }