1 /********************************************************************
3 * Copyright (c) 1997-2016, 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"
26 #define MAX_LENGTH 999
28 #define UNICODE_LIMIT 0x10FFFF
29 #define SURROGATE_HIGH_START 0xD800
30 #define SURROGATE_LOW_END 0xDFFF
32 static int32_t gInBufferSize
= 0;
33 static int32_t gOutBufferSize
= 0;
34 static char gNuConvTestName
[1024];
36 #define nct_min(x,y) ((x<y) ? x : y)
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);
65 #if !UCONFIG_NO_LEGACY_CONVERSION
66 static void TestToUnicodeErrorBehaviour(void);
67 static void TestGetNextErrorBehaviour(void);
70 static void TestRegressionUTF8(void);
71 static void TestRegressionUTF32(void);
72 static void TestAvailableConverters(void);
73 static void TestFlushInternalBuffer(void); /*for improved code coverage in ucnv_cnv.c*/
74 static void TestResetBehaviour(void);
75 static void TestTruncated(void);
76 static void TestUnicodeSet(void);
78 static void TestWithBufferSize(int32_t osize
, int32_t isize
);
81 static void printSeq(const unsigned char* a
, int len
)
86 log_verbose("0x%02X ", a
[i
++]);
90 static void printUSeq(const UChar
* a
, int len
)
95 log_verbose("%0x04X ", a
[i
++]);
99 static void printSeqErr(const unsigned char* a
, int len
)
102 fprintf(stderr
, "\n{");
103 while (i
<len
) fprintf(stderr
, "0x%02X ", a
[i
++]);
104 fprintf(stderr
, "}\n");
107 static void printUSeqErr(const UChar
* a
, int len
)
110 fprintf(stderr
, "\n{");
112 fprintf(stderr
, "0x%04X ", a
[i
++]);
113 fprintf(stderr
,"}\n");
116 void addExtraTests(TestNode
** root
);
118 void addExtraTests(TestNode
** root
)
120 addTest(root
, &TestSurrogateBehaviour
, "tsconv/ncnvtst/TestSurrogateBehaviour");
121 addTest(root
, &TestErrorBehaviour
, "tsconv/ncnvtst/TestErrorBehaviour");
123 #if !UCONFIG_NO_LEGACY_CONVERSION
124 addTest(root
, &TestToUnicodeErrorBehaviour
, "tsconv/ncnvtst/ToUnicodeErrorBehaviour");
125 addTest(root
, &TestGetNextErrorBehaviour
, "tsconv/ncnvtst/TestGetNextErrorBehaviour");
128 addTest(root
, &TestAvailableConverters
, "tsconv/ncnvtst/TestAvailableConverters");
129 addTest(root
, &TestFlushInternalBuffer
, "tsconv/ncnvtst/TestFlushInternalBuffer");
130 addTest(root
, &TestResetBehaviour
, "tsconv/ncnvtst/TestResetBehaviour");
131 addTest(root
, &TestRegressionUTF8
, "tsconv/ncnvtst/TestRegressionUTF8");
132 addTest(root
, &TestRegressionUTF32
, "tsconv/ncnvtst/TestRegressionUTF32");
133 addTest(root
, &TestTruncated
, "tsconv/ncnvtst/TestTruncated");
134 addTest(root
, &TestUnicodeSet
, "tsconv/ncnvtst/TestUnicodeSet");
137 /*test surrogate behaviour*/
138 static void TestSurrogateBehaviour(){
139 log_verbose("Testing for SBCS and LATIN_1\n");
141 UChar sampleText
[] = {0x0031, 0xd801, 0xdc01, 0x0032};
142 const uint8_t expected
[] = {0x31, 0x1a, 0x32};
144 #if !UCONFIG_NO_LEGACY_CONVERSION
146 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
147 expected
, sizeof(expected
), "ibm-920", 0 , TRUE
, U_ZERO_ERROR
))
148 log_err("u-> ibm-920 [UCNV_SBCS] not match.\n");
152 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
153 expected
, sizeof(expected
), "LATIN_1", 0, TRUE
, U_ZERO_ERROR
))
154 log_err("u-> LATIN_1 not match.\n");
158 #if !UCONFIG_NO_LEGACY_CONVERSION
159 log_verbose("Testing for DBCS and MBCS\n");
161 UChar sampleText
[] = {0x00a1, 0xd801, 0xdc01, 0x00a4};
162 const uint8_t expected
[] = {0xa2, 0xae, 0xa1, 0xe0, 0xa2, 0xb4};
163 int32_t offsets
[] = {0x00, 0x00, 0x01, 0x01, 0x03, 0x03 };
166 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
167 expected
, sizeof(expected
), "ibm-1363", 0 , TRUE
, U_ZERO_ERROR
))
168 log_err("u-> ibm-1363 [UCNV_DBCS portion] not match.\n");
169 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
170 expected
, sizeof(expected
), "ibm-1363", offsets
, TRUE
, U_ZERO_ERROR
))
171 log_err("u-> ibm-1363 [UCNV_DBCS portion] not match.\n");
173 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
174 expected
, sizeof(expected
), "ibm-1363", 0 , TRUE
, U_ZERO_ERROR
))
175 log_err("u-> ibm-1363 [UCNV_MBCS] not match.\n");
176 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
177 expected
, sizeof(expected
), "ibm-1363", offsets
, TRUE
, U_ZERO_ERROR
))
178 log_err("u-> ibm-1363 [UCNV_MBCS] not match.\n");
181 log_verbose("Testing for ISO-2022-jp\n");
183 UChar sampleText
[] = { 0x4e00, 0x04e01, 0x0031, 0xd801, 0xdc01, 0x0032};
185 const uint8_t expected
[] = {0x1b, 0x24, 0x42,0x30,0x6c,0x43,0x7a,0x1b,0x28,0x42,
189 int32_t offsets
[] = {0,0,0,0,0,1,1,2,2,2,2,3,5 };
192 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
193 expected
, sizeof(expected
), "iso-2022-jp", 0 , TRUE
, U_ZERO_ERROR
))
194 log_err("u-> not match.\n");
195 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
196 expected
, sizeof(expected
), "iso-2022-jp", offsets
, TRUE
, U_ZERO_ERROR
))
197 log_err("u-> not match.\n");
200 log_verbose("Testing for ISO-2022-cn\n");
202 static const UChar sampleText
[] = { 0x4e00, 0x04e01, 0x0031, 0xd801, 0xdc01, 0x0032};
204 static const uint8_t expected
[] = {
205 0x1B, 0x24, 0x29, 0x41, 0x0E, 0x52, 0x3B,
214 static const int32_t offsets
[] = {
222 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
223 expected
, sizeof(expected
), "iso-2022-cn", 0 , TRUE
, U_ZERO_ERROR
))
224 log_err("u-> not match.\n");
225 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
226 expected
, sizeof(expected
), "iso-2022-cn", offsets
, TRUE
, U_ZERO_ERROR
))
227 log_err("u-> not match.\n");
230 log_verbose("Testing for ISO-2022-kr\n");
232 static const UChar sampleText
[] = { 0x4e00,0xd801, 0xdc01, 0x04e01, 0x0031, 0xd801, 0xdc01, 0x0032};
234 static const uint8_t expected
[] = {0x1B, 0x24, 0x29, 0x43,
242 static const int32_t offsets
[] = {-1, -1, -1, -1,
252 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
253 expected
, sizeof(expected
), "iso-2022-kr", 0 , TRUE
, U_ZERO_ERROR
))
254 log_err("u-> iso-2022-kr [UCNV_DBCS] not match.\n");
255 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
256 expected
, sizeof(expected
), "iso-2022-kr", offsets
, TRUE
, U_ZERO_ERROR
))
257 log_err("u-> iso-2022-kr [UCNV_DBCS] not match.\n");
260 log_verbose("Testing for HZ\n");
262 static const UChar sampleText
[] = { 0x4e00, 0xd801, 0xdc01, 0x04e01, 0x0031, 0xd801, 0xdc01, 0x0032};
264 static const uint8_t expected
[] = {0x7E, 0x7B, 0x52, 0x3B,
266 0x7E, 0x7B, 0x36, 0x21,
272 static const int32_t offsets
[] = {0,0,0,0,
280 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
281 expected
, sizeof(expected
), "HZ", 0 , TRUE
, U_ZERO_ERROR
))
282 log_err("u-> HZ not match.\n");
283 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
284 expected
, sizeof(expected
), "HZ", offsets
, TRUE
, U_ZERO_ERROR
))
285 log_err("u-> HZ not match.\n");
290 log_verbose("Testing for UTF8\n");
292 static const UChar sampleText
[] = { 0x4e00, 0x0701, 0x0031, 0xbfc1, 0xd801, 0xdc01, 0x0032};
293 static const int32_t offsets
[]={0x00, 0x00, 0x00, 0x01, 0x01, 0x02,
294 0x03, 0x03, 0x03, 0x04, 0x04, 0x04,
296 static const uint8_t expected
[] = {0xe4, 0xb8, 0x80, 0xdc, 0x81, 0x31,
297 0xeb, 0xbf, 0x81, 0xF0, 0x90, 0x90, 0x81, 0x32};
300 static const int32_t fromOffsets
[] = { 0x0000, 0x0003, 0x0005, 0x0006, 0x0009, 0x0009, 0x000D };
302 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
303 expected
, sizeof(expected
), "UTF8", offsets
, TRUE
, U_ZERO_ERROR
))
304 log_err("u-> UTF8 with offsets and flush true did not match.\n");
305 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
306 expected
, sizeof(expected
), "UTF8", 0, TRUE
, U_ZERO_ERROR
))
307 log_err("u-> UTF8 with offsets and flush true did not match.\n");
308 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
309 expected
, sizeof(expected
), "UTF8", offsets
, FALSE
, U_ZERO_ERROR
))
310 log_err("u-> UTF8 with offsets and flush true did not match.\n");
311 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
312 expected
, sizeof(expected
), "UTF8", 0, FALSE
, U_ZERO_ERROR
))
313 log_err("u-> UTF8 with offsets and flush true did not match.\n");
315 if(!convertToU(expected
, sizeof(expected
),
316 sampleText
, UPRV_LENGTHOF(sampleText
), "UTF8", 0, TRUE
, U_ZERO_ERROR
))
317 log_err("UTF8 -> u did not match.\n");
318 if(!convertToU(expected
, sizeof(expected
),
319 sampleText
, UPRV_LENGTHOF(sampleText
), "UTF8", 0, FALSE
, U_ZERO_ERROR
))
320 log_err("UTF8 -> u did not match.\n");
321 if(!convertToU(expected
, sizeof(expected
),
322 sampleText
, UPRV_LENGTHOF(sampleText
), "UTF8", fromOffsets
, TRUE
, U_ZERO_ERROR
))
323 log_err("UTF8 ->u did not match.\n");
324 if(!convertToU(expected
, sizeof(expected
),
325 sampleText
, UPRV_LENGTHOF(sampleText
), "UTF8", fromOffsets
, FALSE
, U_ZERO_ERROR
))
326 log_err("UTF8 -> u did not match.\n");
331 /*test various error behaviours*/
332 static void TestErrorBehaviour(){
333 log_verbose("Testing for SBCS and LATIN_1\n");
335 static const UChar sampleText
[] = { 0x0031, 0xd801};
336 static const UChar sampleText2
[] = { 0x0031, 0xd801, 0x0032};
337 static const uint8_t expected0
[] = { 0x31};
338 static const uint8_t expected
[] = { 0x31, 0x1a};
339 static const uint8_t expected2
[] = { 0x31, 0x1a, 0x32};
341 #if !UCONFIG_NO_LEGACY_CONVERSION
343 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
344 expected
, sizeof(expected
), "ibm-920", 0, TRUE
, U_ZERO_ERROR
))
345 log_err("u-> ibm-920 [UCNV_SBCS] \n");
346 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
347 expected0
, sizeof(expected0
), "ibm-920", 0, FALSE
, U_ZERO_ERROR
))
348 log_err("u-> ibm-920 [UCNV_SBCS] \n");
349 if(!convertFromU(sampleText2
, UPRV_LENGTHOF(sampleText2
),
350 expected2
, sizeof(expected2
), "ibm-920", 0, TRUE
, U_ZERO_ERROR
))
351 log_err("u-> ibm-920 [UCNV_SBCS] did not match\n");
355 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
356 expected
, sizeof(expected
), "LATIN_1", 0, TRUE
, U_ZERO_ERROR
))
357 log_err("u-> LATIN_1 is supposed to fail\n");
358 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
359 expected0
, sizeof(expected0
), "LATIN_1", 0, FALSE
, U_ZERO_ERROR
))
360 log_err("u-> LATIN_1 is supposed to fail\n");
362 if(!convertFromU(sampleText2
, UPRV_LENGTHOF(sampleText2
),
363 expected2
, sizeof(expected2
), "LATIN_1", 0, TRUE
, U_ZERO_ERROR
))
364 log_err("u-> LATIN_1 did not match\n");
367 #if !UCONFIG_NO_LEGACY_CONVERSION
368 log_verbose("Testing for DBCS and MBCS\n");
370 static const UChar sampleText
[] = { 0x00a1, 0xd801};
371 static const uint8_t expected
[] = { 0xa2, 0xae};
372 static const int32_t offsets
[] = { 0x00, 0x00};
373 static const uint8_t expectedSUB
[] = { 0xa2, 0xae, 0xa1, 0xe0};
374 static const int32_t offsetsSUB
[] = { 0x00, 0x00, 0x01, 0x01};
376 static const UChar sampleText2
[] = { 0x00a1, 0xd801, 0x00a4};
377 static const uint8_t expected2
[] = { 0xa2, 0xae, 0xa1, 0xe0, 0xa2, 0xb4};
378 static const int32_t offsets2
[] = { 0x00, 0x00, 0x01, 0x01, 0x02, 0x02};
380 static const UChar sampleText3MBCS
[] = { 0x0001, 0x00a4, 0xdc01};
381 static const uint8_t expected3MBCS
[] = { 0x01, 0xa2, 0xb4, 0xa1, 0xe0};
382 static const int32_t offsets3MBCS
[] = { 0x00, 0x01, 0x01, 0x02, 0x02};
384 static const UChar sampleText4MBCS
[] = { 0x0061, 0xFFE4, 0xdc01};
385 static const uint8_t expected4MBCS
[] = { 0x61, 0x8f, 0xa2, 0xc3, 0xf4, 0xfe};
386 static const int32_t offsets4MBCS
[] = { 0x00, 0x01, 0x01, 0x01, 0x02, 0x02 };
389 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
390 expectedSUB
, sizeof(expectedSUB
), "ibm-1363", 0, TRUE
, U_ZERO_ERROR
))
391 log_err("u-> ibm-1363 [UCNV_DBCS portion] is supposed to fail\n");
392 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
393 expected
, sizeof(expected
), "ibm-1363", 0, FALSE
, U_AMBIGUOUS_ALIAS_WARNING
))
394 log_err("u-> ibm-1363 [UCNV_DBCS portion] is supposed to fail\n");
396 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
397 expectedSUB
, sizeof(expectedSUB
), "ibm-1363", offsetsSUB
, TRUE
, U_ZERO_ERROR
))
398 log_err("u-> ibm-1363 [UCNV_DBCS portion] is supposed to fail\n");
399 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
400 expected
, sizeof(expected
), "ibm-1363", offsets
, FALSE
, U_AMBIGUOUS_ALIAS_WARNING
))
401 log_err("u-> ibm-1363 [UCNV_DBCS portion] is supposed to fail\n");
404 if(!convertFromU(sampleText2
, UPRV_LENGTHOF(sampleText2
),
405 expected2
, sizeof(expected2
), "ibm-1363", 0, TRUE
, U_ZERO_ERROR
))
406 log_err("u-> ibm-1363 [UCNV_DBCS portion] did not match \n");
407 if(!convertFromU(sampleText2
, UPRV_LENGTHOF(sampleText2
),
408 expected2
, sizeof(expected2
), "ibm-1363", offsets2
, TRUE
, U_ZERO_ERROR
))
409 log_err("u-> ibm-1363 [UCNV_DBCS portion] did not match \n");
412 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
413 expectedSUB
, sizeof(expectedSUB
), "ibm-1363", 0, TRUE
, U_ZERO_ERROR
))
414 log_err("u-> ibm-1363 [UCNV_MBCS] \n");
415 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
416 expected
, sizeof(expected
), "ibm-1363", 0, FALSE
, U_AMBIGUOUS_ALIAS_WARNING
))
417 log_err("u-> ibm-1363 [UCNV_MBCS] \n");
419 if(!convertFromU(sampleText2
, UPRV_LENGTHOF(sampleText2
),
420 expected2
, sizeof(expected2
), "ibm-1363", 0, TRUE
, U_ZERO_ERROR
))
421 log_err("u-> ibm-1363 [UCNV_DBCS] did not match\n");
422 if(!convertFromU(sampleText2
, UPRV_LENGTHOF(sampleText2
),
423 expected2
, sizeof(expected2
), "ibm-1363", 0, FALSE
, U_ZERO_ERROR
))
424 log_err("u-> ibm-1363 [UCNV_DBCS] did not match\n");
425 if(!convertFromU(sampleText2
, UPRV_LENGTHOF(sampleText2
),
426 expected2
, sizeof(expected2
), "ibm-1363", offsets2
, FALSE
, U_ZERO_ERROR
))
427 log_err("u-> ibm-1363 [UCNV_DBCS] did not match\n");
429 if(!convertFromU(sampleText3MBCS
, UPRV_LENGTHOF(sampleText3MBCS
),
430 expected3MBCS
, sizeof(expected3MBCS
), "ibm-1363", offsets3MBCS
, TRUE
, U_ZERO_ERROR
))
431 log_err("u-> ibm-1363 [UCNV_MBCS] \n");
432 if(!convertFromU(sampleText3MBCS
, UPRV_LENGTHOF(sampleText3MBCS
),
433 expected3MBCS
, sizeof(expected3MBCS
), "ibm-1363", offsets3MBCS
, FALSE
, U_ZERO_ERROR
))
434 log_err("u-> ibm-1363 [UCNV_MBCS] \n");
436 if(!convertFromU(sampleText4MBCS
, UPRV_LENGTHOF(sampleText4MBCS
),
437 expected4MBCS
, sizeof(expected4MBCS
), "IBM-eucJP", offsets4MBCS
, TRUE
, U_ZERO_ERROR
))
438 log_err("u-> euc-jp [UCNV_MBCS] \n");
439 if(!convertFromU(sampleText4MBCS
, UPRV_LENGTHOF(sampleText4MBCS
),
440 expected4MBCS
, sizeof(expected4MBCS
), "IBM-eucJP", offsets4MBCS
, FALSE
, U_ZERO_ERROR
))
441 log_err("u-> euc-jp [UCNV_MBCS] \n");
445 log_verbose("Testing for iso-2022-jp\n");
447 static const UChar sampleText
[] = { 0x0031, 0xd801};
448 static const uint8_t expected
[] = { 0x31};
449 static const uint8_t expectedSUB
[] = { 0x31, 0x1a};
450 static const int32_t offsets
[] = { 0x00, 1};
452 static const UChar sampleText2
[] = { 0x0031, 0xd801, 0x0032};
453 static const uint8_t expected2
[] = { 0x31,0x1A,0x32};
454 static const int32_t offsets2
[] = { 0x00,0x01,0x02};
456 static const UChar sampleText4MBCS
[] = { 0x0061, 0x4e00, 0xdc01};
457 static const uint8_t expected4MBCS
[] = { 0x61, 0x1b, 0x24, 0x42, 0x30, 0x6c,0x1b,0x28,0x42,0x1a};
458 static const int32_t offsets4MBCS
[] = { 0x00, 0x01, 0x01 ,0x01, 0x01, 0x01,0x02,0x02,0x02,0x02 };
459 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
460 expectedSUB
, sizeof(expectedSUB
), "iso-2022-jp", offsets
, TRUE
, U_ZERO_ERROR
))
461 log_err("u-> iso-2022-jp [UCNV_MBCS] \n");
462 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
463 expected
, sizeof(expected
), "iso-2022-jp", offsets
, FALSE
, U_AMBIGUOUS_ALIAS_WARNING
))
464 log_err("u-> iso-2022-jp [UCNV_MBCS] \n");
466 if(!convertFromU(sampleText2
, UPRV_LENGTHOF(sampleText2
),
467 expected2
, sizeof(expected2
), "iso-2022-jp", offsets2
, TRUE
, U_ZERO_ERROR
))
468 log_err("u->iso-2022-jp[UCNV_DBCS] did not match\n");
469 if(!convertFromU(sampleText2
, UPRV_LENGTHOF(sampleText2
),
470 expected2
, sizeof(expected2
), "iso-2022-jp", offsets2
, FALSE
, U_ZERO_ERROR
))
471 log_err("u-> iso-2022-jp [UCNV_DBCS] did not match\n");
472 if(!convertFromU(sampleText2
, UPRV_LENGTHOF(sampleText2
),
473 expected2
, sizeof(expected2
), "iso-2022-jp", offsets2
, FALSE
, U_ZERO_ERROR
))
474 log_err("u-> iso-2022-jp [UCNV_DBCS] did not match\n");
476 if(!convertFromU(sampleText4MBCS
, UPRV_LENGTHOF(sampleText4MBCS
),
477 expected4MBCS
, sizeof(expected4MBCS
), "iso-2022-jp", offsets4MBCS
, TRUE
, U_ZERO_ERROR
))
478 log_err("u-> iso-2022-jp [UCNV_MBCS] \n");
479 if(!convertFromU(sampleText4MBCS
, UPRV_LENGTHOF(sampleText4MBCS
),
480 expected4MBCS
, sizeof(expected4MBCS
), "iso-2022-jp", offsets4MBCS
, FALSE
, U_ZERO_ERROR
))
481 log_err("u-> iso-2022-jp [UCNV_MBCS] \n");
485 log_verbose("Testing for iso-2022-cn\n");
487 static const UChar sampleText
[] = { 0x0031, 0xd801};
488 static const uint8_t expected
[] = { 0x31};
489 static const uint8_t expectedSUB
[] = { 0x31, 0x1A};
490 static const int32_t offsets
[] = { 0x00, 1};
492 static const UChar sampleText2
[] = { 0x0031, 0xd801, 0x0032};
493 static const uint8_t expected2
[] = { 0x31, 0x1A,0x32};
494 static const int32_t offsets2
[] = { 0x00, 0x01,0x02};
496 static const UChar sampleText3MBCS
[] = { 0x0051, 0x0050, 0xdc01};
497 static const uint8_t expected3MBCS
[] = {0x51, 0x50, 0x1A};
498 static const int32_t offsets3MBCS
[] = { 0x00, 0x01, 0x02 };
500 static const UChar sampleText4MBCS
[] = { 0x0061, 0x4e00, 0xdc01};
501 static const uint8_t expected4MBCS
[] = { 0x61, 0x1b, 0x24, 0x29, 0x41, 0x0e, 0x52, 0x3b, 0x0f, 0x1a };
502 static const int32_t offsets4MBCS
[] = { 0x00, 0x01, 0x01 ,0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02 };
503 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
504 expectedSUB
, sizeof(expectedSUB
), "iso-2022-cn", offsets
, TRUE
, U_ZERO_ERROR
))
505 log_err("u-> iso-2022-cn [UCNV_MBCS] \n");
506 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
507 expected
, sizeof(expected
), "iso-2022-cn", offsets
, FALSE
, U_ZERO_ERROR
))
508 log_err("u-> ibm-1363 [UCNV_MBCS] \n");
510 if(!convertFromU(sampleText2
, UPRV_LENGTHOF(sampleText2
),
511 expected2
, sizeof(expected2
), "iso-2022-cn", offsets2
, TRUE
, U_ZERO_ERROR
))
512 log_err("u->iso-2022-cn[UCNV_DBCS] did not match\n");
513 if(!convertFromU(sampleText2
, UPRV_LENGTHOF(sampleText2
),
514 expected2
, sizeof(expected2
), "iso-2022-cn", offsets2
, FALSE
, U_ZERO_ERROR
))
515 log_err("u-> iso-2022-cn [UCNV_DBCS] did not match\n");
516 if(!convertFromU(sampleText2
, UPRV_LENGTHOF(sampleText2
),
517 expected2
, sizeof(expected2
), "iso-2022-cn", offsets2
, FALSE
, U_ZERO_ERROR
))
518 log_err("u-> iso-2022-cn [UCNV_DBCS] did not match\n");
520 if(!convertFromU(sampleText3MBCS
, UPRV_LENGTHOF(sampleText3MBCS
),
521 expected3MBCS
, sizeof(expected3MBCS
), "iso-2022-cn", offsets3MBCS
, TRUE
, U_ZERO_ERROR
))
522 log_err("u->iso-2022-cn [UCNV_MBCS] \n");
523 if(!convertFromU(sampleText3MBCS
, UPRV_LENGTHOF(sampleText3MBCS
),
524 expected3MBCS
, sizeof(expected3MBCS
), "iso-2022-cn", offsets3MBCS
, FALSE
, U_ZERO_ERROR
))
525 log_err("u-> iso-2022-cn[UCNV_MBCS] \n");
527 if(!convertFromU(sampleText4MBCS
, UPRV_LENGTHOF(sampleText4MBCS
),
528 expected4MBCS
, sizeof(expected4MBCS
), "iso-2022-cn", offsets4MBCS
, TRUE
, U_ZERO_ERROR
))
529 log_err("u-> iso-2022-cn [UCNV_MBCS] \n");
530 if(!convertFromU(sampleText4MBCS
, UPRV_LENGTHOF(sampleText4MBCS
),
531 expected4MBCS
, sizeof(expected4MBCS
), "iso-2022-cn", offsets4MBCS
, FALSE
, U_ZERO_ERROR
))
532 log_err("u-> iso-2022-cn [UCNV_MBCS] \n");
536 log_verbose("Testing for iso-2022-kr\n");
538 static const UChar sampleText
[] = { 0x0031, 0xd801};
539 static const uint8_t expected
[] = { 0x1b, 0x24, 0x29, 0x43, 0x31};
540 static const uint8_t expectedSUB
[] = { 0x1b, 0x24, 0x29, 0x43, 0x31, 0x1A};
541 static const int32_t offsets
[] = { -1, -1, -1, -1, 0x00, 1};
543 static const UChar sampleText2
[] = { 0x0031, 0xd801, 0x0032};
544 static const uint8_t expected2
[] = { 0x1b, 0x24, 0x29, 0x43, 0x31, 0x1A, 0x32};
545 static const int32_t offsets2
[] = { -1, -1, -1, -1, 0x00, 0x01, 0x02};
547 static const UChar sampleText3MBCS
[] = { 0x0051, 0x0050, 0xdc01};
548 static const uint8_t expected3MBCS
[] = { 0x1b, 0x24, 0x29, 0x43, 0x51, 0x50, 0x1A };
549 static const int32_t offsets3MBCS
[] = { -1, -1, -1, -1, 0x00, 0x01, 0x02, 0x02 };
551 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
552 expectedSUB
, sizeof(expectedSUB
), "iso-2022-kr", offsets
, TRUE
, U_ZERO_ERROR
))
553 log_err("u-> iso-2022-kr [UCNV_MBCS] \n");
554 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
555 expected
, sizeof(expected
), "iso-2022-kr", offsets
, FALSE
, U_ZERO_ERROR
))
556 log_err("u-> ibm-1363 [UCNV_MBCS] \n");
558 if(!convertFromU(sampleText2
, UPRV_LENGTHOF(sampleText2
),
559 expected2
, sizeof(expected2
), "iso-2022-kr", offsets2
, TRUE
, U_ZERO_ERROR
))
560 log_err("u->iso-2022-kr[UCNV_DBCS] did not match\n");
561 if(!convertFromU(sampleText2
, UPRV_LENGTHOF(sampleText2
),
562 expected2
, sizeof(expected2
), "iso-2022-kr", offsets2
, FALSE
, U_ZERO_ERROR
))
563 log_err("u-> iso-2022-kr [UCNV_DBCS] did not match\n");
564 if(!convertFromU(sampleText2
, UPRV_LENGTHOF(sampleText2
),
565 expected2
, sizeof(expected2
), "iso-2022-kr", offsets2
, FALSE
, U_ZERO_ERROR
))
566 log_err("u-> iso-2022-kr [UCNV_DBCS] did not match\n");
568 if(!convertFromU(sampleText3MBCS
, UPRV_LENGTHOF(sampleText3MBCS
),
569 expected3MBCS
, sizeof(expected3MBCS
), "iso-2022-kr", offsets3MBCS
, TRUE
, U_ZERO_ERROR
))
570 log_err("u->iso-2022-kr [UCNV_MBCS] \n");
571 if(!convertFromU(sampleText3MBCS
, UPRV_LENGTHOF(sampleText3MBCS
),
572 expected3MBCS
, sizeof(expected3MBCS
), "iso-2022-kr", offsets3MBCS
, FALSE
, U_ZERO_ERROR
))
573 log_err("u-> iso-2022-kr[UCNV_MBCS] \n");
577 log_verbose("Testing for HZ\n");
579 static const UChar sampleText
[] = { 0x0031, 0xd801};
580 static const uint8_t expected
[] = { 0x7e, 0x7d, 0x31};
581 static const uint8_t expectedSUB
[] = { 0x7e, 0x7d, 0x31, 0x1A};
582 static const int32_t offsets
[] = { 0x00, 0x00, 0x00, 1};
584 static const UChar sampleText2
[] = { 0x0031, 0xd801, 0x0032};
585 static const uint8_t expected2
[] = { 0x7e, 0x7d, 0x31, 0x1A, 0x32 };
586 static const int32_t offsets2
[] = { 0x00, 0x00, 0x00, 0x01, 0x02 };
588 static const UChar sampleText3MBCS
[] = { 0x0051, 0x0050, 0xdc01};
589 static const uint8_t expected3MBCS
[] = { 0x7e, 0x7d, 0x51, 0x50, 0x1A };
590 static const int32_t offsets3MBCS
[] = { 0x00, 0x00, 0x00, 0x01, 0x02};
592 static const UChar sampleText4MBCS
[] = { 0x0061, 0x4e00, 0xdc01};
593 static const uint8_t expected4MBCS
[] = { 0x7e, 0x7d, 0x61, 0x7e, 0x7b, 0x52, 0x3b, 0x7e, 0x7d, 0x1a };
594 static const int32_t offsets4MBCS
[] = { 0x00, 0x00, 0x00, 0x01, 0x01, 0x01 ,0x01, 0x02, 0x02, 0x02 };
595 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
596 expectedSUB
, sizeof(expectedSUB
), "HZ", offsets
, TRUE
, U_ZERO_ERROR
))
597 log_err("u-> HZ [UCNV_MBCS] \n");
598 if(!convertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
599 expected
, sizeof(expected
), "HZ", offsets
, FALSE
, U_ZERO_ERROR
))
600 log_err("u-> ibm-1363 [UCNV_MBCS] \n");
602 if(!convertFromU(sampleText2
, UPRV_LENGTHOF(sampleText2
),
603 expected2
, sizeof(expected2
), "HZ", offsets2
, TRUE
, U_ZERO_ERROR
))
604 log_err("u->HZ[UCNV_DBCS] did not match\n");
605 if(!convertFromU(sampleText2
, UPRV_LENGTHOF(sampleText2
),
606 expected2
, sizeof(expected2
), "HZ", offsets2
, FALSE
, U_ZERO_ERROR
))
607 log_err("u-> HZ [UCNV_DBCS] did not match\n");
608 if(!convertFromU(sampleText2
, UPRV_LENGTHOF(sampleText2
),
609 expected2
, sizeof(expected2
), "HZ", offsets2
, FALSE
, U_ZERO_ERROR
))
610 log_err("u-> HZ [UCNV_DBCS] did not match\n");
612 if(!convertFromU(sampleText3MBCS
, UPRV_LENGTHOF(sampleText3MBCS
),
613 expected3MBCS
, sizeof(expected3MBCS
), "HZ", offsets3MBCS
, TRUE
, U_ZERO_ERROR
))
614 log_err("u->HZ [UCNV_MBCS] \n");
615 if(!convertFromU(sampleText3MBCS
, UPRV_LENGTHOF(sampleText3MBCS
),
616 expected3MBCS
, sizeof(expected3MBCS
), "HZ", offsets3MBCS
, FALSE
, U_ZERO_ERROR
))
617 log_err("u-> HZ[UCNV_MBCS] \n");
619 if(!convertFromU(sampleText4MBCS
, UPRV_LENGTHOF(sampleText4MBCS
),
620 expected4MBCS
, sizeof(expected4MBCS
), "HZ", offsets4MBCS
, TRUE
, U_ZERO_ERROR
))
621 log_err("u-> HZ [UCNV_MBCS] \n");
622 if(!convertFromU(sampleText4MBCS
, UPRV_LENGTHOF(sampleText4MBCS
),
623 expected4MBCS
, sizeof(expected4MBCS
), "HZ", offsets4MBCS
, FALSE
, U_ZERO_ERROR
))
624 log_err("u-> HZ [UCNV_MBCS] \n");
629 #if !UCONFIG_NO_LEGACY_CONVERSION
630 /*test different convertToUnicode error behaviours*/
631 static void TestToUnicodeErrorBehaviour()
633 log_verbose("Testing error conditions for DBCS\n");
635 uint8_t sampleText
[] = { 0xa2, 0xae, 0x03, 0x04};
636 const UChar expected
[] = { 0x00a1 };
638 if(!convertToU(sampleText
, sizeof(sampleText
),
639 expected
, UPRV_LENGTHOF(expected
), "ibm-1363", 0, TRUE
, U_AMBIGUOUS_ALIAS_WARNING
))
640 log_err("DBCS (ibm-1363)->Unicode did not match.\n");
641 if(!convertToU(sampleText
, sizeof(sampleText
),
642 expected
, UPRV_LENGTHOF(expected
), "ibm-1363", 0, FALSE
, U_AMBIGUOUS_ALIAS_WARNING
))
643 log_err("DBCS (ibm-1363)->Unicode with flush = false did not match.\n");
645 log_verbose("Testing error conditions for SBCS\n");
647 uint8_t sampleText
[] = { 0xa2, 0xFF};
648 const UChar expected
[] = { 0x00c2 };
650 /* uint8_t sampleText2[] = { 0xa2, 0x70 };
651 const UChar expected2[] = { 0x0073 };*/
653 if(!convertToU(sampleText
, sizeof(sampleText
),
654 expected
, UPRV_LENGTHOF(expected
), "ibm-1051", 0, TRUE
, U_ZERO_ERROR
))
655 log_err("SBCS (ibm-1051)->Unicode did not match.\n");
656 if(!convertToU(sampleText
, sizeof(sampleText
),
657 expected
, UPRV_LENGTHOF(expected
), "ibm-1051", 0, FALSE
, U_ZERO_ERROR
))
658 log_err("SBCS (ibm-1051)->Unicode with flush = false did not match.\n");
663 static void TestGetNextErrorBehaviour(){
664 /*Test for unassigned character*/
666 static const char input1
[INPUT_SIZE
]={ 0x70 };
667 const char* source
=(const char*)input1
;
668 UErrorCode err
=U_ZERO_ERROR
;
670 UConverter
*cnv
=ucnv_open("ibm-424", &err
);
672 log_data_err("Unable to open a SBCS(ibm-424) converter: %s\n", u_errorName(err
));
675 c
=ucnv_getNextUChar(cnv
, &source
, source
+ INPUT_SIZE
, &err
);
676 if(err
!= U_INVALID_CHAR_FOUND
&& c
!=0xfffd){
677 log_err("FAIL in TestGetNextErrorBehaviour(unassigned): Expected: U_INVALID_CHAR_ERROR or 0xfffd ----Got:%s and 0x%lx\n", myErrorName(err
), c
);
683 #define MAX_UTF16_LEN 2
684 #define MAX_UTF8_LEN 4
686 /*Regression test for utf8 converter*/
687 static void TestRegressionUTF8(){
691 UChar
*standardForm
= (UChar
*)malloc(MAX_LENGTH
*sizeof(UChar
));
692 uint8_t *utf8
= (uint8_t*)malloc(MAX_LENGTH
);
694 while (currCh
<= UNICODE_LIMIT
) {
697 while(currCh
<= UNICODE_LIMIT
698 && offset16
< (MAX_LENGTH
/sizeof(UChar
) - MAX_UTF16_LEN
)
699 && offset8
< (MAX_LENGTH
- MAX_UTF8_LEN
))
701 if (currCh
== SURROGATE_HIGH_START
) {
702 currCh
= SURROGATE_LOW_END
+ 1; /* Skip surrogate range */
704 UTF16_APPEND_CHAR_SAFE(standardForm
, offset16
, MAX_LENGTH
, currCh
);
705 UTF8_APPEND_CHAR_SAFE(utf8
, offset8
, MAX_LENGTH
, currCh
);
708 if(!convertFromU(standardForm
, offset16
,
709 utf8
, offset8
, "UTF8", 0, TRUE
, U_ZERO_ERROR
)) {
710 log_err("Unicode->UTF8 did not match.\n");
712 if(!convertToU(utf8
, offset8
,
713 standardForm
, offset16
, "UTF8", 0, TRUE
, U_ZERO_ERROR
)) {
714 log_err("UTF8->Unicode did not match.\n");
722 static const char src8
[] = { (char)0xCC, (char)0x81, (char)0xCC, (char)0x80 };
723 static const UChar expected
[] = { 0x0301, 0x0300 };
725 UErrorCode err
= U_ZERO_ERROR
;
726 UChar pivotBuffer
[100];
727 const UChar
* const pivEnd
= pivotBuffer
+ 100;
732 conv8
= ucnv_open("UTF-8", &err
);
735 pivBeg
= pivotBuffer
;
737 ucnv_toUnicode(conv8
, &pivBeg
, pivEnd
, &srcBeg
, srcEnd
, 0, FALSE
, &err
);
738 if (srcBeg
!= srcEnd
) {
739 log_err("Did not consume whole buffer on first call.\n");
743 ucnv_toUnicode(conv8
, &pivBeg
, pivEnd
, &srcBeg
, srcEnd
, 0, TRUE
, &err
);
744 if (srcBeg
!= srcEnd
) {
745 log_err("Did not consume whole buffer on second call.\n");
748 if (U_FAILURE(err
) || (int32_t)(pivBeg
- pivotBuffer
) != 2 || u_strncmp(pivotBuffer
, expected
, 2) != 0) {
749 log_err("Did not get expected results for UTF-8.\n");
755 #define MAX_UTF32_LEN 1
757 static void TestRegressionUTF32(){
758 #if !UCONFIG_ONLY_HTML_CONVERSION
762 UChar
*standardForm
= (UChar
*)malloc(MAX_LENGTH
*sizeof(UChar
));
763 UChar32
*utf32
= (UChar32
*)malloc(MAX_LENGTH
*sizeof(UChar32
));
765 while (currCh
<= UNICODE_LIMIT
) {
768 while(currCh
<= UNICODE_LIMIT
769 && offset16
< (MAX_LENGTH
/sizeof(UChar
) - MAX_UTF16_LEN
)
770 && offset32
< (MAX_LENGTH
/sizeof(UChar32
) - MAX_UTF32_LEN
))
772 if (currCh
== SURROGATE_HIGH_START
) {
773 currCh
= SURROGATE_LOW_END
+ 1; /* Skip surrogate range */
775 UTF16_APPEND_CHAR_SAFE(standardForm
, offset16
, MAX_LENGTH
, currCh
);
776 UTF32_APPEND_CHAR_SAFE(utf32
, offset32
, MAX_LENGTH
, currCh
);
779 if(!convertFromU(standardForm
, offset16
,
780 (const uint8_t *)utf32
, offset32
*sizeof(UChar32
), "UTF32_PlatformEndian", 0, TRUE
, U_ZERO_ERROR
)) {
781 log_err("Unicode->UTF32 did not match.\n");
783 if(!convertToU((const uint8_t *)utf32
, offset32
*sizeof(UChar32
),
784 standardForm
, offset16
, "UTF32_PlatformEndian", 0, TRUE
, U_ZERO_ERROR
)) {
785 log_err("UTF32->Unicode did not match.\n");
792 /* Check for lone surrogate error handling. */
793 static const UChar sampleBadStartSurrogate
[] = { 0x0031, 0xD800, 0x0032 };
794 static const UChar sampleBadEndSurrogate
[] = { 0x0031, 0xDC00, 0x0032 };
795 static const uint8_t expectedUTF32BE
[] = {
796 0x00, 0x00, 0x00, 0x31,
797 0x00, 0x00, 0xff, 0xfd,
798 0x00, 0x00, 0x00, 0x32
800 static const uint8_t expectedUTF32LE
[] = {
801 0x31, 0x00, 0x00, 0x00,
802 0xfd, 0xff, 0x00, 0x00,
803 0x32, 0x00, 0x00, 0x00
805 static const int32_t offsetsUTF32
[] = {
806 0x00, 0x00, 0x00, 0x00,
807 0x01, 0x01, 0x01, 0x01,
808 0x02, 0x02, 0x02, 0x02
811 if(!convertFromU(sampleBadStartSurrogate
, UPRV_LENGTHOF(sampleBadStartSurrogate
),
812 expectedUTF32BE
, sizeof(expectedUTF32BE
), "UTF-32BE", offsetsUTF32
, TRUE
, U_ZERO_ERROR
))
813 log_err("u->UTF-32BE\n");
814 if(!convertFromU(sampleBadEndSurrogate
, UPRV_LENGTHOF(sampleBadEndSurrogate
),
815 expectedUTF32BE
, sizeof(expectedUTF32BE
), "UTF-32BE", offsetsUTF32
, TRUE
, U_ZERO_ERROR
))
816 log_err("u->UTF-32BE\n");
818 if(!convertFromU(sampleBadStartSurrogate
, UPRV_LENGTHOF(sampleBadStartSurrogate
),
819 expectedUTF32LE
, sizeof(expectedUTF32LE
), "UTF-32LE", offsetsUTF32
, TRUE
, U_ZERO_ERROR
))
820 log_err("u->UTF-32LE\n");
821 if(!convertFromU(sampleBadEndSurrogate
, UPRV_LENGTHOF(sampleBadEndSurrogate
),
822 expectedUTF32LE
, sizeof(expectedUTF32LE
), "UTF-32LE", offsetsUTF32
, TRUE
, U_ZERO_ERROR
))
823 log_err("u->UTF-32LE\n");
827 static const char srcBE
[] = { 0, 0, 0, 0x31, 0, 0, 0, 0x30 };
828 static const UChar expected
[] = { 0x0031, 0x0030 };
830 UErrorCode err
= U_ZERO_ERROR
;
831 UChar pivotBuffer
[100];
832 const UChar
* const pivEnd
= pivotBuffer
+ 100;
837 convBE
= ucnv_open("UTF-32BE", &err
);
840 pivBeg
= pivotBuffer
;
842 ucnv_toUnicode(convBE
, &pivBeg
, pivEnd
, &srcBeg
, srcEnd
, 0, FALSE
, &err
);
843 if (srcBeg
!= srcEnd
) {
844 log_err("Did not consume whole buffer on first call.\n");
848 ucnv_toUnicode(convBE
, &pivBeg
, pivEnd
, &srcBeg
, srcEnd
, 0, TRUE
, &err
);
849 if (srcBeg
!= srcEnd
) {
850 log_err("Did not consume whole buffer on second call.\n");
853 if (U_FAILURE(err
) || (int32_t)(pivBeg
- pivotBuffer
) != 2 || u_strncmp(pivotBuffer
, expected
, 2) != 0) {
854 log_err("Did not get expected results for UTF-32BE.\n");
859 static const char srcLE
[] = { 0x31, 0, 0, 0, 0x30, 0, 0, 0 };
860 static const UChar expected
[] = { 0x0031, 0x0030 };
862 UErrorCode err
= U_ZERO_ERROR
;
863 UChar pivotBuffer
[100];
864 const UChar
* const pivEnd
= pivotBuffer
+ 100;
869 convLE
= ucnv_open("UTF-32LE", &err
);
872 pivBeg
= pivotBuffer
;
874 ucnv_toUnicode(convLE
, &pivBeg
, pivEnd
, &srcBeg
, srcEnd
, 0, FALSE
, &err
);
875 if (srcBeg
!= srcEnd
) {
876 log_err("Did not consume whole buffer on first call.\n");
880 ucnv_toUnicode(convLE
, &pivBeg
, pivEnd
, &srcBeg
, srcEnd
, 0, TRUE
, &err
);
881 if (srcBeg
!= srcEnd
) {
882 log_err("Did not consume whole buffer on second call.\n");
885 if (U_FAILURE(err
) || (int32_t)(pivBeg
- pivotBuffer
) != 2 || u_strncmp(pivotBuffer
, expected
, 2) != 0) {
886 log_err("Did not get expected results for UTF-32LE.\n");
893 /*Walk through the available converters*/
894 static void TestAvailableConverters(){
895 UErrorCode status
=U_ZERO_ERROR
;
896 UConverter
*conv
=NULL
;
898 for(i
=0; i
< ucnv_countAvailable(); i
++){
900 conv
=ucnv_open(ucnv_getAvailableName(i
), &status
);
901 if(U_FAILURE(status
)){
902 log_err("ERROR: converter creation failed. Failure in alias table or the data table for \n converter=%s. Error=%s\n",
903 ucnv_getAvailableName(i
), myErrorName(status
));
911 static void TestFlushInternalBuffer(){
912 TestWithBufferSize(MAX_LENGTH
, 1);
913 TestWithBufferSize(1, 1);
914 TestWithBufferSize(1, MAX_LENGTH
);
915 TestWithBufferSize(MAX_LENGTH
, MAX_LENGTH
);
918 static void TestWithBufferSize(int32_t insize
, int32_t outsize
){
920 gInBufferSize
=insize
;
921 gOutBufferSize
= outsize
;
923 log_verbose("Testing fromUnicode for UTF-8 with UCNV_TO_U_CALLBACK_SUBSTITUTE \n");
926 { 0x0031, 0x0032, 0x0033, 0x0000, 0x4e00, 0x4e8c, 0x4e09, 0x002E };
927 const uint8_t expectedUTF8
[] =
928 { 0x31, 0x32, 0x33, 0x00, 0xe4, 0xb8, 0x80, 0xe4, 0xba, 0x8c, 0xe4, 0xb8, 0x89, 0x2E };
929 int32_t toUTF8Offs
[] =
930 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x06, 0x06, 0x06, 0x07};
931 /* int32_t fmUTF8Offs[] =
932 { 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0007, 0x000a, 0x000d };*/
935 if(!testConvertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
936 expectedUTF8
, sizeof(expectedUTF8
), "UTF8", UCNV_FROM_U_CALLBACK_SUBSTITUTE
, toUTF8Offs
,FALSE
))
937 log_err("u-> UTF8 did not match.\n");
940 #if !UCONFIG_NO_LEGACY_CONVERSION
941 log_verbose("Testing fromUnicode with UCNV_FROM_U_CALLBACK_ESCAPE \n");
943 UChar inputTest
[] = { 0x0061, 0xd801, 0xdc01, 0xd801, 0x0061 };
944 const uint8_t toIBM943
[]= { 0x61,
945 0x25, 0x55, 0x44, 0x38, 0x30, 0x31,
946 0x25, 0x55, 0x44, 0x43, 0x30, 0x31,
947 0x25, 0x55, 0x44, 0x38, 0x30, 0x31,
949 int32_t offset
[]= {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 4};
951 if(!testConvertFromU(inputTest
, UPRV_LENGTHOF(inputTest
),
952 toIBM943
, sizeof(toIBM943
), "ibm-943",
953 (UConverterFromUCallback
)UCNV_FROM_U_CALLBACK_ESCAPE
, offset
,FALSE
))
954 log_err("u-> ibm-943 with subst with value did not match.\n");
958 log_verbose("Testing fromUnicode for UTF-8 with UCNV_TO_U_CALLBACK_SUBSTITUTE \n");
960 const uint8_t sampleText1
[] = { 0x31, 0xe4, 0xba, 0x8c,
962 UChar expected1
[] = { 0x0031, 0x4e8c, 0xfffd, 0x0061};
963 int32_t offsets1
[] = { 0x0000, 0x0001, 0x0004, 0x0006};
965 if(!testConvertToU(sampleText1
, sizeof(sampleText1
),
966 expected1
, UPRV_LENGTHOF(expected1
),"utf8", UCNV_TO_U_CALLBACK_SUBSTITUTE
, offsets1
,FALSE
))
967 log_err("utf8->u with substitute did not match.\n");;
970 #if !UCONFIG_NO_LEGACY_CONVERSION
971 log_verbose("Testing toUnicode with UCNV_TO_U_CALLBACK_ESCAPE \n");
974 const uint8_t sampleTxtToU
[]= { 0x00, 0x9f, 0xaf,
975 0x81, 0xad, /*unassigned*/
977 UChar IBM_943toUnicode
[] = { 0x0000, 0x6D63,
978 0x25, 0x58, 0x38, 0x31, 0x25, 0x58, 0x41, 0x44,
980 int32_t fromIBM943Offs
[] = { 0, 1, 3, 3, 3, 3, 3, 3, 3, 3, 5};
982 if(!testConvertToU(sampleTxtToU
, sizeof(sampleTxtToU
),
983 IBM_943toUnicode
, UPRV_LENGTHOF(IBM_943toUnicode
),"ibm-943",
984 (UConverterToUCallback
)UCNV_TO_U_CALLBACK_ESCAPE
, fromIBM943Offs
,FALSE
))
985 log_err("ibm-943->u with substitute with value did not match.\n");
991 static UBool
convertFromU( const UChar
*source
, int sourceLen
, const uint8_t *expect
, int expectLen
,
992 const char *codepage
, const int32_t *expectOffsets
, UBool doFlush
, UErrorCode expectedStatus
)
998 char buffer
[MAX_LENGTH
];
999 int32_t offsetBuffer
[MAX_LENGTH
];
1003 UChar
*sourceLimit
=0;
1004 UErrorCode status
= U_ZERO_ERROR
;
1005 UConverter
*conv
= 0;
1006 conv
= ucnv_open(codepage
, &status
);
1007 if(U_FAILURE(status
))
1009 log_data_err("Couldn't open converter %s\n",codepage
);
1012 log_verbose("Converter %s opened..\n", ucnv_getName(conv
, &status
));
1014 for(i
=0; i
<MAX_LENGTH
; i
++){
1015 buffer
[i
]=(char)0xF0;
1016 offsetBuffer
[i
]=0xFF;
1020 sourceLimit
=(UChar
*)src
+(sourceLen
);
1022 targetLimit
=targ
+MAX_LENGTH
;
1024 ucnv_fromUnicode (conv
,
1026 (const char *)targetLimit
,
1029 expectOffsets
? offs
: NULL
,
1033 if(status
!= expectedStatus
){
1034 log_err("ucnv_fromUnicode() failed for codepage=%s. Error =%s Expected=%s\n", codepage
, myErrorName(status
), myErrorName(expectedStatus
));
1038 log_verbose("\nConversion done [%d uchars in -> %d chars out]. \nResult :",
1039 sourceLen
, targ
-buffer
);
1041 if(expectLen
!= targ
-buffer
)
1043 log_err("Expected %d chars out, got %d FROM Unicode to %s\n", expectLen
, targ
-buffer
, codepage
);
1044 log_verbose("Expected %d chars out, got %d FROM Unicode to %s\n", expectLen
, targ
-buffer
, codepage
);
1045 printSeqErr((const unsigned char *)buffer
, (int32_t)(targ
-buffer
));
1046 printSeqErr((const unsigned char*)expect
, expectLen
);
1050 if(memcmp(buffer
, expect
, expectLen
)){
1051 log_err("String does not match. FROM Unicode to codePage%s\n", codepage
);
1053 printSeqErr((const unsigned char *)buffer
, expectLen
);
1054 log_info("\nExpected:");
1055 printSeqErr((const unsigned char *)expect
, expectLen
);
1059 log_verbose("Matches!\n");
1062 if (expectOffsets
!= 0){
1063 log_verbose("comparing %d offsets..\n", targ
-buffer
);
1064 if(memcmp(offsetBuffer
,expectOffsets
,(targ
-buffer
) * sizeof(int32_t) )){
1065 log_err("did not get the expected offsets. for FROM Unicode to %s\n", codepage
);
1066 log_info("\nGot : ");
1067 printSeqErr((const unsigned char*)buffer
, (int32_t)(targ
-buffer
));
1068 for(p
=buffer
;p
<targ
;p
++)
1069 log_info("%d, ", offsetBuffer
[p
-buffer
]);
1070 log_info("\nExpected: ");
1071 for(i
=0; i
< (targ
-buffer
); i
++)
1072 log_info("%d,", expectOffsets
[i
]);
1080 static UBool
convertToU( const uint8_t *source
, int sourceLen
, const UChar
*expect
, int expectLen
,
1081 const char *codepage
, const int32_t *expectOffsets
, UBool doFlush
, UErrorCode expectedStatus
)
1083 UErrorCode status
= U_ZERO_ERROR
;
1084 UConverter
*conv
= 0;
1088 UChar buffer
[MAX_LENGTH
];
1089 int32_t offsetBuffer
[MAX_LENGTH
];
1093 uint8_t *sourceLimit
=0;
1097 conv
= ucnv_open(codepage
, &status
);
1098 if(U_FAILURE(status
))
1100 log_data_err("Couldn't open converter %s\n",codepage
);
1103 log_verbose("Converter %s opened..\n", ucnv_getName(conv
, &status
));
1107 for(i
=0; i
<MAX_LENGTH
; i
++){
1112 src
=(const char *)source
;
1113 sourceLimit
=(uint8_t*)(src
+(sourceLen
));
1115 targetLimit
=targ
+MAX_LENGTH
;
1120 ucnv_toUnicode (conv
,
1123 (const char **)&src
,
1124 (const char *)sourceLimit
,
1125 expectOffsets
? offs
: NULL
,
1130 if(status
!= expectedStatus
){
1131 log_err("ucnv_fromUnicode() failed for codepage=%s. Error =%s Expected=%s\n", codepage
, myErrorName(status
), myErrorName(expectedStatus
));
1134 log_verbose("\nConversion done [%d uchars in -> %d chars out]. \nResult :",
1135 sourceLen
, targ
-buffer
);
1140 log_verbose("comparing %d uchars (%d bytes)..\n",expectLen
,expectLen
*2);
1142 if (expectOffsets
!= 0) {
1143 if(memcmp(offsetBuffer
, expectOffsets
, (targ
-buffer
) * sizeof(int32_t))){
1145 log_err("did not get the expected offsets from %s To UNICODE\n", codepage
);
1146 log_info("\nGot : ");
1147 for(p
=buffer
;p
<targ
;p
++)
1148 log_info("%d, ", offsetBuffer
[p
-buffer
]);
1149 log_info("\nExpected: ");
1150 for(i
=0; i
<(targ
-buffer
); i
++)
1151 log_info("%d, ", expectOffsets
[i
]);
1152 log_info("\nGot result:");
1153 for(i
=0; i
<(targ
-buffer
); i
++)
1154 log_info("0x%04X,", buffer
[i
]);
1155 log_info("\nFrom Input:");
1156 for(i
=0; i
<(src
-(const char *)source
); i
++)
1157 log_info("0x%02X,", (unsigned char)source
[i
]);
1161 if(memcmp(buffer
, expect
, expectLen
*2)){
1162 log_err("String does not match. from codePage %s TO Unicode\n", codepage
);
1164 printUSeqErr(buffer
, expectLen
);
1165 log_info("\nExpected:");
1166 printUSeqErr(expect
, expectLen
);
1170 log_verbose("Matches!\n");
1177 static UBool
testConvertFromU( const UChar
*source
, int sourceLen
, const uint8_t *expect
, int expectLen
,
1178 const char *codepage
, UConverterFromUCallback callback
, const int32_t *expectOffsets
, UBool testReset
)
1180 UErrorCode status
= U_ZERO_ERROR
;
1181 UConverter
*conv
= 0;
1182 char junkout
[MAX_LENGTH
]; /* FIX */
1183 int32_t junokout
[MAX_LENGTH
]; /* FIX */
1190 int32_t realBufferSize
;
1191 char *realBufferEnd
;
1192 const UChar
*realSourceEnd
;
1193 const UChar
*sourceLimit
;
1194 UBool checkOffsets
= TRUE
;
1197 UConverterFromUCallback oldAction
= NULL
;
1198 const void* oldContext
= NULL
;
1200 for(i
=0;i
<MAX_LENGTH
;i
++)
1201 junkout
[i
] = (char)0xF0;
1202 for(i
=0;i
<MAX_LENGTH
;i
++)
1205 setNuConvTestName(codepage
, "FROM");
1207 log_verbose("\n========= %s\n", gNuConvTestName
);
1209 conv
= ucnv_open(codepage
, &status
);
1210 if(U_FAILURE(status
))
1212 log_data_err("Couldn't open converter %s\n",codepage
);
1216 log_verbose("Converter opened..\n");
1217 /*----setting the callback routine----*/
1218 ucnv_setFromUCallBack (conv
, callback
, NULL
, &oldAction
, &oldContext
, &status
);
1219 if (U_FAILURE(status
)) {
1220 log_err("FAILURE in setting the callback Function! %s\n", myErrorName(status
));
1222 /*------------------------*/
1228 realBufferSize
= UPRV_LENGTHOF(junkout
);
1229 realBufferEnd
= junkout
+ realBufferSize
;
1230 realSourceEnd
= source
+ sourceLen
;
1232 if ( gOutBufferSize
!= realBufferSize
)
1233 checkOffsets
= FALSE
;
1235 if( gInBufferSize
!= MAX_LENGTH
)
1236 checkOffsets
= FALSE
;
1240 end
= nct_min(targ
+ gOutBufferSize
, realBufferEnd
);
1241 sourceLimit
= nct_min(src
+ gInBufferSize
, realSourceEnd
);
1243 doFlush
= (UBool
)(sourceLimit
== realSourceEnd
);
1245 if(targ
== realBufferEnd
)
1247 log_err("Error, overflowed the real buffer while about to call fromUnicode! targ=%08lx %s", targ
, gNuConvTestName
);
1250 log_verbose("calling fromUnicode @ SOURCE:%08lx to %08lx TARGET: %08lx to %08lx, flush=%s\n", src
,sourceLimit
, targ
,end
, doFlush
?"TRUE":"FALSE");
1253 status
= U_ZERO_ERROR
;
1254 if(gInBufferSize
==999 && gOutBufferSize
==999)
1256 ucnv_fromUnicode (conv
,
1262 doFlush
, /* flush if we're at the end of the input data */
1265 ucnv_resetToUnicode(conv
);
1266 if(gInBufferSize
==999 && gOutBufferSize
==999)
1267 ucnv_resetToUnicode(conv
);
1269 } while ( (status
== U_BUFFER_OVERFLOW_ERROR
) || (U_SUCCESS(status
) && sourceLimit
< realSourceEnd
) );
1271 if(U_FAILURE(status
)) {
1272 log_err("Problem doing fromUnicode to %s, errcode %s %s\n", codepage
, myErrorName(status
), gNuConvTestName
);
1276 log_verbose("\nConversion done [%d uchars in -> %d chars out]. \nResult :",
1277 sourceLen
, targ
-junkout
);
1278 if(getTestOption(VERBOSITY_OPTION
))
1281 char offset_str
[999];
1286 for(ptr
= junkout
;ptr
<targ
;ptr
++)
1288 sprintf(junk
+ strlen(junk
), "0x%02x, ", (0xFF) & (unsigned int)*ptr
);
1289 sprintf(offset_str
+ strlen(offset_str
), "0x%02x, ", (0xFF) & (unsigned int)junokout
[ptr
-junkout
]);
1293 printSeq((const unsigned char *)expect
, expectLen
);
1296 log_verbose("\nOffsets:");
1297 log_verbose(offset_str
);
1304 if(expectLen
!= targ
-junkout
)
1306 log_err("Expected %d chars out, got %d %s\n", expectLen
, targ
-junkout
, gNuConvTestName
);
1307 log_verbose("Expected %d chars out, got %d %s\n", expectLen
, targ
-junkout
, gNuConvTestName
);
1309 printSeqErr((const unsigned char*)junkout
, (int32_t)(targ
-junkout
));
1310 log_info("\nExpected:");
1311 printSeqErr((const unsigned char*)expect
, expectLen
);
1315 if (checkOffsets
&& (expectOffsets
!= 0) )
1317 log_verbose("comparing %d offsets..\n", targ
-junkout
);
1318 if(memcmp(junokout
,expectOffsets
,(targ
-junkout
) * sizeof(int32_t) )){
1319 log_err("did not get the expected offsets. %s", gNuConvTestName
);
1321 printSeqErr((const unsigned char*)junkout
, (int32_t)(targ
-junkout
));
1322 for(p
=junkout
;p
<targ
;p
++)
1323 log_err("%d, ", junokout
[p
-junkout
]);
1324 log_err("\nExpected: ");
1325 for(i
=0; i
<(targ
-junkout
); i
++)
1326 log_err("%d,", expectOffsets
[i
]);
1330 log_verbose("comparing..\n");
1331 if(!memcmp(junkout
, expect
, expectLen
))
1333 log_verbose("Matches!\n");
1338 log_err("String does not match. %s\n", gNuConvTestName
);
1339 printUSeqErr(source
, sourceLen
);
1341 printSeqErr((const unsigned char *)junkout
, expectLen
);
1342 log_info("\nExpected:");
1343 printSeqErr((const unsigned char *)expect
, expectLen
);
1349 static UBool
testConvertToU( const uint8_t *source
, int sourcelen
, const UChar
*expect
, int expectlen
,
1350 const char *codepage
, UConverterToUCallback callback
, const int32_t *expectOffsets
, UBool testReset
)
1352 UErrorCode status
= U_ZERO_ERROR
;
1353 UConverter
*conv
= 0;
1354 UChar junkout
[MAX_LENGTH
]; /* FIX */
1355 int32_t junokout
[MAX_LENGTH
]; /* FIX */
1357 const char *realSourceEnd
;
1358 const char *srcLimit
;
1364 UBool checkOffsets
= TRUE
;
1365 int32_t realBufferSize
;
1366 UChar
*realBufferEnd
;
1369 UConverterToUCallback oldAction
= NULL
;
1370 const void* oldContext
= NULL
;
1373 for(i
=0;i
<MAX_LENGTH
;i
++)
1374 junkout
[i
] = 0xFFFE;
1376 for(i
=0;i
<MAX_LENGTH
;i
++)
1379 setNuConvTestName(codepage
, "TO");
1381 log_verbose("\n========= %s\n", gNuConvTestName
);
1383 conv
= ucnv_open(codepage
, &status
);
1384 if(U_FAILURE(status
))
1386 log_data_err("Couldn't open converter %s\n",gNuConvTestName
);
1390 log_verbose("Converter opened..\n");
1391 /*----setting the callback routine----*/
1392 ucnv_setToUCallBack (conv
, callback
, NULL
, &oldAction
, &oldContext
, &status
);
1393 if (U_FAILURE(status
)) {
1394 log_err("FAILURE in setting the callback Function! %s\n", myErrorName(status
));
1396 /*-------------------------------------*/
1398 src
= (const char *)source
;
1402 realBufferSize
= UPRV_LENGTHOF(junkout
);
1403 realBufferEnd
= junkout
+ realBufferSize
;
1404 realSourceEnd
= src
+ sourcelen
;
1406 if ( gOutBufferSize
!= realBufferSize
)
1407 checkOffsets
= FALSE
;
1409 if( gInBufferSize
!= MAX_LENGTH
)
1410 checkOffsets
= FALSE
;
1414 end
= nct_min( targ
+ gOutBufferSize
, realBufferEnd
);
1415 srcLimit
= nct_min(realSourceEnd
, src
+ gInBufferSize
);
1417 if(targ
== realBufferEnd
)
1419 log_err("Error, the end would overflow the real output buffer while about to call toUnicode! tarjey=%08lx %s",targ
,gNuConvTestName
);
1422 log_verbose("calling toUnicode @ %08lx to %08lx\n", targ
,end
);
1424 /* oldTarg = targ; */
1426 status
= U_ZERO_ERROR
;
1427 doFlush
=(UBool
)((gInBufferSize
==999 && gOutBufferSize
==999)?(srcLimit
== realSourceEnd
) : FALSE
);
1429 ucnv_toUnicode (conv
,
1432 (const char **)&src
,
1433 (const char *)srcLimit
,
1435 doFlush
, /* flush if we're at the end of hte source data */
1438 ucnv_resetFromUnicode(conv
);
1439 if(gInBufferSize
==999 && gOutBufferSize
==999)
1440 ucnv_resetToUnicode(conv
);
1441 /* offs += (targ-oldTarg); */
1443 } while ( (status
== U_BUFFER_OVERFLOW_ERROR
) || (U_SUCCESS(status
) && (srcLimit
< realSourceEnd
)) ); /* while we just need another buffer */
1445 if(U_FAILURE(status
))
1447 log_err("Problem doing %s toUnicode, errcode %s %s\n", codepage
, myErrorName(status
), gNuConvTestName
);
1451 log_verbose("\nConversion done. %d bytes -> %d chars.\nResult :",
1452 sourcelen
, targ
-junkout
);
1453 if(getTestOption(VERBOSITY_OPTION
))
1456 char offset_str
[999];
1463 for(ptr
= junkout
;ptr
<targ
;ptr
++)
1465 sprintf(junk
+ strlen(junk
), "0x%04x, ", (0xFFFF) & (unsigned int)*ptr
);
1466 sprintf(offset_str
+ strlen(offset_str
), "0x%04x, ", (0xFFFF) & (unsigned int)junokout
[ptr
-junkout
]);
1473 log_verbose("\nOffsets:");
1474 log_verbose(offset_str
);
1480 log_verbose("comparing %d uchars (%d bytes)..\n",expectlen
,expectlen
*2);
1482 if (checkOffsets
&& (expectOffsets
!= 0))
1484 if(memcmp(junokout
,expectOffsets
,(targ
-junkout
) * sizeof(int32_t))){
1486 log_err("did not get the expected offsets. %s",gNuConvTestName
);
1487 for(p
=junkout
;p
<targ
;p
++)
1488 log_err("%d, ", junokout
[p
-junkout
]);
1489 log_err("\nExpected: ");
1490 for(i
=0; i
<(targ
-junkout
); i
++)
1491 log_err("%d,", expectOffsets
[i
]);
1493 for(i
=0; i
<(targ
-junkout
); i
++)
1494 log_err("%X,", junkout
[i
]);
1496 for(i
=0; i
<(src
-(const char *)source
); i
++)
1497 log_err("%X,", (unsigned char)source
[i
]);
1501 if(!memcmp(junkout
, expect
, expectlen
*2))
1503 log_verbose("Matches!\n");
1508 log_err("String does not match. %s\n", gNuConvTestName
);
1509 log_verbose("String does not match. %s\n", gNuConvTestName
);
1511 printUSeq(junkout
, expectlen
);
1512 log_info("\nExpected:");
1513 printUSeq(expect
, expectlen
);
1519 static void TestResetBehaviour(void){
1520 #if !UCONFIG_NO_LEGACY_CONVERSION
1521 log_verbose("Testing Reset for DBCS and MBCS\n");
1523 static const UChar sampleText
[] = {0x00a1, 0xd801, 0xdc01, 0x00a4};
1524 static const uint8_t expected
[] = {0xa2, 0xae, 0xa1, 0xe0, 0xa2, 0xb4};
1525 static const int32_t offsets
[] = {0x00, 0x00, 0x01, 0x01, 0x03, 0x03 };
1528 static const UChar sampleText1
[] = {0x00a1, 0x00a4, 0x00a7, 0x00a8};
1529 static const uint8_t expected1
[] = {0xa2, 0xae,0xA2,0xB4,0xA1,0xD7,0xA1,0xA7};
1530 static const int32_t offsets1
[] = { 0,2,4,6};
1533 if(!testConvertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
1534 expected
, sizeof(expected
), "ibm-1363", UCNV_FROM_U_CALLBACK_SUBSTITUTE
, NULL
, TRUE
))
1535 log_err("u-> ibm-1363 [UCNV_DBCS portion] not match.\n");
1536 if(!testConvertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
1537 expected
, sizeof(expected
), "ibm-1363", UCNV_FROM_U_CALLBACK_SUBSTITUTE
,offsets
, TRUE
))
1538 log_err("u-> ibm-1363 [UCNV_DBCS portion] not match.\n");
1540 if(!testConvertToU(expected1
, sizeof(expected1
),
1541 sampleText1
, UPRV_LENGTHOF(sampleText1
), "ibm-1363",UCNV_TO_U_CALLBACK_SUBSTITUTE
,
1543 log_err("ibm-1363 -> did not match.\n");
1545 if(!testConvertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
1546 expected
, sizeof(expected
), "ibm-1363", UCNV_FROM_U_CALLBACK_SUBSTITUTE
, NULL
, TRUE
))
1547 log_err("u-> ibm-1363 [UCNV_MBCS] not match.\n");
1548 if(!testConvertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
1549 expected
, sizeof(expected
), "ibm-1363", UCNV_FROM_U_CALLBACK_SUBSTITUTE
,offsets
, TRUE
))
1550 log_err("u-> ibm-1363 [UCNV_MBCS] not match.\n");
1552 if(!testConvertToU(expected1
, sizeof(expected1
),
1553 sampleText1
, UPRV_LENGTHOF(sampleText1
), "ibm-1363",UCNV_TO_U_CALLBACK_SUBSTITUTE
,
1555 log_err("ibm-1363 -> did not match.\n");
1559 log_verbose("Testing Reset for ISO-2022-jp\n");
1561 static const UChar sampleText
[] = { 0x4e00, 0x04e01, 0x0031, 0xd801, 0xdc01, 0x0032};
1563 static const uint8_t expected
[] = {0x1b, 0x24, 0x42,0x30,0x6c,0x43,0x7a,0x1b,0x28,0x42,
1567 static const int32_t offsets
[] = {0,0,0,0,0,1,1,2,2,2,2,3,5 };
1570 static const UChar sampleText1
[] = {0x4e00, 0x04e01, 0x0031,0x001A, 0x0032};
1571 static const uint8_t expected1
[] = {0x1b, 0x24, 0x42,0x30,0x6c,0x43,0x7a,0x1b,0x28,0x42,
1573 static const int32_t offsets1
[] = { 3,5,10,11,12};
1576 if(!testConvertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
1577 expected
, sizeof(expected
), "iso-2022-jp", UCNV_FROM_U_CALLBACK_SUBSTITUTE
, NULL
, TRUE
))
1578 log_err("u-> not match.\n");
1579 if(!testConvertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
1580 expected
, sizeof(expected
), "iso-2022-jp", UCNV_FROM_U_CALLBACK_SUBSTITUTE
,offsets
, TRUE
))
1581 log_err("u-> not match.\n");
1583 if(!testConvertToU(expected1
, sizeof(expected1
),
1584 sampleText1
, UPRV_LENGTHOF(sampleText1
), "iso-2022-jp",UCNV_TO_U_CALLBACK_SUBSTITUTE
,
1586 log_err("iso-2022-jp -> did not match.\n");
1590 log_verbose("Testing Reset for ISO-2022-cn\n");
1592 static const UChar sampleText
[] = { 0x4e00, 0x04e01, 0x0031, 0xd801, 0xdc01, 0x0032};
1594 static const uint8_t expected
[] = {
1595 0x1B, 0x24, 0x29, 0x41, 0x0E, 0x52, 0x3B,
1603 static const int32_t offsets
[] = {
1604 0, 0, 0, 0, 0, 0, 0,
1610 UChar sampleText1
[] = {0x4e00, 0x04e01, 0x0031,0x001A, 0x0032};
1611 static const uint8_t expected1
[] = {
1612 0x1B, 0x24, 0x29, 0x41, 0x0E, 0x52, 0x3B,
1614 0x1B, 0x24, 0x29, 0x47, 0x24, 0x22,
1618 static const int32_t offsets1
[] = { 5,7,13,16,17};
1621 if(!testConvertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
1622 expected
, sizeof(expected
), "iso-2022-cn", UCNV_FROM_U_CALLBACK_SUBSTITUTE
, NULL
, TRUE
))
1623 log_err("u-> not match.\n");
1624 if(!testConvertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
1625 expected
, sizeof(expected
), "iso-2022-cn", UCNV_FROM_U_CALLBACK_SUBSTITUTE
,offsets
, TRUE
))
1626 log_err("u-> not match.\n");
1628 if(!testConvertToU(expected1
, sizeof(expected1
),
1629 sampleText1
, UPRV_LENGTHOF(sampleText1
), "iso-2022-cn",UCNV_TO_U_CALLBACK_SUBSTITUTE
,
1631 log_err("iso-2022-cn -> did not match.\n");
1634 log_verbose("Testing Reset for ISO-2022-kr\n");
1636 UChar sampleText
[] = { 0x4e00,0xd801, 0xdc01, 0x04e01, 0x0031, 0xd801, 0xdc01, 0x0032};
1638 static const uint8_t expected
[] = {0x1B, 0x24, 0x29, 0x43,
1646 static const int32_t offsets
[] = {-1, -1, -1, -1,
1654 static const UChar sampleText1
[] = { 0x4e00,0x0041, 0x04e01, 0x0031, 0x0042, 0x0032};
1656 static const uint8_t expected1
[] = {0x1B, 0x24, 0x29, 0x43,
1664 static const int32_t offsets1
[] = {
1670 if(!testConvertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
1671 expected
, sizeof(expected
), "iso-2022-kr", UCNV_FROM_U_CALLBACK_SUBSTITUTE
, NULL
, TRUE
))
1672 log_err("u-> iso-2022-kr [UCNV_DBCS] not match.\n");
1673 if(!testConvertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
1674 expected
, sizeof(expected
), "iso-2022-kr", UCNV_FROM_U_CALLBACK_SUBSTITUTE
,offsets
, TRUE
))
1675 log_err("u-> iso-2022-kr [UCNV_DBCS] not match.\n");
1676 if(!testConvertToU(expected1
, sizeof(expected1
),
1677 sampleText1
, UPRV_LENGTHOF(sampleText1
), "iso-2022-kr",UCNV_TO_U_CALLBACK_SUBSTITUTE
,
1679 log_err("iso-2022-kr -> did not match.\n");
1682 log_verbose("Testing Reset for HZ\n");
1684 static const UChar sampleText
[] = { 0x4e00, 0xd801, 0xdc01, 0x04e01, 0x0031, 0xd801, 0xdc01, 0x0032};
1686 static const uint8_t expected
[] = {0x7E, 0x7B, 0x52, 0x3B,
1688 0x7E, 0x7B, 0x36, 0x21,
1694 static const int32_t offsets
[] = {0,0,0,0,
1700 static const UChar sampleText1
[] = { 0x4e00, 0x0035, 0x04e01, 0x0031, 0x0041, 0x0032};
1702 static const uint8_t expected1
[] = {0x7E, 0x7B, 0x52, 0x3B,
1704 0x7E, 0x7B, 0x36, 0x21,
1710 static const int32_t offsets1
[] = {2,6,9,13,14,15
1714 if(!testConvertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
1715 expected
, sizeof(expected
), "HZ", UCNV_FROM_U_CALLBACK_SUBSTITUTE
,NULL
, TRUE
))
1716 log_err("u-> not match.\n");
1717 if(!testConvertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
1718 expected
, sizeof(expected
), "HZ", UCNV_FROM_U_CALLBACK_SUBSTITUTE
,offsets
, TRUE
))
1719 log_err("u-> not match.\n");
1720 if(!testConvertToU(expected1
, sizeof(expected1
),
1721 sampleText1
, UPRV_LENGTHOF(sampleText1
), "hz",UCNV_TO_U_CALLBACK_SUBSTITUTE
,
1723 log_err("hz -> did not match.\n");
1728 log_verbose("Testing for UTF8\n");
1730 static const UChar sampleText
[] = { 0x4e00, 0x0701, 0x0031, 0xbfc1, 0xd801, 0xdc01, 0x0032};
1731 int32_t offsets
[]={0x00, 0x00, 0x00, 0x01, 0x01, 0x02,
1732 0x03, 0x03, 0x03, 0x04, 0x04, 0x04,
1734 static const uint8_t expected
[] = {0xe4, 0xb8, 0x80, 0xdc, 0x81, 0x31,
1735 0xeb, 0xbf, 0x81, 0xF0, 0x90, 0x90, 0x81, 0x32};
1738 static const int32_t fromOffsets
[] = { 0x0000, 0x0003, 0x0005, 0x0006, 0x0009, 0x0009, 0x000D };
1740 if(!testConvertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
1741 expected
, sizeof(expected
), "UTF8", UCNV_FROM_U_CALLBACK_SUBSTITUTE
,offsets
, TRUE
))
1742 log_err("u-> UTF8 with offsets and flush true did not match.\n");
1743 if(!testConvertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
1744 expected
, sizeof(expected
), "UTF8", UCNV_FROM_U_CALLBACK_SUBSTITUTE
,NULL
, TRUE
))
1745 log_err("u-> UTF8 with offsets and flush true did not match.\n");
1746 if(!testConvertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
1747 expected
, sizeof(expected
), "UTF8", UCNV_FROM_U_CALLBACK_SUBSTITUTE
,offsets
, TRUE
))
1748 log_err("u-> UTF8 with offsets and flush true did not match.\n");
1749 if(!testConvertFromU(sampleText
, UPRV_LENGTHOF(sampleText
),
1750 expected
, sizeof(expected
), "UTF8", UCNV_FROM_U_CALLBACK_SUBSTITUTE
,NULL
, TRUE
))
1751 log_err("u-> UTF8 with offsets and flush true did not match.\n");
1752 if(!testConvertToU(expected
, sizeof(expected
),
1753 sampleText
, UPRV_LENGTHOF(sampleText
), "UTF8",UCNV_TO_U_CALLBACK_SUBSTITUTE
, NULL
, TRUE
))
1754 log_err("UTF8 -> did not match.\n");
1755 if(!testConvertToU(expected
, sizeof(expected
),
1756 sampleText
, UPRV_LENGTHOF(sampleText
), "UTF8", UCNV_TO_U_CALLBACK_SUBSTITUTE
, NULL
, TRUE
))
1757 log_err("UTF8 -> did not match.\n");
1758 if(!testConvertToU(expected
, sizeof(expected
),
1759 sampleText
, UPRV_LENGTHOF(sampleText
), "UTF8",UCNV_TO_U_CALLBACK_SUBSTITUTE
, fromOffsets
, TRUE
))
1760 log_err("UTF8 -> did not match.\n");
1761 if(!testConvertToU(expected
, sizeof(expected
),
1762 sampleText
, UPRV_LENGTHOF(sampleText
), "UTF8", UCNV_TO_U_CALLBACK_SUBSTITUTE
, fromOffsets
, TRUE
))
1763 log_err("UTF8 -> did not match.\n");
1769 /* Test that U_TRUNCATED_CHAR_FOUND is set. */
1771 doTestTruncated(const char *cnvName
, const uint8_t *bytes
, int32_t length
) {
1775 UChar
*target
, *targetLimit
;
1776 const char *source
, *sourceLimit
;
1778 UErrorCode errorCode
;
1780 errorCode
=U_ZERO_ERROR
;
1781 cnv
=ucnv_open(cnvName
, &errorCode
);
1782 if(U_FAILURE(errorCode
)) {
1783 log_data_err("error TestTruncated: unable to open \"%s\" - %s\n", cnvName
, u_errorName(errorCode
));
1786 ucnv_setToUCallBack(cnv
, UCNV_TO_U_CALLBACK_STOP
, NULL
, NULL
, NULL
, &errorCode
);
1787 if(U_FAILURE(errorCode
)) {
1788 log_data_err("error TestTruncated: unable to set the stop callback on \"%s\" - %s\n",
1789 cnvName
, u_errorName(errorCode
));
1794 source
=(const char *)bytes
;
1795 sourceLimit
=source
+length
;
1797 targetLimit
=buffer
+UPRV_LENGTHOF(buffer
);
1799 /* 1. input bytes with flush=FALSE, then input nothing with flush=TRUE */
1800 ucnv_toUnicode(cnv
, &target
, targetLimit
, &source
, sourceLimit
, NULL
, FALSE
, &errorCode
);
1801 if(U_FAILURE(errorCode
) || source
!=sourceLimit
|| target
!=buffer
) {
1802 log_err("error TestTruncated(%s, 1a): input bytes[%d], flush=FALSE: %s, input left %d, output %d\n",
1803 cnvName
, length
, u_errorName(errorCode
), (int)(sourceLimit
-source
), (int)(target
-buffer
));
1806 errorCode
=U_ZERO_ERROR
;
1809 ucnv_toUnicode(cnv
, &target
, targetLimit
, &source
, sourceLimit
, NULL
, TRUE
, &errorCode
);
1810 if(errorCode
!=U_TRUNCATED_CHAR_FOUND
|| target
!=buffer
) {
1811 log_err("error TestTruncated(%s, 1b): no input (previously %d), flush=TRUE: %s (should be U_TRUNCATED_CHAR_FOUND), output %d\n",
1812 cnvName
, (int)length
, u_errorName(errorCode
), (int)(target
-buffer
));
1815 /* 2. input bytes with flush=TRUE */
1816 ucnv_resetToUnicode(cnv
);
1818 errorCode
=U_ZERO_ERROR
;
1819 source
=(const char *)bytes
;
1821 ucnv_toUnicode(cnv
, &target
, targetLimit
, &source
, sourceLimit
, NULL
, TRUE
, &errorCode
);
1822 if(errorCode
!=U_TRUNCATED_CHAR_FOUND
|| source
!=sourceLimit
|| target
!=buffer
) {
1823 log_err("error TestTruncated(%s, 2): input bytes[%d], flush=TRUE: %s (should be U_TRUNCATED_CHAR_FOUND), input left %d, output %d\n",
1824 cnvName
, length
, u_errorName(errorCode
), (int)(sourceLimit
-source
), (int)(target
-buffer
));
1833 static const struct {
1834 const char *cnvName
;
1835 uint8_t bytes
[8]; /* partial input bytes resulting in no output */
1838 { "IMAP-mailbox-name", { 0x26 }, 1 }, /* & */
1839 { "IMAP-mailbox-name", { 0x26, 0x42 }, 2 }, /* &B */
1840 { "IMAP-mailbox-name", { 0x26, 0x42, 0x42 }, 3 }, /* &BB */
1841 { "IMAP-mailbox-name", { 0x26, 0x41, 0x41 }, 3 }, /* &AA */
1843 { "UTF-7", { 0x2b, 0x42 }, 2 }, /* +B */
1844 { "UTF-8", { 0xd1 }, 1 },
1846 { "UTF-16BE", { 0x4e }, 1 },
1847 { "UTF-16LE", { 0x4e }, 1 },
1848 { "UTF-16", { 0x4e }, 1 },
1849 { "UTF-16", { 0xff }, 1 },
1850 { "UTF-16", { 0xfe, 0xff, 0x4e }, 3 },
1852 { "UTF-32BE", { 0, 0, 0x4e }, 3 },
1853 { "UTF-32LE", { 0x4e }, 1 },
1854 { "UTF-32", { 0, 0, 0x4e }, 3 },
1855 { "UTF-32", { 0xff }, 1 },
1856 { "UTF-32", { 0, 0, 0xfe, 0xff, 0 }, 5 },
1857 { "SCSU", { 0x0e, 0x4e }, 2 }, /* SQU 0x4e */
1859 #if !UCONFIG_NO_LEGACY_CONVERSION
1860 { "BOCU-1", { 0xd5 }, 1 },
1862 { "Shift-JIS", { 0xe0 }, 1 },
1864 { "ibm-939", { 0x0e, 0x41 }, 2 } /* SO 0x41 */
1866 { "BOCU-1", { 0xd5 }, 1 ,}
1871 for(i
=0; i
<UPRV_LENGTHOF(testCases
); ++i
) {
1872 doTestTruncated(testCases
[i
].cnvName
, testCases
[i
].bytes
, testCases
[i
].length
);
1876 typedef struct NameRange
{
1878 UChar32 start
, end
, start2
, end2
, notStart
, notEnd
;
1883 UErrorCode errorCode
;
1889 static const char *const completeSetNames
[]={
1901 #if !UCONFIG_NO_LEGACY_CONVERSION
1906 #if !UCONFIG_NO_LEGACY_CONVERSION
1907 static const char *const lmbcsNames
[]={
1923 static const NameRange nameRanges
[]={
1924 { "US-ASCII", 0, 0x7f, -1, -1, 0x80, 0x10ffff },
1925 #if !UCONFIG_NO_LEGACY_CONVERSION
1926 { "ibm-367", 0, 0x7f, -1, -1, 0x80, 0x10ffff },
1928 { "ISO-8859-1", 0, 0x7f, -1, -1, 0x100, 0x10ffff },
1929 #if !UCONFIG_NO_LEGACY_CONVERSION
1930 { "UTF-8", 0, 0xd7ff, 0xe000, 0x10ffff, 0xd800, 0xdfff },
1931 { "windows-1251", 0, 0x7f, 0x410, 0x44f, 0x3000, 0xd7ff },
1932 /* HZ test case fixed and moved to intltest's conversion.txt, ticket #6002 */
1933 { "shift-jis", 0x3041, 0x3093, 0x30a1, 0x30f3, 0x900, 0x1cff }
1935 { "UTF-8", 0, 0xd7ff, 0xe000, 0x10ffff, 0xd800, 0xdfff }
1939 /* open an empty set */
1940 set
=uset_open(1, 0);
1942 count
=ucnv_countAvailable();
1943 for(i
=0; i
<count
; ++i
) {
1944 errorCode
=U_ZERO_ERROR
;
1945 name
=ucnv_getAvailableName(i
);
1946 cnv
=ucnv_open(name
, &errorCode
);
1947 if(U_FAILURE(errorCode
)) {
1948 log_data_err("error: unable to open converter %s - %s\n",
1949 name
, u_errorName(errorCode
));
1954 ucnv_getUnicodeSet(cnv
, set
, UCNV_ROUNDTRIP_SET
, &errorCode
);
1955 if(U_FAILURE(errorCode
)) {
1956 log_err("error: ucnv_getUnicodeSet(%s) failed - %s\n",
1957 name
, u_errorName(errorCode
));
1958 } else if(uset_size(set
)==0) {
1959 log_err("error: ucnv_getUnicodeSet(%s) returns an empty set\n", name
);
1965 /* test converters that are known to convert all of Unicode (except maybe for surrogates) */
1966 for(i
=0; i
<UPRV_LENGTHOF(completeSetNames
); ++i
) {
1967 errorCode
=U_ZERO_ERROR
;
1968 name
=completeSetNames
[i
];
1969 cnv
=ucnv_open(name
, &errorCode
);
1970 if(U_FAILURE(errorCode
)) {
1971 log_data_err("error: unable to open converter %s - %s\n",
1972 name
, u_errorName(errorCode
));
1977 ucnv_getUnicodeSet(cnv
, set
, UCNV_ROUNDTRIP_SET
, &errorCode
);
1978 if(U_FAILURE(errorCode
)) {
1979 log_err("error: ucnv_getUnicodeSet(%s) failed - %s\n",
1980 name
, u_errorName(errorCode
));
1981 } else if(!uset_containsRange(set
, 0, 0xd7ff) || !uset_containsRange(set
, 0xe000, 0x10ffff)) {
1982 log_err("error: ucnv_getUnicodeSet(%s) does not return an all-Unicode set\n", name
);
1988 #if !UCONFIG_NO_LEGACY_CONVERSION
1989 /* test LMBCS variants which convert all of Unicode except for U+F6xx */
1990 for(i
=0; i
<UPRV_LENGTHOF(lmbcsNames
); ++i
) {
1991 errorCode
=U_ZERO_ERROR
;
1993 cnv
=ucnv_open(name
, &errorCode
);
1994 if(U_FAILURE(errorCode
)) {
1995 log_data_err("error: unable to open converter %s - %s\n",
1996 name
, u_errorName(errorCode
));
2001 ucnv_getUnicodeSet(cnv
, set
, UCNV_ROUNDTRIP_SET
, &errorCode
);
2002 if(U_FAILURE(errorCode
)) {
2003 log_err("error: ucnv_getUnicodeSet(%s) failed - %s\n",
2004 name
, u_errorName(errorCode
));
2005 } else if(!uset_containsRange(set
, 0, 0xf5ff) || !uset_containsRange(set
, 0xf700, 0x10ffff)) {
2006 log_err("error: ucnv_getUnicodeSet(%s) does not return an all-Unicode set (minus U+F6xx)\n", name
);
2013 /* test specific sets */
2014 for(i
=0; i
<UPRV_LENGTHOF(nameRanges
); ++i
) {
2015 errorCode
=U_ZERO_ERROR
;
2016 name
=nameRanges
[i
].name
;
2017 cnv
=ucnv_open(name
, &errorCode
);
2018 if(U_FAILURE(errorCode
)) {
2019 log_data_err("error: unable to open converter %s - %s\n",
2020 name
, u_errorName(errorCode
));
2025 ucnv_getUnicodeSet(cnv
, set
, UCNV_ROUNDTRIP_SET
, &errorCode
);
2026 if(U_FAILURE(errorCode
)) {
2027 log_err("error: ucnv_getUnicodeSet(%s) failed - %s\n",
2028 name
, u_errorName(errorCode
));
2030 !uset_containsRange(set
, nameRanges
[i
].start
, nameRanges
[i
].end
) ||
2031 (nameRanges
[i
].start2
>=0 && !uset_containsRange(set
, nameRanges
[i
].start2
, nameRanges
[i
].end2
))
2033 log_err("error: ucnv_getUnicodeSet(%s) does not contain the expected ranges\n", name
);
2034 } else if(nameRanges
[i
].notStart
>=0) {
2035 /* simulate containsAny() with the C API */
2036 uset_complement(set
);
2037 if(!uset_containsRange(set
, nameRanges
[i
].notStart
, nameRanges
[i
].notEnd
)) {
2038 log_err("error: ucnv_getUnicodeSet(%s) contains part of the unexpected range\n", name
);
2045 errorCode
= U_ZERO_ERROR
;
2046 ucnv_getUnicodeSet(NULL
, set
, UCNV_ROUNDTRIP_SET
, &errorCode
);
2047 if (errorCode
!= U_ILLEGAL_ARGUMENT_ERROR
) {
2048 log_err("error: ucnv_getUnicodeSet(NULL) returned wrong status code %s\n", u_errorName(errorCode
));
2050 errorCode
= U_PARSE_ERROR
;
2051 /* Make sure that it does nothing if an error is passed in. Difficult to proper test for. */
2052 ucnv_getUnicodeSet(NULL
, NULL
, UCNV_ROUNDTRIP_SET
, &errorCode
);
2053 if (errorCode
!= U_PARSE_ERROR
) {
2054 log_err("error: ucnv_getUnicodeSet(NULL) returned wrong status code %s\n", u_errorName(errorCode
));