xref: /llvm-project/libcxx/test/std/strings/basic.string.literals/literal.pass.cpp (revision f4c1258d5633fcf06385ff3fd1f4bf57ab971964)
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 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
29         ASSERT_SAME_TYPE(decltype( L"Hi"s), std::wstring);
30 #endif
31         ASSERT_SAME_TYPE(decltype( u"Hi"s), std::u16string);
32         ASSERT_SAME_TYPE(decltype( U"Hi"s), std::u32string);
33 
34         std::string foo;
35 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
36         std::wstring Lfoo;
37 #endif
38         u8string u8foo;
39         std::u16string ufoo;
40         std::u32string Ufoo;
41 
42         foo   =   ""s;     assert(  foo.size() == 0);
43         u8foo = u8""s;     assert(u8foo.size() == 0);
44 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
45         Lfoo  =  L""s;     assert( Lfoo.size() == 0);
46 #endif
47         ufoo  =  u""s;     assert( ufoo.size() == 0);
48         Ufoo  =  U""s;     assert( Ufoo.size() == 0);
49 
50         foo   =   " "s;    assert(  foo.size() == 1);
51         u8foo = u8" "s;    assert(u8foo.size() == 1);
52 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
53         Lfoo  =  L" "s;    assert( Lfoo.size() == 1);
54 #endif
55         ufoo  =  u" "s;    assert( ufoo.size() == 1);
56         Ufoo  =  U" "s;    assert( Ufoo.size() == 1);
57 
58         foo   =   "ABC"s;     assert(  foo ==   "ABC");   assert(  foo == std::string   (  "ABC"));
59         u8foo = u8"ABC"s;     assert(u8foo == u8"ABC");   assert(u8foo == u8string      (u8"ABC"));
60 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
61         Lfoo  =  L"ABC"s;     assert( Lfoo ==  L"ABC");   assert( Lfoo == std::wstring  ( L"ABC"));
62 #endif
63         ufoo  =  u"ABC"s;     assert( ufoo ==  u"ABC");   assert( ufoo == std::u16string( u"ABC"));
64         Ufoo  =  U"ABC"s;     assert( Ufoo ==  U"ABC");   assert( Ufoo == std::u32string( U"ABC"));
65     }
66     {
67         using namespace std::literals;
68         std::string foo = ""s;
69         assert(foo == std::string());
70     }
71     {
72         using namespace std;
73         std::string foo = ""s;
74         assert(foo == std::string());
75     }
76 
77     return 0;
78 }
79