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: !stdlib=libc++ && (c++03 || c++11 || c++14)
10
11 // <string_view>
12
13 // template<class charT, class traits>
14 // constexpr bool operator<=(basic_string_view<charT, traits> lhs, basic_string_view<charT, traits> rhs);
15 // (plus "sufficient additional overloads" to make implicit conversions work as intended)
16
17 #include <string_view>
18 #include <cassert>
19 #include <string>
20
21 #include "test_macros.h"
22 #include "constexpr_char_traits.h"
23 #include "make_string.h"
24
25 template<class T>
26 struct ConvertibleTo {
27 T t_;
ConvertibleToConvertibleTo28 TEST_CONSTEXPR explicit ConvertibleTo(T t) : t_(t) {}
operator TConvertibleTo29 TEST_CONSTEXPR operator T() const {
30 return t_;
31 }
32 };
33
34 template<class SV>
test()35 TEST_CONSTEXPR_CXX14 bool test()
36 {
37 typedef typename SV::value_type CharT;
38 typedef typename SV::traits_type Traits;
39
40 // Test the behavior of the operator, both with and without implicit conversions.
41 SV v[] = {
42 SV(MAKE_CSTRING(CharT, "")),
43 SV(MAKE_CSTRING(CharT, "abc")),
44 SV(MAKE_CSTRING(CharT, "abcdef")),
45 SV(MAKE_CSTRING(CharT, "acb")),
46 };
47 for (int i = 0; i < 4; ++i) {
48 for (int j = 0; j < 4; ++j) {
49 // See http://eel.is/c++draft/string.view#tab:string.view.comparison.overloads
50 bool expected = (i <= j);
51 assert((v[i] <= v[j]) == expected);
52 assert((v[i].data() <= v[j]) == expected);
53 assert((v[i] <= v[j].data()) == expected);
54 assert((ConvertibleTo<SV>(v[i]) <= v[j]) == expected);
55 assert((v[i] <= ConvertibleTo<SV>(v[j])) == expected);
56
57 if (!TEST_IS_CONSTANT_EVALUATED || TEST_STD_VER >= 20) {
58 assert((std::basic_string<CharT, Traits>(v[i]) <= v[j]) == expected);
59 assert((v[i] <= std::basic_string<CharT, Traits>(v[j])) == expected);
60 }
61 }
62 }
63
64 // Test its behavior with embedded null bytes.
65 SV abc = SV(MAKE_CSTRING(CharT, "abc"));
66 SV abc0def = SV(MAKE_CSTRING(CharT, "abc\0def"), 7);
67 SV abcdef = SV(MAKE_CSTRING(CharT, "abcdef"));
68 assert((abc <= abc0def) == true);
69 assert((abc <= abcdef) == true);
70 assert((abc0def <= abc) == false);
71 assert((abc0def <= abcdef) == true);
72 assert((abcdef <= abc) == false);
73 assert((abcdef <= abc0def) == false);
74
75 assert((abc.data() <= abc0def) == true);
76 assert((abc0def <= abc.data()) == false);
77
78 if (!TEST_IS_CONSTANT_EVALUATED || TEST_STD_VER >= 20) {
79 assert((std::basic_string<CharT, Traits>(abc) <= abc0def) == true);
80 assert((abc0def <= std::basic_string<CharT, Traits>(abc)) == false);
81 }
82
83 return true;
84 }
85
main(int,char **)86 int main(int, char**)
87 {
88 test<std::string_view>();
89 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
90 test<std::wstring_view>();
91 #endif
92 #if TEST_STD_VER >= 11
93 test<std::u16string_view>();
94 test<std::u32string_view>();
95 #endif
96 #if TEST_STD_VER > 14
97 static_assert(test<std::string_view>(), "");
98 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
99 static_assert(test<std::wstring_view>(), "");
100 #endif
101 static_assert(test<std::u16string_view>(), "");
102 static_assert(test<std::u32string_view>(), "");
103 #endif
104
105 #if TEST_STD_VER > 11
106 test<std::basic_string_view<char, constexpr_char_traits<char>>>();
107 static_assert(test<std::basic_string_view<char, constexpr_char_traits<char>>>(), "");
108 #endif
109
110 #if TEST_STD_VER > 17
111 test<std::u8string_view>();
112 static_assert(test<std::u8string_view>());
113 #endif
114
115 return 0;
116 }
117