summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/22_locale/conversions/string/2.cc
blob: 08a28e6cb1028209ffedce4998267f0ff07049e7 (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
// { dg-do run { target c++11 } }

// 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/>.

// 22.3.3.2.2  String conversions

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

template<typename Elem>
struct cvt : std::codecvt<Elem, char, std::mbstate_t> { };

template<typename Elem>
using str_conv = std::wstring_convert<cvt<Elem>, Elem>;

using std::string;
using std::u16string;
using std::u32string;

// test conversion errors, with and without error strings

void test01()
{
  typedef str_conv<char> sc;

  const sc::byte_string berr = "invalid wide string";
  const sc::wide_string werr = u8"invalid byte string";

  sc c(berr, werr);
  string input = "Stop";
  input += char(0xFF);
  string woutput = c.from_bytes(input);
  VERIFY( input == woutput ); // noconv case doesn't detect invalid input
  string winput = u8"Stop";
  winput += char(0xFF);
  string output = c.to_bytes(winput);
  VERIFY( winput == output ); // noconv case doesn't detect invalid input
}

void test02()
{
  typedef str_conv<char16_t> sc;

  const sc::byte_string berr = "invalid wide string";
  const sc::wide_string werr = u"invalid byte string";

  sc c(berr, werr);
  string input = "Stop";
  input += char(0xFF);
  u16string woutput = c.from_bytes(input);
  VERIFY( werr == woutput );
  u16string winput = u"Stop";
  winput += char16_t(0xDC00);
  string output = c.to_bytes(winput);
  VERIFY( berr == output );
}

void test03()
{
  typedef str_conv<char32_t> sc;

  const sc::byte_string berr = "invalid wide string";
  const sc::wide_string werr = U"invalid byte string";

  sc c(berr, werr);
  string input = "Halt";
  input += char(0xff);
  u32string woutput = c.from_bytes(input);
  VERIFY( werr == woutput );
  u32string winput = U"Halt";
  winput += char32_t(-1);
  string output = c.to_bytes(winput);
  VERIFY( berr == output );
}

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