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     {
48         std::wstringstream ss(L" 123 456 ");
49         assert(ss.rdbuf() != 0);
50         assert(ss.good());
51         assert(ss.str() == L" 123 456 ");
52         int i = 0;
53         ss >> i;
54         assert(i == 123);
55         ss >> i;
56         assert(i == 456);
57         ss << i << ' ' << 123;
58         assert(ss.str() == L"456 1236 ");
59     }
60     { // This is https://bugs.llvm.org/show_bug.cgi?id=33727
61         typedef std::basic_string   <char, std::char_traits<char>, NoDefaultAllocator<char> > S;
62         typedef std::basic_stringbuf<char, std::char_traits<char>, NoDefaultAllocator<char> > SB;
63 
64         S s(NoDefaultAllocator<char>(1));
65         SB sb(s);
66     //  This test is not required by the standard, but *where else* could it get the allocator?
67         assert(sb.str().get_allocator() == s.get_allocator());
68     }
69 
70   return 0;
71 }
72