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 -D_LIBCPP_ENABLE_CXX26_REMOVED_CODECVT -D_LIBCPP_ENABLE_CXX26_REMOVED_WSTRING_CONVERT
12 
13 // wstring_convert<Codecvt, Elem, Wide_alloc, Byte_alloc>
14 
15 // byte_string to_bytes(Elem wchar);
16 // byte_string to_bytes(const Elem* wptr);
17 // byte_string to_bytes(const wide_string& wstr);
18 // byte_string to_bytes(const Elem* first, const Elem* 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, std::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>
test()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::wstring ws(1, L'\u1005');
45     std::string bs = myconv.to_bytes(ws[0]);
46     assert(bs == "\xE1\x80\x85");
47     bs = myconv.to_bytes(ws.c_str());
48     assert(bs == "\xE1\x80\x85");
49     bs = myconv.to_bytes(ws);
50     assert(bs == "\xE1\x80\x85");
51     bs = myconv.to_bytes(ws.data(), ws.data() + ws.size());
52     assert(bs == "\xE1\x80\x85");
53     bs = myconv.to_bytes(L"");
54     assert(bs.size() == 0);
55   }
56 }
57 
58 template <class CharT>
test()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::wstring ws(1, *L"\U00040003");
64     std::string bs = myconv.to_bytes(ws[0]);
65     assert(bs == "\xF1\x80\x80\x83");
66     bs = myconv.to_bytes(ws.c_str());
67     assert(bs == "\xF1\x80\x80\x83");
68     bs = myconv.to_bytes(ws);
69     assert(bs == "\xF1\x80\x80\x83");
70     bs = myconv.to_bytes(ws.data(), ws.data() + ws.size());
71     assert(bs == "\xF1\x80\x80\x83");
72     bs = myconv.to_bytes(L"");
73     assert(bs.size() == 0);
74   }
75 }
76 
main(int,char **)77 int main(int, char**) {
78   TestHelper<wchar_t>::test();
79   return 0;
80 }
81