blob: 7bc383f9a4a6e670dfc011f8beed218cbf531c99 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// 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
Howard Hinnant3e519522010-05-11 19:42:16 +00006//
7//===----------------------------------------------------------------------===//
8
9// <sstream>
10
11// template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
12// class basic_stringstream
13
Howard Hinnant66dbf0d2010-08-22 00:26:48 +000014// explicit basic_stringstream(const basic_string<charT,traits,Allocator>& str,
Howard Hinnant3e519522010-05-11 19:42:16 +000015// ios_base::openmode which = ios_base::out|ios_base::in);
16
17#include <sstream>
18#include <cassert>
19
Marshall Clowa054f822017-08-02 17:31:09 +000020template<typename T>
21struct NoDefaultAllocator : std::allocator<T>
22{
23 template<typename U> struct rebind { using other = NoDefaultAllocator<U>; };
Marshall Clow4f4fc2e2017-08-02 18:21:34 +000024 NoDefaultAllocator(int id_) : id(id_) { }
Marshall Clowa054f822017-08-02 17:31:09 +000025 template<typename U> NoDefaultAllocator(const NoDefaultAllocator<U>& a) : id(a.id) { }
26 int id;
27};
28
29
Howard Hinnant3e519522010-05-11 19:42:16 +000030int main()
31{
32 {
33 std::stringstream ss(" 123 456 ");
34 assert(ss.rdbuf() != 0);
35 assert(ss.good());
36 assert(ss.str() == " 123 456 ");
37 int i = 0;
38 ss >> i;
39 assert(i == 123);
40 ss >> i;
41 assert(i == 456);
42 ss << i << ' ' << 123;
43 assert(ss.str() == "456 1236 ");
44 }
45 {
46 std::wstringstream ss(L" 123 456 ");
47 assert(ss.rdbuf() != 0);
48 assert(ss.good());
49 assert(ss.str() == L" 123 456 ");
50 int i = 0;
51 ss >> i;
52 assert(i == 123);
53 ss >> i;
54 assert(i == 456);
55 ss << i << ' ' << 123;
56 assert(ss.str() == L"456 1236 ");
57 }
Marshall Clowa054f822017-08-02 17:31:09 +000058 { // This is https://bugs.llvm.org/show_bug.cgi?id=33727
Stephan T. Lavavejc0990102017-08-05 00:44:27 +000059 typedef std::basic_string <char, std::char_traits<char>, NoDefaultAllocator<char> > S;
60 typedef std::basic_stringbuf<char, std::char_traits<char>, NoDefaultAllocator<char> > SB;
Marshall Clowa054f822017-08-02 17:31:09 +000061
Stephan T. Lavavejc0990102017-08-05 00:44:27 +000062 S s(NoDefaultAllocator<char>(1));
63 SB sb(s);
64 // This test is not required by the standard, but *where else* could it get the allocator?
65 assert(sb.str().get_allocator() == s.get_allocator());
Marshall Clowa054f822017-08-02 17:31:09 +000066 }
Howard Hinnant3e519522010-05-11 19:42:16 +000067}