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 // <string> 10 11 // int compare(const basic_string& str) const // constexpr since C++20 12 13 #include <string> 14 #include <cassert> 15 16 #include "test_macros.h" 17 #include "min_allocator.h" 18 19 TEST_CONSTEXPR_CXX20 int sign(int x) { 20 if (x == 0) 21 return 0; 22 if (x < 0) 23 return -1; 24 return 1; 25 } 26 27 template <class S> 28 TEST_CONSTEXPR_CXX20 void test(const S& s, const S& str, int x) { 29 LIBCPP_ASSERT_NOEXCEPT(s.compare(str)); 30 assert(sign(s.compare(str)) == sign(x)); 31 } 32 33 TEST_CONSTEXPR_CXX20 bool test() { 34 { 35 typedef std::string S; 36 test(S(""), S(""), 0); 37 test(S(""), S("abcde"), -5); 38 test(S(""), S("abcdefghij"), -10); 39 test(S(""), S("abcdefghijklmnopqrst"), -20); 40 test(S("abcde"), S(""), 5); 41 test(S("abcde"), S("abcde"), 0); 42 test(S("abcde"), S("abcdefghij"), -5); 43 test(S("abcde"), S("abcdefghijklmnopqrst"), -15); 44 test(S("abcdefghij"), S(""), 10); 45 test(S("abcdefghij"), S("abcde"), 5); 46 test(S("abcdefghij"), S("abcdefghij"), 0); 47 test(S("abcdefghij"), S("abcdefghijklmnopqrst"), -10); 48 test(S("abcdefghijklmnopqrst"), S(""), 20); 49 test(S("abcdefghijklmnopqrst"), S("abcde"), 15); 50 test(S("abcdefghijklmnopqrst"), S("abcdefghij"), 10); 51 test(S("abcdefghijklmnopqrst"), S("abcdefghijklmnopqrst"), 0); 52 } 53 #if TEST_STD_VER >= 11 54 { 55 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; 56 test(S(""), S(""), 0); 57 test(S(""), S("abcde"), -5); 58 test(S(""), S("abcdefghij"), -10); 59 test(S(""), S("abcdefghijklmnopqrst"), -20); 60 test(S("abcde"), S(""), 5); 61 test(S("abcde"), S("abcde"), 0); 62 test(S("abcde"), S("abcdefghij"), -5); 63 test(S("abcde"), S("abcdefghijklmnopqrst"), -15); 64 test(S("abcdefghij"), S(""), 10); 65 test(S("abcdefghij"), S("abcde"), 5); 66 test(S("abcdefghij"), S("abcdefghij"), 0); 67 test(S("abcdefghij"), S("abcdefghijklmnopqrst"), -10); 68 test(S("abcdefghijklmnopqrst"), S(""), 20); 69 test(S("abcdefghijklmnopqrst"), S("abcde"), 15); 70 test(S("abcdefghijklmnopqrst"), S("abcdefghij"), 10); 71 test(S("abcdefghijklmnopqrst"), S("abcdefghijklmnopqrst"), 0); 72 } 73 #endif 74 75 #if TEST_STD_VER >= 11 76 { // LWG 2946 77 std::string s = " !"; 78 assert(s.compare({"abc", 1}) < 0); 79 } 80 #endif 81 82 return true; 83 } 84 85 int main(int, char**) { 86 test(); 87 #if TEST_STD_VER > 17 88 static_assert(test()); 89 #endif 90 91 return 0; 92 } 93