OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libstdc++-v3/] [testsuite/] [ext/] [codecvt/] [char-2.cc] - Blame information for rev 820

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 742 jeremybenn
// { dg-require-iconv "UCS-2LE" }
2
// { dg-require-iconv "ISO-8859-15" }
3
 
4
// 2000-08-22 Benjamin Kosnik <bkoz@cygnus.com>
5
 
6
// Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009
7
// 2010, 2011
8
// Free Software Foundation
9
//
10
// This file is part of the GNU ISO C++ Library.  This library is free
11
// software; you can redistribute it and/or modify it under the
12
// terms of the GNU General Public License as published by the
13
// Free Software Foundation; either version 3, or (at your option)
14
// any later version.
15
 
16
// This library is distributed in the hope that it will be useful,
17
// but WITHOUT ANY WARRANTY; without even the implied warranty of
18
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
// GNU General Public License for more details.
20
 
21
// You should have received a copy of the GNU General Public License along
22
// with this library; see the file COPYING3.  If not see
23
// <http://www.gnu.org/licenses/>.
24
 
25
// 22.2.1.5 - Template class codecvt [lib.locale.codecvt]
26
#include <locale>
27
#include <cstring>
28
#include <testsuite_hooks.h>
29
#include <ext/codecvt_specializations.h>
30
 
31
/*
32
> how do I check that these conversions are correct?
33
Very easy.  Since all the characters are from ASCII you simply
34
zero-extend the values.
35
 
36
drepper$ echo 'black pearl jasmine tea' | od -t x1
37
0000000 62 6c 61 63 6b 20 70 65 61 72 6c 20 6a 61 73 6d
38
0000020 69 6e 65 20 74 65 61 0a
39
 
40
So the UCS-2 string is
41
 
42
0x0062, 0x006c, 0x0061, ...
43
 
44
You get the idea.  With iconv() you have to take care of the
45
byte-order, though.  UCS-2 can mean little- or big endian.  Looking at
46
your result
47
 
48
> $9 = 25856
49
 
50
it shows that the other byte-order is used (25856 == 0x6500).
51
*/
52
 
53
// Partial specialization using encoding_state.
54
// codecvt<unicode_t, char, encoding_state>
55
// UNICODE - UCS2 (little endian)
56
void test02()
57
{
58
  using namespace std;
59
  typedef codecvt_base::result                  result;
60
  typedef unsigned short                        int_type;
61
  typedef char                                  ext_type;
62
  typedef __gnu_cxx::encoding_state                     state_type;
63
  typedef codecvt<int_type, ext_type, state_type>       unicode_codecvt;
64
  typedef char_traits<int_type>                 int_traits;
65
  typedef char_traits<ext_type>                 ext_traits;
66
 
67
  bool test __attribute__((unused)) = true;
68
  const ext_type*       e_lit = "black pearl jasmine tea";
69
  int                   size = strlen(e_lit);
70
 
71
  char  i_lit_base[50] __attribute__((aligned(__alignof__(int_type)))) =
72
  {
73
    char(0x62), char(0x00), char(0x6c), char(0x00), char(0x61), char(0x00),
74
    char(0x63), char(0x00), char(0x6b), char(0x00), char(0x20), char(0x00),
75
    char(0x70), char(0x00), char(0x65), char(0x00), char(0x61), char(0x00),
76
    char(0x72), char(0x00), char(0x6c), char(0x00), char(0x20), char(0x00),
77
    char(0x6a), char(0x00), char(0x61), char(0x00), char(0x73), char(0x00),
78
    char(0x6d), char(0x00), char(0x69), char(0x00), char(0x6e), char(0x00),
79
    char(0x65), char(0x00), char(0x20), char(0x00), char(0x74), char(0x00),
80
    char(0x65), char(0x00), char(0x61), char(0x00), char(0xa0), char(0x00)
81
  };
82
  const int_type*       i_lit = reinterpret_cast<int_type*>(i_lit_base);
83
 
84
  const ext_type*       efrom_next;
85
  const int_type*       ifrom_next;
86
  ext_type*             e_arr = new ext_type[size + 1];
87
  ext_type*             eto_next;
88
  int_type*             i_arr = new int_type[size + 1];
89
  int_type*             ito_next;
90
 
91
  // construct a locale object with the specialized facet.
92
  locale                loc(locale::classic(), new unicode_codecvt);
93
  // sanity check the constructed locale has the specialized facet.
94
  VERIFY( has_facet<unicode_codecvt>(loc) );
95
  const unicode_codecvt& cvt = use_facet<unicode_codecvt>(loc);
96
 
97
  // in
98
  unicode_codecvt::state_type state01("UCS-2LE", "ISO-8859-15", 0, 0);
99
  // internal encoding is bigger because of bom
100
  result r1 = cvt.in(state01, e_lit, e_lit + size, efrom_next,
101
                     i_arr, i_arr + size + 1, ito_next);
102
  VERIFY( r1 == codecvt_base::ok );
103
  VERIFY( !int_traits::compare(i_arr, i_lit, size) );
104
  VERIFY( efrom_next == e_lit + size );
105
  VERIFY( ito_next == i_arr + size );
106
 
107
  // out
108
  unicode_codecvt::state_type state02("UCS-2LE", "ISO-8859-15", 0, 0);
109
  result r2 = cvt.out(state02, i_lit, i_lit + size, ifrom_next,
110
                       e_arr, e_arr + size, eto_next);
111
  VERIFY( r2 == codecvt_base::ok );
112
  VERIFY( !ext_traits::compare(e_arr, e_lit, size) );
113
  VERIFY( ifrom_next == i_lit + size );
114
  VERIFY( eto_next == e_arr + size );
115
 
116
  // unshift
117
  ext_traits::copy(e_arr, e_lit, size);
118
  unicode_codecvt::state_type state03("UCS-2LE", "ISO-8859-15", 0, 0);
119
  result r3 = cvt.unshift(state03, e_arr, e_arr + size, eto_next);
120
  VERIFY( r3 == codecvt_base::noconv );
121
  VERIFY( !ext_traits::compare(e_arr, e_lit, size) );
122
  VERIFY( eto_next == e_arr );
123
 
124
  int i = cvt.encoding();
125
  VERIFY( i == 2 ); // Target-dependent.
126
 
127
  VERIFY( !cvt.always_noconv() );
128
 
129
  unicode_codecvt::state_type state04("UCS-2LE", "ISO-8859-15", 0, 0);
130
  int j = cvt.length(state03, e_lit, e_lit + size, 5);
131
  VERIFY( j == 5 );
132
 
133
  int k = cvt.max_length();
134
  VERIFY( k == 1 );
135
 
136
  delete [] e_arr;
137
  delete [] i_arr;
138
}
139
 
140
int main ()
141
{
142
  test02();
143
  return 0;
144
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.