1053d81ceSMarshall Clow //===----------------------------------------------------------------------===// 2053d81ceSMarshall Clow // 357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6053d81ceSMarshall Clow // 7053d81ceSMarshall Clow //===----------------------------------------------------------------------===// 8053d81ceSMarshall Clow 9053d81ceSMarshall Clow // <string_view> 10053d81ceSMarshall Clow 11053d81ceSMarshall Clow // constexpr int compare(const charT* s) const; 12053d81ceSMarshall Clow 13053d81ceSMarshall Clow #include <string_view> 14053d81ceSMarshall Clow #include <cassert> 15053d81ceSMarshall Clow 160f901c7eSStephan T. Lavavej #include "test_macros.h" 17053d81ceSMarshall Clow #include "constexpr_char_traits.hpp" 18053d81ceSMarshall Clow 19053d81ceSMarshall Clow int sign ( int x ) { return x > 0 ? 1 : ( x < 0 ? -1 : 0 ); } 20053d81ceSMarshall Clow 21053d81ceSMarshall Clow template<typename CharT> 22053d81ceSMarshall Clow void test1 ( std::basic_string_view<CharT> sv1, const CharT *s, int expected ) { 23053d81ceSMarshall Clow assert ( sign( sv1.compare(s)) == sign(expected)); 24053d81ceSMarshall Clow } 25053d81ceSMarshall Clow 26053d81ceSMarshall Clow template<typename CharT> 27053d81ceSMarshall Clow void 28053d81ceSMarshall Clow test( const CharT *s1, const CharT *s2, int expected) 29053d81ceSMarshall Clow { 30053d81ceSMarshall Clow typedef std::basic_string_view<CharT> string_view_t; 31053d81ceSMarshall Clow string_view_t sv1 ( s1 ); 32053d81ceSMarshall Clow test1 ( sv1, s2, expected ); 33053d81ceSMarshall Clow } 34053d81ceSMarshall Clow 35*2df59c50SJF Bastien int main(int, char**) 36053d81ceSMarshall Clow { 37053d81ceSMarshall Clow { 38053d81ceSMarshall Clow test("", "", 0); 39053d81ceSMarshall Clow test("", "abcde", -5); 40053d81ceSMarshall Clow test("", "abcdefghij", -10); 41053d81ceSMarshall Clow test("", "abcdefghijklmnopqrst", -20); 42053d81ceSMarshall Clow test("abcde", "", 5); 43053d81ceSMarshall Clow test("abcde", "abcde", 0); 44053d81ceSMarshall Clow test("abcde", "abcdefghij", -5); 45053d81ceSMarshall Clow test("abcde", "abcdefghijklmnopqrst", -15); 46053d81ceSMarshall Clow test("abcdefghij", "", 10); 47053d81ceSMarshall Clow test("abcdefghij", "abcde", 5); 48053d81ceSMarshall Clow test("abcdefghij", "abcdefghij", 0); 49053d81ceSMarshall Clow test("abcdefghij", "abcdefghijklmnopqrst", -10); 50053d81ceSMarshall Clow test("abcdefghijklmnopqrst", "", 20); 51053d81ceSMarshall Clow test("abcdefghijklmnopqrst", "abcde", 15); 52053d81ceSMarshall Clow test("abcdefghijklmnopqrst", "abcdefghij", 10); 53053d81ceSMarshall Clow test("abcdefghijklmnopqrst", "abcdefghijklmnopqrst", 0); 54053d81ceSMarshall Clow } 55053d81ceSMarshall Clow 56053d81ceSMarshall Clow { 57053d81ceSMarshall Clow test(L"", L"", 0); 58053d81ceSMarshall Clow test(L"", L"abcde", -5); 59053d81ceSMarshall Clow test(L"", L"abcdefghij", -10); 60053d81ceSMarshall Clow test(L"", L"abcdefghijklmnopqrst", -20); 61053d81ceSMarshall Clow test(L"abcde", L"", 5); 62053d81ceSMarshall Clow test(L"abcde", L"abcde", 0); 63053d81ceSMarshall Clow test(L"abcde", L"abcdefghij", -5); 64053d81ceSMarshall Clow test(L"abcde", L"abcdefghijklmnopqrst", -15); 65053d81ceSMarshall Clow test(L"abcdefghij", L"", 10); 66053d81ceSMarshall Clow test(L"abcdefghij", L"abcde", 5); 67053d81ceSMarshall Clow test(L"abcdefghij", L"abcdefghij", 0); 68053d81ceSMarshall Clow test(L"abcdefghij", L"abcdefghijklmnopqrst", -10); 69053d81ceSMarshall Clow test(L"abcdefghijklmnopqrst", L"", 20); 70053d81ceSMarshall Clow test(L"abcdefghijklmnopqrst", L"abcde", 15); 71053d81ceSMarshall Clow test(L"abcdefghijklmnopqrst", L"abcdefghij", 10); 72053d81ceSMarshall Clow test(L"abcdefghijklmnopqrst", L"abcdefghijklmnopqrst", 0); 73053d81ceSMarshall Clow } 74053d81ceSMarshall Clow 75053d81ceSMarshall Clow #if TEST_STD_VER >= 11 76053d81ceSMarshall Clow { 77053d81ceSMarshall Clow test(U"", U"", 0); 78053d81ceSMarshall Clow test(U"", U"abcde", -5); 79053d81ceSMarshall Clow test(U"", U"abcdefghij", -10); 80053d81ceSMarshall Clow test(U"", U"abcdefghijklmnopqrst", -20); 81053d81ceSMarshall Clow test(U"abcde", U"", 5); 82053d81ceSMarshall Clow test(U"abcde", U"abcde", 0); 83053d81ceSMarshall Clow test(U"abcde", U"abcdefghij", -5); 84053d81ceSMarshall Clow test(U"abcde", U"abcdefghijklmnopqrst", -15); 85053d81ceSMarshall Clow test(U"abcdefghij", U"", 10); 86053d81ceSMarshall Clow test(U"abcdefghij", U"abcde", 5); 87053d81ceSMarshall Clow test(U"abcdefghij", U"abcdefghij", 0); 88053d81ceSMarshall Clow test(U"abcdefghij", U"abcdefghijklmnopqrst", -10); 89053d81ceSMarshall Clow test(U"abcdefghijklmnopqrst", U"", 20); 90053d81ceSMarshall Clow test(U"abcdefghijklmnopqrst", U"abcde", 15); 91053d81ceSMarshall Clow test(U"abcdefghijklmnopqrst", U"abcdefghij", 10); 92053d81ceSMarshall Clow test(U"abcdefghijklmnopqrst", U"abcdefghijklmnopqrst", 0); 93053d81ceSMarshall Clow } 94053d81ceSMarshall Clow 95053d81ceSMarshall Clow { 96053d81ceSMarshall Clow test(u"", u"", 0); 97053d81ceSMarshall Clow test(u"", u"abcde", -5); 98053d81ceSMarshall Clow test(u"", u"abcdefghij", -10); 99053d81ceSMarshall Clow test(u"", u"abcdefghijklmnopqrst", -20); 100053d81ceSMarshall Clow test(u"abcde", u"", 5); 101053d81ceSMarshall Clow test(u"abcde", u"abcde", 0); 102053d81ceSMarshall Clow test(u"abcde", u"abcdefghij", -5); 103053d81ceSMarshall Clow test(u"abcde", u"abcdefghijklmnopqrst", -15); 104053d81ceSMarshall Clow test(u"abcdefghij", u"", 10); 105053d81ceSMarshall Clow test(u"abcdefghij", u"abcde", 5); 106053d81ceSMarshall Clow test(u"abcdefghij", u"abcdefghij", 0); 107053d81ceSMarshall Clow test(u"abcdefghij", u"abcdefghijklmnopqrst", -10); 108053d81ceSMarshall Clow test(u"abcdefghijklmnopqrst", u"", 20); 109053d81ceSMarshall Clow test(u"abcdefghijklmnopqrst", u"abcde", 15); 110053d81ceSMarshall Clow test(u"abcdefghijklmnopqrst", u"abcdefghij", 10); 111053d81ceSMarshall Clow test(u"abcdefghijklmnopqrst", u"abcdefghijklmnopqrst", 0); 112053d81ceSMarshall Clow } 113053d81ceSMarshall Clow #endif 114053d81ceSMarshall Clow 1150f901c7eSStephan T. Lavavej #if TEST_STD_VER > 11 116053d81ceSMarshall Clow { 117053d81ceSMarshall Clow typedef std::basic_string_view<char, constexpr_char_traits<char>> SV; 118053d81ceSMarshall Clow constexpr SV sv1; 119053d81ceSMarshall Clow constexpr SV sv2 { "abcde", 5 }; 120053d81ceSMarshall Clow static_assert ( sv1.compare("") == 0, "" ); 121053d81ceSMarshall Clow static_assert ( sv1.compare("abcde") == -1, "" ); 122053d81ceSMarshall Clow static_assert ( sv2.compare("") == 1, "" ); 123053d81ceSMarshall Clow static_assert ( sv2.compare("abcde") == 0, "" ); 124053d81ceSMarshall Clow } 125053d81ceSMarshall Clow #endif 126*2df59c50SJF Bastien 127*2df59c50SJF Bastien return 0; 128053d81ceSMarshall Clow } 129