15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
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
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier
95a83710eSEric Fiselier // <string>
105a83710eSEric Fiselier
11*425620ccSNikolas Klauser // int compare(const basic_string& str) const // constexpr since C++20
125a83710eSEric Fiselier
135a83710eSEric Fiselier #include <string>
145a83710eSEric Fiselier #include <cassert>
155a83710eSEric Fiselier
1676b26852SMarshall Clow #include "test_macros.h"
175a83710eSEric Fiselier #include "min_allocator.h"
185a83710eSEric Fiselier
sign(int x)19ccc74035SNikolas Klauser TEST_CONSTEXPR_CXX20 int sign(int x)
205a83710eSEric Fiselier {
215a83710eSEric Fiselier if (x == 0)
225a83710eSEric Fiselier return 0;
235a83710eSEric Fiselier if (x < 0)
245a83710eSEric Fiselier return -1;
255a83710eSEric Fiselier return 1;
265a83710eSEric Fiselier }
275a83710eSEric Fiselier
285a83710eSEric Fiselier template <class S>
29ccc74035SNikolas Klauser TEST_CONSTEXPR_CXX20 void
test(const S & s,const S & str,int x)305a83710eSEric Fiselier test(const S& s, const S& str, int x)
315a83710eSEric Fiselier {
32dea74b28Szoecarver LIBCPP_ASSERT_NOEXCEPT(s.compare(str));
335a83710eSEric Fiselier assert(sign(s.compare(str)) == sign(x));
345a83710eSEric Fiselier }
355a83710eSEric Fiselier
test()36*425620ccSNikolas Klauser TEST_CONSTEXPR_CXX20 bool test() {
375a83710eSEric Fiselier {
385a83710eSEric Fiselier typedef std::string S;
395a83710eSEric Fiselier test(S(""), S(""), 0);
405a83710eSEric Fiselier test(S(""), S("abcde"), -5);
415a83710eSEric Fiselier test(S(""), S("abcdefghij"), -10);
425a83710eSEric Fiselier test(S(""), S("abcdefghijklmnopqrst"), -20);
435a83710eSEric Fiselier test(S("abcde"), S(""), 5);
445a83710eSEric Fiselier test(S("abcde"), S("abcde"), 0);
455a83710eSEric Fiselier test(S("abcde"), S("abcdefghij"), -5);
465a83710eSEric Fiselier test(S("abcde"), S("abcdefghijklmnopqrst"), -15);
475a83710eSEric Fiselier test(S("abcdefghij"), S(""), 10);
485a83710eSEric Fiselier test(S("abcdefghij"), S("abcde"), 5);
495a83710eSEric Fiselier test(S("abcdefghij"), S("abcdefghij"), 0);
505a83710eSEric Fiselier test(S("abcdefghij"), S("abcdefghijklmnopqrst"), -10);
515a83710eSEric Fiselier test(S("abcdefghijklmnopqrst"), S(""), 20);
525a83710eSEric Fiselier test(S("abcdefghijklmnopqrst"), S("abcde"), 15);
535a83710eSEric Fiselier test(S("abcdefghijklmnopqrst"), S("abcdefghij"), 10);
545a83710eSEric Fiselier test(S("abcdefghijklmnopqrst"), S("abcdefghijklmnopqrst"), 0);
555a83710eSEric Fiselier }
56f2f2a639SEric Fiselier #if TEST_STD_VER >= 11
575a83710eSEric Fiselier {
585a83710eSEric Fiselier typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
595a83710eSEric Fiselier test(S(""), S(""), 0);
605a83710eSEric Fiselier test(S(""), S("abcde"), -5);
615a83710eSEric Fiselier test(S(""), S("abcdefghij"), -10);
625a83710eSEric Fiselier test(S(""), S("abcdefghijklmnopqrst"), -20);
635a83710eSEric Fiselier test(S("abcde"), S(""), 5);
645a83710eSEric Fiselier test(S("abcde"), S("abcde"), 0);
655a83710eSEric Fiselier test(S("abcde"), S("abcdefghij"), -5);
665a83710eSEric Fiselier test(S("abcde"), S("abcdefghijklmnopqrst"), -15);
675a83710eSEric Fiselier test(S("abcdefghij"), S(""), 10);
685a83710eSEric Fiselier test(S("abcdefghij"), S("abcde"), 5);
695a83710eSEric Fiselier test(S("abcdefghij"), S("abcdefghij"), 0);
705a83710eSEric Fiselier test(S("abcdefghij"), S("abcdefghijklmnopqrst"), -10);
715a83710eSEric Fiselier test(S("abcdefghijklmnopqrst"), S(""), 20);
725a83710eSEric Fiselier test(S("abcdefghijklmnopqrst"), S("abcde"), 15);
735a83710eSEric Fiselier test(S("abcdefghijklmnopqrst"), S("abcdefghij"), 10);
745a83710eSEric Fiselier test(S("abcdefghijklmnopqrst"), S("abcdefghijklmnopqrst"), 0);
755a83710eSEric Fiselier }
765a83710eSEric Fiselier #endif
7776b26852SMarshall Clow
7876b26852SMarshall Clow #if TEST_STD_VER > 3
7976b26852SMarshall Clow { // LWG 2946
8076b26852SMarshall Clow std::string s = " !";
8176b26852SMarshall Clow assert(s.compare({"abc", 1}) < 0);
8276b26852SMarshall Clow }
8376b26852SMarshall Clow #endif
842df59c50SJF Bastien
85ccc74035SNikolas Klauser return true;
86ccc74035SNikolas Klauser }
87ccc74035SNikolas Klauser
main(int,char **)88ccc74035SNikolas Klauser int main(int, char**)
89ccc74035SNikolas Klauser {
90ccc74035SNikolas Klauser test();
91ccc74035SNikolas Klauser #if TEST_STD_VER > 17
92*425620ccSNikolas Klauser static_assert(test());
93ccc74035SNikolas Klauser #endif
94ccc74035SNikolas Klauser
952df59c50SJF Bastien return 0;
965a83710eSEric Fiselier }
97