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 // template <> class codecvt<wchar_t, char, mbstate_t>
12 
13 // result out(stateT& state,
14 //            const internT* from, const internT* from_end, const internT*& from_next,
15 //            externT* to, externT* to_end, externT*& to_next) const;
16 
17 #include <locale>
18 #include <string>
19 #include <vector>
20 #include <cassert>
21 #include <cstddef>
22 #include <cstring>
23 
24 #include "test_macros.h"
25 
26 typedef std::codecvt<wchar_t, char, std::mbstate_t> F;
27 
main(int,char **)28 int main(int, char**)
29 {
30     std::locale l = std::locale::classic();
31     const F& f = std::use_facet<F>(l);
32     {
33         const std::basic_string<F::intern_type> from(L"some text");
34         std::vector<char> to(from.size()+1);
35         std::mbstate_t mbs = {};
36         const F::intern_type* from_next = 0;
37         char* to_next = 0;
38         F::result r = f.out(mbs, from.data(), from.data() + from.size(), from_next,
39                                  to.data(), to.data() + to.size(), to_next);
40         assert(r == F::ok);
41         assert(static_cast<std::size_t>(from_next - from.data()) == from.size());
42         assert(static_cast<std::size_t>(to_next - to.data()) == from.size());
43         assert(to.data() == std::string("some text"));
44     }
45     {
46         std::basic_string<F::intern_type> from(L"some text");
47         from[4] = '\0';
48         std::vector<char> to(from.size()+1);
49         std::mbstate_t mbs = {};
50         const F::intern_type* from_next = 0;
51         char* to_next = 0;
52         F::result r = f.out(mbs, from.data(), from.data() + from.size(), from_next,
53                                  to.data(), to.data() + to.size(), to_next);
54         assert(r == F::ok);
55         assert(static_cast<std::size_t>(from_next - from.data()) == from.size());
56         assert(static_cast<std::size_t>(to_next - to.data()) == from.size());
57         assert(memcmp(to.data(), "some\0text", from.size()) == 0);
58     }
59     {
60         std::basic_string<F::intern_type> from(L"some text");
61         std::vector<char> to(from.size()-1);
62         std::mbstate_t mbs = {};
63         const F::intern_type* from_next = 0;
64         char* to_next = 0;
65         F::result r = f.out(mbs, from.data(), from.data() + from.size(), from_next,
66                                  to.data(), to.data() + to.size()-1, to_next);
67         assert(r == F::partial);
68         assert(static_cast<std::size_t>(from_next - from.data()) == to.size()-1);
69         assert(static_cast<std::size_t>(to_next - to.data()) == to.size()-1);
70         assert(to.data() == std::string("some te"));
71     }
72 
73   return 0;
74 }
75