xref: /llvm-project/libcxx/test/std/strings/basic.string.literals/literal.pass.cpp (revision a40bada91aeda276a772acfbcae6e8de26755a11)
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 #ifndef TEST_HAS_NO_CHAR8_T
17 typedef std::u8string u8string;
18 #else
19 typedef std::string u8string;
20 #endif
21 
main(int,char **)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;
43     assert(foo.size() == 0);
44     u8foo = u8""s;
45     assert(u8foo.size() == 0);
46 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
47     Lfoo = L""s;
48     assert(Lfoo.size() == 0);
49 #endif
50     ufoo = u""s;
51     assert(ufoo.size() == 0);
52     Ufoo = U""s;
53     assert(Ufoo.size() == 0);
54 
55     foo = " "s;
56     assert(foo.size() == 1);
57     u8foo = u8" "s;
58     assert(u8foo.size() == 1);
59 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
60     Lfoo = L" "s;
61     assert(Lfoo.size() == 1);
62 #endif
63     ufoo = u" "s;
64     assert(ufoo.size() == 1);
65     Ufoo = U" "s;
66     assert(Ufoo.size() == 1);
67 
68     foo = "ABC"s;
69     assert(foo == "ABC");
70     assert(foo == std::string("ABC"));
71     u8foo = u8"ABC"s;
72     assert(u8foo == u8"ABC");
73     assert(u8foo == u8string(u8"ABC"));
74 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
75     Lfoo = L"ABC"s;
76     assert(Lfoo == L"ABC");
77     assert(Lfoo == std::wstring(L"ABC"));
78 #endif
79     ufoo = u"ABC"s;
80     assert(ufoo == u"ABC");
81     assert(ufoo == std::u16string(u"ABC"));
82     Ufoo = U"ABC"s;
83     assert(Ufoo == U"ABC");
84     assert(Ufoo == std::u32string(U"ABC"));
85   }
86   {
87     using namespace std::literals;
88     std::string foo = ""s;
89     assert(foo == std::string());
90   }
91   {
92     using namespace std;
93     std::string foo = ""s;
94     assert(foo == std::string());
95   }
96 
97   return 0;
98 }
99