xref: /llvm-project/libcxx/test/std/strings/string.view/string.view.comparison/comparison.verify.cpp (revision 9bb9ec380ace81d040d3a0a0a2ae9a75733ab330)
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: c++03, c++11, c++14, c++17
10 
11 // <string_view>
12 
13 // template<class charT, class traits>
14 //   constexpr auto operator<=>(basic_string_view<charT, traits> lhs, basic_string_view<charT, traits> rhs);
15 //
16 // LWG 3432
17 // [string.view]/4
18 // Mandates: R denotes a comparison category type ([cmp.categories]).
19 
20 #include <string_view>
21 
22 #include "test_macros.h"
23 
24 template <class CharT, class Ordering>
25 struct traits {
26   typedef CharT char_type;
27   typedef int int_type;
28   typedef std::streamoff off_type;
29   typedef std::streampos pos_type;
30   typedef std::mbstate_t state_type;
31   using comparison_category = Ordering;
32 
33   static constexpr void assign(char_type&, const char_type&) noexcept;
34   static constexpr bool eq(char_type&, const char_type&) noexcept;
35   static constexpr bool lt(char_type&, const char_type&) noexcept;
36 
comparetraits37   static constexpr int compare(const char_type*, const char_type*, std::size_t) { return 0; }
38   static constexpr std::size_t length(const char_type*);
39   static constexpr const char_type* find(const char_type*, std::size_t, const char_type&);
40   static constexpr char_type* move(char_type*, const char_type*, std::size_t);
41   static constexpr char_type* copy(char_type*, const char_type*, std::size_t);
42   static constexpr char_type* assign(char_type*, std::size_t, char_type);
43 
44   static constexpr int_type not_eof(int_type) noexcept;
45 
46   static constexpr char_type to_char_type(int_type) noexcept;
47 
48   static constexpr int_type to_int_type(char_type) noexcept;
49 
50   static constexpr bool eq_int_type(int_type, int_type) noexcept;
51 
52   static constexpr int_type eof() noexcept;
53 };
54 
55 template <class CharT, class Ordering, bool Valid>
test()56 void test() {
57   using type = std::basic_string_view<CharT, traits<CharT, Ordering>>;
58   if constexpr (Valid)
59     type{} <=> type{};
60   else
61 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
62   // These diagnostics are issued for
63   // - Every invalid ordering
64   // - Every type
65   // expected-error-re@string_view:* 15 {{static assertion failed{{.*}}return type is not a comparison category type}}
66 
67   // This diagnostic is not issued for Ordering == void.
68   // expected-error@string_view:* 10 {{no matching conversion for static_cast from}}
69 #else
70   // One less test run when wchar_t is unavailable.
71   // expected-error-re@string_view:* 12 {{static assertion failed{{.*}}return type is not a comparison category type}}
72   // expected-error@string_view:* 8 {{no matching conversion for static_cast from}}
73 #endif
74     type{} <=> type{};
75 }
76 
77 template <class CharT>
test_all_orders()78 void test_all_orders() {
79   test<CharT, std::strong_ordering, true>();
80   test<CharT, std::weak_ordering, true>();
81   test<CharT, std::partial_ordering, true>();
82 
83   test<CharT, void, false>();
84   test<CharT, int, false>();
85   test<CharT, float, false>();
86 }
87 
test_all_types()88 void test_all_types() {
89   test_all_orders<char>();
90 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
91   test_all_orders<wchar_t>();
92 #endif
93   test_all_orders<char8_t>();
94   test_all_orders<char16_t>();
95   test_all_orders<char32_t>();
96 }
97