Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / strings / string.view / string.view.comparison / comparison.pass.cpp
blobbf4ebd9258b531dbeca45db54db8f5b6a4109dfa
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 //===----------------------------------------------------------------------===//
9 // Starting with C++20 the spaceship operator was included. This tests
10 // comparison in that context, thus doesn't support older language versions.
11 // These are tested per operator.
13 // UNSUPPORTED: c++03, c++11, c++14, c++17
15 // <string_view>
17 // template<class charT, class traits>
18 // constexpr bool operator==(basic_string_view<charT, traits> lhs, basic_string_view<charT, traits> rhs);
19 // template<class charT, class traits>
20 // constexpr auto operator<=>(basic_string_view<charT, traits> lhs, basic_string_view<charT, traits> rhs);
21 // (plus "sufficient additional overloads" to make implicit conversions work as intended)
23 #include <string_view>
25 #include <array>
26 #include <cassert>
27 #include <string>
29 #include "constexpr_char_traits.h"
30 #include "make_string.h"
31 #include "test_comparisons.h"
32 #include "test_macros.h"
34 #define SV(S) MAKE_STRING_VIEW(CharT, S)
36 // Copied from constexpr_char_traits, but it doesn't have a full implementation.
37 // It has a comparison_category used in the tests.
38 template <class CharT, class Ordering>
39 struct char_traits {
40 using char_type = CharT;
41 using int_type = int;
42 using off_type = std::streamoff;
43 using pos_type = std::streampos;
44 using state_type = std::mbstate_t;
45 using comparison_category = Ordering;
47 static constexpr void assign(char_type& __c1, const char_type& __c2) noexcept { __c1 = __c2; }
48 static constexpr bool eq(char_type __c1, char_type __c2) noexcept { return __c1 == __c2; }
49 static constexpr bool lt(char_type __c1, char_type __c2) noexcept { return __c1 < __c2; }
50 static constexpr int compare(const char_type* __s1, const char_type* __s2, std::size_t __n) {
51 for (; __n; --__n, ++__s1, ++__s2) {
52 if (lt(*__s1, *__s2))
53 return -1;
54 if (lt(*__s2, *__s1))
55 return 1;
57 return 0;
60 static constexpr std::size_t length(const char_type* __s);
61 static constexpr const char_type* find(const char_type* __s, std::size_t __n, const char_type& __a);
62 static constexpr char_type* move(char_type* __s1, const char_type* __s2, std::size_t __n);
63 static constexpr char_type* copy(char_type* __s1, const char_type* __s2, std::size_t __n);
64 static constexpr char_type* assign(char_type* __s, std::size_t __n, char_type __a);
65 static constexpr int_type not_eof(int_type __c) noexcept { return eq_int_type(__c, eof()) ? ~eof() : __c; }
66 static constexpr char_type to_char_type(int_type __c) noexcept { return char_type(__c); }
67 static constexpr int_type to_int_type(char_type __c) noexcept { return int_type(__c); }
68 static constexpr bool eq_int_type(int_type __c1, int_type __c2) noexcept { return __c1 == __c2; }
69 static constexpr int_type eof() noexcept { return int_type(EOF); }
72 template <class T, class Ordering = std::strong_ordering>
73 constexpr void test() {
74 AssertOrderAreNoexcept<T>();
75 AssertOrderReturn<Ordering, T>();
77 using CharT = typename T::value_type;
79 // sorted values
80 std::array v{
81 SV(""),
82 SV("abc"),
83 SV("abcdef"),
86 // sorted values with embedded NUL character
87 std::array vn{
88 SV("abc"),
89 SV("abc\0"),
90 SV("abc\0def"),
92 static_assert(v.size() == vn.size());
94 for (std::size_t i = 0; i < v.size(); ++i) {
95 for (std::size_t j = 0; j < v.size(); ++j) {
96 assert(testOrder(v[i], v[j], i == j ? Ordering::equivalent : i < j ? Ordering::less : Ordering::greater));
97 assert(testOrder(
98 v[i],
99 std::basic_string<CharT>{v[j]},
100 i == j ? Ordering::equivalent
101 : i < j ? Ordering::less
102 : Ordering::greater));
104 assert(testOrder(
105 v[i],
106 std::basic_string<CharT>{v[j]}.c_str(),
107 i == j ? Ordering::equivalent
108 : i < j ? Ordering::less
109 : Ordering::greater));
111 // NUL test omitted for c-strings since it will fail.
112 assert(testOrder(vn[i], vn[j], i == j ? Ordering::equivalent : i < j ? Ordering::less : Ordering::greater));
113 assert(testOrder(
114 vn[i],
115 std::basic_string<CharT>{vn[j]},
116 i == j ? Ordering::equivalent
117 : i < j ? Ordering::less
118 : Ordering::greater));
123 template <class CharT>
124 constexpr void test_all_orderings() {
125 test<std::basic_string_view<CharT>>(); // Strong ordering in its char_traits
126 test<std::basic_string_view<CharT, constexpr_char_traits<CharT>>,
127 std::weak_ordering>(); // No ordering in its char_traits
128 test<std::basic_string_view<CharT, char_traits<CharT, std::weak_ordering>>, std::weak_ordering>();
129 test<std::basic_string_view<CharT, char_traits<CharT, std::partial_ordering>>, std::partial_ordering>();
132 constexpr bool test_all_types() {
133 test_all_orderings<char>();
134 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
135 test_all_orderings<wchar_t>();
136 #endif
137 test_all_orderings<char8_t>();
138 test_all_orderings<char16_t>();
139 test_all_orderings<char32_t>();
141 return true;
144 int main(int, char**) {
145 test_all_types();
146 static_assert(test_all_types());
148 return 0;