ICU-2397 test UTF-8 UCharIterator

X-SVN-Rev: 10829
This commit is contained in:
Markus Scherer 2003-01-10 01:47:52 +00:00
parent a59fdd65d8
commit 7a0647cb87

View File

@ -1173,9 +1173,183 @@ TestCountChar32() {
/* UCharIterator ------------------------------------------------------------ */
/*
* Compare results from two iterators, should be same.
* Assume that the text is not empty and that
* iteration start==0 and iteration limit==length.
*/
static void
compareIterators(UCharIterator *iter1, const char *n1,
UCharIterator *iter2, const char *n2) {
int32_t i, pos1, pos2, middle, length;
UChar32 c1, c2;
/* compare lengths */
length=iter1->getIndex(iter1, UITER_LENGTH);
pos2=iter2->getIndex(iter2, UITER_LENGTH);
if(length!=pos2) {
log_err("%s->getIndex(length)=%d != %d=%s->getIndex(length)\n", n1, length, pos2, n2);
return;
}
/* set into the middle */
middle=length/2;
pos1=iter1->move(iter1, middle, UITER_ZERO);
if(pos1!=middle) {
log_err("%s->move(from 0 to middle %d)=%d does not move to the middle\n", n1, middle, pos1);
return;
}
pos2=iter2->move(iter2, middle, UITER_ZERO);
if(pos2!=middle) {
log_err("%s->move(from 0 to middle %d)=%d does not move to the middle\n", n2, middle, pos2);
return;
}
/* test current() */
c1=iter1->current(iter1);
c2=iter2->current(iter2);
if(c1!=c2) {
log_err("%s->current()=U+%04x != U+%04x=%s->current() at middle=%d\n", n1, c1, c2, n2, middle);
return;
}
/* move forward 3 UChars */
for(i=0; i<3; ++i) {
c1=iter1->next(iter1);
c2=iter2->next(iter2);
if(c1!=c2) {
log_err("%s->next()=U+%04x != U+%04x=%s->next() at %d (started in middle)\n", n1, c1, c2, n2, iter1->getIndex(iter1, UITER_CURRENT));
return;
}
}
/* move backward 5 UChars */
for(i=0; i<5; ++i) {
c1=iter1->previous(iter1);
c2=iter2->previous(iter2);
if(c1!=c2) {
log_err("%s->previous()=U+%04x != U+%04x=%s->previous() at %d (started in middle)\n", n1, c1, c2, n2, iter1->getIndex(iter1, UITER_CURRENT));
return;
}
}
/* iterate forward from the beginning */
pos1=iter1->move(iter1, 0, UITER_START);
if(pos1<0) {
log_err("%s->move(start) failed\n", n1);
return;
}
if(!iter1->hasNext(iter1)) {
log_err("%s->hasNext() at the start returns FALSE\n", n1);
return;
}
pos2=iter2->move(iter2, 0, UITER_START);
if(pos2<0) {
log_err("%s->move(start) failed\n", n2);
return;
}
if(!iter2->hasNext(iter2)) {
log_err("%s->hasNext() at the start returns FALSE\n", n2);
return;
}
do {
c1=iter1->next(iter1);
c2=iter2->next(iter2);
if(c1!=c2) {
log_err("%s->next()=U+%04x != U+%04x=%s->next() at %d\n", n1, c1, c2, n2, iter1->getIndex(iter1, UITER_CURRENT));
return;
}
} while(c1>=0);
if(iter1->hasNext(iter1)) {
log_err("%s->hasNext() at the end returns TRUE\n", n1);
return;
}
if(iter2->hasNext(iter2)) {
log_err("%s->hasNext() at the end returns TRUE\n", n2);
return;
}
/* back to the middle */
pos1=iter1->move(iter1, middle, UITER_ZERO);
if(pos1!=middle) {
log_err("%s->move(from end to middle %d)=%d does not move to the middle\n", n1, middle, pos1);
return;
}
pos2=iter2->move(iter2, middle, UITER_ZERO);
if(pos2!=middle) {
log_err("%s->move(from end to middle %d)=%d does not move to the middle\n", n2, middle, pos2);
return;
}
/* move to index 1 */
pos1=iter1->move(iter1, 1, UITER_ZERO);
if(pos1!=1) {
log_err("%s->move(from middle %d to 1)=%d does not move to 1\n", n1, middle, pos1);
return;
}
pos2=iter2->move(iter2, 1, UITER_ZERO);
if(pos2!=1) {
log_err("%s->move(from middle %d to 1)=%d does not move to 1\n", n2, middle, pos2);
return;
}
/* iterate backward from the end */
pos1=iter1->move(iter1, 0, UITER_LIMIT);
if(pos1<0) {
log_err("%s->move(limit) failed\n", n1);
return;
}
if(!iter1->hasPrevious(iter1)) {
log_err("%s->hasPrevious() at the end returns FALSE\n", n1);
return;
}
pos2=iter2->move(iter2, 0, UITER_LIMIT);
if(pos2<0) {
log_err("%s->move(limit) failed\n", n2);
return;
}
if(!iter2->hasPrevious(iter2)) {
log_err("%s->hasPrevious() at the end returns FALSE\n", n2);
return;
}
do {
c1=iter1->previous(iter1);
c2=iter2->previous(iter2);
if(c1!=c2) {
log_err("%s->previous()=U+%04x != U+%04x=%s->previous() at %d\n", n1, c1, c2, n2, iter1->getIndex(iter1, UITER_CURRENT));
return;
}
} while(c1>=0);
if(iter1->hasPrevious(iter1)) {
log_err("%s->hasPrevious() at the start returns TRUE\n", n1);
return;
}
if(iter2->hasPrevious(iter2)) {
log_err("%s->hasPrevious() at the start returns TRUE\n", n2);
return;
}
}
static void
TestUCharIterator() {
UCharIterator iter;
static const UChar text[]={
0x61, 0x62, 0x63, 0xd801, 0xdffd, 0x78, 0x79, 0x7a, 0
};
char utf8[40];
UCharIterator iter, iter1, iter2;
UErrorCode errorCode;
int32_t length;
/* simple API/code coverage - test NOOP UCharIterator */
uiter_setString(&iter, NULL, 0);
@ -1185,4 +1359,20 @@ TestUCharIterator() {
) {
log_err("NOOP UCharIterator behaves unexpectedly\n");
}
/* compare the same string between UTF-16 and UTF-8 UCharIterators */
errorCode=U_ZERO_ERROR;
u_strToUTF8(utf8, sizeof(utf8), &length, text, -1, &errorCode);
if(U_FAILURE(errorCode)) {
log_err("u_strToUTF8() failed, %s\n", u_errorName(errorCode));
return;
}
uiter_setString(&iter1, text, -1);
uiter_setUTF8(&iter2, utf8, length);
compareIterators(&iter1, "UTF16Iterator", &iter2, "UTF8Iterator");
/* try again with length=-1 */
uiter_setUTF8(&iter2, utf8, -1);
compareIterators(&iter1, "UTF16Iterator", &iter2, "UTF8Iterator_1");
}