Home | History | Annotate | Download | only in string.view.ops
      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 
     11 // <string_view>
     12 // constexpr int compare(basic_string_view str) const noexcept;
     13 
     14 #include <experimental/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::experimental::basic_string_view<CharT> sv1,
     24             std::experimental::basic_string_view<CharT> sv2, int expected ) {
     25     assert ( sign( sv1.compare(sv2)) == sign(expected));
     26 }
     27 
     28 
     29 template<typename CharT>
     30 void test ( const CharT *s1, const CharT  *s2, int expected ) {
     31     typedef std::experimental::basic_string_view<CharT> string_view_t;
     32 
     33     string_view_t sv1 ( s1 );
     34     string_view_t sv2 ( s2 );
     35     test1(sv1, sv2, expected);
     36 }
     37 
     38 int main () {
     39 
     40     test("",                     "", 0);
     41     test("",                     "abcde", -5);
     42     test("",                     "abcdefghij", -10);
     43     test("",                     "abcdefghijklmnopqrst", -20);
     44     test("abcde",                "", 5);
     45     test("abcde",                "abcde", 0);
     46     test("abcde",                "abcdefghij", -5);
     47     test("abcde",                "abcdefghijklmnopqrst", -15);
     48     test("abcdefghij",           "", 10);
     49     test("abcdefghij",           "abcde", 5);
     50     test("abcdefghij",           "abcdefghij", 0);
     51     test("abcdefghij",           "abcdefghijklmnopqrst", -10);
     52     test("abcdefghijklmnopqrst", "", 20);
     53     test("abcdefghijklmnopqrst", "abcde", 15);
     54     test("abcdefghijklmnopqrst", "abcdefghij", 10);
     55     test("abcdefghijklmnopqrst", "abcdefghijklmnopqrst", 0);
     56 
     57     test(L"",                     L"", 0);
     58     test(L"",                     L"abcde", -5);
     59     test(L"",                     L"abcdefghij", -10);
     60     test(L"",                     L"abcdefghijklmnopqrst", -20);
     61     test(L"abcde",                L"", 5);
     62     test(L"abcde",                L"abcde", 0);
     63     test(L"abcde",                L"abcdefghij", -5);
     64     test(L"abcde",                L"abcdefghijklmnopqrst", -15);
     65     test(L"abcdefghij",           L"", 10);
     66     test(L"abcdefghij",           L"abcde", 5);
     67     test(L"abcdefghij",           L"abcdefghij", 0);
     68     test(L"abcdefghij",           L"abcdefghijklmnopqrst", -10);
     69     test(L"abcdefghijklmnopqrst", L"", 20);
     70     test(L"abcdefghijklmnopqrst", L"abcde", 15);
     71     test(L"abcdefghijklmnopqrst", L"abcdefghij", 10);
     72     test(L"abcdefghijklmnopqrst", L"abcdefghijklmnopqrst", 0);
     73 
     74 #if TEST_STD_VER >= 11
     75     test(u"",                     u"", 0);
     76     test(u"",                     u"abcde", -5);
     77     test(u"",                     u"abcdefghij", -10);
     78     test(u"",                     u"abcdefghijklmnopqrst", -20);
     79     test(u"abcde",                u"", 5);
     80     test(u"abcde",                u"abcde", 0);
     81     test(u"abcde",                u"abcdefghij", -5);
     82     test(u"abcde",                u"abcdefghijklmnopqrst", -15);
     83     test(u"abcdefghij",           u"", 10);
     84     test(u"abcdefghij",           u"abcde", 5);
     85     test(u"abcdefghij",           u"abcdefghij", 0);
     86     test(u"abcdefghij",           u"abcdefghijklmnopqrst", -10);
     87     test(u"abcdefghijklmnopqrst", u"", 20);
     88     test(u"abcdefghijklmnopqrst", u"abcde", 15);
     89     test(u"abcdefghijklmnopqrst", u"abcdefghij", 10);
     90     test(u"abcdefghijklmnopqrst", u"abcdefghijklmnopqrst", 0);
     91 
     92     test(U"",                     U"", 0);
     93     test(U"",                     U"abcde", -5);
     94     test(U"",                     U"abcdefghij", -10);
     95     test(U"",                     U"abcdefghijklmnopqrst", -20);
     96     test(U"abcde",                U"", 5);
     97     test(U"abcde",                U"abcde", 0);
     98     test(U"abcde",                U"abcdefghij", -5);
     99     test(U"abcde",                U"abcdefghijklmnopqrst", -15);
    100     test(U"abcdefghij",           U"", 10);
    101     test(U"abcdefghij",           U"abcde", 5);
    102     test(U"abcdefghij",           U"abcdefghij", 0);
    103     test(U"abcdefghij",           U"abcdefghijklmnopqrst", -10);
    104     test(U"abcdefghijklmnopqrst", U"", 20);
    105     test(U"abcdefghijklmnopqrst", U"abcde", 15);
    106     test(U"abcdefghijklmnopqrst", U"abcdefghij", 10);
    107     test(U"abcdefghijklmnopqrst", U"abcdefghijklmnopqrst", 0);
    108 #endif
    109 
    110 #if TEST_STD_VER > 11
    111     {
    112     typedef std::experimental::basic_string_view<char, constexpr_char_traits<char>> SV;
    113     constexpr SV  sv1 { "abcde", 5 };
    114     constexpr SV  sv2 { "abcde", 5 };
    115     constexpr SV  sv3 { "edcba0", 6 };
    116     static_assert ( sv1.compare(sv2) == 0, "" );
    117     static_assert ( sv2.compare(sv1) == 0, "" );
    118     static_assert ( sv3.compare(sv2)  > 0, "" );
    119     static_assert ( sv2.compare(sv3)  < 0, "" );
    120     }
    121 #endif
    122 }
    123