// © 2016 and later: Unicode, Inc. and others. // License & terms of use: http://www.unicode.org/copyright.html /******************************************************************** * COPYRIGHT: * Copyright (c) 1998-2016, 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/ustring.h" #include "unicode/utf16.h" #include "unicode/utf_old.h" #include "cmemory.h" #include "cstring.h" #include "cintltst.h" #include // Obsolete macro from obsolete unicode/utf_old.h, for some old test data. #ifndef UTF_ERROR_VALUE # define UTF_ERROR_VALUE 0xffff #endif #if !U_HIDE_OBSOLETE_UTF_OLD_H static void printUChars(const UChar *uchars) { int16_t i=0; for(i=0; i= 4 && i< 8){ if( #if !U_HIDE_OBSOLETE_UTF_OLD_H !UTF16_IS_LEAD(c) || UTF16_IS_SINGLE(c) || UTF16_IS_TRAIL(c) || #endif !U16_IS_LEAD(c) || U16_IS_SINGLE(c) || U16_IS_TRAIL(c)){ log_err("ERROR: %x is a first surrogate\n", c); } } if(i >= 8 && i< 12){ if( #if !U_HIDE_OBSOLETE_UTF_OLD_H !UTF16_IS_TRAIL(c) || UTF16_IS_SINGLE(c) || UTF16_IS_LEAD(c) || #endif !U16_IS_TRAIL(c) || U16_IS_SINGLE(c) || U16_IS_LEAD(c)) { log_err("ERROR: %x is a second surrogate\n", c); } } } } static 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; #if !U_HIDE_OBSOLETE_UTF_OLD_H UBool multiple; #endif for(i=0; i 0; --offset){ setOffset=offset; #if !U_HIDE_OBSOLETE_UTF_OLD_H 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); } #endif setOffset=offset; U16_PREV_UNSAFE(input, setOffset, c); if(setOffset != movedOffset[i+3]){ log_err("ERROR: U16_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: U16_PREV_CHAR_UNSAFE failed for offset=%ld. Expected:%lx Got:%lx\n", offset, result[i+3], c); } #if !U_HIDE_OBSOLETE_UTF_OLD_H 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); } #endif setOffset=offset; U16_PREV(input, 0, setOffset, c); if(setOffset != movedOffset[i+4]){ log_err("ERROR: U16_PREV failed to move the offset correctly at %d\n ExpectedOffset:%d Got %d\n", offset, movedOffset[i+4], setOffset); } expected = result[i+4]; if(c != expected) { log_err("ERROR: U16_PREV failed for input=%ld. Expected:%lx Got:%lx\n", offset, expected, c); } setOffset=offset; U16_PREV_OR_FFFD(input, 0, setOffset, c); if(setOffset != movedOffset[i+4]){ log_err("ERROR: U16_PREV_OR_FFFD failed to move the offset correctly at %d\n ExpectedOffset:%d Got %d\n", offset, movedOffset[i+4], setOffset); } if(U_IS_SURROGATE(expected)) { expected=0xfffd; } if(c != expected) { log_err("ERROR: U16_PREV_OR_FFFD failed for input=%ld. Expected:%lx Got:%lx\n", offset, expected, c); } #if !U_HIDE_OBSOLETE_UTF_OLD_H 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); } #endif i=(uint16_t)(i+6); } } /* keep this in sync with utf8tst.c's TestNulTerminated() */ static void TestNulTerminated() { static const UChar input[]={ /* 0 */ 0x61, /* 1 */ 0xd801, 0xdc01, /* 3 */ 0xdc01, /* 4 */ 0x62, /* 5 */ 0xd801, /* 6 */ 0x00 /* 7 */ }; static const UChar32 result[]={ 0x61, 0x10401, 0xdc01, 0x62, 0xd801, 0 }; UChar32 c, c2, expected; int32_t i0, i=0, j, k, expectedIndex; int32_t cpIndex=0; do { i0=i; U16_NEXT(input, i, -1, c); expected=result[cpIndex]; if(c!=expected) { log_err("U16_NEXT(from %d)=U+%04x != U+%04x\n", i0, c, expected); } j=i0; U16_NEXT_OR_FFFD(input, j, -1, c); if(U_IS_SURROGATE(expected)) { expected=0xfffd; } if(c!=expected) { log_err("U16_NEXT_OR_FFFD(from %d)=U+%04x != U+%04x\n", i0, c, expected); } if(j!=i) { log_err("U16_NEXT_OR_FFFD() moved to index %d but U16_NEXT() moved to %d\n", j, i); } j=i0; U16_FWD_1(input, j, -1); if(j!=i) { log_err("U16_FWD_1() moved to index %d but U16_NEXT() moved to %d\n", j, i); } ++cpIndex; /* * Move by this many code points from the start. * U16_FWD_N() stops at the end of the string, that is, at the NUL if necessary. */ expectedIndex= (c==0) ? i-1 : i; k=0; U16_FWD_N(input, k, -1, cpIndex); if(k!=expectedIndex) { log_err("U16_FWD_N(code points from 0) moved to index %d but expected %d\n", k, expectedIndex); } } while(c!=0); i=0; do { j=i0=i; U16_NEXT(input, i, -1, c); do { U16_GET(input, 0, j, -1, c2); if(c2!=c) { log_err("U16_NEXT(from %d)=U+%04x != U+%04x=U16_GET(at %d)\n", i0, c, c2, j); } U16_GET_OR_FFFD(input, 0, j, -1, c2); expected= U_IS_SURROGATE(c) ? 0xfffd : c; if(c2!=expected) { log_err("U16_NEXT_OR_FFFD(from %d)=U+%04x != U+%04x=U16_GET_OR_FFFD(at %d)\n", i0, expected, c2, j); } /* U16_SET_CP_LIMIT moves from a non-lead byte to the limit of the code point */ k=j+1; U16_SET_CP_LIMIT(input, 0, k, -1); if(k!=i) { log_err("U16_NEXT() moved to %d but U16_SET_CP_LIMIT(%d) moved to %d\n", i, j+1, k); } } while(++j 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++; } #endif offunsafe=UPRV_LENGTHOF(input); offsafe=UPRV_LENGTHOF(input); i=0; while(offunsafe > 0){ U16_BACK_1_UNSAFE(input, offunsafe); if(offunsafe != back_unsafe[i]){ log_err("ERROR: U16_BACK_1_UNSAFE offset expected:%d, Got:%d\n", back_unsafe[i], offunsafe); } i++; } #if !U_HIDE_OBSOLETE_UTF_OLD_H offunsafe=UPRV_LENGTHOF(input); offsafe=UPRV_LENGTHOF(input); 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++; } #endif offunsafe=UPRV_LENGTHOF(input); offsafe=UPRV_LENGTHOF(input); i=0; while(offsafe > 0){ U16_BACK_1(input,0, offsafe); if(offsafe != back_safe[i]){ log_err("ERROR: U16_BACK_1 offset expected:%d, Got:%d\n", back_unsafe[i], offsafe); } i++; } offunsafe=0; offsafe=0; #if !U_HIDE_OBSOLETE_UTF_OLD_H for(i=0; i 0) { #if !U_HIDE_OBSOLETE_UTF_OLD_H setOffset=offset; UTF16_SET_CHAR_LIMIT_UNSAFE(input, setOffset); if(setOffset != limit_unsafe[i]){ log_err("ERROR: UTF16_SET_CHAR_LIMIT_UNSAFE failed for offset=%ld. Expected:%lx Got:%lx\n", offset, limit_unsafe[i], setOffset); } #endif setOffset=offset; U16_SET_CP_LIMIT_UNSAFE(input, setOffset); if(setOffset != limit_unsafe[i]){ log_err("ERROR: U16_SET_CHAR_LIMIT_UNSAFE failed for offset=%ld. Expected:%lx Got:%lx\n", offset, limit_unsafe[i], setOffset); } } setOffset=offset; U16_SET_CP_LIMIT(input,0, setOffset, UPRV_LENGTHOF(input)); if(setOffset != limit_safe[i]){ log_err("ERROR: U16_SET_CHAR_LIMIT failed for offset=%ld. Expected:%lx Got:%lx\n", offset, limit_safe[i], setOffset); } i++; } } static void TestAppendChar(){ #if !U_HIDE_OBSOLETE_UTF_OLD_H static UChar s[5]={0x0061, 0x0062, 0x0063, 0x0064, 0x0000}; static uint32_t test[]={ /*append-position(unsafe), CHAR to be appended */ 0, 0x20441, 2, 0x0028, 2, 0xdc00, 3, 0xd800, 1, 0x20402, /*append-position(safe), CHAR to be appended */ 0, 0x20441, 2, 0xdc00, 3, 0xd800, 1, 0x20402, 3, 0x20402, 3, 0x10402, 2, 0x10402, }; static uint16_t movedOffset[]={ /*offset-moved-to(unsafe)*/ 2, /*for append-pos: 0 , CHAR 0x20441*/ 3, 3, 4, 3, /*offse-moved-to(safe)*/ 2, /*for append-pos: 0, CHAR 0x20441*/ 3, 4, 3, 4, 4, 4 }; static UChar result[][5]={ /*unsafe*/ {0xd841, 0xdc41, 0x0063, 0x0064, 0x0000}, {0x0061, 0x0062, 0x0028, 0x0064, 0x0000}, {0x0061, 0x0062, 0xdc00, 0x0064, 0x0000}, {0x0061, 0x0062, 0x0063, 0xd800, 0x0000}, {0x0061, 0xd841, 0xdc02, 0x0064, 0x0000}, /*safe*/ {0xd841, 0xdc41, 0x0063, 0x0064, 0x0000}, {0x0061, 0x0062, 0xdc00, 0x0064, 0x0000}, {0x0061, 0x0062, 0x0063, 0xd800, 0x0000}, {0x0061, 0xd841, 0xdc02, 0x0064, 0x0000}, {0x0061, 0x0062, 0x0063, UTF_ERROR_VALUE, 0x0000}, {0x0061, 0x0062, 0x0063, UTF_ERROR_VALUE, 0x0000}, {0x0061, 0x0062, 0xd801, 0xdc02, 0x0000}, }; uint16_t i, count=0; UChar *str=(UChar*)malloc(sizeof(UChar) * (u_strlen(s)+1)); uint16_t offset; for(i=0; i