1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // UNSUPPORTED: c++98, c++03, c++11, c++14
11 // UNSUPPORTED: libcpp-no-deduction-guides
12 
13 // FIXME(EricWF): As of 16/Feb/2017 Clang hits an assertion when compiling this
14 // test with '-g' (as the sanitizers do).
15 // XFAIL: ubsan, asan, msan, tsan
16 
17 // <string_view>
18 
19 // Test that the constructors offered by std::basic_string_view are formulated
20 // so they're compatible with implicit deduction guides.
21 
22 #include <string_view>
23 #include <cassert>
24 
25 #include "test_macros.h"
26 #include "test_allocator.h"
27 #include "constexpr_char_traits.hpp"
28 
29 // Overloads
30 // ---------------
31 // (1)  basic_string_view() - NOT TESTED
32 // (2)  basic_string_view(const basic_string_view&)
33 // (3)  basic_string_view(const CharT*, size_type)
34 // (4)  basic_string_view(const CharT*)
35 int main()
36 {
37   using TestSizeT = test_allocator<char>::size_type;
38   { // Testing (1)
39     // Nothing TODO. Cannot deduce without any arguments.
40   }
41   { // Testing (2)
42     const std::string_view sin("abc");
43     std::basic_string_view s(sin);
44     ASSERT_SAME_TYPE(decltype(s), std::string_view);
45     assert(s == "abc");
46 
47     using WSV = std::basic_string_view<wchar_t, constexpr_char_traits<wchar_t>>;
48     const WSV win(L"abcdef");
49     std::basic_string_view w(win);
50     ASSERT_SAME_TYPE(decltype(w), WSV);
51     assert(w == L"abcdef");
52   }
53   { // Testing (3)
54     std::basic_string_view s("abc", 2);
55     ASSERT_SAME_TYPE(decltype(s), std::string_view);
56     assert(s == "ab");
57 
58     std::basic_string_view w(L"abcdef", 4);
59     ASSERT_SAME_TYPE(decltype(w), std::wstring_view);
60     assert(w == L"abcd");
61   }
62   { // Testing (4)
63     std::basic_string_view s("abc");
64     ASSERT_SAME_TYPE(decltype(s), std::string_view);
65     assert(s == "abc");
66 
67     std::basic_string_view w(L"abcdef");
68     ASSERT_SAME_TYPE(decltype(w), std::wstring_view);
69     assert(w == L"abcdef");
70   }
71 }
72