+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
/*
******************************************************************************
*
-* Copyright (C) 2001-2005, International Business Machines
+* Copyright (C) 2001-2016, International Business Machines
* Corporation and others. All Rights Reserved.
*
******************************************************************************
* file name: trietest.c
-* encoding: US-ASCII
+* encoding: UTF-8
* tab size: 8 (not used)
* indentation:4
*
#include <stdio.h>
#include "unicode/utypes.h"
+#include "unicode/utf16.h"
#include "utrie.h"
#include "cstring.h"
#include "cmemory.h"
#define u_errorName(errorCode) "some error code"
#endif
-#define ARRAY_LENGTH(array) (sizeof(array)/sizeof(array[0]))
-
/* Values for setting possibly overlapping, out-of-order ranges of values */
typedef struct SetRange {
UChar32 start, limit;
UChar32 limit;
uint32_t value;
} CheckRange;
-struct{
- double bogus; /* needed for aligining the storage */
- uint8_t storage[10000000];
-} storageHolder;
static uint32_t U_CALLCONV
c=checkRanges[i].limit;
if(c!=0) {
--c;
- UTF_APPEND_CHAR_UNSAFE(s, length, c);
+ U16_APPEND_UNSAFE(s, length, c);
values[countValues++]=checkRanges[i].value;
}
}
if(
c2==0 ?
c!=*(p-1) :
- !UTF_IS_LEAD(c) || !UTF_IS_TRAIL(c2) || c!=*(p-2) || c2!=*(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);
if(
c2==0 ?
c!=*p:
- !UTF_IS_LEAD(c) || !UTF_IS_TRAIL(c2) || c!=*p || c2!=*(p+1)
+ !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);
UErrorCode errorCode;
UBool overwrite, ok;
uint8_t* storage =NULL;
- storage = (uint8_t*) uprv_malloc(sizeof(uint8_t)*100000);
+ 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,
}
errorCode=U_ZERO_ERROR;
- length=utrie_serialize(newTrie, storage, 1000000,
+ length=utrie_serialize(newTrie, storage, DEFAULT_STORAGE_SIZE,
dataIs32 ? _testFoldedValue32 : _testFoldedValue16,
(UBool)!dataIs32,
&errorCode);
log_err("error: unserialized trie(%s).fromBMP(U+%04lx)==0x%lx instead of 0x%lx\n",
testName, start, value2, value);
}
- if(!UTF_IS_LEAD(start)) {
+ if(!U16_IS_LEAD(start)) {
if(dataIs32) {
value2=UTRIE_GET32_FROM_LEAD(&trie, start);
} else {
const SetRange setRanges[], int32_t countSetRanges,
const CheckRange checkRanges[], int32_t countCheckRanges,
UBool dataIs32, UBool latin1Linear) {
+ union{
+ double bogus; /* needed for aligining the storage */
+ uint8_t storage[32768];
+ } storageHolder;
UTrieGetFoldingOffset *getFoldingOffset;
UNewTrieGetFoldedValue *getFoldedValue;
const CheckRange *enumRanges;
utrie_close(newTrie);
return;
}
+ if (length >= (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) {
log_err("error: unserialized trie(%s).fromBMP(U+%04lx)==0x%lx instead of 0x%lx\n",
testName, start, value2, value);
}
- if(!UTF_IS_LEAD(start)) {
+ if(!U16_IS_LEAD(start)) {
if(dataIs32) {
value2=UTRIE_GET32_FROM_LEAD(&trie, start);
} else {
{0x2f800,0x2fedc, 0x7a, TRUE},
{0x72, 0xdd, 3, TRUE},
{0xdd, 0xde, 4, FALSE},
+ {0x201, 0x220, 6, TRUE}, /* 3 consecutive blocks with the same pattern but discontiguous value ranges */
+ {0x221, 0x240, 6, TRUE},
+ {0x241, 0x260, 6, TRUE},
{0x2f987,0x2fa98, 5, TRUE},
{0x2f777,0x2f833, 0, TRUE},
{0x2f900,0x2ffee, 1, FALSE},
{0x72, 0x5555},
{0xdd, 3},
{0xde, 4},
+ {0x201, 0},
+ {0x220, 6},
+ {0x221, 0},
+ {0x240, 6},
+ {0x241, 0},
+ {0x260, 6},
{0x2f833,0},
{0x2f987,0x7a},
{0x2fa98,5},
static void
TrieTest(void) {
testTrieRanges4("set1",
- setRanges1, ARRAY_LENGTH(setRanges1),
- checkRanges1, ARRAY_LENGTH(checkRanges1));
+ setRanges1, UPRV_LENGTHOF(setRanges1),
+ checkRanges1, UPRV_LENGTHOF(checkRanges1));
testTrieRanges4("set2-overlap",
- setRanges2, ARRAY_LENGTH(setRanges2),
- checkRanges2, ARRAY_LENGTH(checkRanges2));
+ setRanges2, UPRV_LENGTHOF(setRanges2),
+ checkRanges2, UPRV_LENGTHOF(checkRanges2));
testTrieRanges4("set3-initial-9",
- setRanges3, ARRAY_LENGTH(setRanges3),
- checkRanges3, ARRAY_LENGTH(checkRanges3));
+ setRanges3, UPRV_LENGTHOF(setRanges3),
+ checkRanges3, UPRV_LENGTHOF(checkRanges3));
}
/* test utrie_unserializeDummy() -------------------------------------------- */
static void
dummyTest(UBool make16BitTrie) {
- static int32_t mem[UTRIE_DUMMY_SIZE/4];
+ int32_t mem[UTRIE_DUMMY_SIZE/4];
UTrie trie;
UErrorCode errorCode;