2002-07-09 06:16:35 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
|
|
|
*******************************************************************************
|
|
|
|
* file name: uenumtst.c
|
|
|
|
* encoding: US-ASCII
|
|
|
|
* tab size: 8 (not used)
|
|
|
|
* indentation:2
|
|
|
|
*
|
|
|
|
* created on: 2002jul08
|
|
|
|
* created by: Vladimir Weinstein
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cintltst.h"
|
|
|
|
#include "uenumimp.h"
|
|
|
|
#include "cmemory.h"
|
|
|
|
#include "cstring.h"
|
2002-10-11 20:19:33 +00:00
|
|
|
#include "unicode/ustring.h"
|
|
|
|
|
|
|
|
static char quikBuf[256];
|
|
|
|
static char* quikU2C(const UChar* str, int32_t len) {
|
|
|
|
u_UCharsToChars(str, quikBuf, len);
|
|
|
|
quikBuf[len] = 0;
|
|
|
|
return quikBuf;
|
|
|
|
}
|
2002-07-09 06:16:35 +00:00
|
|
|
|
|
|
|
static const char* test1[] = {
|
2002-07-16 22:29:23 +00:00
|
|
|
"first",
|
2002-07-09 06:16:35 +00:00
|
|
|
"second",
|
|
|
|
"third",
|
|
|
|
"fourth"
|
|
|
|
};
|
|
|
|
|
2002-07-09 18:19:16 +00:00
|
|
|
struct chArrayContext {
|
2002-07-16 22:29:23 +00:00
|
|
|
int32_t currIndex;
|
|
|
|
int32_t maxIndex;
|
|
|
|
char *currChar;
|
|
|
|
UChar *currUChar;
|
|
|
|
char **array;
|
2002-07-09 18:19:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct chArrayContext chArrayContext;
|
|
|
|
|
|
|
|
#define cont ((chArrayContext *)en->context)
|
|
|
|
|
2002-07-16 22:29:23 +00:00
|
|
|
static void U_CALLCONV
|
2002-07-12 21:49:02 +00:00
|
|
|
chArrayClose(UEnumeration *en) {
|
2002-07-16 22:29:23 +00:00
|
|
|
if(cont->currUChar != NULL) {
|
2002-07-29 21:04:18 +00:00
|
|
|
free(cont->currUChar);
|
2002-07-16 22:29:23 +00:00
|
|
|
}
|
2002-07-29 21:04:18 +00:00
|
|
|
free(en);
|
2002-07-09 06:16:35 +00:00
|
|
|
}
|
|
|
|
|
2002-07-16 22:29:23 +00:00
|
|
|
static int32_t U_CALLCONV
|
2002-07-12 21:49:02 +00:00
|
|
|
chArrayCount(UEnumeration *en, UErrorCode *status) {
|
2002-07-16 22:29:23 +00:00
|
|
|
return cont->maxIndex;
|
2002-07-09 06:16:35 +00:00
|
|
|
}
|
|
|
|
|
2002-07-16 22:29:23 +00:00
|
|
|
static const UChar* U_CALLCONV
|
2002-07-12 21:49:02 +00:00
|
|
|
chArrayUNext(UEnumeration *en, int32_t *resultLength, UErrorCode *status) {
|
2002-07-16 22:29:23 +00:00
|
|
|
if(cont->currIndex >= cont->maxIndex) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cont->currUChar == NULL) {
|
2002-07-29 21:04:18 +00:00
|
|
|
cont->currUChar = (UChar *)malloc(1024*sizeof(UChar));
|
2002-07-16 22:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cont->currChar = (cont->array)[cont->currIndex];
|
|
|
|
*resultLength = uprv_strlen(cont->currChar);
|
|
|
|
u_charsToUChars(cont->currChar, cont->currUChar, *resultLength);
|
|
|
|
cont->currIndex++;
|
|
|
|
return cont->currUChar;
|
2002-07-09 06:16:35 +00:00
|
|
|
}
|
|
|
|
|
2002-07-16 22:29:23 +00:00
|
|
|
static const char* U_CALLCONV
|
2002-07-12 21:49:02 +00:00
|
|
|
chArrayNext(UEnumeration *en, int32_t *resultLength, UErrorCode *status) {
|
2002-07-16 22:29:23 +00:00
|
|
|
if(cont->currIndex >= cont->maxIndex) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cont->currChar = (cont->array)[cont->currIndex];
|
|
|
|
*resultLength = uprv_strlen(cont->currChar);
|
|
|
|
cont->currIndex++;
|
|
|
|
return cont->currChar;
|
2002-07-09 06:16:35 +00:00
|
|
|
}
|
|
|
|
|
2002-07-16 22:29:23 +00:00
|
|
|
static void U_CALLCONV
|
2002-07-12 21:49:02 +00:00
|
|
|
chArrayReset(UEnumeration *en, UErrorCode *status) {
|
2002-07-16 22:29:23 +00:00
|
|
|
cont->currIndex = 0;
|
2002-07-09 06:16:35 +00:00
|
|
|
}
|
|
|
|
|
2002-07-09 18:19:16 +00:00
|
|
|
chArrayContext myCont = {
|
2002-07-16 22:29:23 +00:00
|
|
|
0, 0,
|
2002-07-09 06:16:35 +00:00
|
|
|
NULL, NULL,
|
2002-07-09 18:19:16 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
UEnumeration chEnum = {
|
2002-08-02 23:01:10 +00:00
|
|
|
NULL,
|
2002-07-09 18:19:16 +00:00
|
|
|
&myCont,
|
2002-07-09 06:16:35 +00:00
|
|
|
chArrayClose,
|
|
|
|
chArrayCount,
|
|
|
|
chArrayUNext,
|
|
|
|
chArrayNext,
|
2002-08-23 17:06:53 +00:00
|
|
|
chArrayReset
|
2002-07-09 06:16:35 +00:00
|
|
|
};
|
|
|
|
|
2002-08-23 17:06:53 +00:00
|
|
|
static const UEnumeration emptyEnumerator = {
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const UEnumeration emptyPartialEnumerator = {
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
uenum_unextDefault,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2002-10-11 20:19:33 +00:00
|
|
|
/********************************************************************/
|
|
|
|
static const UChar _first[] = {102,105,114,115,116,0}; /* "first" */
|
|
|
|
static const UChar _second[]= {115,101,99,111,110,100,0}; /* "second" */
|
|
|
|
static const UChar _third[] = {116,104,105,114,100,0}; /* "third" */
|
|
|
|
static const UChar _fourth[]= {102,111,117,114,116,104,0};/* "fourth" */
|
|
|
|
|
|
|
|
static const UChar* test2[] = {
|
|
|
|
_first, _second, _third, _fourth
|
|
|
|
};
|
|
|
|
|
|
|
|
struct uchArrayContext {
|
|
|
|
int32_t currIndex;
|
|
|
|
int32_t maxIndex;
|
|
|
|
UChar *currUChar;
|
|
|
|
UChar **array;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct uchArrayContext uchArrayContext;
|
|
|
|
|
|
|
|
#define ucont ((uchArrayContext *)en->context)
|
|
|
|
|
|
|
|
static void U_CALLCONV
|
|
|
|
uchArrayClose(UEnumeration *en) {
|
|
|
|
free(en);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t U_CALLCONV
|
|
|
|
uchArrayCount(UEnumeration *en, UErrorCode *status) {
|
|
|
|
return ucont->maxIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const UChar* U_CALLCONV
|
|
|
|
uchArrayUNext(UEnumeration *en, int32_t *resultLength, UErrorCode *status) {
|
|
|
|
if(ucont->currIndex >= ucont->maxIndex) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ucont->currUChar = (ucont->array)[ucont->currIndex];
|
|
|
|
*resultLength = u_strlen(ucont->currUChar);
|
|
|
|
ucont->currIndex++;
|
|
|
|
return ucont->currUChar;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void U_CALLCONV
|
|
|
|
uchArrayReset(UEnumeration *en, UErrorCode *status) {
|
|
|
|
ucont->currIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uchArrayContext myUCont = {
|
|
|
|
0, 0,
|
|
|
|
NULL, NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
UEnumeration uchEnum = {
|
|
|
|
NULL,
|
|
|
|
&myUCont,
|
|
|
|
uchArrayClose,
|
|
|
|
uchArrayCount,
|
|
|
|
uchArrayUNext,
|
|
|
|
uenum_nextDefault,
|
|
|
|
uchArrayReset
|
|
|
|
};
|
|
|
|
|
|
|
|
/********************************************************************/
|
2002-08-23 17:06:53 +00:00
|
|
|
|
2002-07-09 06:16:35 +00:00
|
|
|
static UEnumeration *getchArrayEnum(const char** source, int32_t size) {
|
2002-07-29 21:04:18 +00:00
|
|
|
UEnumeration *en = (UEnumeration *)malloc(sizeof(UEnumeration));
|
2002-07-16 22:29:23 +00:00
|
|
|
memcpy(en, &chEnum, sizeof(UEnumeration));
|
|
|
|
cont->array = (char **)source;
|
|
|
|
cont->maxIndex = size;
|
|
|
|
return en;
|
2002-07-09 06:16:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void EnumerationTest(void) {
|
2002-07-16 22:29:23 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
int32_t len = 0;
|
|
|
|
UEnumeration *en = getchArrayEnum(test1, sizeof(test1)/sizeof(test1[0]));
|
|
|
|
const char *string = NULL;
|
|
|
|
const UChar *uString = NULL;
|
|
|
|
while ((string = uenum_next(en, &len, &status))) {
|
2002-10-11 20:19:33 +00:00
|
|
|
log_verbose("read \"%s\", length %i\n", string, len);
|
2002-07-16 22:29:23 +00:00
|
|
|
}
|
|
|
|
uenum_reset(en, &status);
|
|
|
|
while ((uString = uenum_unext(en, &len, &status))) {
|
2002-10-11 20:19:33 +00:00
|
|
|
log_verbose("read \"%s\" (UChar), length %i\n", quikU2C(uString, len), len);
|
2002-07-16 22:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uenum_close(en);
|
2002-07-09 06:16:35 +00:00
|
|
|
}
|
|
|
|
|
2002-08-23 17:06:53 +00:00
|
|
|
static void EmptyEnumerationTest(void) {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
UEnumeration *emptyEnum = uprv_malloc(sizeof(UEnumeration));
|
|
|
|
|
|
|
|
uprv_memcpy(emptyEnum, &emptyEnumerator, sizeof(UEnumeration));
|
|
|
|
if (uenum_count(emptyEnum, &status) != -1 || status != U_UNSUPPORTED_ERROR) {
|
|
|
|
log_err("uenum_count failed\n");
|
|
|
|
}
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
if (uenum_next(emptyEnum, NULL, &status) != NULL || status != U_UNSUPPORTED_ERROR) {
|
|
|
|
log_err("uenum_next failed\n");
|
|
|
|
}
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
if (uenum_unext(emptyEnum, NULL, &status) != NULL || status != U_UNSUPPORTED_ERROR) {
|
|
|
|
log_err("uenum_unext failed\n");
|
|
|
|
}
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
uenum_reset(emptyEnum, &status);
|
|
|
|
if (status != U_UNSUPPORTED_ERROR) {
|
|
|
|
log_err("uenum_reset failed\n");
|
|
|
|
}
|
|
|
|
uenum_close(emptyEnum);
|
|
|
|
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
if (uenum_next(NULL, NULL, &status) != NULL || status != U_ZERO_ERROR) {
|
|
|
|
log_err("uenum_next(NULL) failed\n");
|
|
|
|
}
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
if (uenum_unext(NULL, NULL, &status) != NULL || status != U_ZERO_ERROR) {
|
|
|
|
log_err("uenum_unext(NULL) failed\n");
|
|
|
|
}
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
uenum_reset(NULL, &status);
|
|
|
|
if (status != U_ZERO_ERROR) {
|
|
|
|
log_err("uenum_reset(NULL) failed\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
emptyEnum = uprv_malloc(sizeof(UEnumeration));
|
|
|
|
uprv_memcpy(emptyEnum, &emptyPartialEnumerator, sizeof(UEnumeration));
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
if (uenum_unext(emptyEnum, NULL, &status) != NULL || status != U_UNSUPPORTED_ERROR) {
|
|
|
|
log_err("partial uenum_unext failed\n");
|
|
|
|
}
|
|
|
|
uenum_close(emptyEnum);
|
|
|
|
}
|
|
|
|
|
2002-10-11 20:19:33 +00:00
|
|
|
static UEnumeration *getuchArrayEnum(const UChar** source, int32_t size) {
|
|
|
|
UEnumeration *en = (UEnumeration *)malloc(sizeof(UEnumeration));
|
|
|
|
memcpy(en, &uchEnum, sizeof(UEnumeration));
|
|
|
|
ucont->array = (UChar **)source;
|
|
|
|
ucont->maxIndex = size;
|
|
|
|
return en;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DefaultNextTest(void) {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
int32_t len = 0;
|
|
|
|
UEnumeration *en = getuchArrayEnum(test2, sizeof(test2)/sizeof(test2[0]));
|
|
|
|
const char *string = NULL;
|
|
|
|
const UChar *uString = NULL;
|
|
|
|
while ((uString = uenum_unext(en, &len, &status))) {
|
|
|
|
log_verbose("read \"%s\" (UChar), length %i\n", quikU2C(uString, len), len);
|
|
|
|
}
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
log_err("FAIL: uenum_unext => %s\n", u_errorName(status));
|
|
|
|
}
|
|
|
|
uenum_reset(en, &status);
|
|
|
|
while ((string = uenum_next(en, &len, &status))) {
|
|
|
|
log_verbose("read \"%s\", length %i\n", string, len);
|
|
|
|
}
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
log_err("FAIL: uenum_next => %s\n", u_errorName(status));
|
|
|
|
}
|
|
|
|
|
|
|
|
uenum_close(en);
|
|
|
|
}
|
|
|
|
|
2002-07-25 18:32:04 +00:00
|
|
|
void addEnumerationTest(TestNode** root);
|
|
|
|
|
2002-07-09 06:16:35 +00:00
|
|
|
void addEnumerationTest(TestNode** root)
|
|
|
|
{
|
|
|
|
addTest(root, &EnumerationTest, "tsutil/uenumtst/EnumerationTest");
|
2002-08-23 17:06:53 +00:00
|
|
|
addTest(root, &EmptyEnumerationTest, "tsutil/uenumtst/EmptyEnumerationTest");
|
2002-10-11 20:19:33 +00:00
|
|
|
addTest(root, &DefaultNextTest, "tsutil/uenumtst/DefaultNextTest");
|
2002-07-16 22:29:23 +00:00
|
|
|
}
|