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 #include <sstream>
18 #include <cassert>
19
20 #include "test_macros.h"
21
22 template<typename T>
23 struct NoDefaultAllocator : std::allocator<T>
24 {
25 template<typename U> struct rebind { using other = NoDefaultAllocator<U>; };
NoDefaultAllocatorNoDefaultAllocator26 NoDefaultAllocator(int id_) : id(id_) { }
NoDefaultAllocatorNoDefaultAllocator27 template<typename U> NoDefaultAllocator(const NoDefaultAllocator<U>& a) : id(a.id) { }
28 int id;
29 };
30
31
main(int,char **)32 int main(int, char**)
33 {
34 {
35 std::stringstream ss(" 123 456 ");
36 assert(ss.rdbuf() != 0);
37 assert(ss.good());
38 assert(ss.str() == " 123 456 ");
39 int i = 0;
40 ss >> i;
41 assert(i == 123);
42 ss >> i;
43 assert(i == 456);
44 ss << i << ' ' << 123;
45 assert(ss.str() == "456 1236 ");
46 }
47 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
48 {
49 std::wstringstream ss(L" 123 456 ");
50 assert(ss.rdbuf() != 0);
51 assert(ss.good());
52 assert(ss.str() == L" 123 456 ");
53 int i = 0;
54 ss >> i;
55 assert(i == 123);
56 ss >> i;
57 assert(i == 456);
58 ss << i << ' ' << 123;
59 assert(ss.str() == L"456 1236 ");
60 }
61 #endif
62 { // This is https://llvm.org/PR33727
63 typedef std::basic_string <char, std::char_traits<char>, NoDefaultAllocator<char> > S;
64 typedef std::basic_stringbuf<char, std::char_traits<char>, NoDefaultAllocator<char> > SB;
65
66 S s(NoDefaultAllocator<char>(1));
67 SB sb(s);
68 // This test is not required by the standard, but *where else* could it get the allocator?
69 assert(sb.str().get_allocator() == s.get_allocator());
70 }
71
72 return 0;
73 }
74