1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <locale>
11
12 // wstring_convert<Codecvt, Elem, Wide_alloc, Byte_alloc>
13
14 // byte_string to_bytes(Elem wchar);
15 // byte_string to_bytes(const Elem* wptr);
16 // byte_string to_bytes(const wide_string& wstr);
17 // byte_string to_bytes(const Elem* first, const Elem* last);
18
19 #include <locale>
20 #include <codecvt>
21 #include <cassert>
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 std::wstring_convert<std::codecvt_utf8<CharT> > myconv;
39 std::wstring ws(1, CharT(0x1005));
40 std::string bs = myconv.to_bytes(ws[0]);
41 assert(bs == "\xE1\x80\x85\x00");
42 bs = myconv.to_bytes(ws.c_str());
43 assert(bs == "\xE1\x80\x85\x00");
44 bs = myconv.to_bytes(ws);
45 assert(bs == "\xE1\x80\x85\x00");
46 bs = myconv.to_bytes(ws.data(), ws.data() + ws.size());
47 assert(bs == "\xE1\x80\x85\x00");
48 bs = myconv.to_bytes(L"");
49 assert(bs.size() == 0);
50 }
51 }
52
53 template <class CharT>
test()54 void TestHelper<CharT, 4>::test() {
55 static_assert((std::is_same<CharT, wchar_t>::value), "");
56 {
57 std::wstring_convert<std::codecvt_utf8<CharT> > myconv;
58 std::wstring ws(1, CharT(0x40003));
59 std::string bs = myconv.to_bytes(ws[0]);
60 assert(bs == "\xF1\x80\x80\x83");
61 bs = myconv.to_bytes(ws.c_str());
62 assert(bs == "\xF1\x80\x80\x83");
63 bs = myconv.to_bytes(ws);
64 assert(bs == "\xF1\x80\x80\x83");
65 bs = myconv.to_bytes(ws.data(), ws.data() + ws.size());
66 assert(bs == "\xF1\x80\x80\x83");
67 bs = myconv.to_bytes(L"");
68 assert(bs.size() == 0);
69 }
70 }
71
main()72 int main() { TestHelper<wchar_t>::test(); }
73