// © 2016 and later: Unicode, Inc. and others. // License & terms of use: http://www.unicode.org/copyright.html /* ****************************************************************************** * * Copyright (C) 2001-2016, International Business Machines * Corporation and others. All Rights Reserved. * ****************************************************************************** * file name: trietest.c * encoding: UTF-8 * tab size: 8 (not used) * indentation:4 * * created on: 2001nov20 * created by: Markus W. Scherer */ #include #include "unicode/utypes.h" #include "unicode/utf16.h" #include "utrie.h" #include "cstring.h" #include "cmemory.h" #if 1 #include "cintltst.h" #else /* definitions from standalone utrie development */ #define log_err printf #define log_verbose printf #undef u_errorName #define u_errorName(errorCode) "some error code" #endif /* Values for setting possibly overlapping, out-of-order ranges of values */ typedef struct SetRange { UChar32 start, limit; uint32_t value; UBool overwrite; } SetRange; /* * Values for testing: * value is set from the previous boundary's limit to before * this boundary's limit */ typedef struct CheckRange { UChar32 limit; uint32_t value; } CheckRange; static uint32_t U_CALLCONV _testFoldedValue32(UNewTrie *trie, UChar32 start, int32_t offset) { uint32_t foldedValue, value; UChar32 limit; UBool inBlockZero; foldedValue=0; limit=start+0x400; while(start>16); } static uint32_t U_CALLCONV _testFoldedValue16(UNewTrie *trie, UChar32 start, int32_t offset) { uint32_t foldedValue, value; UChar32 limit; UBool inBlockZero; foldedValue=0; limit=start+0x400; while(startlimit || limit!=b->limit || value!=b->value) { log_err("error: utrie_enum() delivers wrong range [U+%04lx..U+%04lx[.0x%lx instead of [U+%04lx..U+%04lx[.0x%lx\n", start, limit, value, (b-1)->limit, b->limit, b->value); } return TRUE; } static void testTrieIteration(const char *testName, const UTrie *trie, const CheckRange checkRanges[], int32_t countCheckRanges) { UChar s[100]; uint32_t values[30]; const UChar *p, *limit; uint32_t value; UChar32 c; int32_t i, length, countValues; UChar c2; /* write a string */ length=countValues=0; for(i=0; idata32!=NULL) { UTRIE_NEXT32(trie, p, limit, c, c2, value); } else { UTRIE_NEXT16(trie, p, limit, c, c2, value); } if(value!=values[i]) { log_err("error: wrong value from UTRIE_NEXT(%s)(U+%04lx, U+%04lx): 0x%lx instead of 0x%lx\n", testName, c, c2, value, values[i]); } if( c2==0 ? c!=*(p-1) : !U16_IS_LEAD(c) || !U16_IS_TRAIL(c2) || c!=*(p-2) || c2!=*(p-1) ) { log_err("error: wrong (c, c2) from UTRIE_NEXT(%s): (U+%04lx, U+%04lx)\n", testName, c, c2); continue; } if(c2!=0) { int32_t offset; if(trie->data32==NULL) { value=UTRIE_GET16_FROM_LEAD(trie, c); offset=trie->getFoldingOffset(value); if(offset>0) { value=UTRIE_GET16_FROM_OFFSET_TRAIL(trie, offset, c2); } else { value=trie->initialValue; } } else { value=UTRIE_GET32_FROM_LEAD(trie, c); offset=trie->getFoldingOffset(value); if(offset>0) { value=UTRIE_GET32_FROM_OFFSET_TRAIL(trie, offset, c2); } else { value=trie->initialValue; } } if(value!=values[i]) { log_err("error: wrong value from UTRIE_GETXX_FROM_OFFSET_TRAIL(%s)(U+%04lx, U+%04lx): 0x%lx instead of 0x%lx\n", testName, c, c2, value, values[i]); } } if(c2!=0) { value=0x44; if(trie->data32==NULL) { UTRIE_GET16_FROM_PAIR(trie, c, c2, value); } else { UTRIE_GET32_FROM_PAIR(trie, c, c2, value); } if(value!=values[i]) { log_err("error: wrong value from UTRIE_GETXX_FROM_PAIR(%s)(U+%04lx, U+%04lx): 0x%lx instead of 0x%lx\n", testName, c, c2, value, values[i]); } } ++i; } /* try backward */ p=limit; i=countValues; while(sdata32!=NULL) { UTRIE_PREVIOUS32(trie, s, p, c, c2, value); } else { UTRIE_PREVIOUS16(trie, s, p, c, c2, value); } if(value!=values[i]) { log_err("error: wrong value from UTRIE_PREVIOUS(%s)(U+%04lx, U+%04lx): 0x%lx instead of 0x%lx\n", testName, c, c2, value, values[i]); } if( c2==0 ? c!=*p: !U16_IS_LEAD(c) || !U16_IS_TRAIL(c2) || c!=*p || c2!=*(p+1) ) { log_err("error: wrong (c, c2) from UTRIE_PREVIOUS(%s): (U+%04lx, U+%04lx)\n", testName, c, c2); } } } static void testTrieRangesWithMalloc(const char *testName, const SetRange setRanges[], int32_t countSetRanges, const CheckRange checkRanges[], int32_t countCheckRanges, UBool dataIs32, UBool latin1Linear) { UTrieGetFoldingOffset *getFoldingOffset; const CheckRange *enumRanges; UNewTrie *newTrie; UTrie trie={ 0 }; uint32_t value, value2; UChar32 start, limit; int32_t i, length; UErrorCode errorCode; UBool overwrite, ok; uint8_t* storage =NULL; static const int32_t DEFAULT_STORAGE_SIZE = 32768; storage = (uint8_t*) uprv_malloc(sizeof(uint8_t)*DEFAULT_STORAGE_SIZE); log_verbose("\ntesting Trie '%s'\n", testName); newTrie=utrie_open(NULL, NULL, 2000, checkRanges[0].value, checkRanges[0].value, latin1Linear); /* set values from setRanges[] */ ok=TRUE; for(i=0; i= (int32_t)sizeof(storageHolder.storage)) { log_err("error: utrie_serialize(%s) needs more memory\n", testName); utrie_close(newTrie); return; } /* test linear Latin-1 range from utrie_getData() */ if(latin1Linear) { uint32_t *data; int32_t dataLength; data=utrie_getData(newTrie, &dataLength); start=0; for(i=0; i