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_ostringstream
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_ostringstream<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(ss.view() == SV("0123 456"));
39 ss << 456;
40 assert(ss.view() == SV("0456 456"));
41 ss.str(STR(" 789"));
42 assert(ss.view() == SV(" 789"));
43 ss << SV("abc");
44 assert(ss.view() == SV("abc9"));
45
46 const std::basic_ostringstream<CharT> css(STR("abc"));
47 static_assert(noexcept(css.view()));
48 assert(css.view() == SV("abc"));
49
50 std::basic_ostringstream<CharT, my_char_traits<CharT>> tss;
51 static_assert(std::is_same_v<decltype(tss.view()), std::basic_string_view<CharT, my_char_traits<CharT>>>);
52 }
53
main(int,char **)54 int main(int, char**) {
55 test<char>();
56 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
57 test<wchar_t>();
58 #endif
59 return 0;
60 }
61