Home | History | Annotate | Download | only in string_compare
      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>
     11 
     12 // int compare(const basic_string_view sv) const
     13 
     14 #include <string>
     15 #include <cassert>
     16 
     17 #include "min_allocator.h"
     18 
     19 int sign(int x)
     20 {
     21     if (x == 0)
     22         return 0;
     23     if (x < 0)
     24         return -1;
     25     return 1;
     26 }
     27 
     28 template <class S, class SV>
     29 void
     30 test(const S& s, SV sv, int x)
     31 {
     32     assert(sign(s.compare(sv)) == sign(x));
     33 }
     34 
     35 int main()
     36 {
     37     {
     38     typedef std::string S;
     39     typedef std::string_view SV;
     40     test(S(""), SV(""), 0);
     41     test(S(""), SV("abcde"), -5);
     42     test(S(""), SV("abcdefghij"), -10);
     43     test(S(""), SV("abcdefghijklmnopqrst"), -20);
     44     test(S("abcde"), SV(""), 5);
     45     test(S("abcde"), SV("abcde"), 0);
     46     test(S("abcde"), SV("abcdefghij"), -5);
     47     test(S("abcde"), SV("abcdefghijklmnopqrst"), -15);
     48     test(S("abcdefghij"), SV(""), 10);
     49     test(S("abcdefghij"), SV("abcde"), 5);
     50     test(S("abcdefghij"), SV("abcdefghij"), 0);
     51     test(S("abcdefghij"), SV("abcdefghijklmnopqrst"), -10);
     52     test(S("abcdefghijklmnopqrst"), SV(""), 20);
     53     test(S("abcdefghijklmnopqrst"), SV("abcde"), 15);
     54     test(S("abcdefghijklmnopqrst"), SV("abcdefghij"), 10);
     55     test(S("abcdefghijklmnopqrst"), SV("abcdefghijklmnopqrst"), 0);
     56     }
     57 #if TEST_STD_VER >= 11
     58     {
     59     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
     60     typedef std::string_view SV;
     61     test(S(""), SV(""), 0);
     62     test(S(""), SV("abcde"), -5);
     63     test(S(""), SV("abcdefghij"), -10);
     64     test(S(""), SV("abcdefghijklmnopqrst"), -20);
     65     test(S("abcde"), SV(""), 5);
     66     test(S("abcde"), SV("abcde"), 0);
     67     test(S("abcde"), SV("abcdefghij"), -5);
     68     test(S("abcde"), SV("abcdefghijklmnopqrst"), -15);
     69     test(S("abcdefghij"), SV(""), 10);
     70     test(S("abcdefghij"), SV("abcde"), 5);
     71     test(S("abcdefghij"), SV("abcdefghij"), 0);
     72     test(S("abcdefghij"), SV("abcdefghijklmnopqrst"), -10);
     73     test(S("abcdefghijklmnopqrst"), SV(""), 20);
     74     test(S("abcdefghijklmnopqrst"), SV("abcde"), 15);
     75     test(S("abcdefghijklmnopqrst"), SV("abcdefghij"), 10);
     76     test(S("abcdefghijklmnopqrst"), SV("abcdefghijklmnopqrst"), 0);
     77     }
     78 #endif
     79 }
     80