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