1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <string_view> 11 12 // constexpr int compare(const charT* s) const; 13 14 #include <string_view> 15 #include <cassert> 16 17 #include "test_macros.h" 18 #include "constexpr_char_traits.hpp" 19 20 int sign ( int x ) { return x > 0 ? 1 : ( x < 0 ? -1 : 0 ); } 21 22 template<typename CharT> 23 void test1 ( std::basic_string_view<CharT> sv1, const CharT *s, int expected ) { 24 assert ( sign( sv1.compare(s)) == sign(expected)); 25 } 26 27 template<typename CharT> 28 void 29 test( const CharT *s1, const CharT *s2, int expected) 30 { 31 typedef std::basic_string_view<CharT> string_view_t; 32 string_view_t sv1 ( s1 ); 33 test1 ( sv1, s2, expected ); 34 } 35 36 int main() 37 { 38 { 39 test("", "", 0); 40 test("", "abcde", -5); 41 test("", "abcdefghij", -10); 42 test("", "abcdefghijklmnopqrst", -20); 43 test("abcde", "", 5); 44 test("abcde", "abcde", 0); 45 test("abcde", "abcdefghij", -5); 46 test("abcde", "abcdefghijklmnopqrst", -15); 47 test("abcdefghij", "", 10); 48 test("abcdefghij", "abcde", 5); 49 test("abcdefghij", "abcdefghij", 0); 50 test("abcdefghij", "abcdefghijklmnopqrst", -10); 51 test("abcdefghijklmnopqrst", "", 20); 52 test("abcdefghijklmnopqrst", "abcde", 15); 53 test("abcdefghijklmnopqrst", "abcdefghij", 10); 54 test("abcdefghijklmnopqrst", "abcdefghijklmnopqrst", 0); 55 } 56 57 { 58 test(L"", L"", 0); 59 test(L"", L"abcde", -5); 60 test(L"", L"abcdefghij", -10); 61 test(L"", L"abcdefghijklmnopqrst", -20); 62 test(L"abcde", L"", 5); 63 test(L"abcde", L"abcde", 0); 64 test(L"abcde", L"abcdefghij", -5); 65 test(L"abcde", L"abcdefghijklmnopqrst", -15); 66 test(L"abcdefghij", L"", 10); 67 test(L"abcdefghij", L"abcde", 5); 68 test(L"abcdefghij", L"abcdefghij", 0); 69 test(L"abcdefghij", L"abcdefghijklmnopqrst", -10); 70 test(L"abcdefghijklmnopqrst", L"", 20); 71 test(L"abcdefghijklmnopqrst", L"abcde", 15); 72 test(L"abcdefghijklmnopqrst", L"abcdefghij", 10); 73 test(L"abcdefghijklmnopqrst", L"abcdefghijklmnopqrst", 0); 74 } 75 76 #if TEST_STD_VER >= 11 77 { 78 test(U"", U"", 0); 79 test(U"", U"abcde", -5); 80 test(U"", U"abcdefghij", -10); 81 test(U"", U"abcdefghijklmnopqrst", -20); 82 test(U"abcde", U"", 5); 83 test(U"abcde", U"abcde", 0); 84 test(U"abcde", U"abcdefghij", -5); 85 test(U"abcde", U"abcdefghijklmnopqrst", -15); 86 test(U"abcdefghij", U"", 10); 87 test(U"abcdefghij", U"abcde", 5); 88 test(U"abcdefghij", U"abcdefghij", 0); 89 test(U"abcdefghij", U"abcdefghijklmnopqrst", -10); 90 test(U"abcdefghijklmnopqrst", U"", 20); 91 test(U"abcdefghijklmnopqrst", U"abcde", 15); 92 test(U"abcdefghijklmnopqrst", U"abcdefghij", 10); 93 test(U"abcdefghijklmnopqrst", U"abcdefghijklmnopqrst", 0); 94 } 95 96 { 97 test(u"", u"", 0); 98 test(u"", u"abcde", -5); 99 test(u"", u"abcdefghij", -10); 100 test(u"", u"abcdefghijklmnopqrst", -20); 101 test(u"abcde", u"", 5); 102 test(u"abcde", u"abcde", 0); 103 test(u"abcde", u"abcdefghij", -5); 104 test(u"abcde", u"abcdefghijklmnopqrst", -15); 105 test(u"abcdefghij", u"", 10); 106 test(u"abcdefghij", u"abcde", 5); 107 test(u"abcdefghij", u"abcdefghij", 0); 108 test(u"abcdefghij", u"abcdefghijklmnopqrst", -10); 109 test(u"abcdefghijklmnopqrst", u"", 20); 110 test(u"abcdefghijklmnopqrst", u"abcde", 15); 111 test(u"abcdefghijklmnopqrst", u"abcdefghij", 10); 112 test(u"abcdefghijklmnopqrst", u"abcdefghijklmnopqrst", 0); 113 } 114 #endif 115 116 #if TEST_STD_VER > 11 117 { 118 typedef std::basic_string_view<char, constexpr_char_traits<char>> SV; 119 constexpr SV sv1; 120 constexpr SV sv2 { "abcde", 5 }; 121 static_assert ( sv1.compare("") == 0, "" ); 122 static_assert ( sv1.compare("abcde") == -1, "" ); 123 static_assert ( sv2.compare("") == 1, "" ); 124 static_assert ( sv2.compare("abcde") == 0, "" ); 125 } 126 #endif 127 } 128