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