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 // size_t converted() const; 16 17 // XFAIL: no-wide-characters 18 19 #include <locale> 20 #include <codecvt> 21 #include <cassert> 22 23 #include "test_macros.h" 24 25 template <class CharT, size_t = sizeof(CharT)> 26 struct TestHelper; 27 template <class CharT> 28 struct TestHelper<CharT, 2> { 29 static void test(); 30 }; 31 template <class CharT> 32 struct TestHelper<CharT, 4> { 33 static void test(); 34 }; 35 36 template <class CharT> 37 void TestHelper<CharT, 2>::test() { 38 static_assert((std::is_same<CharT, wchar_t>::value), ""); 39 { 40 typedef std::codecvt_utf8<CharT> Codecvt; 41 typedef std::wstring_convert<Codecvt> Myconv; 42 Myconv myconv; 43 assert(myconv.converted() == 0); 44 std::string bs = myconv.to_bytes(L"\u1005"); 45 assert(myconv.converted() == 1); 46 bs = myconv.to_bytes(L"\u1005e"); 47 assert(myconv.converted() == 2); 48 std::wstring ws = myconv.from_bytes("\xE1\x80\x85"); 49 assert(myconv.converted() == 3); 50 } 51 } 52 53 template <class CharT> 54 void TestHelper<CharT, 4>::test() { 55 static_assert((std::is_same<CharT, wchar_t>::value), ""); 56 { 57 typedef std::codecvt_utf8<CharT> Codecvt; 58 typedef std::wstring_convert<Codecvt> Myconv; 59 Myconv myconv; 60 assert(myconv.converted() == 0); 61 std::string bs = myconv.to_bytes(L"\U00040003"); 62 assert(myconv.converted() == 1); 63 bs = myconv.to_bytes(L"\U00040003e"); 64 assert(myconv.converted() == 2); 65 std::wstring ws = myconv.from_bytes("\xF1\x80\x80\x83"); 66 assert(myconv.converted() == 4); 67 } 68 } 69 70 int main(int, char**) { 71 TestHelper<wchar_t>::test(); 72 return 0; 73 } 74