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