Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // <string_view> |
| 10 | |
| 11 | // constexpr int compare(const charT* s) const; |
| 12 | |
| 13 | #include <string_view> |
| 14 | #include <cassert> |
| 15 | |
Stephan T. Lavavej | 0f901c7 | 2016-11-04 20:26:59 +0000 | [diff] [blame] | 16 | #include "test_macros.h" |
Nico Weber | cc89063 | 2019-08-21 00:14:12 +0000 | [diff] [blame] | 17 | #include "constexpr_char_traits.h" |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 18 | |
| 19 | int sign ( int x ) { return x > 0 ? 1 : ( x < 0 ? -1 : 0 ); } |
| 20 | |
| 21 | template<typename CharT> |
| 22 | void test1 ( std::basic_string_view<CharT> sv1, const CharT *s, int expected ) { |
| 23 | assert ( sign( sv1.compare(s)) == sign(expected)); |
| 24 | } |
| 25 | |
| 26 | template<typename CharT> |
| 27 | void |
| 28 | test( const CharT *s1, const CharT *s2, int expected) |
| 29 | { |
| 30 | typedef std::basic_string_view<CharT> string_view_t; |
| 31 | string_view_t sv1 ( s1 ); |
| 32 | test1 ( sv1, s2, expected ); |
| 33 | } |
| 34 | |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 35 | int main(int, char**) |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 36 | { |
| 37 | { |
| 38 | test("", "", 0); |
| 39 | test("", "abcde", -5); |
| 40 | test("", "abcdefghij", -10); |
| 41 | test("", "abcdefghijklmnopqrst", -20); |
| 42 | test("abcde", "", 5); |
| 43 | test("abcde", "abcde", 0); |
| 44 | test("abcde", "abcdefghij", -5); |
| 45 | test("abcde", "abcdefghijklmnopqrst", -15); |
| 46 | test("abcdefghij", "", 10); |
| 47 | test("abcdefghij", "abcde", 5); |
| 48 | test("abcdefghij", "abcdefghij", 0); |
| 49 | test("abcdefghij", "abcdefghijklmnopqrst", -10); |
| 50 | test("abcdefghijklmnopqrst", "", 20); |
| 51 | test("abcdefghijklmnopqrst", "abcde", 15); |
| 52 | test("abcdefghijklmnopqrst", "abcdefghij", 10); |
| 53 | test("abcdefghijklmnopqrst", "abcdefghijklmnopqrst", 0); |
| 54 | } |
| 55 | |
| 56 | { |
| 57 | test(L"", L"", 0); |
| 58 | test(L"", L"abcde", -5); |
| 59 | test(L"", L"abcdefghij", -10); |
| 60 | test(L"", L"abcdefghijklmnopqrst", -20); |
| 61 | test(L"abcde", L"", 5); |
| 62 | test(L"abcde", L"abcde", 0); |
| 63 | test(L"abcde", L"abcdefghij", -5); |
| 64 | test(L"abcde", L"abcdefghijklmnopqrst", -15); |
| 65 | test(L"abcdefghij", L"", 10); |
| 66 | test(L"abcdefghij", L"abcde", 5); |
| 67 | test(L"abcdefghij", L"abcdefghij", 0); |
| 68 | test(L"abcdefghij", L"abcdefghijklmnopqrst", -10); |
| 69 | test(L"abcdefghijklmnopqrst", L"", 20); |
| 70 | test(L"abcdefghijklmnopqrst", L"abcde", 15); |
| 71 | test(L"abcdefghijklmnopqrst", L"abcdefghij", 10); |
| 72 | test(L"abcdefghijklmnopqrst", L"abcdefghijklmnopqrst", 0); |
| 73 | } |
| 74 | |
| 75 | #if TEST_STD_VER >= 11 |
| 76 | { |
| 77 | test(U"", U"", 0); |
| 78 | test(U"", U"abcde", -5); |
| 79 | test(U"", U"abcdefghij", -10); |
| 80 | test(U"", U"abcdefghijklmnopqrst", -20); |
| 81 | test(U"abcde", U"", 5); |
| 82 | test(U"abcde", U"abcde", 0); |
| 83 | test(U"abcde", U"abcdefghij", -5); |
| 84 | test(U"abcde", U"abcdefghijklmnopqrst", -15); |
| 85 | test(U"abcdefghij", U"", 10); |
| 86 | test(U"abcdefghij", U"abcde", 5); |
| 87 | test(U"abcdefghij", U"abcdefghij", 0); |
| 88 | test(U"abcdefghij", U"abcdefghijklmnopqrst", -10); |
| 89 | test(U"abcdefghijklmnopqrst", U"", 20); |
| 90 | test(U"abcdefghijklmnopqrst", U"abcde", 15); |
| 91 | test(U"abcdefghijklmnopqrst", U"abcdefghij", 10); |
| 92 | test(U"abcdefghijklmnopqrst", U"abcdefghijklmnopqrst", 0); |
| 93 | } |
| 94 | |
| 95 | { |
| 96 | test(u"", u"", 0); |
| 97 | test(u"", u"abcde", -5); |
| 98 | test(u"", u"abcdefghij", -10); |
| 99 | test(u"", u"abcdefghijklmnopqrst", -20); |
| 100 | test(u"abcde", u"", 5); |
| 101 | test(u"abcde", u"abcde", 0); |
| 102 | test(u"abcde", u"abcdefghij", -5); |
| 103 | test(u"abcde", u"abcdefghijklmnopqrst", -15); |
| 104 | test(u"abcdefghij", u"", 10); |
| 105 | test(u"abcdefghij", u"abcde", 5); |
| 106 | test(u"abcdefghij", u"abcdefghij", 0); |
| 107 | test(u"abcdefghij", u"abcdefghijklmnopqrst", -10); |
| 108 | test(u"abcdefghijklmnopqrst", u"", 20); |
| 109 | test(u"abcdefghijklmnopqrst", u"abcde", 15); |
| 110 | test(u"abcdefghijklmnopqrst", u"abcdefghij", 10); |
| 111 | test(u"abcdefghijklmnopqrst", u"abcdefghijklmnopqrst", 0); |
| 112 | } |
| 113 | #endif |
| 114 | |
Stephan T. Lavavej | 0f901c7 | 2016-11-04 20:26:59 +0000 | [diff] [blame] | 115 | #if TEST_STD_VER > 11 |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 116 | { |
| 117 | typedef std::basic_string_view<char, constexpr_char_traits<char>> SV; |
| 118 | constexpr SV sv1; |
| 119 | constexpr SV sv2 { "abcde", 5 }; |
| 120 | static_assert ( sv1.compare("") == 0, "" ); |
Marshall Clow | 98b1532 | 2019-04-24 15:14:14 +0000 | [diff] [blame] | 121 | static_assert ( sv1.compare("abcde") < 0, "" ); |
| 122 | static_assert ( sv2.compare("") > 0, "" ); |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 123 | static_assert ( sv2.compare("abcde") == 0, "" ); |
| 124 | } |
| 125 | #endif |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 126 | |
| 127 | return 0; |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 128 | } |