/******************************************************************** * COPYRIGHT: * Copyright (c) 1998-1999, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ /* * File test.c * * Modification History: * * Date Name Description * 05/01/2000 Madhu Creation ******************************************************************************* */ #include "unicode/utypes.h" #include "unicode/utf16.h" #include "unicode/ustring.h" #include "cmemory.h" #include "cstring.h" #include "cintltst.h" #include void printUChars(const UChar *uchars); void TestCodeUnitValues(); void TestCharLength(); void TestGetChar(); void TestNextPrevChar(); void TestFwdBack(); void TestSetChar(); void TestAppendChar(); void addUTF16Test(TestNode** root) { addTest(root, &TestCodeUnitValues, "utf16tst/TestCodeUnitValues"); addTest(root, &TestCharLength, "utf16tst/TestCharLength" ); addTest(root, &TestGetChar, "utf16tst/TestGetChar" ); addTest(root, &TestNextPrevChar, "utf16tst/TestNextPrevChar" ); addTest(root, &TestFwdBack, "utf16tst/TestFwdBack" ); addTest(root, &TestSetChar, "utf16tst/TestSetChar" ); addTest(root, &TestAppendChar, "utf16tst/TestAppendChar" ); } void TestCodeUnitValues() { static uint16_t codeunit[]={0x0000,0xe065,0x20ac,0xd7ff,0xd800,0xd841,0xd905,0xdbff,0xdc00,0xdc02,0xddee,0xdfff,0}; int16_t i; for(i=0; i= 4 && i< 8){ if(!UTF16_IS_LEAD(c) || UTF16_IS_SINGLE(c) || UTF16_IS_TRAIL(c)){ log_err("ERROR: %x is a first surrogate\n", c); } } if(i >= 8 && i< 12){ if(!UTF16_IS_TRAIL(c) || UTF16_IS_SINGLE(c) || UTF16_IS_LEAD(c)){ log_err("ERROR: %x is a second surrogate\n", c); } } } } void TestCharLength() { static uint32_t codepoint[]={ 1, 0x0061, 1, 0xe065, 1, 0x20ac, 2, 0x20402, 2, 0x23456, 2, 0x24506, 2, 0x20402, 2, 0x10402, 1, 0xd7ff, 1, 0xe000 }; int16_t i; UBool multiple; for(i=0; i 0; --offset){ setOffset=offset; UTF16_PREV_CHAR_UNSAFE(input, setOffset, c); if(setOffset != movedOffset[i+3]){ log_err("ERROR: UTF16_PREV_CHAR_UNSAFE failed to move the offset correctly at %d\n ExpectedOffset:%d Got %d\n", offset, movedOffset[i+3], setOffset); } if(c != result[i+3]){ log_err("ERROR: UTF16_PREV_CHAR_UNSAFE failed for offset=%ld. Expected:%lx Got:%lx\n", offset, result[i+3], c); } setOffset=offset; UTF16_PREV_CHAR_SAFE(input, 0, setOffset, c, FALSE); if(setOffset != movedOffset[i+4]){ log_err("ERROR: UTF16_PREV_CHAR_SAFE failed to move the offset correctly at %d\n ExpectedOffset:%d Got %d\n", offset, movedOffset[i+4], setOffset); } if(c != result[i+4]){ log_err("ERROR: UTF16_PREV_CHAR_SAFE failed for input=%ld. Expected:%lx Got:%lx\n", offset, result[i+4], c); } setOffset=offset; UTF16_PREV_CHAR_SAFE_LENIENT(input, 0, setOffset, c); if(setOffset != movedOffset[i+4]){ log_err("ERROR: UTF16_PREV_CHAR_SAFE failed to move the offset correctly at %d\n ExpectedOffset:%d Got %d\n", offset, movedOffset[i+4], setOffset); } if(c != result[i+4]){ log_err("ERROR: UTF16_PREV_CHAR_SAFE failed for input=%ld. Expected:%lx Got:%lx\n", offset, result[i+4], c); } setOffset=offset; UTF16_PREV_CHAR_SAFE(input, 0, setOffset, c, TRUE); if(setOffset != movedOffset[i+5]){ log_err("ERROR: UTF16_PREV_CHAR_SAFE(strict) failed to move the offset correctly at %d\n ExpectedOffset:%d Got %d\n", offset, movedOffset[i+5], setOffset); } if(c != result[i+5]){ log_err("ERROR: UTF16_PREV_CHAR_SAFE(strict) failed for input=%ld. Expected:%lx Got:%lx\n", offset, result[i+5], c); } setOffset=offset; UTF16_PREV_CHAR_SAFE_STRICT(input, 0, setOffset, c); if(setOffset != movedOffset[i+5]){ log_err("ERROR: UTF16_PREV_CHAR_SAFE(strict) failed to move the offset correctly at %d\n ExpectedOffset:%d Got %d\n", offset, movedOffset[i+5], setOffset); } if(c != result[i+5]){ log_err("ERROR: UTF16_PREV_CHAR_SAFE(strict) failed for input=%ld. Expected:%lx Got:%lx\n", offset, result[i+5], c); } i=(uint16_t)(i+6); } } void TestFwdBack(){ static UChar input[]={0x0061, 0xd800, 0xdc00, 0xdbff, 0xdfff, 0x0062, 0xd841, 0xd7ff, 0xd841, 0xdc41, 0xdc00, 0x0000}; static uint16_t fwd_unsafe[] ={1, 3, 5, 6, 8, 10, 11, 12}; static uint16_t fwd_safe[] ={1, 3, 5, 6, 7, 8, 10, 11, 12}; static uint16_t back_unsafe[]={11, 9, 8, 7, 6, 5, 3, 1, 0}; static uint16_t back_safe[] ={11, 10, 8, 7, 6, 5, 3, 1, 0}; static uint16_t Nvalue[]= {0, 1, 2, 3, 1, 2, 1}; static uint16_t fwd_N_unsafe[] ={0, 1, 5, 10, 11}; static uint16_t fwd_N_safe[] ={0, 1, 5, 8, 10, 12, 12}; /*safe macro keeps it at the end of the string */ static uint16_t back_N_unsafe[]={12, 11, 8, 5, 3}; static uint16_t back_N_safe[] ={12, 11, 8, 5, 3, 0, 0}; uint16_t offunsafe=0, offsafe=0; uint16_t i=0; while(offunsafe < sizeof(input)/U_SIZEOF_UCHAR){ UTF16_FWD_1_UNSAFE(input, offunsafe); if(offunsafe != fwd_unsafe[i]){ log_err("ERROR: Forward_unsafe offset expected:%d, Got:%d\n", fwd_unsafe[i], offunsafe); } i++; } i=0; while(offsafe < sizeof(input)/U_SIZEOF_UCHAR){ UTF16_FWD_1_SAFE(input, offsafe, sizeof(input)/U_SIZEOF_UCHAR); if(offsafe != fwd_safe[i]){ log_err("ERROR: Forward_safe offset expected:%d, Got:%d\n", fwd_safe[i], offsafe); } i++; } offunsafe=sizeof(input)/U_SIZEOF_UCHAR; offsafe=sizeof(input)/U_SIZEOF_UCHAR; i=0; while(offunsafe > 0){ UTF16_BACK_1_UNSAFE(input, offunsafe); if(offunsafe != back_unsafe[i]){ log_err("ERROR: Backward_unsafe offset expected:%d, Got:%d\n", back_unsafe[i], offunsafe); } i++; } i=0; while(offsafe > 0){ UTF16_BACK_1_SAFE(input,0, offsafe); if(offsafe != back_safe[i]){ log_err("ERROR: Backward_safe offset expected:%d, Got:%d\n", back_unsafe[i], offsafe); } i++; } offunsafe=0; offsafe=0; for(i=0; i