| 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 | |
| 9 | // <sstream> |
| 10 | |
| 11 | // template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> > |
| 12 | // class basic_stringstream |
| 13 | |
| 14 | // explicit basic_stringstream(const basic_string<charT,traits,Allocator>& str, |
| 15 | // ios_base::openmode which = ios_base::out|ios_base::in); |
| 16 | |
| 17 | // XFAIL: FROZEN-CXX03-HEADERS-FIXME |
| 18 | |
| 19 | #include <sstream> |
| 20 | #include <cassert> |
| 21 | |
| 22 | #include "test_macros.h" |
| 23 | #include "operator_hijacker.h" |
| 24 | |
| 25 | template<typename T> |
| 26 | struct NoDefaultAllocator : std::allocator<T> |
| 27 | { |
| 28 | template<typename U> struct rebind { using other = NoDefaultAllocator<U>; }; |
| 29 | NoDefaultAllocator(int id_) : id(id_) { } |
| 30 | template<typename U> NoDefaultAllocator(const NoDefaultAllocator<U>& a) : id(a.id) { } |
| 31 | int id; |
| 32 | }; |
| 33 | |
| 34 | |
| 35 | int main(int, char**) |
| 36 | { |
| 37 | { |
| 38 | std::stringstream ss(" 123 456 " ); |
| 39 | assert(ss.rdbuf() != nullptr); |
| 40 | assert(ss.good()); |
| 41 | assert(ss.str() == " 123 456 " ); |
| 42 | int i = 0; |
| 43 | ss >> i; |
| 44 | assert(i == 123); |
| 45 | ss >> i; |
| 46 | assert(i == 456); |
| 47 | ss << i << ' ' << 123; |
| 48 | assert(ss.str() == "456 1236 " ); |
| 49 | } |
| 50 | { |
| 51 | std::basic_stringstream<char, std::char_traits<char>, operator_hijacker_allocator<char> > ss(" 123 456 " ); |
| 52 | assert(ss.rdbuf() != nullptr); |
| 53 | assert(ss.good()); |
| 54 | assert(ss.str() == " 123 456 " ); |
| 55 | int i = 0; |
| 56 | ss >> i; |
| 57 | assert(i == 123); |
| 58 | ss >> i; |
| 59 | assert(i == 456); |
| 60 | ss << i << ' ' << 123; |
| 61 | assert(ss.str() == "456 1236 " ); |
| 62 | } |
| 63 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 64 | { |
| 65 | std::wstringstream ss(L" 123 456 " ); |
| 66 | assert(ss.rdbuf() != nullptr); |
| 67 | assert(ss.good()); |
| 68 | assert(ss.str() == L" 123 456 " ); |
| 69 | int i = 0; |
| 70 | ss >> i; |
| 71 | assert(i == 123); |
| 72 | ss >> i; |
| 73 | assert(i == 456); |
| 74 | ss << i << ' ' << 123; |
| 75 | assert(ss.str() == L"456 1236 " ); |
| 76 | } |
| 77 | { |
| 78 | std::basic_stringstream<wchar_t, std::char_traits<wchar_t>, operator_hijacker_allocator<wchar_t> > ss( |
| 79 | L" 123 456 " ); |
| 80 | assert(ss.rdbuf() != nullptr); |
| 81 | assert(ss.good()); |
| 82 | assert(ss.str() == L" 123 456 " ); |
| 83 | int i = 0; |
| 84 | ss >> i; |
| 85 | assert(i == 123); |
| 86 | ss >> i; |
| 87 | assert(i == 456); |
| 88 | ss << i << ' ' << 123; |
| 89 | assert(ss.str() == L"456 1236 " ); |
| 90 | } |
| 91 | #endif |
| 92 | { // This is https://llvm.org/PR33727 |
| 93 | typedef std::basic_string <char, std::char_traits<char>, NoDefaultAllocator<char> > S; |
| 94 | typedef std::basic_stringbuf<char, std::char_traits<char>, NoDefaultAllocator<char> > SB; |
| 95 | |
| 96 | S s(NoDefaultAllocator<char>(1)); |
| 97 | SB sb(s); |
| 98 | // This test is not required by the standard, but *where else* could it get the allocator? |
| 99 | assert(sb.str().get_allocator() == s.get_allocator()); |
| 100 | } |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |