| 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 | // 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. |
| 12 | |
| 13 | // UNSUPPORTED: c++03, c++11, c++14, c++17 |
| 14 | |
| 15 | // <string> |
| 16 | |
| 17 | // template<class charT, class traits, class Allocator> |
| 18 | // see below operator<=>(const basic_string<charT, traits, Allocator>& lhs, |
| 19 | // const basic_string<charT, traits, Allocator>& rhs) noexcept; |
| 20 | // template<class charT, class traits, class Allocator> |
| 21 | // see below operator<=>(const basic_string<charT, traits, Allocator>& lhs, |
| 22 | // const charT* rhs); |
| 23 | |
| 24 | #include <string> |
| 25 | |
| 26 | #include <array> |
| 27 | #include <cassert> |
| 28 | #include <string_view> |
| 29 | |
| 30 | #include "constexpr_char_traits.h" |
| 31 | #include "make_string.h" |
| 32 | #include "test_comparisons.h" |
| 33 | #include "test_macros.h" |
| 34 | |
| 35 | #define STR(S) MAKE_STRING(CharT, S) |
| 36 | |
| 37 | template <class T, class Ordering = std::strong_ordering> |
| 38 | constexpr void test() { |
| 39 | AssertOrderAreNoexcept<T>(); |
| 40 | AssertOrderReturn<Ordering, T>(); |
| 41 | |
| 42 | using CharT = typename T::value_type; |
| 43 | AssertOrderReturn<Ordering, T, const CharT*>(); |
| 44 | |
| 45 | // sorted values |
| 46 | std::array v{ |
| 47 | STR("" ), |
| 48 | STR("abc" ), |
| 49 | STR("abcdef" ), |
| 50 | STR("acb" ), |
| 51 | }; |
| 52 | |
| 53 | // sorted values with embedded NUL character |
| 54 | std::array vn{ |
| 55 | STR("abc" ), |
| 56 | STR("abc\0" ), |
| 57 | STR("abc\0def" ), |
| 58 | STR("acb\0" ), |
| 59 | }; |
| 60 | static_assert(v.size() == vn.size()); |
| 61 | |
| 62 | for (std::size_t i = 0; i < v.size(); ++i) { |
| 63 | for (std::size_t j = 0; j < v.size(); ++j) { |
| 64 | assert(testOrder(v[i], v[j], i == j ? Ordering::equivalent : i < j ? Ordering::less : Ordering::greater)); |
| 65 | |
| 66 | assert(testOrder( |
| 67 | v[i], |
| 68 | std::basic_string<CharT>{v[j]}.c_str(), |
| 69 | i == j ? Ordering::equivalent |
| 70 | : i < j ? Ordering::less |
| 71 | : Ordering::greater)); |
| 72 | |
| 73 | // NUL test omitted for c-strings since it will fail. |
| 74 | assert(testOrder(vn[i], vn[j], i == j ? Ordering::equivalent : i < j ? Ordering::less : Ordering::greater)); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | constexpr bool test_all_types() { |
| 80 | test<std::string>(); |
| 81 | test<std::basic_string<char, constexpr_char_traits<char>>, std::weak_ordering>(); |
| 82 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 83 | test<std::wstring>(); |
| 84 | test<std::basic_string<wchar_t, constexpr_char_traits<wchar_t>>, std::weak_ordering>(); |
| 85 | #endif |
| 86 | test<std::u8string>(); |
| 87 | test<std::basic_string<char8_t, constexpr_char_traits<char8_t>>, std::weak_ordering>(); |
| 88 | test<std::u16string>(); |
| 89 | test<std::basic_string<char16_t, constexpr_char_traits<char16_t>>, std::weak_ordering>(); |
| 90 | test<std::u32string>(); |
| 91 | test<std::basic_string<char32_t, constexpr_char_traits<char32_t>>, std::weak_ordering>(); |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | int main(int, char**) { |
| 97 | test_all_types(); |
| 98 | static_assert(test_all_types()); |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |