summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/22_locale/codecvt/codecvt_utf16/79980.cc
blob: d8b9729ed5b2d44fde7cb941640822cd5f566c74 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (C) 2017 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-do run { target c++11 } }

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

// PR libstdc++/79980

constexpr std::codecvt_mode mode(std::codecvt_mode m)
{ return static_cast<std::codecvt_mode>(m | std::consume_header); }

template<typename WCh, unsigned long Max = 0x10FFFF,
	 std::codecvt_mode Mode = std::consume_header>
  using Conv
    = std::wstring_convert<std::codecvt_utf16<WCh, Max, mode(Mode)>, WCh>;

void
test01()
{
  const char src[] = "\xFE\xFF\xAB\xCD";
  Conv<char16_t> conv;
  auto dst = conv.from_bytes(src, src+4);
  VERIFY( dst[0] == 0xabcd );
}

void
test02()
{
  const char src[] = "\xFF\xFE\xAB\xCD";
  Conv<char16_t> conv;
  auto dst = conv.from_bytes(src, src+4);
  VERIFY( dst[0] == 0xcdab );
}

void
test03()
{
  const char src[] = "\xFE\xFF\xAB\xCD";
  Conv<char16_t, 0x10FFFF, std::little_endian> conv;
  auto dst = conv.from_bytes(src, src+4);
  VERIFY( dst[0] == 0xabcd );
}

void
test04()
{
  const char src[] = "\xFF\xFE\xAB\xCD";
  Conv<char16_t, 0x10FFFF, std::little_endian> conv;
  auto dst = conv.from_bytes(src, src+4);
  VERIFY( dst[0] == 0xcdab );
}

void
test05()
{
  const char src[] = "\0\x61\xAB\xCD"; // character greater than 0x00FF
  Conv<char16_t, 0xFF> conv("to_bytes failed", u"from_bytes failed");
  std::u16string result = conv.from_bytes(src, src+4);
  VERIFY( result == u"from_bytes failed" );
  VERIFY( conv.converted() == 2 );
}

void
test06()
{
  const char src[] = "\0\x61\xAB\xCD";
  Conv<char16_t> conv("to_bytes failed", u"from_bytes failed");
  std::u16string result = conv.from_bytes(src, src+3); // incomplete character
  VERIFY( result == u"from_bytes failed" );
  VERIFY( conv.converted() == 2 );
}

void
test07()
{
  Conv<char16_t> conv("to_bytes failed", u"from_bytes failed");
  // ucs2 to utf-16 conversion should fail on invalid ucs2 input:
  std::u16string utf16 = u"1234\U00001111\U0001ffff";
  auto out = conv.to_bytes(utf16);
  VERIFY( out == "to_bytes failed" );
  VERIFY( conv.converted() == 5 );

  // And should also fail on incomplete surrogate pair (not return partial):
  out = conv.to_bytes(utf16.substr(0, utf16.size()-1));
  VERIFY( out == "to_bytes failed" );
  VERIFY( conv.converted() == 5 );
}

void
test08()
{
  // Read/write UTF-16 code units from data not correctly aligned for char16_t
  Conv<char16_t, 0x10FFFF, std::generate_header> conv;
  const char src[] = "-\xFE\xFF\0\x61\xAB\xCD";
  auto out = conv.from_bytes(src + 1, src + 7);
  VERIFY( out[0] == 0x0061 );
  VERIFY( out[1] == 0xabcd );
  auto bytes = conv.to_bytes(out);
  VERIFY( bytes == std::string(src + 1, 6) );
}

void
test09()
{
  // Read/write UTF-16 code units from data not correctly aligned for char16_t
  Conv<char32_t, 0x10FFFF, std::generate_header> conv;
  const char src[] = "-\xFE\xFF\xD8\x08\xDF\x45";
  auto out = conv.from_bytes(src + 1, src + 7);
  VERIFY( out == U"\U00012345" );
  auto bytes = conv.to_bytes(out);
  VERIFY( bytes == std::string(src + 1, 6) );
}

int main()
{
  test01();
  test02();
  test03();
  test04();
  test05();
  test06();
  test07();
  test08();
  test09();
}