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