/******************************************************************** * COPYRIGHT: * Copyright (c) 1998-2006, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ /* * File test.c * * Modification History: * * Date Name Description * 07/24/2000 Madhu Creation ******************************************************************************* */ #include "unicode/utypes.h" #include "unicode/utf8.h" #include "cmemory.h" #include "cintltst.h" #define LENGTHOF(array) (sizeof(array)/sizeof((array)[0])) /* lenient UTF-8 ------------------------------------------------------------ */ /* * Lenient UTF-8 differs from conformant UTF-8 in that it allows surrogate * code points with their "natural" encoding. * Effectively, this allows a mix of UTF-8 and CESU-8 as well as encodings of * single surrogates. * * This is not conformant with UTF-8. * * Supplementary code points may be encoded as pairs of 3-byte sequences, but * the macros below do not attempt to assemble such pairs. */ #define L8_NEXT(s, i, length, c) { \ (c)=(uint8_t)(s)[(i)++]; \ if((c)>=0x80) { \ if(U8_IS_LEAD(c)) { \ (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (int32_t)(length), c, -2); \ } else { \ (c)=U_SENTINEL; \ } \ } \ } #define L8_PREV(s, start, i, c) { \ (c)=(uint8_t)(s)[--(i)]; \ if((c)>=0x80) { \ if((c)<=0xbf) { \ (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -2); \ } else { \ (c)=U_SENTINEL; \ } \ } \ } /* -------------------------------------------------------------------------- */ static void printUChars(const uint8_t *uchars, int16_t len); static void TestCodeUnitValues(void); static void TestCharLength(void); static void TestGetChar(void); static void TestNextPrevChar(void); static void TestFwdBack(void); static void TestSetChar(void); static void TestAppendChar(void); static void TestAppend(void); static void TestSurrogates(void); void addUTF8Test(TestNode** root); void addUTF8Test(TestNode** root) { addTest(root, &TestCodeUnitValues, "utf8tst/TestCodeUnitValues"); addTest(root, &TestCharLength, "utf8tst/TestCharLength" ); addTest(root, &TestGetChar, "utf8tst/TestGetChar" ); addTest(root, &TestNextPrevChar, "utf8tst/TestNextPrevChar" ); addTest(root, &TestFwdBack, "utf8tst/TestFwdBack" ); addTest(root, &TestSetChar, "utf8tst/TestSetChar" ); addTest(root, &TestAppendChar, "utf8tst/TestAppendChar" ); addTest(root, &TestAppend, "utf8tst/TestAppend" ); addTest(root, &TestSurrogates, "utf8tst/TestSurrogates" ); } static void TestCodeUnitValues() { static const uint8_t codeunit[]={0x00, 0x65, 0x7e, 0x7f, 0xc0, 0xc4, 0xf0, 0xfd, 0x80, 0x81, 0xbc, 0xbe,}; int16_t i; for(i=0; i= 0 : c != result[i+1]){ log_err("ERROR: UTF8_GET_CHAR_SAFE failed for offset=%ld. Expected:%lx Got:%lx\n", offset, result[i+1], c); } UTF8_GET_CHAR_SAFE(input, 0, offset, sizeof(input), c, FALSE); if(c != result[i+1]){ log_err("ERROR: UTF8_GET_CHAR_SAFE failed for offset=%ld. Expected:%lx Got:%lx\n", offset, result[i+1], c); } UTF8_GET_CHAR_SAFE(input, 0, offset, sizeof(input), c, TRUE); if(c != result[i+2]){ log_err("ERROR: UTF8_GET_CHAR_SAFE(strict) failed for offset=%ld. Expected:%lx Got:%lx\n", offset, result[i+2], c); } i=(uint16_t)(i+3); } } static void TestNextPrevChar(){ static const uint8_t input[]={0x61, 0xf0, 0x90, 0x90, 0x81, 0xc0, 0x80, 0xfd, 0xbe, 0xc2, 0x61, 0x81, 0x90, 0x90, 0xf0, 0x00}; static const UChar32 result[]={ /*next_unsafe next_safe_ns next_safe_s prev_unsafe prev_safe_ns prev_safe_s*/ 0x0061, 0x0061, 0x0061, 0x0000, 0x0000, 0x0000, 0x10401, 0x10401, 0x10401, 0xf0, UTF8_ERROR_VALUE_1, UTF8_ERROR_VALUE_1, 0x90, UTF8_ERROR_VALUE_1, UTF8_ERROR_VALUE_1, 0x2841410, UTF8_ERROR_VALUE_1, UTF8_ERROR_VALUE_1, 0x90, UTF8_ERROR_VALUE_1, UTF8_ERROR_VALUE_1, 0xa1050, UTF8_ERROR_VALUE_1, UTF8_ERROR_VALUE_1, 0x81, UTF8_ERROR_VALUE_1, UTF8_ERROR_VALUE_1, 0x2841, UTF8_ERROR_VALUE_1, UTF8_ERROR_VALUE_1, 0x00, UTF8_ERROR_VALUE_2, UTF8_ERROR_VALUE_2, 0x61, 0x61, 0x61, 0x80, UTF8_ERROR_VALUE_1, UTF8_ERROR_VALUE_1, 0xc2, UTF8_ERROR_VALUE_1, UTF8_ERROR_VALUE_1, 0xfd, UTF8_ERROR_VALUE_2, UTF8_ERROR_VALUE_2, 0x77e, UTF8_ERROR_VALUE_2, UTF8_ERROR_VALUE_2, 0xbe, UTF8_ERROR_VALUE_1, UTF8_ERROR_VALUE_1, 0xfd, UTF8_ERROR_VALUE_1, UTF8_ERROR_VALUE_1, 0xa1, UTF8_ERROR_VALUE_1, UTF8_ERROR_VALUE_1, 0x00, UTF8_ERROR_VALUE_2, UTF8_ERROR_VALUE_2, 0x61, 0x61, 0x61, 0xc0, UTF8_ERROR_VALUE_1, UTF8_ERROR_VALUE_1, 0x81, UTF8_ERROR_VALUE_1, UTF8_ERROR_VALUE_1, 0x10401, 0x10401, 0x10401, 0x90, UTF8_ERROR_VALUE_1, UTF8_ERROR_VALUE_1, 0x410, UTF_ERROR_VALUE, UTF_ERROR_VALUE, 0x90, UTF8_ERROR_VALUE_1, UTF8_ERROR_VALUE_1, 0x410, UTF8_ERROR_VALUE_2, UTF8_ERROR_VALUE_2, 0x0840, UTF8_ERROR_VALUE_1, UTF8_ERROR_VALUE_1, 0xf0, UTF8_ERROR_VALUE_1, UTF8_ERROR_VALUE_1, 0x0000, 0x0000, 0x0000, 0x0061, 0x0061, 0x0061 }; static const int32_t movedOffset[]={ /*next_unsafe next_safe_ns next_safe_s prev_unsafe prev_safe_ns prev_safe_s*/ 1, 1, 1, 15, 15, 15, 5, 5, 5, 14, 14 , 14, 3, 3, 3, 9, 13, 13, 4, 4, 4, 9, 12, 12, 5, 5, 5, 9, 11, 11, 7, 7, 7, 10, 10, 10, 7, 7, 7, 9, 9, 9, 8, 9, 9, 7, 7, 7, 9, 9, 9, 7, 7, 7, 11, 10, 10, 5, 5, 5, 11, 11, 11, 5, 5, 5, 12, 12, 12, 1, 1, 1, 13, 13, 13, 1, 1, 1, 14, 14, 14, 1, 1, 1, 14, 15, 15, 1, 1, 1, 14, 16, 16, 0, 0, 0, }; UChar32 c=0x0000; uint32_t i=0; uint32_t offset=0; int32_t setOffset=0; for(offset=0; offset= 0 : c != result[i+1]){ log_err("ERROR: U8_NEXT failed for input=%ld. Expected:%lx Got:%lx\n", offset, result[i+1], c); } setOffset=offset; UTF8_NEXT_CHAR_SAFE(input, setOffset, sizeof(input), c, TRUE); if(setOffset != movedOffset[i+1]){ log_err("ERROR: UTF8_NEXT_CHAR_SAFE(strict) failed to move the offset correctly at %d\n ExpectedOffset:%d Got %d\n", offset, movedOffset[i+2], setOffset); } if(c != result[i+2]){ log_err("ERROR: UTF8_NEXT_CHAR_SAFE(strict) failed for input=%ld. Expected:%lx Got:%lx\n", offset, result[i+2], c); } i=i+6; } i=0; for(offset=sizeof(input); offset > 0; --offset){ setOffset=offset; UTF8_PREV_CHAR_UNSAFE(input, setOffset, c); if(setOffset != movedOffset[i+3]){ log_err("ERROR: UTF8_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: UTF8_PREV_CHAR_UNSAFE failed for offset=%ld. Expected:%lx Got:%lx\n", offset, result[i+3], c); } setOffset=offset; UTF8_PREV_CHAR_SAFE(input, 0, setOffset, c, FALSE); if(setOffset != movedOffset[i+4]){ log_err("ERROR: UTF8_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: UTF8_PREV_CHAR_SAFE failed for input=%ld. Expected:%lx Got:%lx\n", offset, result[i+4], c); } setOffset=offset; U8_PREV(input, 0, setOffset, c); if(setOffset != movedOffset[i+4]){ log_err("ERROR: U8_PREV failed to move the offset correctly at %d\n ExpectedOffset:%d Got %d\n", offset, movedOffset[i+4], setOffset); } if(UTF_IS_ERROR(result[i+4]) ? c >= 0 : c != result[i+4]){ log_err("ERROR: U8_PREV failed for input=%ld. Expected:%lx Got:%lx\n", offset, result[i+4], c); } setOffset=offset; UTF8_PREV_CHAR_SAFE(input, 0, setOffset, c, TRUE); if(setOffset != movedOffset[i+5]){ log_err("ERROR: UTF8_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: UTF8_PREV_CHAR_SAFE(strict) failed for input=%ld. Expected:%lx Got:%lx\n", offset, result[i+5], c); } i=i+6; } { /* test non-characters */ static const uint8_t nonChars[]={ 0xef, 0xb7, 0x90, /* U+fdd0 */ 0xef, 0xbf, 0xbf, /* U+feff */ 0xf0, 0x9f, 0xbf, 0xbe, /* U+1fffe */ 0xf0, 0xbf, 0xbf, 0xbf, /* U+3ffff */ 0xf4, 0x8f, 0xbf, 0xbe /* U+10fffe */ }; UChar32 ch; int32_t idx; for(idx=0; idx<(int32_t)sizeof(nonChars);) { U8_NEXT(nonChars, idx, sizeof(nonChars), ch); if(!U_IS_UNICODE_NONCHAR(ch)) { log_err("U8_NEXT(before %d) failed to read a non-character\n", idx); } } for(idx=(int32_t)sizeof(nonChars); idx>0;) { U8_PREV(nonChars, 0, idx, ch); if(!U_IS_UNICODE_NONCHAR(ch)) { log_err("U8_PREV(at %d) failed to read a non-character\n", idx); } } } } static void TestFwdBack(){ static const uint8_t input[]={0x61, 0xF0, 0x90, 0x90, 0x81, 0xff, 0x62, 0xc0, 0x80, 0x7f, 0x8f, 0xc0, 0x63, 0x81, 0x90, 0x90, 0xF0, 0x00}; static const uint16_t fwd_unsafe[] ={1, 5, 6, 7, 9, 10, 11, 13, 14, 15, 16, 20, }; static const uint16_t fwd_safe[] ={1, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}; static const uint16_t back_unsafe[]={17, 16, 12, 11, 9, 7, 6, 5, 1, 0}; static const uint16_t back_safe[] ={17, 16, 15, 14, 13, 12, 11, 10, 9, 7, 6, 5, 1, 0}; static const uint16_t Nvalue[]= {0, 1, 2, 3, 1, 2, 1, 5}; static const uint16_t fwd_N_unsafe[] ={0, 1, 6, 10, 11, 14, 15}; static const uint16_t fwd_N_safe[] ={0, 1, 6, 10, 11, 13, 14, 18}; /*safe macro keeps it at the end of the string */ static const uint16_t back_N_unsafe[]={18, 17, 12, 7, 6, 1, 0}; static const uint16_t back_N_safe[] ={18, 17, 15, 12, 11, 9, 7, 0}; uint32_t offunsafe=0, offsafe=0; uint32_t i=0; while(offunsafe < sizeof(input)){ UTF8_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(offunsafe < sizeof(input)){ U8_FWD_1_UNSAFE(input, offunsafe); if(offunsafe != fwd_unsafe[i]){ log_err("ERROR: U8_FWD_1_UNSAFE offset expected:%d, Got:%d\n", fwd_unsafe[i], offunsafe); } i++; } i=0; while(offsafe < sizeof(input)){ UTF8_FWD_1_SAFE(input, offsafe, sizeof(input)); if(offsafe != fwd_safe[i]){ log_err("ERROR: Forward_safe offset expected:%d, Got:%d\n", fwd_safe[i], offsafe); } i++; } i=0; while(offsafe < sizeof(input)){ U8_FWD_1(input, offsafe, sizeof(input)); if(offsafe != fwd_safe[i]){ log_err("ERROR: U8_FWD_1 offset expected:%d, Got:%d\n", fwd_safe[i], offsafe); } i++; } offunsafe=sizeof(input); i=0; while(offunsafe > 0){ UTF8_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++; } offunsafe=sizeof(input); i=0; while(offunsafe > 0){ U8_BACK_1_UNSAFE(input, offunsafe); if(offunsafe != back_unsafe[i]){ log_err("ERROR: U8_BACK_1_UNSAFE offset expected:%d, Got:%d\n", back_unsafe[i], offunsafe); } i++; } i=0; offsafe=sizeof(input); while(offsafe > 0){ UTF8_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++; } i=0; offsafe=sizeof(input); while(offsafe > 0){ U8_BACK_1(input, 0, offsafe); if(offsafe != back_safe[i]){ log_err("ERROR: U8_BACK_1 offset expected:%d, Got:%d\n", back_unsafe[i], offsafe); } i++; } offunsafe=0; for(i=0; i=0 : cs!=cu) { log_err("U8_NEXT(b[%ld])=U+%04lX != U+%04lX\n", (long)i, (long)cs, (long)cu); } /* L8_NEXT() returns surrogate code points like U8_NEXT_UNSAFE() */ if(cl!=cu) { log_err("L8_NEXT(b[%ld])=U+%04lX != U+%04lX\n", (long)i, (long)cl, (long)cu); } if(is!=iu || il!=iu) { log_err("U8_NEXT(b[%ld]) or L8_NEXT(b[%ld]) did not advance the index correctly\n", (long)i, (long)i); } ++k; /* next code point */ i=iu; /* advance by one UTF-8 sequence */ } while(i>0) { --k; /* previous code point */ j=i; U8_PREV_UNSAFE(b, j, cu); iu=j; j=i; U8_PREV(b, 0, j, cs); is=j; j=i; L8_PREV(b, 0, j, cl); il=j; if(cu!=cp[k]) { log_err("U8_PREV_UNSAFE(b[%ld])=U+%04lX != U+%04lX\n", (long)i, (long)cu, (long)cp[k]); } /* U8_PREV() returns <0 for surrogate code points */ if(U_IS_SURROGATE(cu) ? cs>=0 : cs!=cu) { log_err("U8_PREV(b[%ld])=U+%04lX != U+%04lX\n", (long)i, (long)cs, (long)cu); } /* L8_PREV() returns surrogate code points like U8_PREV_UNSAFE() */ if(cl!=cu) { log_err("L8_PREV(b[%ld])=U+%04lX != U+%04lX\n", (long)i, (long)cl, (long)cu); } if(is!=iu || il !=iu) { log_err("U8_PREV(b[%ld]) or L8_PREV(b[%ld]) did not advance the index correctly\n", (long)i, (long)i); } i=iu; /* go back by one UTF-8 sequence */ } } static void printUChars(const uint8_t *uchars, int16_t len){ int16_t i=0; for(i=0; i