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 // UNSUPPORTED: c++03, c++11, c++14, c++17
10
11 // <sstream>
12
13 // template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
14 // class basic_stringstream
15
16 // basic_string_view<charT, traits> view() const noexcept;
17
18 #include <sstream>
19 #include <cassert>
20 #include <type_traits>
21
22 #include "make_string.h"
23 #include "test_macros.h"
24
25 #define STR(S) MAKE_STRING(CharT, S)
26 #define SV(S) MAKE_STRING_VIEW(CharT, S)
27
28 template <class CharT>
29 struct my_char_traits : public std::char_traits<CharT> {};
30
31 template <class CharT>
test()32 static void test() {
33 std::basic_stringstream<CharT> ss(STR(" 123 456 "));
34 static_assert(noexcept(ss.view()));
35 assert(ss.view() == SV(" 123 456 "));
36 int i = 0;
37 ss >> i;
38 assert(i == 123);
39 ss >> i;
40 assert(i == 456);
41 ss << i << ' ' << 123;
42 assert(ss.view() == SV("456 1236 "));
43 ss.str(STR("5466 89 "));
44 ss >> i;
45 assert(i == 5466);
46 ss >> i;
47 assert(i == 89);
48 ss << i << ' ' << 321;
49 assert(ss.view() == SV("89 3219 "));
50
51 const std::basic_stringstream<CharT> css(STR("abc"));
52 static_assert(noexcept(css.view()));
53 assert(css.view() == SV("abc"));
54
55 std::basic_stringstream<CharT, my_char_traits<CharT>> tss;
56 static_assert(std::is_same_v<decltype(tss.view()), std::basic_string_view<CharT, my_char_traits<CharT>>>);
57 }
58
main(int,char **)59 int main(int, char**) {
60 test<char>();
61 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
62 test<wchar_t>();
63 #endif
64 std::stringstream ss;
65 ss.write("\xd1", 1);
66 assert(ss.view().length() == 1);
67 return 0;
68 }
69