1 /********************************************************************
3 * Copyright (c) 1997-2004, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /********************************************************************************
10 * Modification History:
12 * Madhu Katragadda 7/7/2000 Converter Tests for extended code coverage
13 *********************************************************************************
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"
25 #define MAX_LENGTH 999
27 #define UNICODE_LIMIT 0x10FFFF
28 #define SURROGATE_HIGH_START 0xD800
29 #define SURROGATE_LOW_END 0xDFFF
31 static int32_t gInBufferSize
= 0;
32 static int32_t gOutBufferSize
= 0;
33 static char gNuConvTestName
[1024];
35 #define nct_min(x,y) ((x<y) ? x : y)
36 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
38 static void printSeq(const unsigned char* a
, int len
);
39 static void printSeqErr(const unsigned char* a
, int len
);
40 static void printUSeq(const UChar
* a
, int len
);
41 static void printUSeqErr(const UChar
* a
, int len
);
42 static 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
);
44 static 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
);
47 static 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
);
49 static 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
);
52 static void setNuConvTestName(const char *codepage
, const char *direction
)
54 sprintf(gNuConvTestName
, "[Testing %s %s Unicode, InputBufSiz=%d, OutputBufSiz=%d]",
62 static void TestSurrogateBehaviour(void);
63 static void TestErrorBehaviour(void);
64 static void TestToUnicodeErrorBehaviour(void);
65 static void TestGetNextErrorBehaviour(void);
66 static void TestRegressionUTF8(void);
67 static void TestRegressionUTF32(void);
68 static void TestAvailableConverters(void);
69 static void TestFlushInternalBuffer(void); /*for improved code coverage in ucnv_cnv.c*/
70 static void TestResetBehaviour(void);
71 static void TestTruncated(void);
72 static void TestUnicodeSet(void);
74 static void TestWithBufferSize(int32_t osize
, int32_t isize
);
77 static void printSeq(const unsigned char* a
, int len
)
82 log_verbose("0x%02X ", a
[i
++]);
86 static void printUSeq(const UChar
* a
, int len
)
91 log_verbose("%0x04X ", a
[i
++]);
95 static void printSeqErr(const unsigned char* a
, int len
)
98 fprintf(stderr
, "\n{");
99 while (i
<len
) fprintf(stderr
, "0x%02X ", a
[i
++]);
100 fprintf(stderr
, "}\n");
103 static void printUSeqErr(const UChar
* a
, int len
)
106 fprintf(stderr
, "\n{");
108 fprintf(stderr
, "0x%04X ", a
[i
++]);
109 fprintf(stderr
,"}\n");
112 void addExtraTests(TestNode
** root
);
114 void addExtraTests(TestNode
** root
)
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");
129 /*test surrogate behaviour*/
130 static void TestSurrogateBehaviour(){
131 log_verbose("Testing for SBCS and LATIN_1\n");
133 UChar sampleText
[] = {0x0031, 0xd801, 0xdc01, 0x0032};
134 const uint8_t expected
[] = {0x31, 0x1a, 0x32};
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");
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");
146 log_verbose("Testing for DBCS and MBCS\n");
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 };
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");
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");
167 log_verbose("Testing for ISO-2022-jp\n");
169 UChar sampleText
[] = { 0x4e00, 0x04e01, 0x0031, 0xd801, 0xdc01, 0x0032};
171 const uint8_t expected
[] = {0x1b, 0x24, 0x42,0x30,0x6c,0x43,0x7a,0x1b,0x28,0x42,
175 int32_t offsets
[] = {0,0,0,0,0,1,1,2,2,2,2,3,5 };
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");
185 log_verbose("Testing for ISO-2022-cn\n");
187 static const UChar sampleText
[] = { 0x4e00, 0x04e01, 0x0031, 0xd801, 0xdc01, 0x0032};
189 static const uint8_t expected
[] = {
190 0x1B, 0x24, 0x29, 0x41, 0x0E, 0x52, 0x3B,
199 static const int32_t offsets
[] = {
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");
214 log_verbose("Testing for ISO-2022-kr\n");
216 static const UChar sampleText
[] = { 0x4e00,0xd801, 0xdc01, 0x04e01, 0x0031, 0xd801, 0xdc01, 0x0032};
218 static const uint8_t expected
[] = {0x1B, 0x24, 0x29, 0x43,
226 static const int32_t offsets
[] = {-1, -1, -1, -1,
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");
243 log_verbose("Testing for HZ\n");
245 static const UChar sampleText
[] = { 0x4e00, 0xd801, 0xdc01, 0x04e01, 0x0031, 0xd801, 0xdc01, 0x0032};
247 static const uint8_t expected
[] = {0x7E, 0x7B, 0x52, 0x3B,
249 0x7E, 0x7B, 0x36, 0x21,
255 static const int32_t offsets
[] = {0,0,0,0,
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");
271 log_verbose("Testing for UTF8\n");
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,
277 static const uint8_t expected
[] = {0xe4, 0xb8, 0x80, 0xdc, 0x81, 0x31,
278 0xeb, 0xbf, 0x81, 0xF0, 0x90, 0x90, 0x81, 0x32};
281 static const int32_t fromOffsets
[] = { 0x0000, 0x0003, 0x0005, 0x0006, 0x0009, 0x0009, 0x000D };
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");
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");
315 /*test various error behaviours*/
316 static void TestErrorBehaviour(){
317 log_verbose("Testing for SBCS and LATIN_1\n");
319 static const UChar sampleText
[] = { 0x0031, 0xd801};
320 static const UChar sampleText2
[] = { 0x0031, 0xd801, 0x0032};
321 static const uint8_t expected0
[] = { 0x31};
322 static const uint8_t expected
[] = { 0x31, 0x1a};
323 static const uint8_t expected2
[] = { 0x31, 0x1a, 0x32};
326 if(!convertFromU(sampleText
, sizeof(sampleText
)/sizeof(sampleText
[0]),
327 expected
, sizeof(expected
), "ibm-920", 0, TRUE
, U_ZERO_ERROR
))
328 log_err("u-> ibm-920 [UCNV_SBCS] \n");
329 if(!convertFromU(sampleText
, sizeof(sampleText
)/sizeof(sampleText
[0]),
330 expected0
, sizeof(expected0
), "ibm-920", 0, FALSE
, U_ZERO_ERROR
))
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");
338 if(!convertFromU(sampleText
, sizeof(sampleText
)/sizeof(sampleText
[0]),
339 expected
, sizeof(expected
), "LATIN_1", 0, TRUE
, U_ZERO_ERROR
))
340 log_err("u-> LATIN_1 is supposed to fail\n");
341 if(!convertFromU(sampleText
, sizeof(sampleText
)/sizeof(sampleText
[0]),
342 expected0
, sizeof(expected0
), "LATIN_1", 0, FALSE
, U_ZERO_ERROR
))
343 log_err("u-> LATIN_1 is supposed to fail\n");
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");
351 log_verbose("Testing for DBCS and MBCS\n");
353 static const UChar sampleText
[] = { 0x00a1, 0xd801};
354 static const uint8_t expected
[] = { 0xa2, 0xae};
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};
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};
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};
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 };
376 if(!convertFromU(sampleText
, sizeof(sampleText
)/sizeof(sampleText
[0]),
377 expectedSUB
, sizeof(expectedSUB
), "ibm-1363", 0, TRUE
, U_ZERO_ERROR
))
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");
383 if(!convertFromU(sampleText
, sizeof(sampleText
)/sizeof(sampleText
[0]),
384 expectedSUB
, sizeof(expectedSUB
), "ibm-1363", offsetsSUB
, TRUE
, U_ZERO_ERROR
))
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");
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");
399 if(!convertFromU(sampleText
, sizeof(sampleText
)/sizeof(sampleText
[0]),
400 expectedSUB
, sizeof(expectedSUB
), "ibm-1363", 0, TRUE
, U_ZERO_ERROR
))
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");
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");
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");
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");
431 log_verbose("Testing for iso-2022-jp\n");
433 static const UChar sampleText
[] = { 0x0031, 0xd801};
434 static const uint8_t expected
[] = { 0x31};
435 static const uint8_t expectedSUB
[] = { 0x31, 0x1a};
436 static const int32_t offsets
[] = { 0x00, 1};
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};
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]),
446 expectedSUB
, sizeof(expectedSUB
), "iso-2022-jp", offsets
, TRUE
, U_ZERO_ERROR
))
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");
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");
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");
470 log_verbose("Testing for iso-2022-cn\n");
472 static const UChar sampleText
[] = { 0x0031, 0xd801};
473 static const uint8_t expected
[] = { 0x31};
474 static const uint8_t expectedSUB
[] = { 0x31, 0x1A};
475 static const int32_t offsets
[] = { 0x00, 1};
477 static const UChar sampleText2
[] = { 0x0031, 0xd801, 0x0032};
478 static const uint8_t expected2
[] = { 0x31, 0x1A,0x32};
479 static const int32_t offsets2
[] = { 0x00, 0x01,0x02};
481 static const UChar sampleText3MBCS
[] = { 0x0051, 0x0050, 0xdc01};
482 static const uint8_t expected3MBCS
[] = {0x51, 0x50, 0x1A};
483 static const int32_t offsets3MBCS
[] = { 0x00, 0x01, 0x02 };
485 static const UChar sampleText4MBCS
[] = { 0x0061, 0x4e00, 0xdc01};
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 };
488 if(!convertFromU(sampleText
, sizeof(sampleText
)/sizeof(sampleText
[0]),
489 expectedSUB
, sizeof(expectedSUB
), "iso-2022-cn", offsets
, TRUE
, U_ZERO_ERROR
))
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");
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");
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");
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");
520 log_verbose("Testing for iso-2022-kr\n");
522 static const UChar sampleText
[] = { 0x0031, 0xd801};
523 static const uint8_t expected
[] = { 0x1b, 0x24, 0x29, 0x43, 0x31};
524 static const uint8_t expectedSUB
[] = { 0x1b, 0x24, 0x29, 0x43, 0x31, 0x1A};
525 static const int32_t offsets
[] = { -1, -1, -1, -1, 0x00, 1};
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};
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 };
535 if(!convertFromU(sampleText
, sizeof(sampleText
)/sizeof(sampleText
[0]),
536 expectedSUB
, sizeof(expectedSUB
), "iso-2022-kr", offsets
, TRUE
, U_ZERO_ERROR
))
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");
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");
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");
561 log_verbose("Testing for HZ\n");
563 static const UChar sampleText
[] = { 0x0031, 0xd801};
564 static const uint8_t expected
[] = { 0x7e, 0x7d, 0x31};
565 static const uint8_t expectedSUB
[] = { 0x7e, 0x7d, 0x31, 0x1A};
566 static const int32_t offsets
[] = { 0x00, 0x00, 0x00, 1};
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 };
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};
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]),
580 expectedSUB
, sizeof(expectedSUB
), "HZ", offsets
, TRUE
, U_ZERO_ERROR
))
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");
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");
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");
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");
614 /*test different convertToUnicode error behaviours*/
615 static void TestToUnicodeErrorBehaviour()
617 log_verbose("Testing error conditions for DBCS\n");
619 uint8_t sampleText
[] = { 0xa2, 0xae, 0x03, 0x04};
620 const UChar expected
[] = { 0x00a1 };
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");
629 log_verbose("Testing error conditions for SBCS\n");
631 uint8_t sampleText
[] = { 0xa2, 0xFF};
632 const UChar expected
[] = { 0x00c2 };
634 /* uint8_t sampleText2[] = { 0xa2, 0x70 };
635 const UChar expected2[] = { 0x0073 };*/
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");
647 static void TestGetNextErrorBehaviour(){
648 /*Test for unassigned character*/
650 static const char input1
[INPUT_SIZE
]={ 0x70 };
651 const char* source
=(const char*)input1
;
652 UErrorCode err
=U_ZERO_ERROR
;
654 UConverter
*cnv
=ucnv_open("ibm-424", &err
);
656 log_data_err("Unable to open a SBCS(ibm-424) converter: %s\n", u_errorName(err
));
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
);
666 #define MAX_UTF16_LEN 2
667 #define MAX_UTF8_LEN 4
669 /*Regression test for utf8 converter*/
670 static void TestRegressionUTF8(){
674 UChar
*standardForm
= (UChar
*)malloc(MAX_LENGTH
*sizeof(UChar
));
675 uint8_t *utf8
= (uint8_t*)malloc(MAX_LENGTH
);
677 while (currCh
<= UNICODE_LIMIT
) {
680 while(currCh
<= UNICODE_LIMIT
681 && offset16
< (MAX_LENGTH
/sizeof(UChar
) - MAX_UTF16_LEN
)
682 && offset8
< (MAX_LENGTH
- MAX_UTF8_LEN
))
684 if (currCh
== SURROGATE_HIGH_START
) {
685 currCh
= SURROGATE_LOW_END
+ 1; /* Skip surrogate range */
687 UTF16_APPEND_CHAR_SAFE(standardForm
, offset16
, MAX_LENGTH
, currCh
);
688 UTF8_APPEND_CHAR_SAFE(utf8
, offset8
, MAX_LENGTH
, currCh
);
691 if(!convertFromU(standardForm
, offset16
,
692 utf8
, offset8
, "UTF8", 0, TRUE
, U_ZERO_ERROR
)) {
693 log_err("Unicode->UTF8 did not match.\n");
695 if(!convertToU(utf8
, offset8
,
696 standardForm
, offset16
, "UTF8", 0, TRUE
, U_ZERO_ERROR
)) {
697 log_err("UTF8->Unicode did not match.\n");
704 #define MAX_UTF32_LEN 1
706 static void TestRegressionUTF32(){
710 UChar
*standardForm
= (UChar
*)malloc(MAX_LENGTH
*sizeof(UChar
));
711 UChar32
*utf32
= (UChar32
*)malloc(MAX_LENGTH
*sizeof(UChar32
));
713 while (currCh
<= UNICODE_LIMIT
) {
716 while(currCh
<= UNICODE_LIMIT
717 && offset16
< (MAX_LENGTH
/sizeof(UChar
) - MAX_UTF16_LEN
)
718 && offset32
< (MAX_LENGTH
/sizeof(UChar32
) - MAX_UTF32_LEN
))
720 if (currCh
== SURROGATE_HIGH_START
) {
721 currCh
= SURROGATE_LOW_END
+ 1; /* Skip surrogate range */
723 UTF16_APPEND_CHAR_SAFE(standardForm
, offset16
, MAX_LENGTH
, currCh
);
724 UTF32_APPEND_CHAR_SAFE(utf32
, offset32
, MAX_LENGTH
, currCh
);
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");
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");
740 /*Walk through the available converters*/
741 static void TestAvailableConverters(){
742 UErrorCode status
=U_ZERO_ERROR
;
743 UConverter
*conv
=NULL
;
745 for(i
=0; i
< ucnv_countAvailable(); i
++){
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
));
758 static void TestFlushInternalBuffer(){
759 TestWithBufferSize(MAX_LENGTH
, 1);
760 TestWithBufferSize(1, 1);
761 TestWithBufferSize(1, MAX_LENGTH
);
762 TestWithBufferSize(MAX_LENGTH
, MAX_LENGTH
);
765 static void TestWithBufferSize(int32_t insize
, int32_t outsize
){
767 gInBufferSize
=insize
;
768 gOutBufferSize
= outsize
;
770 log_verbose("Testing fromUnicode for UTF-8 with UCNV_TO_U_CALLBACK_SUBSTITUTE \n");
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 };*/
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");
787 log_verbose("Testing fromUnicode with UCNV_FROM_U_CALLBACK_ESCAPE \n");
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,
795 int32_t offset
[]= {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 4};
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");
803 log_verbose("Testing fromUnicode for UTF-8 with UCNV_TO_U_CALLBACK_SUBSTITUTE \n");
805 const uint8_t sampleText1
[] = { 0x31, 0xe4, 0xba, 0x8c,
807 UChar expected1
[] = { 0x0031, 0x4e8c, 0xfffd, 0x0061};
808 int32_t offsets1
[] = { 0x0000, 0x0001, 0x0004, 0x0006};
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");;
816 log_verbose("Testing toUnicode with UCNV_TO_U_CALLBACK_ESCAPE \n");
819 const uint8_t sampleTxtToU
[]= { 0x00, 0x9f, 0xaf,
820 0x81, 0xad, /*unassigned*/
822 UChar IBM_943toUnicode
[] = { 0x0000, 0x6D63,
823 0x25, 0x58, 0x38, 0x31, 0x25, 0x58, 0x41, 0x44,
825 int32_t fromIBM943Offs
[] = { 0, 1, 3, 3, 3, 3, 3, 3, 3, 3, 5};
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");
836 static 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
)
843 uint8_t buffer
[MAX_LENGTH
];
844 int32_t offsetBuffer
[MAX_LENGTH
];
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
))
854 log_data_err("Couldn't open converter %s\n",codepage
);
857 log_verbose("Converter %s opened..\n", ucnv_getName(conv
, &status
));
859 for(i
=0; i
<MAX_LENGTH
; i
++){
861 offsetBuffer
[i
]=0xFF;
865 sourceLimit
=(UChar
*)src
+(sourceLen
);
867 targetLimit
=targ
+MAX_LENGTH
;
869 ucnv_fromUnicode (conv
,
871 (const char *)targetLimit
,
874 expectOffsets
? offs
: NULL
,
878 if(status
!= expectedStatus
){
879 log_err("ucnv_fromUnicode() failed for codepage=%s. Error =%s Expected=%s\n", codepage
, myErrorName(status
), myErrorName(expectedStatus
));
883 log_verbose("\nConversion done [%d uchars in -> %d chars out]. \nResult :",
884 sourceLen
, targ
-buffer
);
886 if(expectLen
!= targ
-buffer
)
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
);
895 if(memcmp(buffer
, expect
, expectLen
)){
896 log_err("String does not match. FROM Unicode to codePage%s\n", codepage
);
898 printSeqErr((const unsigned char *)buffer
, expectLen
);
899 log_info("\nExpected:");
900 printSeqErr((const unsigned char *)expect
, expectLen
);
904 log_verbose("Matches!\n");
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
);
911 log_info("\nGot : ");
912 printSeqErr((const unsigned char*)buffer
, targ
-buffer
);
913 for(p
=buffer
;p
<targ
;p
++)
914 log_info("%d, ", offsetBuffer
[p
-buffer
]);
915 log_info("\nExpected: ");
916 for(i
=0; i
< (targ
-buffer
); i
++)
917 log_info("%d,", expectOffsets
[i
]);
925 static 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
)
928 UErrorCode status
= U_ZERO_ERROR
;
929 UConverter
*conv
= 0;
933 UChar buffer
[MAX_LENGTH
];
934 int32_t offsetBuffer
[MAX_LENGTH
];
938 uint8_t *sourceLimit
=0;
942 conv
= ucnv_open(codepage
, &status
);
943 if(U_FAILURE(status
))
945 log_data_err("Couldn't open converter %s\n",codepage
);
948 log_verbose("Converter %s opened..\n", ucnv_getName(conv
, &status
));
952 for(i
=0; i
<MAX_LENGTH
; i
++){
958 sourceLimit
=(uint8_t*)(src
+(sourceLen
));
960 targetLimit
=targ
+MAX_LENGTH
;
965 ucnv_toUnicode (conv
,
969 (const char *)sourceLimit
,
970 expectOffsets
? offs
: NULL
,
975 if(status
!= expectedStatus
){
976 log_err("ucnv_fromUnicode() failed for codepage=%s. Error =%s Expected=%s\n", codepage
, myErrorName(status
), myErrorName(expectedStatus
));
979 log_verbose("\nConversion done [%d uchars in -> %d chars out]. \nResult :",
980 sourceLen
, targ
-buffer
);
985 log_verbose("comparing %d uchars (%d bytes)..\n",expectLen
,expectLen
*2);
987 if (expectOffsets
!= 0) {
988 if(memcmp(offsetBuffer
, expectOffsets
, (targ
-buffer
) * sizeof(int32_t))){
990 log_err("did not get the expected offsets from %s To UNICODE\n", codepage
);
991 log_info("\nGot : ");
992 for(p
=buffer
;p
<targ
;p
++)
993 log_info("%d, ", offsetBuffer
[p
-buffer
]);
994 log_info("\nExpected: ");
995 for(i
=0; i
<(targ
-buffer
); i
++)
996 log_info("%d, ", expectOffsets
[i
]);
997 log_info("\nGot result:");
998 for(i
=0; i
<(targ
-buffer
); i
++)
999 log_info("0x%04X,", buffer
[i
]);
1000 log_info("\nFrom Input:");
1001 for(i
=0; i
<(src
-source
); i
++)
1002 log_info("0x%02X,", (unsigned char)source
[i
]);
1006 if(memcmp(buffer
, expect
, expectLen
*2)){
1007 log_err("String does not match. from codePage %s TO Unicode\n", codepage
);
1009 printUSeqErr(buffer
, expectLen
);
1010 log_info("\nExpected:");
1011 printUSeqErr(expect
, expectLen
);
1015 log_verbose("Matches!\n");
1022 static 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
)
1025 UErrorCode status
= U_ZERO_ERROR
;
1026 UConverter
*conv
= 0;
1027 uint8_t junkout
[MAX_LENGTH
]; /* FIX */
1028 int32_t junokout
[MAX_LENGTH
]; /* FIX */
1035 int32_t realBufferSize
;
1036 uint8_t *realBufferEnd
;
1037 const UChar
*realSourceEnd
;
1038 const UChar
*sourceLimit
;
1039 UBool checkOffsets
= TRUE
;
1042 UConverterFromUCallback oldAction
= NULL
;
1043 const void* oldContext
= NULL
;
1045 for(i
=0;i
<MAX_LENGTH
;i
++)
1047 for(i
=0;i
<MAX_LENGTH
;i
++)
1050 setNuConvTestName(codepage
, "FROM");
1052 log_verbose("\n========= %s\n", gNuConvTestName
);
1054 conv
= ucnv_open(codepage
, &status
);
1055 if(U_FAILURE(status
))
1057 log_data_err("Couldn't open converter %s\n",codepage
);
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
));
1067 /*------------------------*/
1073 realBufferSize
= (sizeof(junkout
)/sizeof(junkout
[0]));
1074 realBufferEnd
= junkout
+ realBufferSize
;
1075 realSourceEnd
= source
+ sourceLen
;
1077 if ( gOutBufferSize
!= realBufferSize
)
1078 checkOffsets
= FALSE
;
1080 if( gInBufferSize
!= MAX_LENGTH
)
1081 checkOffsets
= FALSE
;
1085 end
= nct_min(targ
+ gOutBufferSize
, realBufferEnd
);
1086 sourceLimit
= nct_min(src
+ gInBufferSize
, realSourceEnd
);
1088 doFlush
= (UBool
)(sourceLimit
== realSourceEnd
);
1090 if(targ
== realBufferEnd
)
1092 log_err("Error, overflowed the real buffer while about to call fromUnicode! targ=%08lx %s", targ
, gNuConvTestName
);
1095 log_verbose("calling fromUnicode @ SOURCE:%08lx to %08lx TARGET: %08lx to %08lx, flush=%s\n", src
,sourceLimit
, targ
,end
, doFlush
?"TRUE":"FALSE");
1098 status
= U_ZERO_ERROR
;
1099 if(gInBufferSize
==999 && gOutBufferSize
==999)
1101 ucnv_fromUnicode (conv
,
1107 doFlush
, /* flush if we're at the end of the input data */
1110 ucnv_resetToUnicode(conv
);
1111 if(gInBufferSize
==999 && gOutBufferSize
==999)
1112 ucnv_resetToUnicode(conv
);
1114 } while ( (status
== U_BUFFER_OVERFLOW_ERROR
) || (U_SUCCESS(status
) && sourceLimit
< realSourceEnd
) );
1116 if(U_FAILURE(status
)) {
1117 log_err("Problem doing fromUnicode to %s, errcode %s %s\n", codepage
, myErrorName(status
), gNuConvTestName
);
1121 log_verbose("\nConversion done [%d uchars in -> %d chars out]. \nResult :",
1122 sourceLen
, targ
-junkout
);
1126 char offset_str
[999];
1131 for(ptr
= junkout
;ptr
<targ
;ptr
++)
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
]);
1138 printSeq((const unsigned char *)expect
, expectLen
);
1141 log_verbose("\nOffsets:");
1142 log_verbose(offset_str
);
1149 if(expectLen
!= targ
-junkout
)
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
);
1154 printSeqErr((const unsigned char*)junkout
, targ
-junkout
);
1155 log_info("\nExpected:");
1156 printSeqErr((const unsigned char*)expect
, expectLen
);
1160 if (checkOffsets
&& (expectOffsets
!= 0) )
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
);
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
]);
1175 log_verbose("comparing..\n");
1176 if(!memcmp(junkout
, expect
, expectLen
))
1178 log_verbose("Matches!\n");
1183 log_err("String does not match. %s\n", gNuConvTestName
);
1184 printUSeqErr(source
, sourceLen
);
1186 printSeqErr((const unsigned char *)junkout
, expectLen
);
1187 log_info("\nExpected:");
1188 printSeqErr((const unsigned char *)expect
, expectLen
);
1194 static 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
)
1197 UErrorCode status
= U_ZERO_ERROR
;
1198 UConverter
*conv
= 0;
1199 UChar junkout
[MAX_LENGTH
]; /* FIX */
1200 int32_t junokout
[MAX_LENGTH
]; /* FIX */
1202 const uint8_t *realSourceEnd
;
1203 const uint8_t *srcLimit
;
1209 UBool checkOffsets
= TRUE
;
1210 int32_t realBufferSize
;
1211 UChar
*realBufferEnd
;
1214 UConverterToUCallback oldAction
= NULL
;
1215 const void* oldContext
= NULL
;
1218 for(i
=0;i
<MAX_LENGTH
;i
++)
1219 junkout
[i
] = 0xFFFE;
1221 for(i
=0;i
<MAX_LENGTH
;i
++)
1224 setNuConvTestName(codepage
, "TO");
1226 log_verbose("\n========= %s\n", gNuConvTestName
);
1228 conv
= ucnv_open(codepage
, &status
);
1229 if(U_FAILURE(status
))
1231 log_data_err("Couldn't open converter %s\n",gNuConvTestName
);
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
));
1241 /*-------------------------------------*/
1247 realBufferSize
= (sizeof(junkout
)/sizeof(junkout
[0]));
1248 realBufferEnd
= junkout
+ realBufferSize
;
1249 realSourceEnd
= src
+ sourcelen
;
1251 if ( gOutBufferSize
!= realBufferSize
)
1252 checkOffsets
= FALSE
;
1254 if( gInBufferSize
!= MAX_LENGTH
)
1255 checkOffsets
= FALSE
;
1259 end
= nct_min( targ
+ gOutBufferSize
, realBufferEnd
);
1260 srcLimit
= nct_min(realSourceEnd
, src
+ gInBufferSize
);
1262 if(targ
== realBufferEnd
)
1264 log_err("Error, the end would overflow the real output buffer while about to call toUnicode! tarjey=%08lx %s",targ
,gNuConvTestName
);
1267 log_verbose("calling toUnicode @ %08lx to %08lx\n", targ
,end
);
1269 /* oldTarg = targ; */
1271 status
= U_ZERO_ERROR
;
1272 doFlush
=(UBool
)((gInBufferSize
==999 && gOutBufferSize
==999)?(srcLimit
== realSourceEnd
) : FALSE
);
1274 ucnv_toUnicode (conv
,
1277 (const char **)&src
,
1278 (const char *)srcLimit
,
1280 doFlush
, /* flush if we're at the end of hte source data */
1283 ucnv_resetFromUnicode(conv
);
1284 if(gInBufferSize
==999 && gOutBufferSize
==999)
1285 ucnv_resetToUnicode(conv
);
1286 /* offs += (targ-oldTarg); */
1288 } while ( (status
== U_BUFFER_OVERFLOW_ERROR
) || (U_SUCCESS(status
) && (srcLimit
< realSourceEnd
)) ); /* while we just need another buffer */
1290 if(U_FAILURE(status
))
1292 log_err("Problem doing %s toUnicode, errcode %s %s\n", codepage
, myErrorName(status
), gNuConvTestName
);
1296 log_verbose("\nConversion done. %d bytes -> %d chars.\nResult :",
1297 sourcelen
, targ
-junkout
);
1301 char offset_str
[999];
1308 for(ptr
= junkout
;ptr
<targ
;ptr
++)
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
]);
1318 log_verbose("\nOffsets:");
1319 log_verbose(offset_str
);
1325 log_verbose("comparing %d uchars (%d bytes)..\n",expectlen
,expectlen
*2);
1327 if (checkOffsets
&& (expectOffsets
!= 0))
1329 if(memcmp(junokout
,expectOffsets
,(targ
-junkout
) * sizeof(int32_t))){
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
]);
1338 for(i
=0; i
<(targ
-junkout
); i
++)
1339 log_err("%X,", junkout
[i
]);
1341 for(i
=0; i
<(src
-source
); i
++)
1342 log_err("%X,", (unsigned char)source
[i
]);
1346 if(!memcmp(junkout
, expect
, expectlen
*2))
1348 log_verbose("Matches!\n");
1353 log_err("String does not match. %s\n", gNuConvTestName
);
1354 log_verbose("String does not match. %s\n", gNuConvTestName
);
1356 printUSeq(junkout
, expectlen
);
1357 log_info("\nExpected:");
1358 printUSeq(expect
, expectlen
);
1364 static void TestResetBehaviour(void){
1365 log_verbose("Testing Reset for DBCS and MBCS\n");
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 };
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};
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");
1384 if(!testConvertToU(expected1
, sizeof(expected1
),
1385 sampleText1
, sizeof(sampleText1
)/sizeof(sampleText1
[0]), "ibm-1363",UCNV_TO_U_CALLBACK_SUBSTITUTE
,
1387 log_err("ibm-1363 -> did not match.\n");
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");
1396 if(!testConvertToU(expected1
, sizeof(expected1
),
1397 sampleText1
, sizeof(sampleText1
)/sizeof(sampleText1
[0]), "ibm-1363",UCNV_TO_U_CALLBACK_SUBSTITUTE
,
1399 log_err("ibm-1363 -> did not match.\n");
1402 log_verbose("Testing Reset for ISO-2022-jp\n");
1404 static const UChar sampleText
[] = { 0x4e00, 0x04e01, 0x0031, 0xd801, 0xdc01, 0x0032};
1406 static const uint8_t expected
[] = {0x1b, 0x24, 0x42,0x30,0x6c,0x43,0x7a,0x1b,0x28,0x42,
1410 static const int32_t offsets
[] = {0,0,0,0,0,1,1,2,2,2,2,3,5 };
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,
1416 static const int32_t offsets1
[] = { 3,5,10,11,12};
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");
1426 if(!testConvertToU(expected1
, sizeof(expected1
),
1427 sampleText1
, sizeof(sampleText1
)/sizeof(sampleText1
[0]), "iso-2022-jp",UCNV_TO_U_CALLBACK_SUBSTITUTE
,
1429 log_err("iso-2022-jp -> did not match.\n");
1432 log_verbose("Testing Reset for ISO-2022-cn\n");
1434 static const UChar sampleText
[] = { 0x4e00, 0x04e01, 0x0031, 0xd801, 0xdc01, 0x0032};
1436 static const uint8_t expected
[] = {
1437 0x1B, 0x24, 0x29, 0x41, 0x0E, 0x52, 0x3B,
1445 static const int32_t offsets
[] = {
1446 0, 0, 0, 0, 0, 0, 0,
1452 UChar sampleText1
[] = {0x4e00, 0x04e01, 0x0031,0x001A, 0x0032};
1453 static const uint8_t expected1
[] = {
1454 0x1B, 0x24, 0x29, 0x41, 0x0E, 0x52, 0x3B,
1456 0x1B, 0x24, 0x29, 0x47, 0x24, 0x22,
1460 static const int32_t offsets1
[] = { 5,7,13,16,17};
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");
1470 if(!testConvertToU(expected1
, sizeof(expected1
),
1471 sampleText1
, sizeof(sampleText1
)/sizeof(sampleText1
[0]), "iso-2022-cn",UCNV_TO_U_CALLBACK_SUBSTITUTE
,
1473 log_err("iso-2022-cn -> did not match.\n");
1475 log_verbose("Testing Reset for ISO-2022-kr\n");
1477 UChar sampleText
[] = { 0x4e00,0xd801, 0xdc01, 0x04e01, 0x0031, 0xd801, 0xdc01, 0x0032};
1479 static const uint8_t expected
[] = {0x1B, 0x24, 0x29, 0x43,
1487 static const int32_t offsets
[] = {-1, -1, -1, -1,
1495 static const UChar sampleText1
[] = { 0x4e00,0x0041, 0x04e01, 0x0031, 0x0042, 0x0032};
1497 static const uint8_t expected1
[] = {0x1B, 0x24, 0x29, 0x43,
1505 static const int32_t offsets1
[] = {
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
,
1520 log_err("iso-2022-kr -> did not match.\n");
1522 log_verbose("Testing Reset for HZ\n");
1524 static const UChar sampleText
[] = { 0x4e00, 0xd801, 0xdc01, 0x04e01, 0x0031, 0xd801, 0xdc01, 0x0032};
1526 static const uint8_t expected
[] = {0x7E, 0x7B, 0x52, 0x3B,
1528 0x7E, 0x7B, 0x36, 0x21,
1534 static const int32_t offsets
[] = {0,0,0,0,
1540 static const UChar sampleText1
[] = { 0x4e00, 0x0035, 0x04e01, 0x0031, 0x0041, 0x0032};
1542 static const uint8_t expected1
[] = {0x7E, 0x7B, 0x52, 0x3B,
1544 0x7E, 0x7B, 0x36, 0x21,
1550 static const int32_t offsets1
[] = {2,6,9,13,14,15
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
,
1563 log_err("hz -> did not match.\n");
1566 log_verbose("Testing for UTF8\n");
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,
1572 static const uint8_t expected
[] = {0xe4, 0xb8, 0x80, 0xdc, 0x81, 0x31,
1573 0xeb, 0xbf, 0x81, 0xF0, 0x90, 0x90, 0x81, 0x32};
1576 static const int32_t fromOffsets
[] = { 0x0000, 0x0003, 0x0005, 0x0006, 0x0009, 0x0009, 0x000D };
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");
1607 /* Test that U_TRUNCATED_CHAR_FOUND is set. */
1609 doTestTruncated(const char *cnvName
, const uint8_t *bytes
, int32_t length
) {
1613 UChar
*target
, *targetLimit
;
1614 const char *source
, *sourceLimit
;
1616 UErrorCode errorCode
;
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
));
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
));
1632 source
=(const char *)bytes
;
1633 sourceLimit
=source
+length
;
1635 targetLimit
=buffer
+LENGTHOF(buffer
);
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
));
1644 errorCode
=U_ZERO_ERROR
;
1647 ucnv_toUnicode(cnv
, &target
, targetLimit
, &source
, sourceLimit
, NULL
, TRUE
, &errorCode
);
1648 if(errorCode
!=U_TRUNCATED_CHAR_FOUND
|| target
!=buffer
) {
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
));
1653 /* 2. input bytes with flush=TRUE */
1654 ucnv_resetToUnicode(cnv
);
1656 errorCode
=U_ZERO_ERROR
;
1657 source
=(const char *)bytes
;
1659 ucnv_toUnicode(cnv
, &target
, targetLimit
, &source
, sourceLimit
, NULL
, TRUE
, &errorCode
);
1660 if(errorCode
!=U_TRUNCATED_CHAR_FOUND
|| source
!=sourceLimit
|| target
!=buffer
) {
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
));
1671 static const struct {
1672 const char *cnvName
;
1673 uint8_t bytes
[8]; /* partial input bytes resulting in no output */
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 */
1681 { "UTF-7", { 0x2b, 0x42 }, 2 }, /* +B */
1682 { "UTF-8", { 0xd1 }, 1 },
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 },
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 },
1696 { "SCSU", { 0x0e, 0x4e }, 2 }, /* SQU 0x4e */
1697 { "BOCU-1", { 0xd5 }, 1 },
1699 { "Shift-JIS", { 0xe0 }, 1 },
1701 { "ibm-939", { 0x0e, 0x41 }, 2 } /* SO 0x41 */
1705 for(i
=0; i
<LENGTHOF(testCases
); ++i
) {
1706 doTestTruncated(testCases
[i
].cnvName
, testCases
[i
].bytes
, testCases
[i
].length
);
1710 typedef struct NameRange
{
1712 UChar32 start
, end
, start2
, end2
, notStart
, notEnd
;
1717 UErrorCode errorCode
;
1723 static const char *const completeSetNames
[]={
1739 static const char *const lmbcsNames
[]={
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 }
1764 /* open an empty set */
1765 set
=uset_open(1, 0);
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
));
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
);
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
));
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
);
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
;
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
));
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
);
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
));
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
));
1853 !uset_containsRange(set
, nameRanges
[i
].start
, nameRanges
[i
].end
) ||
1854 (nameRanges
[i
].start2
>=0 && !uset_containsRange(set
, nameRanges
[i
].start2
, nameRanges
[i
].end2
))
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
);