1// Copyright (C) 2015 Free Software Foundation, Inc.
2//
3// This file is part of the GNU ISO C++ Library.  This library is free
4// software; you can redistribute it and/or modify it under the
5// terms of the GNU General Public License as published by the
6// Free Software Foundation; either version 3, or (at your option)
7// any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License along
15// with this library; see the file COPYING3.  If not see
16// <http://www.gnu.org/licenses/>.
17
18// { dg-options "-std=gnu++11" }
19// { dg-require-cstdint "" }
20
21// [locale.codecvt], C++11 22.4.1.4.  specialization.
22
23#include <locale>
24#include <cstring>
25#include <testsuite_hooks.h>
26
27void
28test01()
29{
30  using namespace std;
31  typedef codecvt<char16_t, char, mbstate_t> codecvt_c16;
32  locale loc_c = locale::classic();
33  VERIFY(has_facet<codecvt_c16>(loc_c));
34  const codecvt_c16* const cvt = &use_facet<codecvt_c16>(loc_c);
35
36  VERIFY(!cvt->always_noconv());
37  VERIFY(cvt->max_length() == 3);
38  VERIFY(cvt->encoding() == 0);
39
40  const char u8dat[] = u8"H\U000000E4ll\U000000F6 \U0001F63F \U000056FD "
41    u8"\U0000222B f(\U000003BA) exp(-2\U000003C0\U000003C9) d\U000003BA "
42    u8"\U0001F6BF \U0001F6BF \U0001F648 \U00000413\U00000435\U0000043E"
43    u8"\U00000433\U00000440\U00000430\U00000444\U00000438\U0000044F \U0000FB05";
44  const char* const u8dat_end = std::end(u8dat);
45
46  const char16_t u16dat[] = u"H\U000000E4ll\U000000F6 \U0001F63F \U000056FD "
47    u"\U0000222B f(\U000003BA) exp(-2\U000003C0\U000003C9) d\U000003BA "
48    u"\U0001F6BF \U0001F6BF \U0001F648 \U00000413\U00000435\U0000043E"
49    u"\U00000433\U00000440\U00000430\U00000444\U00000438\U0000044F \U0000FB05";
50  const char16_t* const u16dat_end = std::end(u16dat);
51
52  {
53    const size_t len = u16dat_end - u16dat + 1;
54    char16_t* const buffer = new char16_t[len];
55    char16_t* const buffer_end = buffer + len;
56
57    const char* from_next;
58    char16_t* to_next;
59
60    codecvt_c16::state_type state01;
61    state01 = {};
62    codecvt_base::result res = cvt->in(state01, u8dat, u8dat_end, from_next,
63                                       buffer, buffer_end, to_next);
64
65    VERIFY(res == codecvt_base::ok);
66    VERIFY(from_next == u8dat_end);
67    VERIFY(std::memcmp((void*)buffer, (void*)u16dat, sizeof(u16dat)) == 0);
68
69    delete[] buffer;
70  }
71
72  {
73    const size_t len = u8dat_end - u8dat + 1;
74    char* const buffer = new char[len];
75    char* const buffer_end = buffer + len;
76
77    const char16_t* from_next;
78    char* to_next;
79
80    codecvt_c16::state_type state01;
81    state01 = {};
82    codecvt_base::result res = cvt->out(state01, u16dat, u16dat_end, from_next,
83                                        buffer, buffer_end, to_next);
84
85    VERIFY(res == codecvt_base::ok);
86    VERIFY(from_next == u16dat_end);
87    VERIFY(std::memcmp((void*)buffer, (void*)u8dat, sizeof(u8dat)) == 0);
88
89    delete[] buffer;
90  }
91}
92
93int
94main()
95{
96  test01();
97}
98