xref: /llvm-project/libcxx/test/std/strings/string.view/string.view.comparison/equal.pass.cpp (revision 164c204a19f7859d570003d4c5e82faf48cb65a9)
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 { return t_; }
30 };
31 
32 template <class SV>
test()33 TEST_CONSTEXPR_CXX14 bool test() {
34   typedef typename SV::value_type CharT;
35   typedef typename SV::traits_type Traits;
36 
37   // Test the behavior of the operator, both with and without implicit conversions.
38   SV v[] = {
39       SV(MAKE_CSTRING(CharT, "")),
40       SV(MAKE_CSTRING(CharT, "abc")),
41       SV(MAKE_CSTRING(CharT, "abcdef")),
42       SV(MAKE_CSTRING(CharT, "acb")),
43   };
44   for (int i = 0; i < 4; ++i) {
45     for (int j = 0; j < 4; ++j) {
46       // See http://eel.is/c++draft/string.view#tab:string.view.comparison.overloads
47       bool expected = (i == j);
48       assert((v[i] == v[j]) == expected);
49       assert((v[i].data() == v[j]) == expected);
50       assert((v[i] == v[j].data()) == expected);
51       assert((ConvertibleTo<SV>(v[i]) == v[j]) == expected);
52       assert((v[i] == ConvertibleTo<SV>(v[j])) == expected);
53 
54       if (TEST_STD_AT_LEAST_20_OR_RUNTIME_EVALUATED) {
55         assert((std::basic_string<CharT, Traits>(v[i]) == v[j]) == expected);
56         assert((v[i] == std::basic_string<CharT, Traits>(v[j])) == expected);
57       }
58     }
59   }
60 
61   // Test its behavior with embedded null bytes.
62   SV abc     = SV(MAKE_CSTRING(CharT, "abc"));
63   SV abc0def = SV(MAKE_CSTRING(CharT, "abc\0def"), 7);
64   SV abcdef  = SV(MAKE_CSTRING(CharT, "abcdef"));
65   assert((abc == abc0def) == false);
66   assert((abc == abcdef) == false);
67   assert((abc0def == abc) == false);
68   assert((abc0def == abcdef) == false);
69   assert((abcdef == abc) == false);
70   assert((abcdef == abc0def) == false);
71 
72   assert((abc.data() == abc0def) == false);
73   assert((abc0def == abc.data()) == false);
74 
75   if (TEST_STD_AT_LEAST_20_OR_RUNTIME_EVALUATED) {
76     assert((std::basic_string<CharT, Traits>(abc) == abc0def) == false);
77     assert((abc0def == std::basic_string<CharT, Traits>(abc)) == false);
78   }
79 
80   return true;
81 }
82 
main(int,char **)83 int main(int, char**) {
84   test<std::string_view>();
85 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
86   test<std::wstring_view>();
87 #endif
88 #if TEST_STD_VER >= 11
89   test<std::u16string_view>();
90   test<std::u32string_view>();
91 #endif
92 #if TEST_STD_VER > 14
93   static_assert(test<std::string_view>(), "");
94 #  ifndef TEST_HAS_NO_WIDE_CHARACTERS
95   static_assert(test<std::wstring_view>(), "");
96 #  endif
97   static_assert(test<std::u16string_view>(), "");
98   static_assert(test<std::u32string_view>(), "");
99 #endif
100 
101 #if TEST_STD_VER > 11
102   test<std::basic_string_view<char, constexpr_char_traits<char>>>();
103   static_assert(test<std::basic_string_view<char, constexpr_char_traits<char>>>(), "");
104 #endif
105 
106 #if TEST_STD_VER > 17
107   test<std::u8string_view>();
108   static_assert(test<std::u8string_view>());
109 #endif
110 
111   return 0;
112 }
113