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 || TEST_STD_VER >= 20) { 56 assert((std::basic_string<CharT, Traits>(v[i]) == v[j]) == expected); 57 assert((v[i] == std::basic_string<CharT, Traits>(v[j])) == expected); 58 } 59 } 60 } 61 62 // Test its behavior with embedded null bytes. 63 SV abc = SV(MAKE_CSTRING(CharT, "abc")); 64 SV abc0def = SV(MAKE_CSTRING(CharT, "abc\0def"), 7); 65 SV abcdef = SV(MAKE_CSTRING(CharT, "abcdef")); 66 assert((abc == abc0def) == false); 67 assert((abc == abcdef) == false); 68 assert((abc0def == abc) == false); 69 assert((abc0def == abcdef) == false); 70 assert((abcdef == abc) == false); 71 assert((abcdef == abc0def) == false); 72 73 assert((abc.data() == abc0def) == false); 74 assert((abc0def == abc.data()) == false); 75 76 if (!TEST_IS_CONSTANT_EVALUATED || TEST_STD_VER >= 20) { 77 assert((std::basic_string<CharT, Traits>(abc) == abc0def) == false); 78 assert((abc0def == std::basic_string<CharT, Traits>(abc)) == false); 79 } 80 81 return true; 82 } 83 84 int main(int, char**) 85 { 86 test<std::string_view>(); 87 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 88 test<std::wstring_view>(); 89 #endif 90 #if TEST_STD_VER >= 11 91 test<std::u16string_view>(); 92 test<std::u32string_view>(); 93 #endif 94 #if TEST_STD_VER > 14 95 static_assert(test<std::string_view>(), ""); 96 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 97 static_assert(test<std::wstring_view>(), ""); 98 #endif 99 static_assert(test<std::u16string_view>(), ""); 100 static_assert(test<std::u32string_view>(), ""); 101 #endif 102 103 #if TEST_STD_VER > 11 104 test<std::basic_string_view<char, constexpr_char_traits<char>>>(); 105 static_assert(test<std::basic_string_view<char, constexpr_char_traits<char>>>(), ""); 106 #endif 107 108 #if TEST_STD_VER > 17 109 test<std::u8string_view>(); 110 static_assert(test<std::u8string_view>()); 111 #endif 112 113 return 0; 114 } 115