]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/cintltst/ncnvtst.c
ICU-6.2.4.tar.gz
[apple/icu.git] / icuSources / test / cintltst / ncnvtst.c
CommitLineData
b75a7d8f
A
1/********************************************************************
2 * COPYRIGHT:
374ca955 3 * Copyright (c) 1997-2004, International Business Machines Corporation and
b75a7d8f
A
4 * others. All Rights Reserved.
5 ********************************************************************/
6/********************************************************************************
7*
8* File CCONVTST.C
9*
10* Modification History:
11* Name Description
12* Madhu Katragadda 7/7/2000 Converter Tests for extended code coverage
13*********************************************************************************
14*/
15#include <stdio.h>
374ca955
A
16#include <stdlib.h>
17#include <string.h>
b75a7d8f
A
18#include "unicode/uloc.h"
19#include "unicode/ucnv.h"
20#include "unicode/utypes.h"
21#include "unicode/ustring.h"
22#include "unicode/uset.h"
23#include "cintltst.h"
24
25#define MAX_LENGTH 999
26
27#define UNICODE_LIMIT 0x10FFFF
28#define SURROGATE_HIGH_START 0xD800
29#define SURROGATE_LOW_END 0xDFFF
30
31static int32_t gInBufferSize = 0;
32static int32_t gOutBufferSize = 0;
33static char gNuConvTestName[1024];
34
35#define nct_min(x,y) ((x<y) ? x : y)
36#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
37
38static void printSeq(const unsigned char* a, int len);
39static void printSeqErr(const unsigned char* a, int len);
40static void printUSeq(const UChar* a, int len);
41static void printUSeqErr(const UChar* a, int len);
42static UBool convertFromU( const UChar *source, int sourceLen, const uint8_t *expect, int expectLen,
43 const char *codepage, const int32_t *expectOffsets, UBool doFlush, UErrorCode expectedStatus);
44static UBool convertToU( const uint8_t *source, int sourceLen, const UChar *expect, int expectLen,
45 const char *codepage, const int32_t *expectOffsets, UBool doFlush, UErrorCode expectedStatus);
46
47static UBool testConvertFromU( const UChar *source, int sourceLen, const uint8_t *expect, int expectLen,
48 const char *codepage, UConverterFromUCallback callback, const int32_t *expectOffsets, UBool testReset);
49static UBool testConvertToU( const uint8_t *source, int sourcelen, const UChar *expect, int expectlen,
50 const char *codepage, UConverterToUCallback callback, const int32_t *expectOffsets, UBool testReset);
51
52static void setNuConvTestName(const char *codepage, const char *direction)
53{
374ca955
A
54 sprintf(gNuConvTestName, "[Testing %s %s Unicode, InputBufSiz=%d, OutputBufSiz=%d]",
55 codepage,
56 direction,
57 (int)gInBufferSize,
58 (int)gOutBufferSize);
b75a7d8f
A
59}
60
61
62static void TestSurrogateBehaviour(void);
63static void TestErrorBehaviour(void);
64static void TestToUnicodeErrorBehaviour(void);
65static void TestGetNextErrorBehaviour(void);
66static void TestRegressionUTF8(void);
67static void TestRegressionUTF32(void);
68static void TestAvailableConverters(void);
69static void TestFlushInternalBuffer(void); /*for improved code coverage in ucnv_cnv.c*/
70static void TestResetBehaviour(void);
71static void TestTruncated(void);
72static void TestUnicodeSet(void);
73
74static void TestWithBufferSize(int32_t osize, int32_t isize);
75
76
77static void printSeq(const unsigned char* a, int len)
78{
79 int i=0;
80 log_verbose("\n{");
81 while (i<len)
82 log_verbose("0x%02X ", a[i++]);
83 log_verbose("}\n");
84}
85
86static void printUSeq(const UChar* a, int len)
87{
88 int i=0;
89 log_verbose("\n{");
90 while (i<len)
91 log_verbose("%0x04X ", a[i++]);
92 log_verbose("}\n");
93}
94
95static void printSeqErr(const unsigned char* a, int len)
96{
97 int i=0;
98 fprintf(stderr, "\n{");
99 while (i<len) fprintf(stderr, "0x%02X ", a[i++]);
100 fprintf(stderr, "}\n");
101}
102
103static void printUSeqErr(const UChar* a, int len)
104{
105 int i=0;
106 fprintf(stderr, "\n{");
107 while (i<len)
108 fprintf(stderr, "0x%04X ", a[i++]);
109 fprintf(stderr,"}\n");
110}
111
112void addExtraTests(TestNode** root);
113
114void addExtraTests(TestNode** root)
115{
116 addTest(root, &TestSurrogateBehaviour, "tsconv/ncnvtst/TestSurrogateBehaviour");
117 addTest(root, &TestErrorBehaviour, "tsconv/ncnvtst/TestErrorBehaviour");
118 addTest(root, &TestToUnicodeErrorBehaviour, "tsconv/ncnvtst/ToUnicodeErrorBehaviour");
119 addTest(root, &TestGetNextErrorBehaviour, "tsconv/ncnvtst/TestGetNextErrorBehaviour");
120 addTest(root, &TestAvailableConverters, "tsconv/ncnvtst/TestAvailableConverters");
121 addTest(root, &TestFlushInternalBuffer, "tsconv/ncnvtst/TestFlushInternalBuffer");
122 addTest(root, &TestResetBehaviour, "tsconv/ncnvtst/TestResetBehaviour");
123 addTest(root, &TestRegressionUTF8, "tsconv/ncnvtst/TestRegressionUTF8");
124 addTest(root, &TestRegressionUTF32, "tsconv/ncnvtst/TestRegressionUTF32");
125 addTest(root, &TestTruncated, "tsconv/ncnvtst/TestTruncated");
126 addTest(root, &TestUnicodeSet, "tsconv/ncnvtst/TestUnicodeSet");
127}
128
129/*test surrogate behaviour*/
130static void TestSurrogateBehaviour(){
131 log_verbose("Testing for SBCS and LATIN_1\n");
132 {
133 UChar sampleText[] = {0x0031, 0xd801, 0xdc01, 0x0032};
134 const uint8_t expected[] = {0x31, 0x1a, 0x32};
135 /*SBCS*/
136 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
137 expected, sizeof(expected), "ibm-920", 0 , TRUE, U_ZERO_ERROR))
138 log_err("u-> ibm-920 [UCNV_SBCS] not match.\n");
139
140 /*LATIN_1*/
141 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
142 expected, sizeof(expected), "LATIN_1", 0, TRUE, U_ZERO_ERROR ))
143 log_err("u-> LATIN_1 not match.\n");
144
145 }
146 log_verbose("Testing for DBCS and MBCS\n");
147 {
148 UChar sampleText[] = {0x00a1, 0xd801, 0xdc01, 0x00a4};
149 const uint8_t expected[] = {0xa2, 0xae, 0xa1, 0xe0, 0xa2, 0xb4};
150 int32_t offsets[] = {0x00, 0x00, 0x01, 0x01, 0x03, 0x03 };
151
152 /*DBCS*/
153 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
154 expected, sizeof(expected), "ibm-1363", 0 , TRUE, U_ZERO_ERROR))
155 log_err("u-> ibm-1363 [UCNV_DBCS portion] not match.\n");
156 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
157 expected, sizeof(expected), "ibm-1363", offsets , TRUE, U_ZERO_ERROR))
158 log_err("u-> ibm-1363 [UCNV_DBCS portion] not match.\n");
159 /*MBCS*/
160 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
161 expected, sizeof(expected), "ibm-1363", 0 , TRUE, U_ZERO_ERROR))
162 log_err("u-> ibm-1363 [UCNV_MBCS] not match.\n");
163 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
164 expected, sizeof(expected), "ibm-1363", offsets, TRUE, U_ZERO_ERROR))
165 log_err("u-> ibm-1363 [UCNV_MBCS] not match.\n");
166 }
167 log_verbose("Testing for ISO-2022-jp\n");
168 {
169 UChar sampleText[] = { 0x4e00, 0x04e01, 0x0031, 0xd801, 0xdc01, 0x0032};
170
171 const uint8_t expected[] = {0x1b, 0x24, 0x42,0x30,0x6c,0x43,0x7a,0x1b,0x28,0x42,
172 0x31,0x1A, 0x32};
173
174
175 int32_t offsets[] = {0,0,0,0,0,1,1,2,2,2,2,3,5 };
176
177 /*iso-2022-jp*/
178 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
179 expected, sizeof(expected), "iso-2022-jp", 0 , TRUE, U_ZERO_ERROR))
180 log_err("u-> not match.\n");
181 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
182 expected, sizeof(expected), "iso-2022-jp", offsets , TRUE, U_ZERO_ERROR))
183 log_err("u-> not match.\n");
184 }
185 log_verbose("Testing for ISO-2022-cn\n");
186 {
187 static const UChar sampleText[] = { 0x4e00, 0x04e01, 0x0031, 0xd801, 0xdc01, 0x0032};
188
189 static const uint8_t expected[] = {
190 0x1B, 0x24, 0x29, 0x41, 0x0E, 0x52, 0x3B,
191 0x36, 0x21,
192 0x0F, 0x31,
193 0x1A,
374ca955 194 0x32
b75a7d8f
A
195 };
196
197
198
199 static const int32_t offsets[] = {
200 0, 0, 0, 0, 0, 0, 0,
201 1, 1,
202 2, 2,
203 3,
374ca955 204 5, };
b75a7d8f
A
205
206 /*iso-2022-CN*/
207 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
208 expected, sizeof(expected), "iso-2022-cn", 0 , TRUE, U_ZERO_ERROR))
209 log_err("u-> not match.\n");
210 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
211 expected, sizeof(expected), "iso-2022-cn", offsets , TRUE, U_ZERO_ERROR))
212 log_err("u-> not match.\n");
213 }
214 log_verbose("Testing for ISO-2022-kr\n");
215 {
216 static const UChar sampleText[] = { 0x4e00,0xd801, 0xdc01, 0x04e01, 0x0031, 0xd801, 0xdc01, 0x0032};
217
218 static const uint8_t expected[] = {0x1B, 0x24, 0x29, 0x43,
219 0x0E, 0x6C, 0x69,
220 0x0f, 0x1A,
221 0x0e, 0x6F, 0x4B,
222 0x0F, 0x31,
223 0x1A,
224 0x32 };
225
226 static const int32_t offsets[] = {-1, -1, -1, -1,
227 0, 0, 0,
228 1, 1,
229 3, 3, 3,
230 4, 4,
231 5,
232 7,
233 };
234
235 /*iso-2022-kr*/
236 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
237 expected, sizeof(expected), "iso-2022-kr", 0 , TRUE, U_ZERO_ERROR))
238 log_err("u-> iso-2022-kr [UCNV_DBCS] not match.\n");
239 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
240 expected, sizeof(expected), "iso-2022-kr", offsets , TRUE, U_ZERO_ERROR))
241 log_err("u-> iso-2022-kr [UCNV_DBCS] not match.\n");
242 }
243 log_verbose("Testing for HZ\n");
244 {
245 static const UChar sampleText[] = { 0x4e00, 0xd801, 0xdc01, 0x04e01, 0x0031, 0xd801, 0xdc01, 0x0032};
246
247 static const uint8_t expected[] = {0x7E, 0x7B, 0x52, 0x3B,
248 0x7E, 0x7D, 0x1A,
249 0x7E, 0x7B, 0x36, 0x21,
250 0x7E, 0x7D, 0x31,
251 0x1A,
252 0x32 };
253
254
255 static const int32_t offsets[] = {0,0,0,0,
256 1,1,1,
257 3,3,3,3,
258 4,4,4,
259 5,
260 7,};
261
262 /*hz*/
263 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
264 expected, sizeof(expected), "HZ", 0 , TRUE, U_ZERO_ERROR))
265 log_err("u-> not match.\n");
266 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
267 expected, sizeof(expected), "HZ", offsets , TRUE, U_ZERO_ERROR))
268 log_err("u-> not match.\n");
269 }
270 /*UTF-8*/
271 log_verbose("Testing for UTF8\n");
272 {
273 static const UChar sampleText[] = { 0x4e00, 0x0701, 0x0031, 0xbfc1, 0xd801, 0xdc01, 0x0032};
274 static const int32_t offsets[]={0x00, 0x00, 0x00, 0x01, 0x01, 0x02,
275 0x03, 0x03, 0x03, 0x04, 0x04, 0x04,
276 0x04, 0x06 };
277 static const uint8_t expected[] = {0xe4, 0xb8, 0x80, 0xdc, 0x81, 0x31,
278 0xeb, 0xbf, 0x81, 0xF0, 0x90, 0x90, 0x81, 0x32};
279
280
281 static const int32_t fromOffsets[] = { 0x0000, 0x0003, 0x0005, 0x0006, 0x0009, 0x0009, 0x000D };
282 /*UTF-8*/
283 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
284 expected, sizeof(expected), "UTF8", offsets, TRUE, U_ZERO_ERROR ))
285 log_err("u-> UTF8 with offsets and flush true did not match.\n");
286 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
287 expected, sizeof(expected), "UTF8", 0, TRUE, U_ZERO_ERROR ))
288 log_err("u-> UTF8 with offsets and flush true did not match.\n");
289 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
290 expected, sizeof(expected), "UTF8", offsets, FALSE, U_ZERO_ERROR ))
291 log_err("u-> UTF8 with offsets and flush true did not match.\n");
292 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
293 expected, sizeof(expected), "UTF8", 0, FALSE, U_ZERO_ERROR ))
294 log_err("u-> UTF8 with offsets and flush true did not match.\n");
295
296 if(!convertToU(expected, sizeof(expected),
297 sampleText, sizeof(sampleText)/sizeof(sampleText[0]), "UTF8", 0, TRUE, U_ZERO_ERROR ))
298 log_err("UTF8 -> did not match.\n");
299 if(!convertToU(expected, sizeof(expected),
300 sampleText, sizeof(sampleText)/sizeof(sampleText[0]), "UTF8", 0, FALSE, U_ZERO_ERROR ))
301 log_err("UTF8 -> did not match.\n");
302 if(!convertToU(expected, sizeof(expected),
303 sampleText, sizeof(sampleText)/sizeof(sampleText[0]), "UTF8", fromOffsets, TRUE, U_ZERO_ERROR ))
304 log_err("UTF8 -> did not match.\n");
305 if(!convertToU(expected, sizeof(expected),
306 sampleText, sizeof(sampleText)/sizeof(sampleText[0]), "UTF8", fromOffsets, FALSE, U_ZERO_ERROR ))
307 log_err("UTF8 -> did not match.\n");
308
309 }
310
311
312
313}
314
315/*test various error behaviours*/
316static void TestErrorBehaviour(){
317 log_verbose("Testing for SBCS and LATIN_1\n");
318 {
319 static const UChar sampleText[] = { 0x0031, 0xd801};
320 static const UChar sampleText2[] = { 0x0031, 0xd801, 0x0032};
374ca955
A
321 static const uint8_t expected0[] = { 0x31};
322 static const uint8_t expected[] = { 0x31, 0x1a};
b75a7d8f
A
323 static const uint8_t expected2[] = { 0x31, 0x1a, 0x32};
324
325 /*SBCS*/
326 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
374ca955 327 expected, sizeof(expected), "ibm-920", 0, TRUE, U_ZERO_ERROR))
b75a7d8f
A
328 log_err("u-> ibm-920 [UCNV_SBCS] \n");
329 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
374ca955 330 expected0, sizeof(expected0), "ibm-920", 0, FALSE, U_ZERO_ERROR))
b75a7d8f
A
331 log_err("u-> ibm-920 [UCNV_SBCS] \n");
332 if(!convertFromU(sampleText2, sizeof(sampleText2)/sizeof(sampleText2[0]),
333 expected2, sizeof(expected2), "ibm-920", 0, TRUE, U_ZERO_ERROR))
334 log_err("u-> ibm-920 [UCNV_SBCS] did not match\n");
335
336
337 /*LATIN_1*/
338 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
374ca955 339 expected, sizeof(expected), "LATIN_1", 0, TRUE, U_ZERO_ERROR))
b75a7d8f
A
340 log_err("u-> LATIN_1 is supposed to fail\n");
341 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
374ca955 342 expected0, sizeof(expected0), "LATIN_1", 0, FALSE, U_ZERO_ERROR))
b75a7d8f
A
343 log_err("u-> LATIN_1 is supposed to fail\n");
344
345 if(!convertFromU(sampleText2, sizeof(sampleText2)/sizeof(sampleText2[0]),
346 expected2, sizeof(expected2), "LATIN_1", 0, TRUE, U_ZERO_ERROR))
347 log_err("u-> LATIN_1 did not match\n");
348 }
349
350
351 log_verbose("Testing for DBCS and MBCS\n");
352 {
353 static const UChar sampleText[] = { 0x00a1, 0xd801};
354 static const uint8_t expected[] = { 0xa2, 0xae};
374ca955
A
355 static const int32_t offsets[] = { 0x00, 0x00};
356 static const uint8_t expectedSUB[] = { 0xa2, 0xae, 0xa1, 0xe0};
357 static const int32_t offsetsSUB[] = { 0x00, 0x00, 0x01, 0x01};
b75a7d8f
A
358
359 static const UChar sampleText2[] = { 0x00a1, 0xd801, 0x00a4};
360 static const uint8_t expected2[] = { 0xa2, 0xae, 0xa1, 0xe0, 0xa2, 0xb4};
361 static const int32_t offsets2[] = { 0x00, 0x00, 0x01, 0x01, 0x02, 0x02};
362
363 static const UChar sampleText3MBCS[] = { 0x0001, 0x00a4, 0xdc01};
364 static const uint8_t expected3MBCS[] = { 0x01, 0xa2, 0xb4, 0xa1, 0xe0};
365 static const int32_t offsets3MBCS[] = { 0x00, 0x01, 0x01, 0x02, 0x02};
366
367 static const UChar sampleText4MBCS[] = { 0x0061, 0x00a6, 0xdc01};
368 static const uint8_t expected4MBCS[] = { 0x61, 0x8f, 0xa2, 0xc3, 0xf4, 0xfe};
369 static const int32_t offsets4MBCS[] = { 0x00, 0x01, 0x01, 0x01, 0x02, 0x02 };
370
371
372
373
374
375 /*DBCS*/
376 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
374ca955 377 expectedSUB, sizeof(expectedSUB), "ibm-1363", 0, TRUE, U_ZERO_ERROR))
b75a7d8f
A
378 log_err("u-> ibm-1363 [UCNV_DBCS portion] is supposed to fail\n");
379 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
380 expected, sizeof(expected), "ibm-1363", 0, FALSE, U_ZERO_ERROR))
381 log_err("u-> ibm-1363 [UCNV_DBCS portion] is supposed to fail\n");
382
383 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
374ca955 384 expectedSUB, sizeof(expectedSUB), "ibm-1363", offsetsSUB, TRUE, U_ZERO_ERROR))
b75a7d8f
A
385 log_err("u-> ibm-1363 [UCNV_DBCS portion] is supposed to fail\n");
386 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
387 expected, sizeof(expected), "ibm-1363", offsets, FALSE, U_ZERO_ERROR))
388 log_err("u-> ibm-1363 [UCNV_DBCS portion] is supposed to fail\n");
389
390
391 if(!convertFromU(sampleText2, sizeof(sampleText2)/sizeof(sampleText2[0]),
392 expected2, sizeof(expected2), "ibm-1363", 0, TRUE, U_ZERO_ERROR))
393 log_err("u-> ibm-1363 [UCNV_DBCS portion] did not match \n");
394 if(!convertFromU(sampleText2, sizeof(sampleText2)/sizeof(sampleText2[0]),
395 expected2, sizeof(expected2), "ibm-1363", offsets2, TRUE, U_ZERO_ERROR))
396 log_err("u-> ibm-1363 [UCNV_DBCS portion] did not match \n");
397
398 /*MBCS*/
399 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
374ca955 400 expectedSUB, sizeof(expectedSUB), "ibm-1363", 0, TRUE, U_ZERO_ERROR))
b75a7d8f
A
401 log_err("u-> ibm-1363 [UCNV_MBCS] \n");
402 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
403 expected, sizeof(expected), "ibm-1363", 0, FALSE, U_ZERO_ERROR))
404 log_err("u-> ibm-1363 [UCNV_MBCS] \n");
405
406 if(!convertFromU(sampleText2, sizeof(sampleText2)/sizeof(sampleText2[0]),
407 expected2, sizeof(expected2), "ibm-1363", 0, TRUE, U_ZERO_ERROR))
408 log_err("u-> ibm-1363 [UCNV_DBCS] did not match\n");
409 if(!convertFromU(sampleText2, sizeof(sampleText2)/sizeof(sampleText2[0]),
410 expected2, sizeof(expected2), "ibm-1363", 0, FALSE, U_ZERO_ERROR))
411 log_err("u-> ibm-1363 [UCNV_DBCS] did not match\n");
412 if(!convertFromU(sampleText2, sizeof(sampleText2)/sizeof(sampleText2[0]),
413 expected2, sizeof(expected2), "ibm-1363", offsets2, FALSE, U_ZERO_ERROR))
414 log_err("u-> ibm-1363 [UCNV_DBCS] did not match\n");
415
416 if(!convertFromU(sampleText3MBCS, sizeof(sampleText3MBCS)/sizeof(sampleText3MBCS[0]),
417 expected3MBCS, sizeof(expected3MBCS), "ibm-1363", offsets3MBCS, TRUE, U_ZERO_ERROR))
418 log_err("u-> ibm-1363 [UCNV_MBCS] \n");
419 if(!convertFromU(sampleText3MBCS, sizeof(sampleText3MBCS)/sizeof(sampleText3MBCS[0]),
420 expected3MBCS, sizeof(expected3MBCS), "ibm-1363", offsets3MBCS, FALSE, U_ZERO_ERROR))
421 log_err("u-> ibm-1363 [UCNV_MBCS] \n");
422
423 if(!convertFromU(sampleText4MBCS, sizeof(sampleText4MBCS)/sizeof(sampleText4MBCS[0]),
424 expected4MBCS, sizeof(expected4MBCS), "euc-jp", offsets4MBCS, TRUE, U_ZERO_ERROR))
425 log_err("u-> euc-jp [UCNV_MBCS] \n");
426 if(!convertFromU(sampleText4MBCS, sizeof(sampleText4MBCS)/sizeof(sampleText4MBCS[0]),
427 expected4MBCS, sizeof(expected4MBCS), "euc-jp", offsets4MBCS, FALSE, U_ZERO_ERROR))
428 log_err("u-> euc-jp [UCNV_MBCS] \n");
429 }
430 /*iso-2022-jp*/
431 log_verbose("Testing for iso-2022-jp\n");
432 {
433 static const UChar sampleText[] = { 0x0031, 0xd801};
434 static const uint8_t expected[] = { 0x31};
374ca955
A
435 static const uint8_t expectedSUB[] = { 0x31, 0x1a};
436 static const int32_t offsets[] = { 0x00, 1};
b75a7d8f
A
437
438 static const UChar sampleText2[] = { 0x0031, 0xd801, 0x0032};
439 static const uint8_t expected2[] = { 0x31,0x1A,0x32};
440 static const int32_t offsets2[] = { 0x00,0x01,0x02};
441
b75a7d8f
A
442 static const UChar sampleText4MBCS[] = { 0x0061, 0x4e00, 0xdc01};
443 static const uint8_t expected4MBCS[] = { 0x61, 0x1b, 0x24, 0x42, 0x30, 0x6c,0x1b,0x28,0x42,0x1a};
444 static const int32_t offsets4MBCS[] = { 0x00, 0x01, 0x01 ,0x01, 0x01, 0x01,0x02,0x02,0x02,0x02 };
445 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
374ca955 446 expectedSUB, sizeof(expectedSUB), "iso-2022-jp", offsets, TRUE, U_ZERO_ERROR))
b75a7d8f
A
447 log_err("u-> iso-2022-jp [UCNV_MBCS] \n");
448 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
449 expected, sizeof(expected), "iso-2022-jp", offsets, FALSE, U_ZERO_ERROR))
450 log_err("u-> ibm-1363 [UCNV_MBCS] \n");
451
452 if(!convertFromU(sampleText2, sizeof(sampleText2)/sizeof(sampleText2[0]),
453 expected2, sizeof(expected2), "iso-2022-jp", offsets2, TRUE, U_ZERO_ERROR))
454 log_err("u->iso-2022-jp[UCNV_DBCS] did not match\n");
455 if(!convertFromU(sampleText2, sizeof(sampleText2)/sizeof(sampleText2[0]),
456 expected2, sizeof(expected2), "iso-2022-jp", offsets2, FALSE, U_ZERO_ERROR))
457 log_err("u-> iso-2022-jp [UCNV_DBCS] did not match\n");
458 if(!convertFromU(sampleText2, sizeof(sampleText2)/sizeof(sampleText2[0]),
459 expected2, sizeof(expected2), "iso-2022-jp", offsets2, FALSE, U_ZERO_ERROR))
460 log_err("u-> iso-2022-jp [UCNV_DBCS] did not match\n");
461
b75a7d8f
A
462 if(!convertFromU(sampleText4MBCS, sizeof(sampleText4MBCS)/sizeof(sampleText4MBCS[0]),
463 expected4MBCS, sizeof(expected4MBCS), "iso-2022-jp", offsets4MBCS, TRUE, U_ZERO_ERROR))
464 log_err("u-> iso-2022-jp [UCNV_MBCS] \n");
465 if(!convertFromU(sampleText4MBCS, sizeof(sampleText4MBCS)/sizeof(sampleText4MBCS[0]),
466 expected4MBCS, sizeof(expected4MBCS), "iso-2022-jp", offsets4MBCS, FALSE, U_ZERO_ERROR))
467 log_err("u-> iso-2022-jp [UCNV_MBCS] \n");
468 }
469 /*iso-2022-cn*/
470 log_verbose("Testing for iso-2022-cn\n");
471 {
472 static const UChar sampleText[] = { 0x0031, 0xd801};
374ca955
A
473 static const uint8_t expected[] = { 0x31};
474 static const uint8_t expectedSUB[] = { 0x31, 0x1A};
475 static const int32_t offsets[] = { 0x00, 1};
b75a7d8f
A
476
477 static const UChar sampleText2[] = { 0x0031, 0xd801, 0x0032};
374ca955
A
478 static const uint8_t expected2[] = { 0x31, 0x1A,0x32};
479 static const int32_t offsets2[] = { 0x00, 0x01,0x02};
b75a7d8f
A
480
481 static const UChar sampleText3MBCS[] = { 0x0051, 0x0050, 0xdc01};
374ca955
A
482 static const uint8_t expected3MBCS[] = {0x51, 0x50, 0x1A};
483 static const int32_t offsets3MBCS[] = { 0x00, 0x01, 0x02 };
b75a7d8f
A
484
485 static const UChar sampleText4MBCS[] = { 0x0061, 0x4e00, 0xdc01};
374ca955
A
486 static const uint8_t expected4MBCS[] = { 0x61, 0x1b, 0x24, 0x29, 0x41, 0x0e, 0x52, 0x3b, 0x0f, 0x1a };
487 static const int32_t offsets4MBCS[] = { 0x00, 0x01, 0x01 ,0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02 };
b75a7d8f 488 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
374ca955 489 expectedSUB, sizeof(expectedSUB), "iso-2022-cn", offsets, TRUE, U_ZERO_ERROR))
b75a7d8f
A
490 log_err("u-> iso-2022-cn [UCNV_MBCS] \n");
491 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
492 expected, sizeof(expected), "iso-2022-cn", offsets, FALSE, U_ZERO_ERROR))
493 log_err("u-> ibm-1363 [UCNV_MBCS] \n");
494
495 if(!convertFromU(sampleText2, sizeof(sampleText2)/sizeof(sampleText2[0]),
496 expected2, sizeof(expected2), "iso-2022-cn", offsets2, TRUE, U_ZERO_ERROR))
497 log_err("u->iso-2022-cn[UCNV_DBCS] did not match\n");
498 if(!convertFromU(sampleText2, sizeof(sampleText2)/sizeof(sampleText2[0]),
499 expected2, sizeof(expected2), "iso-2022-cn", offsets2, FALSE, U_ZERO_ERROR))
500 log_err("u-> iso-2022-cn [UCNV_DBCS] did not match\n");
501 if(!convertFromU(sampleText2, sizeof(sampleText2)/sizeof(sampleText2[0]),
502 expected2, sizeof(expected2), "iso-2022-cn", offsets2, FALSE, U_ZERO_ERROR))
503 log_err("u-> iso-2022-cn [UCNV_DBCS] did not match\n");
504
505 if(!convertFromU(sampleText3MBCS, sizeof(sampleText3MBCS)/sizeof(sampleText3MBCS[0]),
506 expected3MBCS, sizeof(expected3MBCS), "iso-2022-cn", offsets3MBCS, TRUE, U_ZERO_ERROR))
507 log_err("u->iso-2022-cn [UCNV_MBCS] \n");
508 if(!convertFromU(sampleText3MBCS, sizeof(sampleText3MBCS)/sizeof(sampleText3MBCS[0]),
509 expected3MBCS, sizeof(expected3MBCS), "iso-2022-cn", offsets3MBCS, FALSE, U_ZERO_ERROR))
510 log_err("u-> iso-2022-cn[UCNV_MBCS] \n");
511
512 if(!convertFromU(sampleText4MBCS, sizeof(sampleText4MBCS)/sizeof(sampleText4MBCS[0]),
513 expected4MBCS, sizeof(expected4MBCS), "iso-2022-cn", offsets4MBCS, TRUE, U_ZERO_ERROR))
514 log_err("u-> iso-2022-cn [UCNV_MBCS] \n");
515 if(!convertFromU(sampleText4MBCS, sizeof(sampleText4MBCS)/sizeof(sampleText4MBCS[0]),
516 expected4MBCS, sizeof(expected4MBCS), "iso-2022-cn", offsets4MBCS, FALSE, U_ZERO_ERROR))
517 log_err("u-> iso-2022-cn [UCNV_MBCS] \n");
518 }
519 /*iso-2022-kr*/
520 log_verbose("Testing for iso-2022-kr\n");
521 {
522 static const UChar sampleText[] = { 0x0031, 0xd801};
523 static const uint8_t expected[] = { 0x1b, 0x24, 0x29, 0x43, 0x31};
374ca955
A
524 static const uint8_t expectedSUB[] = { 0x1b, 0x24, 0x29, 0x43, 0x31, 0x1A};
525 static const int32_t offsets[] = { -1, -1, -1, -1, 0x00, 1};
b75a7d8f
A
526
527 static const UChar sampleText2[] = { 0x0031, 0xd801, 0x0032};
528 static const uint8_t expected2[] = { 0x1b, 0x24, 0x29, 0x43, 0x31, 0x1A, 0x32};
529 static const int32_t offsets2[] = { -1, -1, -1, -1, 0x00, 0x01, 0x02};
530
531 static const UChar sampleText3MBCS[] = { 0x0051, 0x0050, 0xdc01};
532 static const uint8_t expected3MBCS[] = { 0x1b, 0x24, 0x29, 0x43, 0x51, 0x50, 0x1A };
533 static const int32_t offsets3MBCS[] = { -1, -1, -1, -1, 0x00, 0x01, 0x02, 0x02 };
534
b75a7d8f 535 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
374ca955 536 expectedSUB, sizeof(expectedSUB), "iso-2022-kr", offsets, TRUE, U_ZERO_ERROR))
b75a7d8f
A
537 log_err("u-> iso-2022-kr [UCNV_MBCS] \n");
538 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
539 expected, sizeof(expected), "iso-2022-kr", offsets, FALSE, U_ZERO_ERROR))
540 log_err("u-> ibm-1363 [UCNV_MBCS] \n");
541
542 if(!convertFromU(sampleText2, sizeof(sampleText2)/sizeof(sampleText2[0]),
543 expected2, sizeof(expected2), "iso-2022-kr", offsets2, TRUE, U_ZERO_ERROR))
544 log_err("u->iso-2022-kr[UCNV_DBCS] did not match\n");
545 if(!convertFromU(sampleText2, sizeof(sampleText2)/sizeof(sampleText2[0]),
546 expected2, sizeof(expected2), "iso-2022-kr", offsets2, FALSE, U_ZERO_ERROR))
547 log_err("u-> iso-2022-kr [UCNV_DBCS] did not match\n");
548 if(!convertFromU(sampleText2, sizeof(sampleText2)/sizeof(sampleText2[0]),
549 expected2, sizeof(expected2), "iso-2022-kr", offsets2, FALSE, U_ZERO_ERROR))
550 log_err("u-> iso-2022-kr [UCNV_DBCS] did not match\n");
551
552 if(!convertFromU(sampleText3MBCS, sizeof(sampleText3MBCS)/sizeof(sampleText3MBCS[0]),
553 expected3MBCS, sizeof(expected3MBCS), "iso-2022-kr", offsets3MBCS, TRUE, U_ZERO_ERROR))
554 log_err("u->iso-2022-kr [UCNV_MBCS] \n");
555 if(!convertFromU(sampleText3MBCS, sizeof(sampleText3MBCS)/sizeof(sampleText3MBCS[0]),
556 expected3MBCS, sizeof(expected3MBCS), "iso-2022-kr", offsets3MBCS, FALSE, U_ZERO_ERROR))
557 log_err("u-> iso-2022-kr[UCNV_MBCS] \n");
b75a7d8f
A
558 }
559
560 /*HZ*/
561 log_verbose("Testing for HZ\n");
562 {
563 static const UChar sampleText[] = { 0x0031, 0xd801};
564 static const uint8_t expected[] = { 0x7e, 0x7d, 0x31};
374ca955
A
565 static const uint8_t expectedSUB[] = { 0x7e, 0x7d, 0x31, 0x1A};
566 static const int32_t offsets[] = { 0x00, 0x00, 0x00, 1};
b75a7d8f
A
567
568 static const UChar sampleText2[] = { 0x0031, 0xd801, 0x0032};
569 static const uint8_t expected2[] = { 0x7e, 0x7d, 0x31, 0x1A, 0x32 };
570 static const int32_t offsets2[] = { 0x00, 0x00, 0x00, 0x01, 0x02 };
571
572 static const UChar sampleText3MBCS[] = { 0x0051, 0x0050, 0xdc01};
573 static const uint8_t expected3MBCS[] = { 0x7e, 0x7d, 0x51, 0x50, 0x1A };
574 static const int32_t offsets3MBCS[] = { 0x00, 0x00, 0x00, 0x01, 0x02};
575
576 static const UChar sampleText4MBCS[] = { 0x0061, 0x4e00, 0xdc01};
577 static const uint8_t expected4MBCS[] = { 0x7e, 0x7d, 0x61, 0x7e, 0x7b, 0x52, 0x3b, 0x7e, 0x7d, 0x1a };
578 static const int32_t offsets4MBCS[] = { 0x00, 0x00, 0x00, 0x01, 0x01, 0x01 ,0x01, 0x02, 0x02, 0x02 };
579 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
374ca955 580 expectedSUB, sizeof(expectedSUB), "HZ", offsets, TRUE, U_ZERO_ERROR))
b75a7d8f
A
581 log_err("u-> HZ [UCNV_MBCS] \n");
582 if(!convertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
583 expected, sizeof(expected), "HZ", offsets, FALSE, U_ZERO_ERROR))
584 log_err("u-> ibm-1363 [UCNV_MBCS] \n");
585
586 if(!convertFromU(sampleText2, sizeof(sampleText2)/sizeof(sampleText2[0]),
587 expected2, sizeof(expected2), "HZ", offsets2, TRUE, U_ZERO_ERROR))
588 log_err("u->HZ[UCNV_DBCS] did not match\n");
589 if(!convertFromU(sampleText2, sizeof(sampleText2)/sizeof(sampleText2[0]),
590 expected2, sizeof(expected2), "HZ", offsets2, FALSE, U_ZERO_ERROR))
591 log_err("u-> HZ [UCNV_DBCS] did not match\n");
592 if(!convertFromU(sampleText2, sizeof(sampleText2)/sizeof(sampleText2[0]),
593 expected2, sizeof(expected2), "HZ", offsets2, FALSE, U_ZERO_ERROR))
594 log_err("u-> HZ [UCNV_DBCS] did not match\n");
595
596 if(!convertFromU(sampleText3MBCS, sizeof(sampleText3MBCS)/sizeof(sampleText3MBCS[0]),
597 expected3MBCS, sizeof(expected3MBCS), "HZ", offsets3MBCS, TRUE, U_ZERO_ERROR))
598 log_err("u->HZ [UCNV_MBCS] \n");
599 if(!convertFromU(sampleText3MBCS, sizeof(sampleText3MBCS)/sizeof(sampleText3MBCS[0]),
600 expected3MBCS, sizeof(expected3MBCS), "HZ", offsets3MBCS, FALSE, U_ZERO_ERROR))
601 log_err("u-> HZ[UCNV_MBCS] \n");
602
603 if(!convertFromU(sampleText4MBCS, sizeof(sampleText4MBCS)/sizeof(sampleText4MBCS[0]),
604 expected4MBCS, sizeof(expected4MBCS), "HZ", offsets4MBCS, TRUE, U_ZERO_ERROR))
605 log_err("u-> HZ [UCNV_MBCS] \n");
606 if(!convertFromU(sampleText4MBCS, sizeof(sampleText4MBCS)/sizeof(sampleText4MBCS[0]),
607 expected4MBCS, sizeof(expected4MBCS), "HZ", offsets4MBCS, FALSE, U_ZERO_ERROR))
608 log_err("u-> HZ [UCNV_MBCS] \n");
609 }
610
611
612}
613
614/*test different convertToUnicode error behaviours*/
615static void TestToUnicodeErrorBehaviour()
616{
617 log_verbose("Testing error conditions for DBCS\n");
618 {
619 uint8_t sampleText[] = { 0xa2, 0xae, 0x03, 0x04};
620 const UChar expected[] = { 0x00a1 };
621
b75a7d8f
A
622 if(!convertToU(sampleText, sizeof(sampleText),
623 expected, sizeof(expected)/sizeof(expected[0]), "ibm-1363", 0, TRUE, U_ZERO_ERROR ))
624 log_err("DBCS (ibm-1363)->Unicode did not match.\n");
625 if(!convertToU(sampleText, sizeof(sampleText),
626 expected, sizeof(expected)/sizeof(expected[0]), "ibm-1363", 0, FALSE, U_ZERO_ERROR ))
627 log_err("DBCS (ibm-1363)->Unicode with flush = false did not match.\n");
b75a7d8f
A
628 }
629 log_verbose("Testing error conditions for SBCS\n");
630 {
631 uint8_t sampleText[] = { 0xa2, 0xFF};
632 const UChar expected[] = { 0x00c2 };
633
634 /* uint8_t sampleText2[] = { 0xa2, 0x70 };
635 const UChar expected2[] = { 0x0073 };*/
636
637 if(!convertToU(sampleText, sizeof(sampleText),
638 expected, sizeof(expected)/sizeof(expected[0]), "ibm-1051", 0, TRUE, U_ZERO_ERROR ))
639 log_err("SBCS (ibm-1051)->Unicode did not match.\n");
640 if(!convertToU(sampleText, sizeof(sampleText),
641 expected, sizeof(expected)/sizeof(expected[0]), "ibm-1051", 0, FALSE, U_ZERO_ERROR ))
642 log_err("SBCS (ibm-1051)->Unicode with flush = false did not match.\n");
643
644 }
b75a7d8f
A
645}
646
647static void TestGetNextErrorBehaviour(){
648 /*Test for unassigned character*/
649#define INPUT_SIZE 1
650 static const char input1[INPUT_SIZE]={ 0x70 };
651 const char* source=(const char*)input1;
652 UErrorCode err=U_ZERO_ERROR;
653 UChar32 c=0;
654 UConverter *cnv=ucnv_open("ibm-424", &err);
655 if(U_FAILURE(err)) {
656 log_data_err("Unable to open a SBCS(ibm-424) converter: %s\n", u_errorName(err));
657 return;
658 }
659 c=ucnv_getNextUChar(cnv, &source, source + INPUT_SIZE, &err);
660 if(err != U_INVALID_CHAR_FOUND && c!=0xfffd){
661 log_err("FAIL in TestGetNextErrorBehaviour(unassigned): Expected: U_INVALID_CHAR_ERROR or 0xfffd ----Got:%s and 0x%lx\n", myErrorName(err), c);
662 }
663 ucnv_close(cnv);
664}
665
666#define MAX_UTF16_LEN 2
667#define MAX_UTF8_LEN 4
668
669/*Regression test for utf8 converter*/
670static void TestRegressionUTF8(){
671 UChar32 currCh = 0;
672 int32_t offset8;
673 int32_t offset16;
674 UChar *standardForm = (UChar*)malloc(MAX_LENGTH*sizeof(UChar));
675 uint8_t *utf8 = (uint8_t*)malloc(MAX_LENGTH);
676
677 while (currCh <= UNICODE_LIMIT) {
678 offset16 = 0;
679 offset8 = 0;
680 while(currCh <= UNICODE_LIMIT
681 && offset16 < (MAX_LENGTH/sizeof(UChar) - MAX_UTF16_LEN)
682 && offset8 < (MAX_LENGTH - MAX_UTF8_LEN))
683 {
684 if (currCh == SURROGATE_HIGH_START) {
685 currCh = SURROGATE_LOW_END + 1; /* Skip surrogate range */
686 }
687 UTF16_APPEND_CHAR_SAFE(standardForm, offset16, MAX_LENGTH, currCh);
688 UTF8_APPEND_CHAR_SAFE(utf8, offset8, MAX_LENGTH, currCh);
689 currCh++;
690 }
691 if(!convertFromU(standardForm, offset16,
692 utf8, offset8, "UTF8", 0, TRUE, U_ZERO_ERROR )) {
693 log_err("Unicode->UTF8 did not match.\n");
694 }
695 if(!convertToU(utf8, offset8,
696 standardForm, offset16, "UTF8", 0, TRUE, U_ZERO_ERROR )) {
697 log_err("UTF8->Unicode did not match.\n");
698 }
699 }
700 free(standardForm);
701 free(utf8);
702}
703
704#define MAX_UTF32_LEN 1
705
706static void TestRegressionUTF32(){
707 UChar32 currCh = 0;
708 int32_t offset32;
709 int32_t offset16;
710 UChar *standardForm = (UChar*)malloc(MAX_LENGTH*sizeof(UChar));
711 UChar32 *utf32 = (UChar32*)malloc(MAX_LENGTH*sizeof(UChar32));
712
713 while (currCh <= UNICODE_LIMIT) {
714 offset16 = 0;
715 offset32 = 0;
716 while(currCh <= UNICODE_LIMIT
717 && offset16 < (MAX_LENGTH/sizeof(UChar) - MAX_UTF16_LEN)
718 && offset32 < (MAX_LENGTH/sizeof(UChar32) - MAX_UTF32_LEN))
719 {
720 if (currCh == SURROGATE_HIGH_START) {
721 currCh = SURROGATE_LOW_END + 1; /* Skip surrogate range */
722 }
723 UTF16_APPEND_CHAR_SAFE(standardForm, offset16, MAX_LENGTH, currCh);
724 UTF32_APPEND_CHAR_SAFE(utf32, offset32, MAX_LENGTH, currCh);
725 currCh++;
726 }
727 if(!convertFromU(standardForm, offset16,
728 (const uint8_t *)utf32, offset32*sizeof(UChar32), "UTF32_PlatformEndian", 0, TRUE, U_ZERO_ERROR )) {
729 log_err("Unicode->UTF32 did not match.\n");
730 }
731 if(!convertToU((const uint8_t *)utf32, offset32*sizeof(UChar32),
732 standardForm, offset16, "UTF32_PlatformEndian", 0, TRUE, U_ZERO_ERROR )) {
733 log_err("UTF32->Unicode did not match.\n");
734 }
735 }
736 free(standardForm);
737 free(utf32);
738}
739
740/*Walk through the available converters*/
741static void TestAvailableConverters(){
742 UErrorCode status=U_ZERO_ERROR;
743 UConverter *conv=NULL;
744 int32_t i=0;
745 for(i=0; i < ucnv_countAvailable(); i++){
746 status=U_ZERO_ERROR;
747 conv=ucnv_open(ucnv_getAvailableName(i), &status);
748 if(U_FAILURE(status)){
749 log_err("ERROR: converter creation failed. Failure in alias table or the data table for \n converter=%s. Error=%s\n",
750 ucnv_getAvailableName(i), myErrorName(status));
751 continue;
752 }
753 ucnv_close(conv);
754 }
755
756}
757
758static void TestFlushInternalBuffer(){
759 TestWithBufferSize(MAX_LENGTH, 1);
760 TestWithBufferSize(1, 1);
761 TestWithBufferSize(1, MAX_LENGTH);
762 TestWithBufferSize(MAX_LENGTH, MAX_LENGTH);
763}
764
765static void TestWithBufferSize(int32_t insize, int32_t outsize){
766
767 gInBufferSize =insize;
768 gOutBufferSize = outsize;
769
770 log_verbose("Testing fromUnicode for UTF-8 with UCNV_TO_U_CALLBACK_SUBSTITUTE \n");
771 {
772 UChar sampleText[] =
773 { 0x0031, 0x0032, 0x0033, 0x0000, 0x4e00, 0x4e8c, 0x4e09, 0x002E };
774 const uint8_t expectedUTF8[] =
775 { 0x31, 0x32, 0x33, 0x00, 0xe4, 0xb8, 0x80, 0xe4, 0xba, 0x8c, 0xe4, 0xb8, 0x89, 0x2E };
776 int32_t toUTF8Offs[] =
777 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x06, 0x06, 0x06, 0x07};
778 /* int32_t fmUTF8Offs[] =
779 { 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0007, 0x000a, 0x000d };*/
780
781 /*UTF-8*/
782 if(!testConvertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
783 expectedUTF8, sizeof(expectedUTF8), "UTF8", UCNV_FROM_U_CALLBACK_SUBSTITUTE, toUTF8Offs ,FALSE))
784 log_err("u-> UTF8 did not match.\n");
785 }
786
787 log_verbose("Testing fromUnicode with UCNV_FROM_U_CALLBACK_ESCAPE \n");
788 {
789 UChar inputTest[] = { 0x0061, 0xd801, 0xdc01, 0xd801, 0x0061 };
790 const uint8_t toIBM943[]= { 0x61,
791 0x25, 0x55, 0x44, 0x38, 0x30, 0x31,
792 0x25, 0x55, 0x44, 0x43, 0x30, 0x31,
793 0x25, 0x55, 0x44, 0x38, 0x30, 0x31,
794 0x61 };
795 int32_t offset[]= {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 4};
796
797 if(!testConvertFromU(inputTest, sizeof(inputTest)/sizeof(inputTest[0]),
798 toIBM943, sizeof(toIBM943), "ibm-943",
799 (UConverterFromUCallback)UCNV_FROM_U_CALLBACK_ESCAPE, offset,FALSE))
800 log_err("u-> ibm-943 with subst with value did not match.\n");
801 }
802
803 log_verbose("Testing fromUnicode for UTF-8 with UCNV_TO_U_CALLBACK_SUBSTITUTE \n");
804 {
805 const uint8_t sampleText1[] = { 0x31, 0xe4, 0xba, 0x8c,
806 0xe0, 0x80, 0x61};
807 UChar expected1[] = { 0x0031, 0x4e8c, 0xfffd, 0x0061};
808 int32_t offsets1[] = { 0x0000, 0x0001, 0x0004, 0x0006};
809
810 if(!testConvertToU(sampleText1, sizeof(sampleText1),
811 expected1, sizeof(expected1)/sizeof(expected1[0]),"utf8", UCNV_TO_U_CALLBACK_SUBSTITUTE, offsets1,FALSE))
812 log_err("utf8->u with substitute did not match.\n");;
813 }
814
815
816 log_verbose("Testing toUnicode with UCNV_TO_U_CALLBACK_ESCAPE \n");
817 /*to Unicode*/
818 {
819 const uint8_t sampleTxtToU[]= { 0x00, 0x9f, 0xaf,
820 0x81, 0xad, /*unassigned*/
821 0x89, 0xd3 };
822 UChar IBM_943toUnicode[] = { 0x0000, 0x6D63,
823 0x25, 0x58, 0x38, 0x31, 0x25, 0x58, 0x41, 0x44,
824 0x7B87};
825 int32_t fromIBM943Offs [] = { 0, 1, 3, 3, 3, 3, 3, 3, 3, 3, 5};
826
827 if(!testConvertToU(sampleTxtToU, sizeof(sampleTxtToU),
828 IBM_943toUnicode, sizeof(IBM_943toUnicode)/sizeof(IBM_943toUnicode[0]),"ibm-943",
829 (UConverterToUCallback)UCNV_TO_U_CALLBACK_ESCAPE, fromIBM943Offs,FALSE))
830 log_err("ibm-943->u with substitute with value did not match.\n");
831
832 }
833
834}
835
836static UBool convertFromU( const UChar *source, int sourceLen, const uint8_t *expect, int expectLen,
837 const char *codepage, const int32_t *expectOffsets, UBool doFlush, UErrorCode expectedStatus)
838{
839
840 int32_t i=0;
841 uint8_t *p=0;
842 const UChar *src;
843 uint8_t buffer[MAX_LENGTH];
844 int32_t offsetBuffer[MAX_LENGTH];
845 int32_t *offs=0;
846 uint8_t *targ;
847 uint8_t *targetLimit;
848 UChar *sourceLimit=0;
849 UErrorCode status = U_ZERO_ERROR;
850 UConverter *conv = 0;
851 conv = ucnv_open(codepage, &status);
852 if(U_FAILURE(status))
853 {
854 log_data_err("Couldn't open converter %s\n",codepage);
855 return TRUE;
856 }
857 log_verbose("Converter %s opened..\n", ucnv_getName(conv, &status));
858
859 for(i=0; i<MAX_LENGTH; i++){
860 buffer[i]=0xF0;
861 offsetBuffer[i]=0xFF;
862 }
863
864 src=source;
865 sourceLimit=(UChar*)src+(sourceLen);
866 targ=buffer;
867 targetLimit=targ+MAX_LENGTH;
868 offs=offsetBuffer;
869 ucnv_fromUnicode (conv,
870 (char **)&targ,
871 (const char *)targetLimit,
872 &src,
873 sourceLimit,
874 expectOffsets ? offs : NULL,
875 doFlush,
876 &status);
877 ucnv_close(conv);
878 if(status != expectedStatus){
879 log_err("ucnv_fromUnicode() failed for codepage=%s. Error =%s Expected=%s\n", codepage, myErrorName(status), myErrorName(expectedStatus));
880 return FALSE;
881 }
882
883 log_verbose("\nConversion done [%d uchars in -> %d chars out]. \nResult :",
884 sourceLen, targ-buffer);
885
886 if(expectLen != targ-buffer)
887 {
888 log_err("Expected %d chars out, got %d FROM Unicode to %s\n", expectLen, targ-buffer, codepage);
889 log_verbose("Expected %d chars out, got %d FROM Unicode to %s\n", expectLen, targ-buffer, codepage);
890 printSeqErr((const unsigned char *)buffer, targ-buffer);
891 printSeqErr((const unsigned char*)expect, expectLen);
892 return FALSE;
893 }
894
895 if(memcmp(buffer, expect, expectLen)){
896 log_err("String does not match. FROM Unicode to codePage%s\n", codepage);
374ca955 897 log_info("\nGot:");
b75a7d8f 898 printSeqErr((const unsigned char *)buffer, expectLen);
374ca955 899 log_info("\nExpected:");
b75a7d8f
A
900 printSeqErr((const unsigned char *)expect, expectLen);
901 return FALSE;
902 }
903 else {
904 log_verbose("Matches!\n");
905 }
906
907 if (expectOffsets != 0){
908 log_verbose("comparing %d offsets..\n", targ-buffer);
909 if(memcmp(offsetBuffer,expectOffsets,(targ-buffer) * sizeof(int32_t) )){
910 log_err("did not get the expected offsets. for FROM Unicode to %s\n", codepage);
374ca955 911 log_info("\nGot : ");
b75a7d8f
A
912 printSeqErr((const unsigned char*)buffer, targ-buffer);
913 for(p=buffer;p<targ;p++)
374ca955
A
914 log_info("%d, ", offsetBuffer[p-buffer]);
915 log_info("\nExpected: ");
b75a7d8f 916 for(i=0; i< (targ-buffer); i++)
374ca955 917 log_info("%d,", expectOffsets[i]);
b75a7d8f
A
918 }
919 }
920
921 return TRUE;
922}
923
924
925static UBool convertToU( const uint8_t *source, int sourceLen, const UChar *expect, int expectLen,
926 const char *codepage, const int32_t *expectOffsets, UBool doFlush, UErrorCode expectedStatus)
927{
928 UErrorCode status = U_ZERO_ERROR;
929 UConverter *conv = 0;
930 int32_t i=0;
931 UChar *p=0;
932 const uint8_t* src;
933 UChar buffer[MAX_LENGTH];
934 int32_t offsetBuffer[MAX_LENGTH];
935 int32_t *offs=0;
936 UChar *targ;
937 UChar *targetLimit;
938 uint8_t *sourceLimit=0;
939
940
941
942 conv = ucnv_open(codepage, &status);
943 if(U_FAILURE(status))
944 {
945 log_data_err("Couldn't open converter %s\n",codepage);
946 return TRUE;
947 }
948 log_verbose("Converter %s opened..\n", ucnv_getName(conv, &status));
949
950
951
952 for(i=0; i<MAX_LENGTH; i++){
953 buffer[i]=0xFFFE;
954 offsetBuffer[i]=-1;
955 }
956
957 src=source;
958 sourceLimit=(uint8_t*)(src+(sourceLen));
959 targ=buffer;
960 targetLimit=targ+MAX_LENGTH;
961 offs=offsetBuffer;
962
963
964
965 ucnv_toUnicode (conv,
966 &targ,
967 targetLimit,
968 (const char **)&src,
969 (const char *)sourceLimit,
970 expectOffsets ? offs : NULL,
971 doFlush,
972 &status);
973
974 ucnv_close(conv);
975 if(status != expectedStatus){
976 log_err("ucnv_fromUnicode() failed for codepage=%s. Error =%s Expected=%s\n", codepage, myErrorName(status), myErrorName(expectedStatus));
977 return FALSE;
978 }
979 log_verbose("\nConversion done [%d uchars in -> %d chars out]. \nResult :",
980 sourceLen, targ-buffer);
981
982
983
984
985 log_verbose("comparing %d uchars (%d bytes)..\n",expectLen,expectLen*2);
986
987 if (expectOffsets != 0) {
988 if(memcmp(offsetBuffer, expectOffsets, (targ-buffer) * sizeof(int32_t))){
989
990 log_err("did not get the expected offsets from %s To UNICODE\n", codepage);
374ca955 991 log_info("\nGot : ");
b75a7d8f 992 for(p=buffer;p<targ;p++)
374ca955
A
993 log_info("%d, ", offsetBuffer[p-buffer]);
994 log_info("\nExpected: ");
b75a7d8f 995 for(i=0; i<(targ-buffer); i++)
374ca955
A
996 log_info("%d, ", expectOffsets[i]);
997 log_info("\nGot result:");
b75a7d8f 998 for(i=0; i<(targ-buffer); i++)
374ca955
A
999 log_info("0x%04X,", buffer[i]);
1000 log_info("\nFrom Input:");
b75a7d8f 1001 for(i=0; i<(src-source); i++)
374ca955
A
1002 log_info("0x%02X,", (unsigned char)source[i]);
1003 log_info("\n");
b75a7d8f
A
1004 }
1005 }
1006 if(memcmp(buffer, expect, expectLen*2)){
1007 log_err("String does not match. from codePage %s TO Unicode\n", codepage);
374ca955 1008 log_info("\nGot:");
b75a7d8f 1009 printUSeqErr(buffer, expectLen);
374ca955 1010 log_info("\nExpected:");
b75a7d8f
A
1011 printUSeqErr(expect, expectLen);
1012 return FALSE;
1013 }
1014 else {
1015 log_verbose("Matches!\n");
1016 }
1017
1018 return TRUE;
1019}
1020
1021
1022static UBool testConvertFromU( const UChar *source, int sourceLen, const uint8_t *expect, int expectLen,
1023 const char *codepage, UConverterFromUCallback callback , const int32_t *expectOffsets, UBool testReset)
1024{
1025 UErrorCode status = U_ZERO_ERROR;
1026 UConverter *conv = 0;
1027 uint8_t junkout[MAX_LENGTH]; /* FIX */
1028 int32_t junokout[MAX_LENGTH]; /* FIX */
1029 uint8_t *p;
1030 const UChar *src;
1031 uint8_t *end;
1032 uint8_t *targ;
1033 int32_t *offs;
1034 int i;
1035 int32_t realBufferSize;
1036 uint8_t *realBufferEnd;
1037 const UChar *realSourceEnd;
1038 const UChar *sourceLimit;
1039 UBool checkOffsets = TRUE;
1040 UBool doFlush;
1041
1042 UConverterFromUCallback oldAction = NULL;
1043 const void* oldContext = NULL;
1044
1045 for(i=0;i<MAX_LENGTH;i++)
1046 junkout[i] = 0xF0;
1047 for(i=0;i<MAX_LENGTH;i++)
1048 junokout[i] = 0xFF;
1049
1050 setNuConvTestName(codepage, "FROM");
1051
1052 log_verbose("\n========= %s\n", gNuConvTestName);
1053
1054 conv = ucnv_open(codepage, &status);
1055 if(U_FAILURE(status))
1056 {
1057 log_data_err("Couldn't open converter %s\n",codepage);
1058 return TRUE;
1059 }
1060
1061 log_verbose("Converter opened..\n");
1062 /*----setting the callback routine----*/
1063 ucnv_setFromUCallBack (conv, callback, NULL, &oldAction, &oldContext, &status);
1064 if (U_FAILURE(status)) {
1065 log_err("FAILURE in setting the callback Function! %s\n", myErrorName(status));
1066 }
1067 /*------------------------*/
1068
1069 src = source;
1070 targ = junkout;
1071 offs = junokout;
1072
1073 realBufferSize = (sizeof(junkout)/sizeof(junkout[0]));
1074 realBufferEnd = junkout + realBufferSize;
1075 realSourceEnd = source + sourceLen;
1076
1077 if ( gOutBufferSize != realBufferSize )
1078 checkOffsets = FALSE;
1079
1080 if( gInBufferSize != MAX_LENGTH )
1081 checkOffsets = FALSE;
1082
1083 do
1084 {
1085 end = nct_min(targ + gOutBufferSize, realBufferEnd);
1086 sourceLimit = nct_min(src + gInBufferSize, realSourceEnd);
1087
1088 doFlush = (UBool)(sourceLimit == realSourceEnd);
1089
1090 if(targ == realBufferEnd)
1091 {
1092 log_err("Error, overflowed the real buffer while about to call fromUnicode! targ=%08lx %s", targ, gNuConvTestName);
1093 return FALSE;
1094 }
1095 log_verbose("calling fromUnicode @ SOURCE:%08lx to %08lx TARGET: %08lx to %08lx, flush=%s\n", src,sourceLimit, targ,end, doFlush?"TRUE":"FALSE");
1096
1097
1098 status = U_ZERO_ERROR;
1099 if(gInBufferSize ==999 && gOutBufferSize==999)
1100 doFlush = FALSE;
1101 ucnv_fromUnicode (conv,
1102 (char **)&targ,
1103 (const char *)end,
1104 &src,
1105 sourceLimit,
1106 offs,
1107 doFlush, /* flush if we're at the end of the input data */
1108 &status);
1109 if(testReset)
1110 ucnv_resetToUnicode(conv);
1111 if(gInBufferSize ==999 && gOutBufferSize==999)
1112 ucnv_resetToUnicode(conv);
1113
1114 } while ( (status == U_BUFFER_OVERFLOW_ERROR) || (U_SUCCESS(status) && sourceLimit < realSourceEnd) );
1115
1116 if(U_FAILURE(status)) {
1117 log_err("Problem doing fromUnicode to %s, errcode %s %s\n", codepage, myErrorName(status), gNuConvTestName);
1118 return FALSE;
1119 }
1120
1121 log_verbose("\nConversion done [%d uchars in -> %d chars out]. \nResult :",
1122 sourceLen, targ-junkout);
1123 if(VERBOSITY)
1124 {
1125 char junk[999];
1126 char offset_str[999];
1127 uint8_t *ptr;
1128
1129 junk[0] = 0;
1130 offset_str[0] = 0;
1131 for(ptr = junkout;ptr<targ;ptr++)
1132 {
1133 sprintf(junk + strlen(junk), "0x%02x, ", (0xFF) & (unsigned int)*ptr);
1134 sprintf(offset_str + strlen(offset_str), "0x%02x, ", (0xFF) & (unsigned int)junokout[ptr-junkout]);
1135 }
1136
1137 log_verbose(junk);
1138 printSeq((const unsigned char *)expect, expectLen);
1139 if ( checkOffsets )
1140 {
1141 log_verbose("\nOffsets:");
1142 log_verbose(offset_str);
1143 }
1144 log_verbose("\n");
1145 }
1146 ucnv_close(conv);
1147
1148
1149 if(expectLen != targ-junkout)
1150 {
1151 log_err("Expected %d chars out, got %d %s\n", expectLen, targ-junkout, gNuConvTestName);
1152 log_verbose("Expected %d chars out, got %d %s\n", expectLen, targ-junkout, gNuConvTestName);
374ca955 1153 log_info("\nGot:");
b75a7d8f 1154 printSeqErr((const unsigned char*)junkout, targ-junkout);
374ca955 1155 log_info("\nExpected:");
b75a7d8f
A
1156 printSeqErr((const unsigned char*)expect, expectLen);
1157 return FALSE;
1158 }
1159
1160 if (checkOffsets && (expectOffsets != 0) )
1161 {
1162 log_verbose("comparing %d offsets..\n", targ-junkout);
1163 if(memcmp(junokout,expectOffsets,(targ-junkout) * sizeof(int32_t) )){
1164 log_err("did not get the expected offsets. %s", gNuConvTestName);
1165 log_err("Got : ");
1166 printSeqErr((const unsigned char*)junkout, targ-junkout);
1167 for(p=junkout;p<targ;p++)
1168 log_err("%d, ", junokout[p-junkout]);
1169 log_err("\nExpected: ");
1170 for(i=0; i<(targ-junkout); i++)
1171 log_err("%d,", expectOffsets[i]);
1172 }
1173 }
1174
1175 log_verbose("comparing..\n");
1176 if(!memcmp(junkout, expect, expectLen))
1177 {
1178 log_verbose("Matches!\n");
1179 return TRUE;
1180 }
1181 else
1182 {
1183 log_err("String does not match. %s\n", gNuConvTestName);
1184 printUSeqErr(source, sourceLen);
374ca955 1185 log_info("\nGot:");
b75a7d8f 1186 printSeqErr((const unsigned char *)junkout, expectLen);
374ca955 1187 log_info("\nExpected:");
b75a7d8f
A
1188 printSeqErr((const unsigned char *)expect, expectLen);
1189
1190 return FALSE;
1191 }
1192}
1193
1194static UBool testConvertToU( const uint8_t *source, int sourcelen, const UChar *expect, int expectlen,
1195 const char *codepage, UConverterToUCallback callback, const int32_t *expectOffsets, UBool testReset)
1196{
1197 UErrorCode status = U_ZERO_ERROR;
1198 UConverter *conv = 0;
1199 UChar junkout[MAX_LENGTH]; /* FIX */
1200 int32_t junokout[MAX_LENGTH]; /* FIX */
1201 const uint8_t *src;
1202 const uint8_t *realSourceEnd;
1203 const uint8_t *srcLimit;
1204 UChar *p;
1205 UChar *targ;
1206 UChar *end;
1207 int32_t *offs;
1208 int i;
1209 UBool checkOffsets = TRUE;
1210 int32_t realBufferSize;
1211 UChar *realBufferEnd;
1212 UBool doFlush;
1213
1214 UConverterToUCallback oldAction = NULL;
1215 const void* oldContext = NULL;
1216
1217
1218 for(i=0;i<MAX_LENGTH;i++)
1219 junkout[i] = 0xFFFE;
1220
1221 for(i=0;i<MAX_LENGTH;i++)
1222 junokout[i] = -1;
1223
1224 setNuConvTestName(codepage, "TO");
1225
1226 log_verbose("\n========= %s\n", gNuConvTestName);
1227
1228 conv = ucnv_open(codepage, &status);
1229 if(U_FAILURE(status))
1230 {
1231 log_data_err("Couldn't open converter %s\n",gNuConvTestName);
1232 return TRUE;
1233 }
1234
1235 log_verbose("Converter opened..\n");
1236 /*----setting the callback routine----*/
1237 ucnv_setToUCallBack (conv, callback, NULL, &oldAction, &oldContext, &status);
1238 if (U_FAILURE(status)) {
1239 log_err("FAILURE in setting the callback Function! %s\n", myErrorName(status));
1240 }
1241 /*-------------------------------------*/
1242
1243 src = source;
1244 targ = junkout;
1245 offs = junokout;
1246
1247 realBufferSize = (sizeof(junkout)/sizeof(junkout[0]));
1248 realBufferEnd = junkout + realBufferSize;
1249 realSourceEnd = src + sourcelen;
1250
1251 if ( gOutBufferSize != realBufferSize )
1252 checkOffsets = FALSE;
1253
1254 if( gInBufferSize != MAX_LENGTH )
1255 checkOffsets = FALSE;
1256
1257 do
1258 {
1259 end = nct_min( targ + gOutBufferSize, realBufferEnd);
1260 srcLimit = nct_min(realSourceEnd, src + gInBufferSize);
1261
1262 if(targ == realBufferEnd)
1263 {
1264 log_err("Error, the end would overflow the real output buffer while about to call toUnicode! tarjey=%08lx %s",targ,gNuConvTestName);
1265 return FALSE;
1266 }
1267 log_verbose("calling toUnicode @ %08lx to %08lx\n", targ,end);
1268
1269 /* oldTarg = targ; */
1270
1271 status = U_ZERO_ERROR;
1272 doFlush=(UBool)((gInBufferSize ==999 && gOutBufferSize==999)?(srcLimit == realSourceEnd) : FALSE);
1273
1274 ucnv_toUnicode (conv,
1275 &targ,
1276 end,
1277 (const char **)&src,
1278 (const char *)srcLimit,
1279 offs,
1280 doFlush, /* flush if we're at the end of hte source data */
1281 &status);
1282 if(testReset)
1283 ucnv_resetFromUnicode(conv);
1284 if(gInBufferSize ==999 && gOutBufferSize==999)
1285 ucnv_resetToUnicode(conv);
1286 /* offs += (targ-oldTarg); */
1287
1288 } while ( (status == U_BUFFER_OVERFLOW_ERROR) || (U_SUCCESS(status) && (srcLimit < realSourceEnd)) ); /* while we just need another buffer */
1289
1290 if(U_FAILURE(status))
1291 {
1292 log_err("Problem doing %s toUnicode, errcode %s %s\n", codepage, myErrorName(status), gNuConvTestName);
1293 return FALSE;
1294 }
1295
1296 log_verbose("\nConversion done. %d bytes -> %d chars.\nResult :",
1297 sourcelen, targ-junkout);
1298 if(VERBOSITY)
1299 {
1300 char junk[999];
1301 char offset_str[999];
1302
1303 UChar *ptr;
1304
1305 junk[0] = 0;
1306 offset_str[0] = 0;
1307
1308 for(ptr = junkout;ptr<targ;ptr++)
1309 {
1310 sprintf(junk + strlen(junk), "0x%04x, ", (0xFFFF) & (unsigned int)*ptr);
1311 sprintf(offset_str + strlen(offset_str), "0x%04x, ", (0xFFFF) & (unsigned int)junokout[ptr-junkout]);
1312 }
1313
1314 log_verbose(junk);
1315
1316 if ( checkOffsets )
1317 {
1318 log_verbose("\nOffsets:");
1319 log_verbose(offset_str);
1320 }
1321 log_verbose("\n");
1322 }
1323 ucnv_close(conv);
1324
1325 log_verbose("comparing %d uchars (%d bytes)..\n",expectlen,expectlen*2);
1326
1327 if (checkOffsets && (expectOffsets != 0))
1328 {
1329 if(memcmp(junokout,expectOffsets,(targ-junkout) * sizeof(int32_t))){
1330
1331 log_err("did not get the expected offsets. %s",gNuConvTestName);
1332 for(p=junkout;p<targ;p++)
1333 log_err("%d, ", junokout[p-junkout]);
1334 log_err("\nExpected: ");
1335 for(i=0; i<(targ-junkout); i++)
1336 log_err("%d,", expectOffsets[i]);
1337 log_err("");
1338 for(i=0; i<(targ-junkout); i++)
1339 log_err("%X,", junkout[i]);
1340 log_err("");
1341 for(i=0; i<(src-source); i++)
1342 log_err("%X,", (unsigned char)source[i]);
1343 }
1344 }
1345
1346 if(!memcmp(junkout, expect, expectlen*2))
1347 {
1348 log_verbose("Matches!\n");
1349 return TRUE;
1350 }
1351 else
1352 {
1353 log_err("String does not match. %s\n", gNuConvTestName);
1354 log_verbose("String does not match. %s\n", gNuConvTestName);
374ca955 1355 log_info("\nGot:");
b75a7d8f 1356 printUSeq(junkout, expectlen);
374ca955 1357 log_info("\nExpected:");
b75a7d8f
A
1358 printUSeq(expect, expectlen);
1359 return FALSE;
1360 }
1361}
1362
1363
1364static void TestResetBehaviour(void){
b75a7d8f
A
1365 log_verbose("Testing Reset for DBCS and MBCS\n");
1366 {
1367 static const UChar sampleText[] = {0x00a1, 0xd801, 0xdc01, 0x00a4};
1368 static const uint8_t expected[] = {0xa2, 0xae, 0xa1, 0xe0, 0xa2, 0xb4};
1369 static const int32_t offsets[] = {0x00, 0x00, 0x01, 0x01, 0x03, 0x03 };
1370
1371
1372 static const UChar sampleText1[] = {0x00a1, 0x00a4, 0x00a7, 0x00a8};
1373 static const uint8_t expected1[] = {0xa2, 0xae,0xA2,0xB4,0xA1,0xD7,0xA1,0xA7};
1374 static const int32_t offsets1[] = { 0,2,4,6};
1375
1376 /*DBCS*/
1377 if(!testConvertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
1378 expected, sizeof(expected), "ibm-1363", UCNV_FROM_U_CALLBACK_SUBSTITUTE , NULL, TRUE))
1379 log_err("u-> ibm-1363 [UCNV_DBCS portion] not match.\n");
1380 if(!testConvertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
1381 expected, sizeof(expected), "ibm-1363", UCNV_FROM_U_CALLBACK_SUBSTITUTE,offsets , TRUE))
1382 log_err("u-> ibm-1363 [UCNV_DBCS portion] not match.\n");
1383
1384 if(!testConvertToU(expected1, sizeof(expected1),
1385 sampleText1, sizeof(sampleText1)/sizeof(sampleText1[0]), "ibm-1363",UCNV_TO_U_CALLBACK_SUBSTITUTE ,
1386 offsets1, TRUE))
1387 log_err("ibm-1363 -> did not match.\n");
1388 /*MBCS*/
1389 if(!testConvertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
1390 expected, sizeof(expected), "ibm-1363", UCNV_FROM_U_CALLBACK_SUBSTITUTE , NULL, TRUE))
1391 log_err("u-> ibm-1363 [UCNV_MBCS] not match.\n");
1392 if(!testConvertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
1393 expected, sizeof(expected), "ibm-1363", UCNV_FROM_U_CALLBACK_SUBSTITUTE,offsets , TRUE))
1394 log_err("u-> ibm-1363 [UCNV_MBCS] not match.\n");
1395
1396 if(!testConvertToU(expected1, sizeof(expected1),
1397 sampleText1, sizeof(sampleText1)/sizeof(sampleText1[0]), "ibm-1363",UCNV_TO_U_CALLBACK_SUBSTITUTE ,
1398 offsets1, TRUE))
1399 log_err("ibm-1363 -> did not match.\n");
1400
1401 }
1402 log_verbose("Testing Reset for ISO-2022-jp\n");
1403 {
1404 static const UChar sampleText[] = { 0x4e00, 0x04e01, 0x0031, 0xd801, 0xdc01, 0x0032};
1405
1406 static const uint8_t expected[] = {0x1b, 0x24, 0x42,0x30,0x6c,0x43,0x7a,0x1b,0x28,0x42,
1407 0x31,0x1A, 0x32};
1408
1409
1410 static const int32_t offsets[] = {0,0,0,0,0,1,1,2,2,2,2,3,5 };
1411
1412
1413 static const UChar sampleText1[] = {0x4e00, 0x04e01, 0x0031,0x001A, 0x0032};
1414 static const uint8_t expected1[] = {0x1b, 0x24, 0x42,0x30,0x6c,0x43,0x7a,0x1b,0x28,0x42,
1415 0x31,0x1A, 0x32};
1416 static const int32_t offsets1[] = { 3,5,10,11,12};
1417
1418 /*iso-2022-jp*/
1419 if(!testConvertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
1420 expected, sizeof(expected), "iso-2022-jp", UCNV_FROM_U_CALLBACK_SUBSTITUTE , NULL, TRUE))
1421 log_err("u-> not match.\n");
1422 if(!testConvertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
1423 expected, sizeof(expected), "iso-2022-jp", UCNV_FROM_U_CALLBACK_SUBSTITUTE,offsets , TRUE))
1424 log_err("u-> not match.\n");
1425
1426 if(!testConvertToU(expected1, sizeof(expected1),
1427 sampleText1, sizeof(sampleText1)/sizeof(sampleText1[0]), "iso-2022-jp",UCNV_TO_U_CALLBACK_SUBSTITUTE ,
1428 offsets1, TRUE))
1429 log_err("iso-2022-jp -> did not match.\n");
1430
1431 }
1432 log_verbose("Testing Reset for ISO-2022-cn\n");
1433 {
1434 static const UChar sampleText[] = { 0x4e00, 0x04e01, 0x0031, 0xd801, 0xdc01, 0x0032};
1435
1436 static const uint8_t expected[] = {
1437 0x1B, 0x24, 0x29, 0x41, 0x0E, 0x52, 0x3B,
1438 0x36, 0x21,
1439 0x0f, 0x31,
1440 0x1A,
374ca955 1441 0x32
b75a7d8f
A
1442 };
1443
1444
1445 static const int32_t offsets[] = {
1446 0, 0, 0, 0, 0, 0, 0,
1447 1, 1,
1448 2, 2,
1449 3,
374ca955 1450 5, };
b75a7d8f
A
1451
1452 UChar sampleText1[] = {0x4e00, 0x04e01, 0x0031,0x001A, 0x0032};
1453 static const uint8_t expected1[] = {
1454 0x1B, 0x24, 0x29, 0x41, 0x0E, 0x52, 0x3B,
1455 0x36, 0x21,
374ca955 1456 0x1B, 0x24, 0x29, 0x47, 0x24, 0x22,
b75a7d8f
A
1457 0x0f, 0x1A,
1458 0x32
1459 };
374ca955 1460 static const int32_t offsets1[] = { 5,7,13,16,17};
b75a7d8f
A
1461
1462 /*iso-2022-CN*/
1463 if(!testConvertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
1464 expected, sizeof(expected), "iso-2022-cn", UCNV_FROM_U_CALLBACK_SUBSTITUTE , NULL, TRUE))
1465 log_err("u-> not match.\n");
1466 if(!testConvertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
1467 expected, sizeof(expected), "iso-2022-cn", UCNV_FROM_U_CALLBACK_SUBSTITUTE,offsets , TRUE))
1468 log_err("u-> not match.\n");
1469
1470 if(!testConvertToU(expected1, sizeof(expected1),
1471 sampleText1, sizeof(sampleText1)/sizeof(sampleText1[0]), "iso-2022-cn",UCNV_TO_U_CALLBACK_SUBSTITUTE ,
1472 offsets1, TRUE))
1473 log_err("iso-2022-cn -> did not match.\n");
1474 }
1475 log_verbose("Testing Reset for ISO-2022-kr\n");
1476 {
1477 UChar sampleText[] = { 0x4e00,0xd801, 0xdc01, 0x04e01, 0x0031, 0xd801, 0xdc01, 0x0032};
1478
1479 static const uint8_t expected[] = {0x1B, 0x24, 0x29, 0x43,
1480 0x0E, 0x6C, 0x69,
1481 0x0f, 0x1A,
1482 0x0e, 0x6F, 0x4B,
1483 0x0F, 0x31,
1484 0x1A,
1485 0x32 };
1486
1487 static const int32_t offsets[] = {-1, -1, -1, -1,
1488 0, 0, 0,
1489 1, 1,
1490 3, 3, 3,
1491 4, 4,
1492 5,
1493 7,
1494 };
1495 static const UChar sampleText1[] = { 0x4e00,0x0041, 0x04e01, 0x0031, 0x0042, 0x0032};
1496
1497 static const uint8_t expected1[] = {0x1B, 0x24, 0x29, 0x43,
1498 0x0E, 0x6C, 0x69,
1499 0x0f, 0x41,
1500 0x0e, 0x6F, 0x4B,
1501 0x0F, 0x31,
1502 0x42,
1503 0x32 };
1504
1505 static const int32_t offsets1[] = {
1506 5, 8, 10,
1507 13, 14, 15
1508
1509 };
1510 /*iso-2022-kr*/
1511 if(!testConvertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
1512 expected, sizeof(expected), "iso-2022-kr", UCNV_FROM_U_CALLBACK_SUBSTITUTE , NULL, TRUE))
1513 log_err("u-> iso-2022-kr [UCNV_DBCS] not match.\n");
1514 if(!testConvertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
1515 expected, sizeof(expected), "iso-2022-kr", UCNV_FROM_U_CALLBACK_SUBSTITUTE,offsets , TRUE))
1516 log_err("u-> iso-2022-kr [UCNV_DBCS] not match.\n");
1517 if(!testConvertToU(expected1, sizeof(expected1),
1518 sampleText1, sizeof(sampleText1)/sizeof(sampleText1[0]), "iso-2022-kr",UCNV_TO_U_CALLBACK_SUBSTITUTE ,
1519 offsets1, TRUE))
1520 log_err("iso-2022-kr -> did not match.\n");
1521 }
1522 log_verbose("Testing Reset for HZ\n");
1523 {
1524 static const UChar sampleText[] = { 0x4e00, 0xd801, 0xdc01, 0x04e01, 0x0031, 0xd801, 0xdc01, 0x0032};
1525
1526 static const uint8_t expected[] = {0x7E, 0x7B, 0x52, 0x3B,
1527 0x7E, 0x7D, 0x1A,
1528 0x7E, 0x7B, 0x36, 0x21,
1529 0x7E, 0x7D, 0x31,
1530 0x1A,
1531 0x32 };
1532
1533
1534 static const int32_t offsets[] = {0,0,0,0,
1535 1,1,1,
1536 3,3,3,3,
1537 4,4,4,
1538 5,
1539 7,};
1540 static const UChar sampleText1[] = { 0x4e00, 0x0035, 0x04e01, 0x0031, 0x0041, 0x0032};
1541
1542 static const uint8_t expected1[] = {0x7E, 0x7B, 0x52, 0x3B,
1543 0x7E, 0x7D, 0x35,
1544 0x7E, 0x7B, 0x36, 0x21,
1545 0x7E, 0x7D, 0x31,
1546 0x41,
1547 0x32 };
1548
1549
1550 static const int32_t offsets1[] = {2,6,9,13,14,15
1551 };
1552
1553 /*hz*/
1554 if(!testConvertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
1555 expected, sizeof(expected), "HZ", UCNV_FROM_U_CALLBACK_SUBSTITUTE,NULL , TRUE))
1556 log_err("u-> not match.\n");
1557 if(!testConvertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
1558 expected, sizeof(expected), "HZ", UCNV_FROM_U_CALLBACK_SUBSTITUTE,offsets , TRUE))
1559 log_err("u-> not match.\n");
1560 if(!testConvertToU(expected1, sizeof(expected1),
1561 sampleText1, sizeof(sampleText1)/sizeof(sampleText1[0]), "hz",UCNV_TO_U_CALLBACK_SUBSTITUTE ,
1562 offsets1, TRUE))
1563 log_err("hz -> did not match.\n");
1564 }
1565 /*UTF-8*/
1566 log_verbose("Testing for UTF8\n");
1567 {
1568 static const UChar sampleText[] = { 0x4e00, 0x0701, 0x0031, 0xbfc1, 0xd801, 0xdc01, 0x0032};
1569 int32_t offsets[]={0x00, 0x00, 0x00, 0x01, 0x01, 0x02,
1570 0x03, 0x03, 0x03, 0x04, 0x04, 0x04,
1571 0x04, 0x06 };
1572 static const uint8_t expected[] = {0xe4, 0xb8, 0x80, 0xdc, 0x81, 0x31,
1573 0xeb, 0xbf, 0x81, 0xF0, 0x90, 0x90, 0x81, 0x32};
1574
1575
1576 static const int32_t fromOffsets[] = { 0x0000, 0x0003, 0x0005, 0x0006, 0x0009, 0x0009, 0x000D };
1577 /*UTF-8*/
1578 if(!testConvertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
1579 expected, sizeof(expected), "UTF8", UCNV_FROM_U_CALLBACK_SUBSTITUTE,offsets , TRUE))
1580 log_err("u-> UTF8 with offsets and flush true did not match.\n");
1581 if(!testConvertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
1582 expected, sizeof(expected), "UTF8", UCNV_FROM_U_CALLBACK_SUBSTITUTE,NULL , TRUE))
1583 log_err("u-> UTF8 with offsets and flush true did not match.\n");
1584 if(!testConvertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
1585 expected, sizeof(expected), "UTF8", UCNV_FROM_U_CALLBACK_SUBSTITUTE,offsets , TRUE))
1586 log_err("u-> UTF8 with offsets and flush true did not match.\n");
1587 if(!testConvertFromU(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
1588 expected, sizeof(expected), "UTF8", UCNV_FROM_U_CALLBACK_SUBSTITUTE,NULL , TRUE))
1589 log_err("u-> UTF8 with offsets and flush true did not match.\n");
1590 if(!testConvertToU(expected, sizeof(expected),
1591 sampleText, sizeof(sampleText)/sizeof(sampleText[0]), "UTF8",UCNV_TO_U_CALLBACK_SUBSTITUTE , NULL, TRUE))
1592 log_err("UTF8 -> did not match.\n");
1593 if(!testConvertToU(expected, sizeof(expected),
1594 sampleText, sizeof(sampleText)/sizeof(sampleText[0]), "UTF8", UCNV_TO_U_CALLBACK_SUBSTITUTE , NULL, TRUE))
1595 log_err("UTF8 -> did not match.\n");
1596 if(!testConvertToU(expected, sizeof(expected),
1597 sampleText, sizeof(sampleText)/sizeof(sampleText[0]), "UTF8",UCNV_TO_U_CALLBACK_SUBSTITUTE , fromOffsets, TRUE))
1598 log_err("UTF8 -> did not match.\n");
1599 if(!testConvertToU(expected, sizeof(expected),
1600 sampleText, sizeof(sampleText)/sizeof(sampleText[0]), "UTF8", UCNV_TO_U_CALLBACK_SUBSTITUTE , fromOffsets, TRUE))
1601 log_err("UTF8 -> did not match.\n");
1602
1603 }
1604
1605}
1606
1607/* Test that U_TRUNCATED_CHAR_FOUND is set. */
1608static void
1609doTestTruncated(const char *cnvName, const uint8_t *bytes, int32_t length) {
1610 UConverter *cnv;
1611
1612 UChar buffer[2];
1613 UChar *target, *targetLimit;
1614 const char *source, *sourceLimit;
1615
1616 UErrorCode errorCode;
1617
1618 errorCode=U_ZERO_ERROR;
1619 cnv=ucnv_open(cnvName, &errorCode);
1620 if(U_FAILURE(errorCode)) {
1621 log_data_err("error TestTruncated: unable to open \"%s\" - %s\n", cnvName, u_errorName(errorCode));
1622 return;
1623 }
374ca955
A
1624 ucnv_setToUCallBack(cnv, UCNV_TO_U_CALLBACK_STOP, NULL, NULL, NULL, &errorCode);
1625 if(U_FAILURE(errorCode)) {
1626 log_data_err("error TestTruncated: unable to set the stop callback on \"%s\" - %s\n",
1627 cnvName, u_errorName(errorCode));
1628 ucnv_close(cnv);
1629 return;
1630 }
b75a7d8f
A
1631
1632 source=(const char *)bytes;
1633 sourceLimit=source+length;
1634 target=buffer;
1635 targetLimit=buffer+LENGTHOF(buffer);
1636
1637 /* 1. input bytes with flush=FALSE, then input nothing with flush=TRUE */
1638 ucnv_toUnicode(cnv, &target, targetLimit, &source, sourceLimit, NULL, FALSE, &errorCode);
1639 if(U_FAILURE(errorCode) || source!=sourceLimit || target!=buffer) {
1640 log_err("error TestTruncated(%s, 1a): input bytes[%d], flush=FALSE: %s, input left %d, output %d\n",
1641 cnvName, length, u_errorName(errorCode), (int)(sourceLimit-source), (int)(target-buffer));
1642 }
1643
1644 errorCode=U_ZERO_ERROR;
1645 source=sourceLimit;
1646 target=buffer;
1647 ucnv_toUnicode(cnv, &target, targetLimit, &source, sourceLimit, NULL, TRUE, &errorCode);
1648 if(errorCode!=U_TRUNCATED_CHAR_FOUND || target!=buffer) {
374ca955
A
1649 log_err("error TestTruncated(%s, 1b): no input (previously %d), flush=TRUE: %s (should be U_TRUNCATED_CHAR_FOUND), output %d\n",
1650 cnvName, (int)length, u_errorName(errorCode), (int)(target-buffer));
b75a7d8f
A
1651 }
1652
b75a7d8f
A
1653 /* 2. input bytes with flush=TRUE */
1654 ucnv_resetToUnicode(cnv);
1655
1656 errorCode=U_ZERO_ERROR;
1657 source=(const char *)bytes;
1658 target=buffer;
1659 ucnv_toUnicode(cnv, &target, targetLimit, &source, sourceLimit, NULL, TRUE, &errorCode);
374ca955 1660 if(errorCode!=U_TRUNCATED_CHAR_FOUND || source!=sourceLimit || target!=buffer) {
b75a7d8f
A
1661 log_err("error TestTruncated(%s, 2): input bytes[%d], flush=TRUE: %s (should be U_TRUNCATED_CHAR_FOUND), input left %d, output %d\n",
1662 cnvName, length, u_errorName(errorCode), (int)(sourceLimit-source), (int)(target-buffer));
1663 }
1664
1665
1666 ucnv_close(cnv);
1667}
1668
1669static void
1670TestTruncated() {
1671 static const struct {
1672 const char *cnvName;
1673 uint8_t bytes[8]; /* partial input bytes resulting in no output */
1674 int32_t length;
1675 } testCases[]={
1676 { "IMAP-mailbox-name", { 0x26 }, 1 }, /* & */
1677 { "IMAP-mailbox-name", { 0x26, 0x42 }, 2 }, /* &B */
1678 { "IMAP-mailbox-name", { 0x26, 0x42, 0x42 }, 3 }, /* &BB */
1679 { "IMAP-mailbox-name", { 0x26, 0x41, 0x41 }, 3 }, /* &AA */
1680
1681 { "UTF-7", { 0x2b, 0x42 }, 2 }, /* +B */
1682 { "UTF-8", { 0xd1 }, 1 },
1683
1684 { "UTF-16BE", { 0x4e }, 1 },
1685 { "UTF-16LE", { 0x4e }, 1 },
1686 { "UTF-16", { 0x4e }, 1 },
1687 { "UTF-16", { 0xff }, 1 },
1688 { "UTF-16", { 0xfe, 0xff, 0x4e }, 3 },
1689
1690 { "UTF-32BE", { 0, 0, 0x4e }, 3 },
1691 { "UTF-32LE", { 0x4e }, 1 },
1692 { "UTF-32", { 0, 0, 0x4e }, 3 },
1693 { "UTF-32", { 0xff }, 1 },
1694 { "UTF-32", { 0, 0, 0xfe, 0xff, 0 }, 5 },
1695
1696 { "SCSU", { 0x0e, 0x4e }, 2 }, /* SQU 0x4e */
1697 { "BOCU-1", { 0xd5 }, 1 },
1698
1699 { "Shift-JIS", { 0xe0 }, 1 },
1700
1701 { "ibm-939", { 0x0e, 0x41 }, 2 } /* SO 0x41 */
1702 };
1703 int32_t i;
1704
1705 for(i=0; i<LENGTHOF(testCases); ++i) {
1706 doTestTruncated(testCases[i].cnvName, testCases[i].bytes, testCases[i].length);
1707 }
1708}
1709
1710typedef struct NameRange {
1711 const char *name;
1712 UChar32 start, end, start2, end2, notStart, notEnd;
1713} NameRange;
1714
1715static void
1716TestUnicodeSet() {
1717 UErrorCode errorCode;
1718 UConverter *cnv;
1719 USet *set;
1720 const char *name;
1721 int32_t i, count;
1722
1723 static const char *const completeSetNames[]={
1724 "UTF-7",
1725 "UTF-8",
1726 "UTF-16",
1727 "UTF-16BE",
1728 "UTF-16LE",
1729 "UTF-32",
1730 "UTF-32BE",
1731 "UTF-32LE",
1732 "SCSU",
1733 "BOCU-1",
1734 "CESU-8",
1735 "gb18030",
374ca955
A
1736 "IMAP-mailbox-name"
1737 };
1738
1739 static const char *const lmbcsNames[]={
b75a7d8f
A
1740 "LMBCS-1",
1741 "LMBCS-2",
1742 "LMBCS-3",
1743 "LMBCS-4",
1744 "LMBCS-5",
1745 "LMBCS-6",
1746 "LMBCS-8",
1747 "LMBCS-11",
1748 "LMBCS-16",
1749 "LMBCS-17",
1750 "LMBCS-18",
1751 "LMBCS-19"
1752 };
1753
1754 static const NameRange nameRanges[]={
1755 { "US-ASCII", 0, 0x7f, -1, -1, 0x80, 0x10ffff },
1756 { "ibm-367", 0, 0x7f, -1, -1, 0x80, 0x10ffff },
1757 { "ISO-8859-1", 0, 0x7f, -1, -1, 0x100, 0x10ffff },
1758 { "UTF-8", 0, 0xd7ff, 0xe000, 0x10ffff, 0xd800, 0xdfff },
1759 { "windows-1251", 0, 0x7f, 0x410, 0x44f, 0x3000, 0xd7ff },
1760 { "HZ", 0x410, 0x44f, 0x4e00, 0x4eff, 0xac00, 0xd7ff },
1761 { "shift-jis", 0x3041, 0x3093, 0x30a1, 0x30f3, 0x900, 0x1cff }
1762 };
1763
1764 /* open an empty set */
1765 set=uset_open(1, 0);
1766
1767 count=ucnv_countAvailable();
1768 for(i=0; i<count; ++i) {
1769 errorCode=U_ZERO_ERROR;
1770 name=ucnv_getAvailableName(i);
1771 cnv=ucnv_open(name, &errorCode);
1772 if(U_FAILURE(errorCode)) {
1773 log_err("error: unable to open converter %s - %s\n",
1774 name, u_errorName(errorCode));
1775 continue;
1776 }
1777
1778 uset_clear(set);
1779 ucnv_getUnicodeSet(cnv, set, UCNV_ROUNDTRIP_SET, &errorCode);
1780 if(U_FAILURE(errorCode)) {
1781 log_err("error: ucnv_getUnicodeSet(%s) failed - %s\n",
1782 name, u_errorName(errorCode));
1783 } else if(uset_size(set)==0) {
1784 log_err("error: ucnv_getUnicodeSet(%s) returns an empty set\n", name);
1785 }
1786
1787 ucnv_close(cnv);
1788 }
1789
1790 /* test converters that are known to convert all of Unicode (except maybe for surrogates) */
1791 for(i=0; i<LENGTHOF(completeSetNames); ++i) {
1792 errorCode=U_ZERO_ERROR;
1793 name=completeSetNames[i];
1794 cnv=ucnv_open(name, &errorCode);
1795 if(U_FAILURE(errorCode)) {
1796 log_err("error: unable to open converter %s - %s\n",
1797 name, u_errorName(errorCode));
1798 continue;
1799 }
1800
1801 uset_clear(set);
1802 ucnv_getUnicodeSet(cnv, set, UCNV_ROUNDTRIP_SET, &errorCode);
1803 if(U_FAILURE(errorCode)) {
1804 log_err("error: ucnv_getUnicodeSet(%s) failed - %s\n",
1805 name, u_errorName(errorCode));
1806 } else if(!uset_containsRange(set, 0, 0xd7ff) || !uset_containsRange(set, 0xe000, 0x10ffff)) {
1807 log_err("error: ucnv_getUnicodeSet(%s) does not return an all-Unicode set\n", name);
1808 }
1809
1810 ucnv_close(cnv);
1811 }
1812
374ca955
A
1813 /* test LMBCS variants which convert all of Unicode except for U+F6xx */
1814 for(i=0; i<LENGTHOF(lmbcsNames); ++i) {
1815 errorCode=U_ZERO_ERROR;
1816 name=lmbcsNames[i];
1817 cnv=ucnv_open(name, &errorCode);
1818 if(U_FAILURE(errorCode)) {
1819 log_err("error: unable to open converter %s - %s\n",
1820 name, u_errorName(errorCode));
1821 continue;
1822 }
1823
1824 uset_clear(set);
1825 ucnv_getUnicodeSet(cnv, set, UCNV_ROUNDTRIP_SET, &errorCode);
1826 if(U_FAILURE(errorCode)) {
1827 log_err("error: ucnv_getUnicodeSet(%s) failed - %s\n",
1828 name, u_errorName(errorCode));
1829 } else if(!uset_containsRange(set, 0, 0xf5ff) || !uset_containsRange(set, 0xf700, 0x10ffff)) {
1830 log_err("error: ucnv_getUnicodeSet(%s) does not return an all-Unicode set (minus U+F6xx)\n", name);
1831 }
1832
1833 ucnv_close(cnv);
1834 }
1835
b75a7d8f
A
1836 /* test specific sets */
1837 for(i=0; i<LENGTHOF(nameRanges); ++i) {
1838 errorCode=U_ZERO_ERROR;
1839 name=nameRanges[i].name;
1840 cnv=ucnv_open(name, &errorCode);
1841 if(U_FAILURE(errorCode)) {
1842 log_data_err("error: unable to open converter %s - %s\n",
1843 name, u_errorName(errorCode));
1844 continue;
1845 }
1846
1847 uset_clear(set);
1848 ucnv_getUnicodeSet(cnv, set, UCNV_ROUNDTRIP_SET, &errorCode);
1849 if(U_FAILURE(errorCode)) {
1850 log_err("error: ucnv_getUnicodeSet(%s) failed - %s\n",
1851 name, u_errorName(errorCode));
1852 } else if(
1853 !uset_containsRange(set, nameRanges[i].start, nameRanges[i].end) ||
1854 (nameRanges[i].start2>=0 && !uset_containsRange(set, nameRanges[i].start2, nameRanges[i].end2))
1855 ) {
1856 log_err("error: ucnv_getUnicodeSet(%s) does not contain the expected ranges\n", name);
1857 } else if(nameRanges[i].notStart>=0) {
1858 /* simulate containsAny() with the C API */
1859 uset_complement(set);
1860 if(!uset_containsRange(set, nameRanges[i].notStart, nameRanges[i].notEnd)) {
1861 log_err("error: ucnv_getUnicodeSet(%s) contains part of the unexpected range\n", name);
1862 }
1863 }
1864
1865 ucnv_close(cnv);
1866 }
1867
1868 uset_close(set);
1869}