summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/22_locale/codecvt/utf8.cc
blob: 987233a130634a1b0cfb4ee0d6effd80810d166a (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
// Copyright (C) 2015 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-require-cstdint "" }
// { dg-options "-std=gnu++11" }

#include <locale>
#include <iterator>
#include <string>
#include <testsuite_hooks.h>

const char expected[] = u8"£¥€";
const std::size_t expected_len = std::char_traits<char>::length(expected);

template<typename C>
void test(const C* from)
{
  auto len = std::char_traits<C>::length(from);
  std::mbstate_t state{};
  char buf[16] = { };
  using test_type = std::codecvt<C, char, std::mbstate_t>;
  const test_type& cvt = std::use_facet<test_type>(std::locale::classic());
  auto from_end = from + len;
  auto from_next = from;
  auto buf_end = std::end(buf);
  auto buf_next = buf;
  auto res = cvt.out(state, from, from_end, from_next, buf, buf_end, buf_next);
  VERIFY( res == std::codecvt_base::ok );
  VERIFY( from_next == from_end );
  VERIFY( (buf_next - buf) == expected_len );
  VERIFY( 0 == std::char_traits<char>::compare(buf, expected, expected_len) );

  C buf2[16];
  auto exp_end = expected + expected_len;
  auto exp_next = expected;
  auto buf2_end = std::end(buf2);
  auto buf2_next = buf2;
  res = cvt.in(state, expected, exp_end, exp_next, buf2, buf2_end, buf2_next);
  VERIFY( res == std::codecvt_base::ok );
  VERIFY( exp_next == exp_end );
  VERIFY( (buf2_next - buf2) == len );
  VERIFY( 0 == std::char_traits<C>::compare(buf2, from, len) );
}

void
test01()
{
  test(u"£¥€");
}

void
test02()
{
  test(U"£¥€");
}

int
main()
{
  test01();
  test02();
}