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// constexpr int compare(basic_string_view str) const noexcept;
14
15#include <string_view>
16#include <cassert>
17
18#include "test_macros.h"
19#include "constexpr_char_traits.h"
20
21int sign(int x) { return x > 0 ? 1 : (x < 0 ? -1 : 0); }
22
23template <typename CharT>
24void test1(std::basic_string_view<CharT> sv1, std::basic_string_view<CharT> sv2, int expected) {
25 assert(sign(sv1.compare(sv2)) == sign(expected));
26}
27
28template <typename CharT>
29void test(const CharT* s1, const CharT* s2, int expected) {
30 typedef std::basic_string_view<CharT> string_view_t;
31
32 string_view_t sv1(s1);
33 string_view_t sv2(s2);
34 test1(sv1, sv2, expected);
35}
36
37int main(int, char**) {
38 test(s1: "", s2: "", expected: 0);
39 test(s1: "", s2: "abcde", expected: -5);
40 test(s1: "", s2: "abcdefghij", expected: -10);
41 test(s1: "", s2: "abcdefghijklmnopqrst", expected: -20);
42 test(s1: "abcde", s2: "", expected: 5);
43 test(s1: "abcde", s2: "abcde", expected: 0);
44 test(s1: "abcde", s2: "abcdefghij", expected: -5);
45 test(s1: "abcde", s2: "abcdefghijklmnopqrst", expected: -15);
46 test(s1: "abcdefghij", s2: "", expected: 10);
47 test(s1: "abcdefghij", s2: "abcde", expected: 5);
48 test(s1: "abcdefghij", s2: "abcdefghij", expected: 0);
49 test(s1: "abcdefghij", s2: "abcdefghijklmnopqrst", expected: -10);
50 test(s1: "abcdefghijklmnopqrst", s2: "", expected: 20);
51 test(s1: "abcdefghijklmnopqrst", s2: "abcde", expected: 15);
52 test(s1: "abcdefghijklmnopqrst", s2: "abcdefghij", expected: 10);
53 test(s1: "abcdefghijklmnopqrst", s2: "abcdefghijklmnopqrst", expected: 0);
54
55#ifndef TEST_HAS_NO_WIDE_CHARACTERS
56 test(s1: L"", s2: L"", expected: 0);
57 test(s1: L"", s2: L"abcde", expected: -5);
58 test(s1: L"", s2: L"abcdefghij", expected: -10);
59 test(s1: L"", s2: L"abcdefghijklmnopqrst", expected: -20);
60 test(s1: L"abcde", s2: L"", expected: 5);
61 test(s1: L"abcde", s2: L"abcde", expected: 0);
62 test(s1: L"abcde", s2: L"abcdefghij", expected: -5);
63 test(s1: L"abcde", s2: L"abcdefghijklmnopqrst", expected: -15);
64 test(s1: L"abcdefghij", s2: L"", expected: 10);
65 test(s1: L"abcdefghij", s2: L"abcde", expected: 5);
66 test(s1: L"abcdefghij", s2: L"abcdefghij", expected: 0);
67 test(s1: L"abcdefghij", s2: L"abcdefghijklmnopqrst", expected: -10);
68 test(s1: L"abcdefghijklmnopqrst", s2: L"", expected: 20);
69 test(s1: L"abcdefghijklmnopqrst", s2: L"abcde", expected: 15);
70 test(s1: L"abcdefghijklmnopqrst", s2: L"abcdefghij", expected: 10);
71 test(s1: L"abcdefghijklmnopqrst", s2: L"abcdefghijklmnopqrst", expected: 0);
72#endif
73
74#if TEST_STD_VER >= 11
75 test(u"", u"", 0);
76 test(u"", u"abcde", -5);
77 test(u"", u"abcdefghij", -10);
78 test(u"", u"abcdefghijklmnopqrst", -20);
79 test(u"abcde", u"", 5);
80 test(u"abcde", u"abcde", 0);
81 test(u"abcde", u"abcdefghij", -5);
82 test(u"abcde", u"abcdefghijklmnopqrst", -15);
83 test(u"abcdefghij", u"", 10);
84 test(u"abcdefghij", u"abcde", 5);
85 test(u"abcdefghij", u"abcdefghij", 0);
86 test(u"abcdefghij", u"abcdefghijklmnopqrst", -10);
87 test(u"abcdefghijklmnopqrst", u"", 20);
88 test(u"abcdefghijklmnopqrst", u"abcde", 15);
89 test(u"abcdefghijklmnopqrst", u"abcdefghij", 10);
90 test(u"abcdefghijklmnopqrst", u"abcdefghijklmnopqrst", 0);
91
92 test(U"", U"", 0);
93 test(U"", U"abcde", -5);
94 test(U"", U"abcdefghij", -10);
95 test(U"", U"abcdefghijklmnopqrst", -20);
96 test(U"abcde", U"", 5);
97 test(U"abcde", U"abcde", 0);
98 test(U"abcde", U"abcdefghij", -5);
99 test(U"abcde", U"abcdefghijklmnopqrst", -15);
100 test(U"abcdefghij", U"", 10);
101 test(U"abcdefghij", U"abcde", 5);
102 test(U"abcdefghij", U"abcdefghij", 0);
103 test(U"abcdefghij", U"abcdefghijklmnopqrst", -10);
104 test(U"abcdefghijklmnopqrst", U"", 20);
105 test(U"abcdefghijklmnopqrst", U"abcde", 15);
106 test(U"abcdefghijklmnopqrst", U"abcdefghij", 10);
107 test(U"abcdefghijklmnopqrst", U"abcdefghijklmnopqrst", 0);
108#endif
109
110#if TEST_STD_VER > 11
111 {
112 typedef std::basic_string_view<char, constexpr_char_traits<char>> SV;
113 constexpr SV sv1{"abcde", 5};
114 constexpr SV sv2{"abcde", 5};
115 constexpr SV sv3{"edcba0", 6};
116 static_assert(sv1.compare(sv2) == 0, "");
117 static_assert(sv2.compare(sv1) == 0, "");
118 static_assert(sv3.compare(sv2) > 0, "");
119 static_assert(sv2.compare(sv3) < 0, "");
120 }
121#endif
122
123 return 0;
124}
125

source code of libcxx/test/std/strings/string.view/string.view.ops/compare.sv.pass.cpp