]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/utf16tst.c
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 1998-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
11 * Modification History:
13 * Date Name Description
14 * 05/01/2000 Madhu Creation
15 *******************************************************************************
18 #include "unicode/utypes.h"
19 #include "unicode/ustring.h"
20 #include "unicode/utf16.h"
21 #include "unicode/utf_old.h"
27 // Obsolete macro from obsolete unicode/utf_old.h, for some old test data.
28 #ifndef UTF_ERROR_VALUE
29 # define UTF_ERROR_VALUE 0xffff
32 #if !U_HIDE_OBSOLETE_UTF_OLD_H
33 static void printUChars ( const UChar
* uchars
) {
35 for ( i
= 0 ; i
< u_strlen ( uchars
); i
++) {
36 printf ( " %x " , *( uchars
+ i
));
41 static void TestCodeUnitValues ( void );
42 static void TestCharLength ( void );
43 static void TestGetChar ( void );
44 static void TestNextPrevChar ( void );
45 static void TestNulTerminated ( void );
46 static void TestFwdBack ( void );
47 static void TestSetChar ( void );
48 static void TestAppendChar ( void );
49 static void TestAppend ( void );
50 static void TestSurrogate ( void );
52 void addUTF16Test ( TestNode
** root
);
55 addUTF16Test ( TestNode
** root
)
57 addTest ( root
, & TestCodeUnitValues
, "utf16tst/TestCodeUnitValues" );
58 addTest ( root
, & TestCharLength
, "utf16tst/TestCharLength" );
59 addTest ( root
, & TestGetChar
, "utf16tst/TestGetChar" );
60 addTest ( root
, & TestNextPrevChar
, "utf16tst/TestNextPrevChar" );
61 addTest ( root
, & TestNulTerminated
, "utf16tst/TestNulTerminated" );
62 addTest ( root
, & TestFwdBack
, "utf16tst/TestFwdBack" );
63 addTest ( root
, & TestSetChar
, "utf16tst/TestSetChar" );
64 addTest ( root
, & TestAppendChar
, "utf16tst/TestAppendChar" );
65 addTest ( root
, & TestAppend
, "utf16tst/TestAppend" );
66 addTest ( root
, & TestSurrogate
, "utf16tst/TestSurrogate" );
69 static void TestCodeUnitValues ()
71 static uint16_t codeunit
[]={ 0x0000 , 0xe065 , 0x20ac , 0xd7ff , 0xd800 , 0xd841 , 0xd905 , 0xdbff , 0xdc00 , 0xdc02 , 0xddee , 0xdfff , 0 };
74 for ( i
= 0 ; i
< UPRV_LENGTHOF ( codeunit
); i
++){
76 log_verbose ( "Testing code unit value of %x \n " , c
);
79 #if !U_HIDE_OBSOLETE_UTF_OLD_H
80 ! UTF16_IS_SINGLE ( c
) || UTF16_IS_LEAD ( c
) || UTF16_IS_TRAIL ( c
) ||
82 ! U16_IS_SINGLE ( c
) || U16_IS_LEAD ( c
) || U16_IS_TRAIL ( c
)) {
83 log_err ( "ERROR: %x is a single character \n " , c
);
88 #if !U_HIDE_OBSOLETE_UTF_OLD_H
89 ! UTF16_IS_LEAD ( c
) || UTF16_IS_SINGLE ( c
) || UTF16_IS_TRAIL ( c
) ||
91 ! U16_IS_LEAD ( c
) || U16_IS_SINGLE ( c
) || U16_IS_TRAIL ( c
)){
92 log_err ( "ERROR: %x is a first surrogate \n " , c
);
97 #if !U_HIDE_OBSOLETE_UTF_OLD_H
98 ! UTF16_IS_TRAIL ( c
) || UTF16_IS_SINGLE ( c
) || UTF16_IS_LEAD ( c
) ||
100 ! U16_IS_TRAIL ( c
) || U16_IS_SINGLE ( c
) || U16_IS_LEAD ( c
)) {
101 log_err ( "ERROR: %x is a second surrogate \n " , c
);
107 static void TestCharLength ()
109 static uint32_t codepoint
[]={
123 #if !U_HIDE_OBSOLETE_UTF_OLD_H
126 for ( i
= 0 ; i
< UPRV_LENGTHOF ( codepoint
); i
=( int16_t )( i
+ 2 )){
127 UChar32 c
= codepoint
[ i
+ 1 ];
129 #if !U_HIDE_OBSOLETE_UTF_OLD_H
130 UTF16_CHAR_LENGTH ( c
) != ( uint16_t ) codepoint
[ i
] ||
132 U16_LENGTH ( c
) != ( uint16_t ) codepoint
[ i
]) {
133 log_err ( "The no: of code units for %l x:- Expected: %d Got: %d \n " , c
, codepoint
[ i
], U16_LENGTH ( c
));
135 log_verbose ( "The no: of code units for %l x is %d \n " , c
, U16_LENGTH ( c
));
137 #if !U_HIDE_OBSOLETE_UTF_OLD_H
138 multiple
=( UBool
)( codepoint
[ i
] == 1 ? FALSE
: TRUE
);
139 if ( UTF16_NEED_MULTIPLE_UCHAR ( c
) != multiple
){
140 log_err ( "ERROR: UTF16_NEED_MULTIPLE_UCHAR failed for %l x \n " , c
);
146 static void TestGetChar ()
148 static UChar input
[]={
165 static UChar32 result
[]={
166 /*codepoint-unsafe, codepoint-safe(not strict) codepoint-safe(strict)*/
167 ( UChar32
) 0xfca10000 , 0xdc00 , UTF_ERROR_VALUE
,
168 0x20ac , 0x20ac , 0x20ac ,
169 0x12861 , 0xd841 , UTF_ERROR_VALUE
,
171 0x20402 , 0x20402 , 0x20402 ,
172 0x20402 , 0x20402 , 0x20402 ,
173 0x20806 , 0x20806 , 0x20806 ,
174 0x20806 , 0x20806 , 0x20806 ,
176 0x203ff , 0xd842 , UTF_ERROR_VALUE
,
177 0xd7ff , 0xd7ff , 0xd7ff ,
178 0xfc41 , 0xdc41 , UTF_ERROR_VALUE
,
179 0xe000 , 0xe000 , 0xe000 ,
180 0x11734 , 0xd800 , UTF_ERROR_VALUE
185 for ( offset
= 0 ; offset
< UPRV_LENGTHOF ( input
); offset
++) {
186 if ( 0 < offset
&& offset
< UPRV_LENGTHOF ( input
)- 1 ){
187 #if !U_HIDE_OBSOLETE_UTF_OLD_H
188 UTF16_GET_CHAR_UNSAFE ( input
, offset
, c
);
190 log_err ( "ERROR: UTF16_GET_CHAR_UNSAFE failed for offset= %l d. Expected: %l x Got: %l x \n " , offset
, result
[ i
], c
);
193 U16_GET_UNSAFE ( input
, offset
, c
);
195 log_err ( "ERROR: U16_GET_CHAR_UNSAFE failed for offset= %l d. Expected: %l x Got: %l x \n " , offset
, result
[ i
], c
);
198 expected
= result
[ i
+ 1 ];
199 #if !U_HIDE_OBSOLETE_UTF_OLD_H
200 UTF16_GET_CHAR_SAFE ( input
, 0 , offset
, UPRV_LENGTHOF ( input
), c
, FALSE
);
202 log_err ( "ERROR: UTF16_GET_CHAR_SAFE failed for offset= %l d. Expected: %l x Got: %l x \n " , offset
, expected
, c
);
205 U16_GET ( input
, 0 , offset
, UPRV_LENGTHOF ( input
), c
);
207 log_err ( "ERROR: U16_GET failed for offset= %l d. Expected: %l x Got: %l x \n " , offset
, expected
, c
);
210 U16_GET_OR_FFFD ( input
, 0 , offset
, UPRV_LENGTHOF ( input
), c
);
211 if ( U_IS_SURROGATE ( expected
)) { expected
= 0xfffd ; }
213 log_err ( "ERROR: U16_GET_OR_FFFD failed for offset= %l d. Expected: %l x Got: %l x \n " , offset
, expected
, c
);
215 #if !U_HIDE_OBSOLETE_UTF_OLD_H
216 UTF16_GET_CHAR_SAFE ( input
, 0 , offset
, UPRV_LENGTHOF ( input
), c
, TRUE
);
217 if ( c
!= result
[ i
+ 2 ]){
218 log_err ( "ERROR: UTF16_GET_CHAR_SAFE(strict) failed for offset= %l d. Expected: %l x Got: %l x \n " , offset
, result
[ i
+ 2 ], c
);
225 static void TestNextPrevChar (){
227 static UChar input
[]={ 0x0061 , 0xd800 , 0xdc00 , 0xdbff , 0xdfff , 0x0062 , 0xd841 , 0xd7ff , 0xd841 , 0xdc41 , 0xdc00 , 0x0000 };
228 static UChar32 result
[]={
229 /*next_unsafe next_safe_ns next_safe_s prev_unsafe prev_safe_ns prev_safe_s*/
230 0x0061 , 0x0061 , 0x0061 , 0x0000 , 0x0000 , 0x0000 ,
231 0x10000 , 0x10000 , 0x10000 , 0x120400 , 0xdc00 , UTF_ERROR_VALUE
,
232 0xdc00 , 0xdc00 , UTF_ERROR_VALUE
, 0x20441 , 0x20441 , 0x20441 ,
233 0x10ffff , 0x10ffff , 0x10ffff , 0xd841 , 0xd841 , UTF_ERROR_VALUE
,
234 0xdfff , 0xdfff , UTF_ERROR_VALUE
, 0xd7ff , 0xd7ff , 0xd7ff ,
235 0x0062 , 0x0062 , 0x0062 , 0xd841 , 0xd841 , UTF_ERROR_VALUE
,
236 0x1ffff , 0xd841 , UTF_ERROR_VALUE
, 0x0062 , 0x0062 , 0x0062 ,
237 0xd7ff , 0xd7ff , 0xd7ff , 0x10ffff , 0x10ffff , 0x10ffff ,
238 0x20441 , 0x20441 , 0x20441 , 0xdbff , 0xdbff , UTF_ERROR_VALUE
,
239 0xdc41 , 0xdc41 , UTF_ERROR_VALUE
, 0x10000 , 0x10000 , 0x10000 ,
240 0xdc00 , 0xdc00 , UTF_ERROR_VALUE
, 0xd800 , 0xd800 , UTF_ERROR_VALUE
,
241 0x0000 , 0x0000 , 0x0000 , 0x0061 , 0x0061 , 0x0061
243 static uint16_t movedOffset
[]={
244 /*next_unsafe next_safe_ns next_safe_s prev_unsafe prev_safe_ns prev_safe_s*/
260 UChar32 c
= 0x0000 , expected
;
262 uint16_t offset
= 0 , setOffset
= 0 ;
263 for ( offset
= 0 ; offset
< UPRV_LENGTHOF ( input
); offset
++){
265 #if !U_HIDE_OBSOLETE_UTF_OLD_H
266 UTF16_NEXT_CHAR_UNSAFE ( input
, setOffset
, c
);
267 if ( setOffset
!= movedOffset
[ i
]){
268 log_err ( "ERROR: UTF16_NEXT_CHAR_UNSAFE failed to move the offset correctly at %d \n ExpectedOffset: %d Got %d \n " ,
269 offset
, movedOffset
[ i
], setOffset
);
272 log_err ( "ERROR: UTF16_NEXT_CHAR_UNSAFE failed for offset= %l d. Expected: %l x Got: %l x \n " , offset
, result
[ i
], c
);
276 U16_NEXT_UNSAFE ( input
, setOffset
, c
);
277 if ( setOffset
!= movedOffset
[ i
]){
278 log_err ( "ERROR: U16_NEXT_CHAR_UNSAFE failed to move the offset correctly at %d \n ExpectedOffset: %d Got %d \n " ,
279 offset
, movedOffset
[ i
], setOffset
);
282 log_err ( "ERROR: U16_NEXT_CHAR_UNSAFE failed for offset= %l d. Expected: %l x Got: %l x \n " , offset
, result
[ i
], c
);
284 expected
= result
[ i
+ 1 ];
285 #if !U_HIDE_OBSOLETE_UTF_OLD_H
287 UTF16_NEXT_CHAR_SAFE ( input
, setOffset
, UPRV_LENGTHOF ( input
), c
, FALSE
);
288 if ( setOffset
!= movedOffset
[ i
+ 1 ]){
289 log_err ( "ERROR: UTF16_NEXT_CHAR_SAFE failed to move the offset correctly at %d \n ExpectedOffset: %d Got %d \n " ,
290 offset
, movedOffset
[ i
+ 1 ], setOffset
);
293 log_err ( "ERROR: UTF16_NEXT_CHAR_SAFE failed for input= %l d. Expected: %l x Got: %l x \n " , offset
, expected
, c
);
297 U16_NEXT ( input
, setOffset
, UPRV_LENGTHOF ( input
), c
);
298 if ( setOffset
!= movedOffset
[ i
+ 1 ]){
299 log_err ( "ERROR: U16_NEXT failed to move the offset correctly at %d \n ExpectedOffset: %d Got %d \n " ,
300 offset
, movedOffset
[ i
+ 1 ], setOffset
);
303 log_err ( "ERROR: U16_NEXT failed for input= %l d. Expected: %l x Got: %l x \n " , offset
, expected
, c
);
307 U16_NEXT_OR_FFFD ( input
, setOffset
, UPRV_LENGTHOF ( input
), c
);
308 if ( setOffset
!= movedOffset
[ i
+ 1 ]){
309 log_err ( "ERROR: U16_NEXT_OR_FFFD failed to move the offset correctly at %d \n ExpectedOffset: %d Got %d \n " ,
310 offset
, movedOffset
[ i
+ 1 ], setOffset
);
312 if ( U_IS_SURROGATE ( expected
)) { expected
= 0xfffd ; }
314 log_err ( "ERROR: U16_NEXT_OR_FFFD failed for input= %l d. Expected: %l x Got: %l x \n " , offset
, expected
, c
);
316 #if !U_HIDE_OBSOLETE_UTF_OLD_H
318 UTF16_NEXT_CHAR_SAFE ( input
, setOffset
, UPRV_LENGTHOF ( input
), c
, TRUE
);
319 if ( setOffset
!= movedOffset
[ i
+ 1 ]){
320 log_err ( "ERROR: UTF16_NEXT_CHAR_SAFE(strict) failed to move the offset correctly at %d \n ExpectedOffset: %d Got %d \n " ,
321 offset
, movedOffset
[ i
+ 2 ], setOffset
);
323 if ( c
!= result
[ i
+ 2 ]){
324 log_err ( "ERROR: UTF16_NEXT_CHAR_SAFE(strict) failed for input= %l d. Expected: %l x Got: %l x \n " , offset
, result
[ i
+ 2 ], c
);
330 for ( offset
=( uint16_t ) UPRV_LENGTHOF ( input
); offset
> 0 ; -- offset
){
332 #if !U_HIDE_OBSOLETE_UTF_OLD_H
333 UTF16_PREV_CHAR_UNSAFE ( input
, setOffset
, c
);
334 if ( setOffset
!= movedOffset
[ i
+ 3 ]){
335 log_err ( "ERROR: UTF16_PREV_CHAR_UNSAFE failed to move the offset correctly at %d \n ExpectedOffset: %d Got %d \n " ,
336 offset
, movedOffset
[ i
+ 3 ], setOffset
);
338 if ( c
!= result
[ i
+ 3 ]){
339 log_err ( "ERROR: UTF16_PREV_CHAR_UNSAFE failed for offset= %l d. Expected: %l x Got: %l x \n " , offset
, result
[ i
+ 3 ], c
);
343 U16_PREV_UNSAFE ( input
, setOffset
, c
);
344 if ( setOffset
!= movedOffset
[ i
+ 3 ]){
345 log_err ( "ERROR: U16_PREV_CHAR_UNSAFE failed to move the offset correctly at %d \n ExpectedOffset: %d Got %d \n " ,
346 offset
, movedOffset
[ i
+ 3 ], setOffset
);
348 if ( c
!= result
[ i
+ 3 ]){
349 log_err ( "ERROR: U16_PREV_CHAR_UNSAFE failed for offset= %l d. Expected: %l x Got: %l x \n " , offset
, result
[ i
+ 3 ], c
);
351 #if !U_HIDE_OBSOLETE_UTF_OLD_H
353 UTF16_PREV_CHAR_SAFE ( input
, 0 , setOffset
, c
, FALSE
);
354 if ( setOffset
!= movedOffset
[ i
+ 4 ]){
355 log_err ( "ERROR: UTF16_PREV_CHAR_SAFE failed to move the offset correctly at %d \n ExpectedOffset: %d Got %d \n " ,
356 offset
, movedOffset
[ i
+ 4 ], setOffset
);
358 if ( c
!= result
[ i
+ 4 ]){
359 log_err ( "ERROR: UTF16_PREV_CHAR_SAFE failed for input= %l d. Expected: %l x Got: %l x \n " , offset
, result
[ i
+ 4 ], c
);
363 U16_PREV ( input
, 0 , setOffset
, c
);
364 if ( setOffset
!= movedOffset
[ i
+ 4 ]){
365 log_err ( "ERROR: U16_PREV failed to move the offset correctly at %d \n ExpectedOffset: %d Got %d \n " ,
366 offset
, movedOffset
[ i
+ 4 ], setOffset
);
368 expected
= result
[ i
+ 4 ];
370 log_err ( "ERROR: U16_PREV failed for input= %l d. Expected: %l x Got: %l x \n " , offset
, expected
, c
);
374 U16_PREV_OR_FFFD ( input
, 0 , setOffset
, c
);
375 if ( setOffset
!= movedOffset
[ i
+ 4 ]){
376 log_err ( "ERROR: U16_PREV_OR_FFFD failed to move the offset correctly at %d \n ExpectedOffset: %d Got %d \n " ,
377 offset
, movedOffset
[ i
+ 4 ], setOffset
);
379 if ( U_IS_SURROGATE ( expected
)) { expected
= 0xfffd ; }
381 log_err ( "ERROR: U16_PREV_OR_FFFD failed for input= %l d. Expected: %l x Got: %l x \n " , offset
, expected
, c
);
383 #if !U_HIDE_OBSOLETE_UTF_OLD_H
385 UTF16_PREV_CHAR_SAFE ( input
, 0 , setOffset
, c
, TRUE
);
386 if ( setOffset
!= movedOffset
[ i
+ 5 ]){
387 log_err ( "ERROR: UTF16_PREV_CHAR_SAFE(strict) failed to move the offset correctly at %d \n ExpectedOffset: %d Got %d \n " ,
388 offset
, movedOffset
[ i
+ 5 ], setOffset
);
390 if ( c
!= result
[ i
+ 5 ]){
391 log_err ( "ERROR: UTF16_PREV_CHAR_SAFE(strict) failed for input= %l d. Expected: %l x Got: %l x \n " , offset
, result
[ i
+ 5 ], c
);
399 /* keep this in sync with utf8tst.c's TestNulTerminated() */
400 static void TestNulTerminated () {
401 static const UChar input
[]={
403 /* 1 */ 0xd801 , 0xdc01 ,
410 static const UChar32 result
[]={
419 UChar32 c
, c2
, expected
;
420 int32_t i0
, i
= 0 , j
, k
, expectedIndex
;
424 U16_NEXT ( input
, i
, - 1 , c
);
425 expected
= result
[ cpIndex
];
427 log_err ( "U16_NEXT(from %d )=U+ %0 4x != U+ %0 4x \n " , i0
, c
, expected
);
430 U16_NEXT_OR_FFFD ( input
, j
, - 1 , c
);
431 if ( U_IS_SURROGATE ( expected
)) { expected
= 0xfffd ; }
433 log_err ( "U16_NEXT_OR_FFFD(from %d )=U+ %0 4x != U+ %0 4x \n " , i0
, c
, expected
);
436 log_err ( "U16_NEXT_OR_FFFD() moved to index %d but U16_NEXT() moved to %d \n " , j
, i
);
439 U16_FWD_1 ( input
, j
, - 1 );
441 log_err ( "U16_FWD_1() moved to index %d but U16_NEXT() moved to %d \n " , j
, i
);
445 * Move by this many code points from the start.
446 * U16_FWD_N() stops at the end of the string, that is, at the NUL if necessary.
448 expectedIndex
= ( c
== 0 ) ? i
- 1 : i
;
450 U16_FWD_N ( input
, k
, - 1 , cpIndex
);
451 if ( k
!= expectedIndex
) {
452 log_err ( "U16_FWD_N(code points from 0) moved to index %d but expected %d \n " , k
, expectedIndex
);
459 U16_NEXT ( input
, i
, - 1 , c
);
461 U16_GET ( input
, 0 , j
, - 1 , c2
);
463 log_err ( "U16_NEXT(from %d )=U+ %0 4x != U+ %0 4x=U16_GET(at %d ) \n " , i0
, c
, c2
, j
);
465 U16_GET_OR_FFFD ( input
, 0 , j
, - 1 , c2
);
466 expected
= U_IS_SURROGATE ( c
) ? 0xfffd : c
;
468 log_err ( "U16_NEXT_OR_FFFD(from %d )=U+ %0 4x != U+ %0 4x=U16_GET_OR_FFFD(at %d ) \n " , i0
, expected
, c2
, j
);
470 /* U16_SET_CP_LIMIT moves from a non-lead byte to the limit of the code point */
472 U16_SET_CP_LIMIT ( input
, 0 , k
, - 1 );
474 log_err ( "U16_NEXT() moved to %d but U16_SET_CP_LIMIT( %d ) moved to %d \n " , i
, j
+ 1 , k
);
480 static void TestFwdBack (){
481 static UChar input
[]={ 0x0061 , 0xd800 , 0xdc00 , 0xdbff , 0xdfff , 0x0062 , 0xd841 , 0xd7ff , 0xd841 , 0xdc41 , 0xdc00 , 0x0000 };
482 static uint16_t fwd_unsafe
[] ={ 1 , 3 , 5 , 6 , 8 , 10 , 11 , 12 };
483 static uint16_t fwd_safe
[] ={ 1 , 3 , 5 , 6 , 7 , 8 , 10 , 11 , 12 };
484 static uint16_t back_unsafe
[]={ 11 , 9 , 8 , 7 , 6 , 5 , 3 , 1 , 0 };
485 static uint16_t back_safe
[] ={ 11 , 10 , 8 , 7 , 6 , 5 , 3 , 1 , 0 };
487 static uint16_t Nvalue
[]= { 0 , 1 , 2 , 3 , 1 , 2 , 1 };
488 static uint16_t fwd_N_unsafe
[] ={ 0 , 1 , 5 , 10 , 11 };
489 static uint16_t fwd_N_safe
[] ={ 0 , 1 , 5 , 8 , 10 , 12 , 12 }; /*safe macro keeps it at the end of the string */
490 static uint16_t back_N_unsafe
[]={ 12 , 11 , 8 , 5 , 3 };
491 static uint16_t back_N_safe
[] ={ 12 , 11 , 8 , 5 , 3 , 0 , 0 };
493 uint16_t offunsafe
= 0 , offsafe
= 0 ;
495 #if !U_HIDE_OBSOLETE_UTF_OLD_H
496 while ( offunsafe
< UPRV_LENGTHOF ( input
)){
497 UTF16_FWD_1_UNSAFE ( input
, offunsafe
);
498 if ( offunsafe
!= fwd_unsafe
[ i
]){
499 log_err ( "ERROR: Forward_unsafe offset expected: %d , Got: %d \n " , fwd_unsafe
[ i
], offunsafe
);
504 offunsafe
= 0 , offsafe
= 0 ;
506 while ( offunsafe
< UPRV_LENGTHOF ( input
)){
507 U16_FWD_1_UNSAFE ( input
, offunsafe
);
508 if ( offunsafe
!= fwd_unsafe
[ i
]){
509 log_err ( "ERROR: U16_FWD_1_UNSAFE offset expected: %d , Got: %d \n " , fwd_unsafe
[ i
], offunsafe
);
513 #if !U_HIDE_OBSOLETE_UTF_OLD_H
514 offunsafe
= 0 , offsafe
= 0 ;
516 while ( offsafe
< UPRV_LENGTHOF ( input
)){
517 UTF16_FWD_1_SAFE ( input
, offsafe
, UPRV_LENGTHOF ( input
));
518 if ( offsafe
!= fwd_safe
[ i
]){
519 log_err ( "ERROR: Forward_safe offset expected: %d , Got: %d \n " , fwd_safe
[ i
], offsafe
);
524 offunsafe
= 0 , offsafe
= 0 ;
526 while ( offsafe
< UPRV_LENGTHOF ( input
)){
527 U16_FWD_1 ( input
, offsafe
, UPRV_LENGTHOF ( input
));
528 if ( offsafe
!= fwd_safe
[ i
]){
529 log_err ( "ERROR: U16_FWD_1 offset expected: %d , Got: %d \n " , fwd_safe
[ i
], offsafe
);
533 #if !U_HIDE_OBSOLETE_UTF_OLD_H
534 offunsafe
= UPRV_LENGTHOF ( input
);
535 offsafe
= UPRV_LENGTHOF ( input
);
537 while ( offunsafe
> 0 ){
538 UTF16_BACK_1_UNSAFE ( input
, offunsafe
);
539 if ( offunsafe
!= back_unsafe
[ i
]){
540 log_err ( "ERROR: Backward_unsafe offset expected: %d , Got: %d \n " , back_unsafe
[ i
], offunsafe
);
545 offunsafe
= UPRV_LENGTHOF ( input
);
546 offsafe
= UPRV_LENGTHOF ( input
);
548 while ( offunsafe
> 0 ){
549 U16_BACK_1_UNSAFE ( input
, offunsafe
);
550 if ( offunsafe
!= back_unsafe
[ i
]){
551 log_err ( "ERROR: U16_BACK_1_UNSAFE offset expected: %d , Got: %d \n " , back_unsafe
[ i
], offunsafe
);
555 #if !U_HIDE_OBSOLETE_UTF_OLD_H
556 offunsafe
= UPRV_LENGTHOF ( input
);
557 offsafe
= UPRV_LENGTHOF ( input
);
560 UTF16_BACK_1_SAFE ( input
, 0 , offsafe
);
561 if ( offsafe
!= back_safe
[ i
]){
562 log_err ( "ERROR: Backward_safe offset expected: %d , Got: %d \n " , back_unsafe
[ i
], offsafe
);
567 offunsafe
= UPRV_LENGTHOF ( input
);
568 offsafe
= UPRV_LENGTHOF ( input
);
571 U16_BACK_1 ( input
, 0 , offsafe
);
572 if ( offsafe
!= back_safe
[ i
]){
573 log_err ( "ERROR: U16_BACK_1 offset expected: %d , Got: %d \n " , back_unsafe
[ i
], offsafe
);
580 #if !U_HIDE_OBSOLETE_UTF_OLD_H
581 for ( i
= 0 ; i
< UPRV_LENGTHOF ( Nvalue
)- 2 ; i
++){ /*didn't want it to fail(we assume 0<i<length)*/
582 UTF16_FWD_N_UNSAFE ( input
, offunsafe
, Nvalue
[ i
]);
583 if ( offunsafe
!= fwd_N_unsafe
[ i
]){
584 log_err ( "ERROR: Forward_N_unsafe offset expected: %d , Got: %d \n " , fwd_N_unsafe
[ i
], offunsafe
);
589 for ( i
= 0 ; i
< UPRV_LENGTHOF ( Nvalue
)- 2 ; i
++){ /*didn't want it to fail(we assume 0<i<length)*/
590 U16_FWD_N_UNSAFE ( input
, offunsafe
, Nvalue
[ i
]);
591 if ( offunsafe
!= fwd_N_unsafe
[ i
]){
592 log_err ( "ERROR: U16_FWD_N_UNSAFE offset expected: %d , Got: %d \n " , fwd_N_unsafe
[ i
], offunsafe
);
595 #if !U_HIDE_OBSOLETE_UTF_OLD_H
597 for ( i
= 0 ; i
< UPRV_LENGTHOF ( Nvalue
); i
++){
598 UTF16_FWD_N_SAFE ( input
, offsafe
, UPRV_LENGTHOF ( input
), Nvalue
[ i
]);
599 if ( offsafe
!= fwd_N_safe
[ i
]){
600 log_err ( "ERROR: Forward_N_safe offset expected: %d , Got: %d \n " , fwd_N_safe
[ i
], offsafe
);
605 for ( i
= 0 ; i
< UPRV_LENGTHOF ( Nvalue
); i
++){
606 U16_FWD_N ( input
, offsafe
, UPRV_LENGTHOF ( input
), Nvalue
[ i
]);
607 if ( offsafe
!= fwd_N_safe
[ i
]){
608 log_err ( "ERROR: U16_FWD_N offset expected: %d , Got: %d \n " , fwd_N_safe
[ i
], offsafe
);
611 #if !U_HIDE_OBSOLETE_UTF_OLD_H
612 offunsafe
= UPRV_LENGTHOF ( input
);
613 for ( i
= 0 ; i
< UPRV_LENGTHOF ( Nvalue
)- 2 ; i
++){
614 UTF16_BACK_N_UNSAFE ( input
, offunsafe
, Nvalue
[ i
]);
615 if ( offunsafe
!= back_N_unsafe
[ i
]){
616 log_err ( "ERROR: backward_N_unsafe offset expected: %d , Got: %d \n " , back_N_unsafe
[ i
], offunsafe
);
620 offunsafe
= UPRV_LENGTHOF ( input
);
621 for ( i
= 0 ; i
< UPRV_LENGTHOF ( Nvalue
)- 2 ; i
++){
622 U16_BACK_N_UNSAFE ( input
, offunsafe
, Nvalue
[ i
]);
623 if ( offunsafe
!= back_N_unsafe
[ i
]){
624 log_err ( "ERROR: U16_BACK_N_UNSAFE offset expected: %d , Got: %d \n " , back_N_unsafe
[ i
], offunsafe
);
627 #if !U_HIDE_OBSOLETE_UTF_OLD_H
628 offsafe
= UPRV_LENGTHOF ( input
);
629 for ( i
= 0 ; i
< UPRV_LENGTHOF ( Nvalue
); i
++){
630 UTF16_BACK_N_SAFE ( input
, 0 , offsafe
, Nvalue
[ i
]);
631 if ( offsafe
!= back_N_safe
[ i
]){
632 log_err ( "ERROR: backward_N_safe offset expected: %d , Got: %d \n " , back_N_safe
[ i
], offsafe
);
636 offsafe
= UPRV_LENGTHOF ( input
);
637 for ( i
= 0 ; i
< UPRV_LENGTHOF ( Nvalue
); i
++){
638 U16_BACK_N ( input
, 0 , offsafe
, Nvalue
[ i
]);
639 if ( offsafe
!= back_N_safe
[ i
]){
640 log_err ( "ERROR: U16_BACK_N offset expected: %d , Got: %d \n " , back_N_safe
[ i
], offsafe
);
645 static void TestSetChar (){
646 static UChar input
[]={ 0x0061 , 0xd800 , 0xdc00 , 0xdbff , 0xdfff , 0x0062 , 0xd841 , 0xd7ff , 0xd841 , 0xdc41 , 0xdc00 , 0x0000 };
647 static uint16_t start_unsafe
[]={ 0 , 1 , 1 , 3 , 3 , 5 , 6 , 7 , 8 , 8 , 9 , 11 };
648 static uint16_t start_safe
[] ={ 0 , 1 , 1 , 3 , 3 , 5 , 6 , 7 , 8 , 8 , 10 , 11 };
649 static uint16_t limit_unsafe
[]={ 0 , 1 , 3 , 3 , 5 , 5 , 6 , 8 , 8 , 10 , 10 , 11 };
650 static uint16_t limit_safe
[] ={ 0 , 1 , 3 , 3 , 5 , 5 , 6 , 7 , 8 , 10 , 10 , 11 };
653 uint16_t offset
= 0 , setOffset
= 0 ;
654 for ( offset
= 0 ; offset
< UPRV_LENGTHOF ( input
); offset
++){
655 #if !U_HIDE_OBSOLETE_UTF_OLD_H
657 UTF16_SET_CHAR_START_UNSAFE ( input
, setOffset
);
658 if ( setOffset
!= start_unsafe
[ i
]){
659 log_err ( "ERROR: UTF16_SET_CHAR_START_UNSAFE failed for offset= %l d. Expected: %l x Got: %l x \n " , offset
, start_unsafe
[ i
], setOffset
);
663 U16_SET_CP_START_UNSAFE ( input
, setOffset
);
664 if ( setOffset
!= start_unsafe
[ i
]){
665 log_err ( "ERROR: U16_SET_CHAR_START_UNSAFE failed for offset= %l d. Expected: %l x Got: %l x \n " , offset
, start_unsafe
[ i
], setOffset
);
667 #if !U_HIDE_OBSOLETE_UTF_OLD_H
669 UTF16_SET_CHAR_START_SAFE ( input
, 0 , setOffset
);
670 if ( setOffset
!= start_safe
[ i
]){
671 log_err ( "ERROR: UTF16_SET_CHAR_START_SAFE failed for offset= %l d. Expected: %l x Got: %l x \n " , offset
, start_safe
[ i
], setOffset
);
675 U16_SET_CP_START ( input
, 0 , setOffset
);
676 if ( setOffset
!= start_safe
[ i
]){
677 log_err ( "ERROR: U16_SET_CHAR_START failed for offset= %l d. Expected: %l x Got: %l x \n " , offset
, start_safe
[ i
], setOffset
);
681 #if !U_HIDE_OBSOLETE_UTF_OLD_H
683 UTF16_SET_CHAR_LIMIT_UNSAFE ( input
, setOffset
);
684 if ( setOffset
!= limit_unsafe
[ i
]){
685 log_err ( "ERROR: UTF16_SET_CHAR_LIMIT_UNSAFE failed for offset= %l d. Expected: %l x Got: %l x \n " , offset
, limit_unsafe
[ i
], setOffset
);
689 U16_SET_CP_LIMIT_UNSAFE ( input
, setOffset
);
690 if ( setOffset
!= limit_unsafe
[ i
]){
691 log_err ( "ERROR: U16_SET_CHAR_LIMIT_UNSAFE failed for offset= %l d. Expected: %l x Got: %l x \n " , offset
, limit_unsafe
[ i
], setOffset
);
696 U16_SET_CP_LIMIT ( input
, 0 , setOffset
, UPRV_LENGTHOF ( input
));
697 if ( setOffset
!= limit_safe
[ i
]){
698 log_err ( "ERROR: U16_SET_CHAR_LIMIT failed for offset= %l d. Expected: %l x Got: %l x \n " , offset
, limit_safe
[ i
], setOffset
);
705 static void TestAppendChar (){
706 #if !U_HIDE_OBSOLETE_UTF_OLD_H
707 static UChar s
[ 5 ]={ 0x0061 , 0x0062 , 0x0063 , 0x0064 , 0x0000 };
708 static uint32_t test
[]={
709 /*append-position(unsafe), CHAR to be appended */
716 /*append-position(safe), CHAR to be appended */
726 static uint16_t movedOffset
[]={
727 /*offset-moved-to(unsafe)*/
728 2 , /*for append-pos: 0 , CHAR 0x20441*/
733 /*offse-moved-to(safe)*/
734 2 , /*for append-pos: 0, CHAR 0x20441*/
743 static UChar result
[][ 5 ]={
745 { 0xd841 , 0xdc41 , 0x0063 , 0x0064 , 0x0000 },
746 { 0x0061 , 0x0062 , 0x0028 , 0x0064 , 0x0000 },
747 { 0x0061 , 0x0062 , 0xdc00 , 0x0064 , 0x0000 },
748 { 0x0061 , 0x0062 , 0x0063 , 0xd800 , 0x0000 },
749 { 0x0061 , 0xd841 , 0xdc02 , 0x0064 , 0x0000 },
752 { 0xd841 , 0xdc41 , 0x0063 , 0x0064 , 0x0000 },
753 { 0x0061 , 0x0062 , 0xdc00 , 0x0064 , 0x0000 },
754 { 0x0061 , 0x0062 , 0x0063 , 0xd800 , 0x0000 },
755 { 0x0061 , 0xd841 , 0xdc02 , 0x0064 , 0x0000 },
756 { 0x0061 , 0x0062 , 0x0063 , UTF_ERROR_VALUE
, 0x0000 },
757 { 0x0061 , 0x0062 , 0x0063 , UTF_ERROR_VALUE
, 0x0000 },
758 { 0x0061 , 0x0062 , 0xd801 , 0xdc02 , 0x0000 },
763 UChar
* str
=( UChar
*) malloc ( sizeof ( UChar
) * ( u_strlen ( s
)+ 1 ));
765 for ( i
= 0 ; i
< UPRV_LENGTHOF ( test
); i
=( uint16_t )( i
+ 2 )){
768 offset
=( uint16_t ) test
[ i
];
769 UTF16_APPEND_CHAR_UNSAFE ( str
, offset
, test
[ i
+ 1 ]);
770 if ( offset
!= movedOffset
[ count
]){
771 log_err ( "ERROR: UTF16_APPEND_CHAR_UNSAFE failed to move the offset correctly for count= %d . \n ExpectedOffset= %d currentOffset= %d \n " ,
772 count
, movedOffset
[ count
], offset
);
775 if ( u_strcmp ( str
, result
[ count
]) != 0 ){
776 log_err ( "ERROR: UTF16_APPEND_CHAR_UNSAFE failed for count= %d . Expected:" , count
);
777 printUChars ( result
[ count
]);
784 offset
=( uint16_t ) test
[ i
];
785 UTF16_APPEND_CHAR_SAFE ( str
, offset
, ( uint16_t ) u_strlen ( str
), test
[ i
+ 1 ]);
786 if ( offset
!= movedOffset
[ count
]){
787 log_err ( "ERROR: UTF16_APPEND_CHAR_SAFE failed to move the offset correctly for count= %d . \n ExpectedOffset= %d currentOffset= %d \n " ,
788 count
, movedOffset
[ count
], offset
);
791 if ( u_strcmp ( str
, result
[ count
]) != 0 ){
792 log_err ( "ERROR: UTF16_APPEND_CHAR_SAFE failed for count= %d . Expected:" , count
);
793 printUChars ( result
[ count
]);
805 static void TestAppend () {
806 static const UChar32 codePoints
[]={
807 0x61 , 0xdf , 0x901 , 0x3040 ,
808 0xac00 , 0xd800 , 0xdbff , 0xdcde ,
809 0xdffd , 0xe000 , 0xffff , 0x10000 ,
810 0x12345 , 0xe0021 , 0x10ffff , 0x110000 ,
811 0x234567 , 0x7fffffff , - 1 , - 1000 ,
814 static const UChar expectUnsafe
[]={
815 0x61 , 0xdf , 0x901 , 0x3040 ,
816 0xac00 , 0xd800 , 0xdbff , 0xdcde ,
817 0xdffd , 0xe000 , 0xffff , 0xd800 , 0xdc00 ,
818 0xd808 , 0xdf45 , 0xdb40 , 0xdc21 , 0xdbff , 0xdfff , /* not 0x110000 */
819 /* none from this line */
822 0x61 , 0xdf , 0x901 , 0x3040 ,
823 0xac00 , 0xd800 , 0xdbff , 0xdcde ,
824 0xdffd , 0xe000 , 0xffff , 0xd800 , 0xdc00 ,
825 0xd808 , 0xdf45 , 0xdb40 , 0xdc21 , 0xdbff , 0xdfff , /* not 0x110000 */
826 /* none from this line */
833 UBool isError
, expectIsError
, wrongIsError
;
836 for ( i
= 0 ; i
< UPRV_LENGTHOF ( codePoints
); ++ i
) {
838 if ( c
< 0 || 0x10ffff < c
) {
839 continue ; /* skip non-code points for U16_APPEND_UNSAFE */
842 U16_APPEND_UNSAFE ( buffer
, length
, c
);
844 if ( length
!= UPRV_LENGTHOF ( expectUnsafe
) || 0 != memcmp ( buffer
, expectUnsafe
, length
* U_SIZEOF_UCHAR
)) {
845 log_err ( "U16_APPEND_UNSAFE did not generate the expected output \n " );
850 for ( i
= 0 ; i
< UPRV_LENGTHOF ( codePoints
); ++ i
) {
852 expectIsError
= c
< 0 || 0x10ffff < c
; /* || U_IS_SURROGATE(c); */ /* surrogates in UTF-32 shouldn't be used, but it's okay to pass them around internally. */
855 U16_APPEND ( buffer
, length
, UPRV_LENGTHOF ( buffer
), c
, isError
);
856 wrongIsError
|= isError
!= expectIsError
;
859 log_err ( "U16_APPEND did not set isError correctly \n " );
861 if ( length
!= UPRV_LENGTHOF ( expectSafe
) || 0 != memcmp ( buffer
, expectSafe
, length
* U_SIZEOF_UCHAR
)) {
862 log_err ( "U16_APPEND did not generate the expected output \n " );
866 static void TestSurrogate (){
867 static UChar32 s
[] = { 0x10000 , 0x10ffff , 0x50000 , 0x100000 , 0x1abcd };
870 UChar first
= U16_LEAD ( s
[ i
]);
871 UChar second
= U16_TRAIL ( s
[ i
]);
872 /* algorithm from the Unicode consortium */
873 UChar firstresult
= ( UChar
)((( s
[ i
] - 0x10000 ) / 0x400 ) + 0xD800 );
874 UChar secondresult
= ( UChar
)((( s
[ i
] - 0x10000 ) % 0x400 ) + 0xDC00 );
877 #if !U_HIDE_OBSOLETE_UTF_OLD_H
878 first
!= UTF16_LEAD ( s
[ i
]) || first
!= UTF_FIRST_SURROGATE ( s
[ i
]) ||
880 first
!= firstresult
) {
881 log_err ( "Failure in first surrogate in 0x %x expected to be 0x %x \n " ,
885 #if !U_HIDE_OBSOLETE_UTF_OLD_H
886 second
!= UTF16_TRAIL ( s
[ i
]) || second
!= UTF_SECOND_SURROGATE ( s
[ i
]) ||
888 second
!= secondresult
) {
889 log_err ( "Failure in second surrogate in 0x %x expected to be 0x %x \n " ,