summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/22_locale/codecvt/char16_t.cc
blob: 012ccf5a4b854fd5b2c8bd0ebb65aaba6118beb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (C) 2015-2016 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

// { dg-options "-std=gnu++11" }
// { dg-require-cstdint "" }

// [locale.codecvt], C++11 22.4.1.4.  specialization.

#include <locale>
#include <cstring>
#include <testsuite_hooks.h>

void
test01()
{
  using namespace std;
  typedef codecvt<char16_t, char, mbstate_t> codecvt_c16;
  locale loc_c = locale::classic();
  VERIFY(has_facet<codecvt_c16>(loc_c));
  const codecvt_c16* const cvt = &use_facet<codecvt_c16>(loc_c);

  VERIFY(!cvt->always_noconv());
  VERIFY(cvt->max_length() == 3);
  VERIFY(cvt->encoding() == 0);

  const char u8dat[] = u8"H\U000000E4ll\U000000F6 \U0001F63F \U000056FD "
    u8"\U0000222B f(\U000003BA) exp(-2\U000003C0\U000003C9) d\U000003BA "
    u8"\U0001F6BF \U0001F6BF \U0001F648 \U00000413\U00000435\U0000043E"
    u8"\U00000433\U00000440\U00000430\U00000444\U00000438\U0000044F \U0000FB05";
  const char* const u8dat_end = std::end(u8dat);

  const char16_t u16dat[] = u"H\U000000E4ll\U000000F6 \U0001F63F \U000056FD "
    u"\U0000222B f(\U000003BA) exp(-2\U000003C0\U000003C9) d\U000003BA "
    u"\U0001F6BF \U0001F6BF \U0001F648 \U00000413\U00000435\U0000043E"
    u"\U00000433\U00000440\U00000430\U00000444\U00000438\U0000044F \U0000FB05";
  const char16_t* const u16dat_end = std::end(u16dat);

  {
    const size_t len = u16dat_end - u16dat + 1;
    char16_t* const buffer = new char16_t[len];
    char16_t* const buffer_end = buffer + len;

    const char* from_next;
    char16_t* to_next;

    codecvt_c16::state_type state01;
    state01 = {};
    codecvt_base::result res = cvt->in(state01, u8dat, u8dat_end, from_next,
                                       buffer, buffer_end, to_next);

    VERIFY(res == codecvt_base::ok);
    VERIFY(from_next == u8dat_end);
    VERIFY(std::memcmp((void*)buffer, (void*)u16dat, sizeof(u16dat)) == 0);

    delete[] buffer;
  }

  {
    const size_t len = u8dat_end - u8dat + 1;
    char* const buffer = new char[len];
    char* const buffer_end = buffer + len;

    const char16_t* from_next;
    char* to_next;

    codecvt_c16::state_type state01;
    state01 = {};
    codecvt_base::result res = cvt->out(state01, u16dat, u16dat_end, from_next,
                                        buffer, buffer_end, to_next);

    VERIFY(res == codecvt_base::ok);
    VERIFY(from_next == u16dat_end);
    VERIFY(std::memcmp((void*)buffer, (void*)u8dat, sizeof(u8dat)) == 0);

    delete[] buffer;
  }
}

int
main()
{
  test01();
}