xref: /llvm-project/libcxx/test/std/strings/basic.string.literals/literal.pass.cpp (revision d4b59a05fc7507cf69993109443dc5af47ae4fa8)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11
10 
11 #include <string>
12 #include <cassert>
13 
14 #include "test_macros.h"
15 
16 #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
17     typedef std::u8string u8string;
18 #else
19     typedef std::string   u8string;
20 #endif
21 
22 int main(int, char**) {
23     {
24         using namespace std::literals::string_literals;
25 
26         ASSERT_SAME_TYPE(decltype(  "Hi"s), std::string);
27         ASSERT_SAME_TYPE(decltype(u8"Hi"s), u8string);
28         ASSERT_SAME_TYPE(decltype( L"Hi"s), std::wstring);
29         ASSERT_SAME_TYPE(decltype( u"Hi"s), std::u16string);
30         ASSERT_SAME_TYPE(decltype( U"Hi"s), std::u32string);
31 
32         std::string foo;
33         std::wstring Lfoo;
34         u8string u8foo;
35         std::u16string ufoo;
36         std::u32string Ufoo;
37 
38         foo   =   ""s;     assert(  foo.size() == 0);
39         u8foo = u8""s;     assert(u8foo.size() == 0);
40         Lfoo  =  L""s;     assert( Lfoo.size() == 0);
41         ufoo  =  u""s;     assert( ufoo.size() == 0);
42         Ufoo  =  U""s;     assert( Ufoo.size() == 0);
43 
44         foo   =   " "s;    assert(  foo.size() == 1);
45         u8foo = u8" "s;    assert(u8foo.size() == 1);
46         Lfoo  =  L" "s;    assert( Lfoo.size() == 1);
47         ufoo  =  u" "s;    assert( ufoo.size() == 1);
48         Ufoo  =  U" "s;    assert( Ufoo.size() == 1);
49 
50         foo   =   "ABC"s;     assert(  foo ==   "ABC");   assert(  foo == std::string   (  "ABC"));
51         u8foo = u8"ABC"s;     assert(u8foo == u8"ABC");   assert(u8foo == u8string      (u8"ABC"));
52         Lfoo  =  L"ABC"s;     assert( Lfoo ==  L"ABC");   assert( Lfoo == std::wstring  ( L"ABC"));
53         ufoo  =  u"ABC"s;     assert( ufoo ==  u"ABC");   assert( ufoo == std::u16string( u"ABC"));
54         Ufoo  =  U"ABC"s;     assert( Ufoo ==  U"ABC");   assert( Ufoo == std::u32string( U"ABC"));
55     }
56     {
57         using namespace std::literals;
58         std::string foo = ""s;
59         assert(foo == std::string());
60     }
61     {
62         using namespace std;
63         std::string foo = ""s;
64         assert(foo == std::string());
65     }
66 
67     return 0;
68 }
69