stream_in.pass.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // test:
  9. // template <class charT, class traits, size_t N>
  10. // basic_istream<charT, traits>&
  11. // operator>>(basic_istream<charT, traits>& is, bitset<N>& x);
  12. #include <bitset>
  13. #include <sstream>
  14. #include <cassert>
  15. #include "test_macros.h"
  16. int main(int, char**)
  17. {
  18. {
  19. std::istringstream in("01011010");
  20. std::bitset<8> b;
  21. in >> b;
  22. assert(b.to_ulong() == 0x5A);
  23. }
  24. {
  25. // Make sure that input-streaming an empty bitset does not cause the
  26. // failbit to be set (LWG 3199).
  27. std::istringstream in("01011010");
  28. std::bitset<0> b;
  29. in >> b;
  30. assert(b.to_string() == "");
  31. assert(!in.bad());
  32. assert(!in.fail());
  33. assert(!in.eof());
  34. assert(in.good());
  35. }
  36. #ifndef TEST_HAS_NO_EXCEPTIONS
  37. {
  38. std::stringbuf sb;
  39. std::istream is(&sb);
  40. is.exceptions(std::ios::failbit);
  41. bool threw = false;
  42. try {
  43. std::bitset<8> b;
  44. is >> b;
  45. } catch (std::ios::failure const&) {
  46. threw = true;
  47. }
  48. assert(!is.bad());
  49. assert(is.fail());
  50. assert(is.eof());
  51. assert(threw);
  52. }
  53. {
  54. std::stringbuf sb;
  55. std::istream is(&sb);
  56. is.exceptions(std::ios::eofbit);
  57. bool threw = false;
  58. try {
  59. std::bitset<8> b;
  60. is >> b;
  61. } catch (std::ios::failure const&) {
  62. threw = true;
  63. }
  64. assert(!is.bad());
  65. assert(is.fail());
  66. assert(is.eof());
  67. assert(threw);
  68. }
  69. #endif // TEST_HAS_NO_EXCEPTIONS
  70. return 0;
  71. }