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 // <locale>
10 
11 // wstring_convert<Codecvt, Elem, Wide_alloc, Byte_alloc>
12 
13 // wide_string from_bytes(char byte);
14 // wide_string from_bytes(const char* ptr);
15 // wide_string from_bytes(const byte_string& str);
16 // wide_string from_bytes(const char* first, const char* last);
17 
18 // XFAIL: no-wide-characters
19 
20 #include <locale>
21 #include <codecvt>
22 #include <cassert>
23 
24 #include "test_macros.h"
25 
26 template <class CharT, size_t = sizeof(CharT)>
27 struct TestHelper;
28 template <class CharT>
29 struct TestHelper<CharT, 2> {
30   static void test();
31 };
32 template <class CharT>
33 struct TestHelper<CharT, 4> {
34   static void test();
35 };
36 
37 template <class CharT>
38 void TestHelper<CharT, 2>::test() {
39   static_assert((std::is_same<CharT, wchar_t>::value), "");
40   {
41     std::wstring_convert<std::codecvt_utf8<CharT> > myconv;
42     std::string bs("\xE1\x80\x85");
43     std::wstring ws = myconv.from_bytes('a');
44     assert(ws == L"a");
45     ws = myconv.from_bytes(bs.c_str());
46     assert(ws == L"\u1005");
47     ws = myconv.from_bytes(bs);
48     assert(ws == L"\u1005");
49     ws = myconv.from_bytes(bs.data(), bs.data() + bs.size());
50     assert(ws == L"\u1005");
51     ws = myconv.from_bytes("");
52     assert(ws.size() == 0);
53   }
54 }
55 
56 template <class CharT>
57 void TestHelper<CharT, 4>::test() {
58   static_assert((std::is_same<CharT, wchar_t>::value), "");
59   {
60     std::wstring_convert<std::codecvt_utf8<CharT> > myconv;
61     std::string bs("\xF1\x80\x80\x83");
62     std::wstring ws = myconv.from_bytes('a');
63     assert(ws == L"a");
64     ws = myconv.from_bytes(bs.c_str());
65     assert(ws == L"\U00040003");
66     ws = myconv.from_bytes(bs);
67     assert(ws == L"\U00040003");
68     ws = myconv.from_bytes(bs.data(), bs.data() + bs.size());
69     assert(ws == L"\U00040003");
70     ws = myconv.from_bytes("");
71     assert(ws.size() == 0);
72   }
73 }
74 
75 int main(int, char**) {
76   TestHelper<wchar_t>::test();
77   return 0;
78 }
79