blob: f808bcd14dead949b7619c92dadc7988be91e0e4 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Howard Hinnant5b08a8a2010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnant3e519522010-05-11 19:42:16 +00004//
Howard Hinnant412dbeb2010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant3e519522010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10// <sstream>
11
12// template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
13// class basic_stringstream
14
Howard Hinnant66dbf0d2010-08-22 00:26:48 +000015// explicit basic_stringstream(const basic_string<charT,traits,Allocator>& str,
Howard Hinnant3e519522010-05-11 19:42:16 +000016// ios_base::openmode which = ios_base::out|ios_base::in);
17
18#include <sstream>
19#include <cassert>
20
Marshall Clowa054f822017-08-02 17:31:09 +000021template<typename T>
22struct NoDefaultAllocator : std::allocator<T>
23{
24 template<typename U> struct rebind { using other = NoDefaultAllocator<U>; };
Marshall Clow4f4fc2e2017-08-02 18:21:34 +000025 NoDefaultAllocator(int id_) : id(id_) { }
Marshall Clowa054f822017-08-02 17:31:09 +000026 template<typename U> NoDefaultAllocator(const NoDefaultAllocator<U>& a) : id(a.id) { }
27 int id;
28};
29
30
Howard Hinnant3e519522010-05-11 19:42:16 +000031int main()
32{
33 {
34 std::stringstream ss(" 123 456 ");
35 assert(ss.rdbuf() != 0);
36 assert(ss.good());
37 assert(ss.str() == " 123 456 ");
38 int i = 0;
39 ss >> i;
40 assert(i == 123);
41 ss >> i;
42 assert(i == 456);
43 ss << i << ' ' << 123;
44 assert(ss.str() == "456 1236 ");
45 }
46 {
47 std::wstringstream ss(L" 123 456 ");
48 assert(ss.rdbuf() != 0);
49 assert(ss.good());
50 assert(ss.str() == L" 123 456 ");
51 int i = 0;
52 ss >> i;
53 assert(i == 123);
54 ss >> i;
55 assert(i == 456);
56 ss << i << ' ' << 123;
57 assert(ss.str() == L"456 1236 ");
58 }
Marshall Clowa054f822017-08-02 17:31:09 +000059 { // This is https://bugs.llvm.org/show_bug.cgi?id=33727
60 typedef std::basic_string <char, std::char_traits<char>, NoDefaultAllocator<char> > S;
61 typedef std::basic_stringbuf<char, std::char_traits<char>, NoDefaultAllocator<char> > SB;
62
63 S s(NoDefaultAllocator<char>(1));
64 SB sb(s);
65 // This test is not required by the standard, but *where else* could it get the allocator?
66 assert(sb.str().get_allocator() == s.get_allocator());
67 }
Howard Hinnant3e519522010-05-11 19:42:16 +000068}