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 // <string_view> 10 11 // template<class charT, class traits> 12 // constexpr bool operator<(basic_string_view<charT, traits> lhs, basic_string_view<charT, traits> rhs); 13 // (plus "sufficient additional overloads" to make implicit conversions work as intended) 14 15 #include <string_view> 16 #include <cassert> 17 #include <string> 18 19 #include "test_macros.h" 20 #include "constexpr_char_traits.h" 21 #include "make_string.h" 22 23 template<class T> 24 struct ConvertibleTo { 25 T t_; 26 TEST_CONSTEXPR explicit ConvertibleTo(T t) : t_(t) {} 27 TEST_CONSTEXPR operator T() const { 28 return t_; 29 } 30 }; 31 32 template<class SV> 33 TEST_CONSTEXPR_CXX14 bool test() 34 { 35 typedef typename SV::value_type CharT; 36 typedef typename SV::traits_type Traits; 37 38 // Test the behavior of the operator, both with and without implicit conversions. 39 SV v[] = { 40 SV(MAKE_CSTRING(CharT, "")), 41 SV(MAKE_CSTRING(CharT, "abc")), 42 SV(MAKE_CSTRING(CharT, "abcdef")), 43 SV(MAKE_CSTRING(CharT, "acb")), 44 }; 45 for (int i = 0; i < 4; ++i) { 46 for (int j = 0; j < 4; ++j) { 47 // See http://eel.is/c++draft/string.view#tab:string.view.comparison.overloads 48 bool expected = (i < j); 49 assert((v[i] < v[j]) == expected); 50 assert((v[i].data() < v[j]) == expected); 51 assert((v[i] < v[j].data()) == expected); 52 assert((ConvertibleTo<SV>(v[i]) < v[j]) == expected); 53 assert((v[i] < ConvertibleTo<SV>(v[j])) == expected); 54 55 if (!TEST_IS_CONSTANT_EVALUATED) { 56 // TODO FIXME: once P0980 "Making std::string constexpr" is implemented 57 assert((std::basic_string<CharT, Traits>(v[i]) < v[j]) == expected); 58 assert((v[i] < std::basic_string<CharT, Traits>(v[j])) == expected); 59 } 60 } 61 } 62 63 // Test its behavior with embedded null bytes. 64 SV abc = SV(MAKE_CSTRING(CharT, "abc")); 65 SV abc0def = SV(MAKE_CSTRING(CharT, "abc\0def"), 7); 66 SV abcdef = SV(MAKE_CSTRING(CharT, "abcdef")); 67 assert((abc < abc0def) == true); 68 assert((abc < abcdef) == true); 69 assert((abc0def < abc) == false); 70 assert((abc0def < abcdef) == true); 71 assert((abcdef < abc) == false); 72 assert((abcdef < abc0def) == false); 73 74 assert((abc.data() < abc0def) == true); 75 assert((abc0def < abc.data()) == false); 76 77 if (!TEST_IS_CONSTANT_EVALUATED) { 78 // TODO FIXME: once P0980 "Making std::string constexpr" is implemented 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 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