Home | History | Annotate | Download | only in string.cons
      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 // UNSUPPORTED: c++98, c++03, c++11, c++14
     11 // UNSUPPORTED: libcpp-no-deduction-guides
     12 //
     13 // This feature is not yet ready in clang
     14 // http://b/36401676
     15 // XFAIL: *
     16 
     17 // <string>
     18 
     19 // Test that the constructors offered by std::basic_string are formulated
     20 // so they're compatible with implicit deduction guides.
     21 
     22 #include <string>
     23 #include <string_view>
     24 #include <cassert>
     25 
     26 #include "test_macros.h"
     27 #include "test_allocator.h"
     28 #include "test_iterators.h"
     29 #include "constexpr_char_traits.hpp"
     30 
     31 template <class T, class Alloc = std::allocator<T>>
     32 using BStr = std::basic_string<T, std::char_traits<T>, Alloc>;
     33 
     34 // Overloads
     35 //  using A = Allocator;
     36 //  using BS = basic_string
     37 //  using BSV = basic_string_view
     38 // ---------------
     39 // (1)  basic_string() - NOT TESTED
     40 // (2)  basic_string(A const&) - BROKEN
     41 // (3)  basic_string(size_type, CharT, const A& = A())
     42 // (4)  basic_string(BS const&, size_type, A const& = A())
     43 // (5)  basic_string(BS const&, size_type, size_type, A const& = A()) - PARTIALLY BROKEN
     44 // (6)  basic_string(const CharT*, size_type, A const& = A())
     45 // (7)  basic_string(const CharT*, A const& = A())
     46 // (8)  basic_string(InputIt, InputIt, A const& = A()) - BROKEN
     47 // (9)  basic_string(BS const&)
     48 // (10) basic_string(BS const&, A const&)
     49 // (11) basic_string(BS&&)
     50 // (12) basic_string(BS&&, A const&)
     51 // (13) basic_string(initializer_list<CharT>, A const& = A())
     52 // (14) basic_string(BSV, A const& = A())
     53 // (15) basic_string(const T&, size_type, size_type, A const& = A()) - BROKEN
     54 int main()
     55 {
     56   using TestSizeT = test_allocator<char>::size_type;
     57   { // Testing (1)
     58     // Nothing TODO. Cannot deduce without any arguments.
     59   }
     60   { // Testing (2)
     61     // This overload isn't compatible with implicit deduction guides as
     62     // specified in the standard.
     63     // const test_allocator<char> alloc{};
     64     // std::basic_string s(alloc);
     65   }
     66   { // Testing (3) w/o allocator
     67     std::basic_string s(6ull, 'a');
     68     ASSERT_SAME_TYPE(decltype(s), std::string);
     69     assert(s == "aaaaaa");
     70 
     71     std::basic_string w(2ull, L'b');
     72     ASSERT_SAME_TYPE(decltype(w), std::wstring);
     73     assert(w == L"bb");
     74   }
     75   { // Testing (3) w/ allocator
     76     std::basic_string s(6ull, 'a', test_allocator<char>{});
     77     ASSERT_SAME_TYPE(decltype(s), BStr<char,test_allocator<char>>);
     78     assert(s == "aaaaaa");
     79 
     80     std::basic_string w(2ull, L'b', test_allocator<wchar_t>{});
     81     ASSERT_SAME_TYPE(decltype(w), BStr<wchar_t, test_allocator<wchar_t>>);
     82     assert(w == L"bb");
     83   }
     84   { // Testing (4) w/o allocator
     85     const std::string sin("abc");
     86     std::basic_string s(sin, (size_t)1);
     87     ASSERT_SAME_TYPE(decltype(s), std::string);
     88     assert(s == "bc");
     89 
     90     using WStr = std::basic_string<wchar_t,
     91                                   constexpr_char_traits<wchar_t>,
     92                                   test_allocator<wchar_t>>;
     93     const WStr win(L"abcdef");
     94     std::basic_string w(win, (TestSizeT)3);
     95     ASSERT_SAME_TYPE(decltype(w), WStr);
     96     assert(w == L"def");
     97   }
     98   { // Testing (4) w/ allocator
     99     const std::string sin("abc");
    100     std::basic_string s(sin, (size_t)1, std::allocator<char>{});
    101     ASSERT_SAME_TYPE(decltype(s), std::string);
    102     assert(s == "bc");
    103 
    104     using WStr = std::basic_string<wchar_t,
    105                                   constexpr_char_traits<wchar_t>,
    106                                   test_allocator<wchar_t>>;
    107     const WStr win(L"abcdef");
    108     std::basic_string w(win, (TestSizeT)3, test_allocator<wchar_t>{});
    109     ASSERT_SAME_TYPE(decltype(w), WStr);
    110     assert(w == L"def");
    111   }
    112   { // Testing (5) w/o allocator
    113 #if 0 // FIXME: This doesn't work
    114     const std::string sin("abc");
    115     std::basic_string s(sin, (size_t)1, (size_t)3);
    116     ASSERT_SAME_TYPE(decltype(s), std::string);
    117     assert(s == "bc");
    118 
    119     using WStr = std::basic_string<wchar_t,
    120                                   constexpr_char_traits<wchar_t>,
    121                                   test_allocator<wchar_t>>;
    122     const WStr win(L"abcdef");
    123     std::basic_string w(win, (TestSizeT)2, (TestSizeT)3);
    124     ASSERT_SAME_TYPE(decltype(w), WStr);
    125     assert(w == L"cde");
    126 #endif
    127   }
    128   { // Testing (5) w/ allocator
    129     const std::string sin("abc");
    130     std::basic_string s(sin, (size_t)1, (size_t)3, std::allocator<char>{});
    131     ASSERT_SAME_TYPE(decltype(s), std::string);
    132     assert(s == "bc");
    133 
    134     using WStr = std::basic_string<wchar_t,
    135                                   constexpr_char_traits<wchar_t>,
    136                                   test_allocator<wchar_t>>;
    137     const WStr win(L"abcdef");
    138     std::basic_string w(win, (TestSizeT)2, (TestSizeT)3, test_allocator<wchar_t>{});
    139     ASSERT_SAME_TYPE(decltype(w), WStr);
    140     assert(w == L"cde");
    141   }
    142   { // Testing (6) w/o allocator
    143     std::basic_string s("abc", (size_t)2);
    144     ASSERT_SAME_TYPE(decltype(s), std::string);
    145     assert(s == "ab");
    146 
    147     std::basic_string w(L"abcdef", (size_t)3);
    148     ASSERT_SAME_TYPE(decltype(w), std::wstring);
    149     assert(w == L"abc");
    150   }
    151   { // Testing (6) w/ allocator
    152     std::basic_string s("abc", (size_t)2, std::allocator<char>{});
    153     ASSERT_SAME_TYPE(decltype(s), std::string);
    154     assert(s == "ab");
    155 
    156     using WStr = std::basic_string<wchar_t,
    157                                   std::char_traits<wchar_t>,
    158                                   test_allocator<wchar_t>>;
    159     std::basic_string w(L"abcdef", (TestSizeT)3, test_allocator<wchar_t>{});
    160     ASSERT_SAME_TYPE(decltype(w), WStr);
    161     assert(w == L"abc");
    162   }
    163   { // Testing (7) w/o allocator
    164     std::basic_string s("abc");
    165     ASSERT_SAME_TYPE(decltype(s), std::string);
    166     assert(s == "abc");
    167 
    168     std::basic_string w(L"abcdef");
    169     ASSERT_SAME_TYPE(decltype(w), std::wstring);
    170     assert(w == L"abcdef");
    171   }
    172   { // Testing (7) w/ allocator
    173     std::basic_string s("abc", std::allocator<char>{});
    174     ASSERT_SAME_TYPE(decltype(s), std::string);
    175     assert(s == "abc");
    176 
    177     using WStr = std::basic_string<wchar_t,
    178                                   std::char_traits<wchar_t>,
    179                                   test_allocator<wchar_t>>;
    180     std::basic_string w(L"abcdef", test_allocator<wchar_t>{});
    181     ASSERT_SAME_TYPE(decltype(w), WStr);
    182     assert(w == L"abcdef");
    183   }
    184   { // (8) w/o allocator
    185     // This overload isn't compatible with implicit deduction guides as
    186     // specified in the standard.
    187     // FIXME: Propose adding an explicit guide to the standard?
    188   }
    189   { // (8) w/ allocator
    190     // This overload isn't compatible with implicit deduction guides as
    191     // specified in the standard.
    192     // FIXME: Propose adding an explicit guide to the standard?
    193 #if 0
    194     using It = input_iterator<const char*>;
    195     const char* input = "abcdef";
    196     std::basic_string s(It(input), It(input + 3), std::allocator<char>{});
    197     ASSERT_SAME_TYPE(decltype(s), std::string);
    198 #endif
    199   }
    200   { // Testing (9)
    201     const std::string sin("abc");
    202     std::basic_string s(sin);
    203     ASSERT_SAME_TYPE(decltype(s), std::string);
    204     assert(s == "abc");
    205 
    206     using WStr = std::basic_string<wchar_t,
    207                                   constexpr_char_traits<wchar_t>,
    208                                   test_allocator<wchar_t>>;
    209     const WStr win(L"abcdef");
    210     std::basic_string w(win);
    211     ASSERT_SAME_TYPE(decltype(w), WStr);
    212     assert(w == L"abcdef");
    213   }
    214   { // Testing (10)
    215     const std::string sin("abc");
    216     std::basic_string s(sin, std::allocator<char>{});
    217     ASSERT_SAME_TYPE(decltype(s), std::string);
    218     assert(s == "abc");
    219 
    220     using WStr = std::basic_string<wchar_t,
    221                                   constexpr_char_traits<wchar_t>,
    222                                   test_allocator<wchar_t>>;
    223     const WStr win(L"abcdef");
    224     std::basic_string w(win, test_allocator<wchar_t>{});
    225     ASSERT_SAME_TYPE(decltype(w), WStr);
    226     assert(w == L"abcdef");
    227   }
    228   { // Testing (11)
    229     std::string sin("abc");
    230     std::basic_string s(std::move(sin));
    231     ASSERT_SAME_TYPE(decltype(s), std::string);
    232     assert(s == "abc");
    233 
    234     using WStr = std::basic_string<wchar_t,
    235                                   constexpr_char_traits<wchar_t>,
    236                                   test_allocator<wchar_t>>;
    237     WStr win(L"abcdef");
    238     std::basic_string w(std::move(win));
    239     ASSERT_SAME_TYPE(decltype(w), WStr);
    240     assert(w == L"abcdef");
    241   }
    242   { // Testing (12)
    243     std::string sin("abc");
    244     std::basic_string s(std::move(sin), std::allocator<char>{});
    245     ASSERT_SAME_TYPE(decltype(s), std::string);
    246     assert(s == "abc");
    247 
    248     using WStr = std::basic_string<wchar_t,
    249                                   constexpr_char_traits<wchar_t>,
    250                                   test_allocator<wchar_t>>;
    251     WStr win(L"abcdef");
    252     std::basic_string w(std::move(win), test_allocator<wchar_t>{});
    253     ASSERT_SAME_TYPE(decltype(w), WStr);
    254     assert(w == L"abcdef");
    255   }
    256   { // Testing (13) w/o allocator
    257     std::basic_string s({'a', 'b', 'c'});
    258     ASSERT_SAME_TYPE(decltype(s), std::string);
    259     assert(s == "abc");
    260 
    261     std::basic_string w({L'a', L'b', L'c'});
    262     ASSERT_SAME_TYPE(decltype(w), std::wstring);
    263     assert(w == L"abc");
    264   }
    265   { // Testing (13) w/ allocator
    266     std::basic_string s({'a', 'b', 'c'}, test_allocator<char>{});
    267     ASSERT_SAME_TYPE(decltype(s), BStr<char, test_allocator<char>>);
    268     assert(s == "abc");
    269 
    270     std::basic_string w({L'a', L'b', L'c'}, test_allocator<wchar_t>{});
    271     ASSERT_SAME_TYPE(decltype(w), BStr<wchar_t, test_allocator<wchar_t>>);
    272     assert(w == L"abc");
    273   }
    274   { // Testing (14) w/o allocator
    275     std::string_view sv("abc");
    276     std::basic_string s(sv);
    277     ASSERT_SAME_TYPE(decltype(s), std::string);
    278     assert(s == "abc");
    279 
    280     using Expect = std::basic_string<wchar_t, constexpr_char_traits<wchar_t>>;
    281     std::basic_string_view<wchar_t, constexpr_char_traits<wchar_t>> BSV(L"abcdef");
    282     std::basic_string w(BSV);
    283     ASSERT_SAME_TYPE(decltype(w), Expect);
    284     assert(w == L"abcdef");
    285   }
    286   { // Testing (14) w/ allocator
    287     using ExpectS = std::basic_string<char, std::char_traits<char>, test_allocator<char>>;
    288     std::string_view sv("abc");
    289     std::basic_string s(sv, test_allocator<char>{});
    290     ASSERT_SAME_TYPE(decltype(s), ExpectS);
    291     assert(s == "abc");
    292 
    293     using ExpectW = std::basic_string<wchar_t, constexpr_char_traits<wchar_t>,
    294                                       test_allocator<wchar_t>>;
    295     std::basic_string_view<wchar_t, constexpr_char_traits<wchar_t>> BSV(L"abcdef");
    296     std::basic_string w(BSV, test_allocator<wchar_t>{});
    297     ASSERT_SAME_TYPE(decltype(w), ExpectW);
    298     assert(w == L"abcdef");
    299   }
    300   { // Testing (15)
    301     // This overload isn't compatible with implicit deduction guides as
    302     // specified in the standard.
    303   }
    304 }
    305