/********************************************************************
- * COPYRIGHT:
- * Copyright (c) 1997-2003, International Business Machines Corporation and
+ * COPYRIGHT:
+ * Copyright (c) 1997-2016, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
#include "unicode/uchar.h"
#include "cmemory.h"
-#define ARRAY_LENGTH(array) (sizeof array / sizeof array[0])
-
static UErrorCode status = U_ZERO_ERROR;
CollationIteratorTest::CollationIteratorTest()
if(U_FAILURE(status)) {
delete en_us;
en_us = 0;
- errln("Collator creation failed with %s", u_errorName(status));
+ errcheckln(status, "Collator creation failed with %s", u_errorName(status));
return;
}
void CollationIteratorTest::TestOffset(/* char* par */)
{
CollationElementIterator *iter = en_us->createCollationElementIterator(test1);
+ UErrorCode status = U_ZERO_ERROR;
+ // testing boundaries
+ iter->setOffset(0, status);
+ if (U_FAILURE(status) || iter->previous(status) != CollationElementIterator::NULLORDER) {
+ errln("Error: After setting offset to 0, we should be at the end "
+ "of the backwards iteration");
+ }
+ iter->setOffset(test1.length(), status);
+ if (U_FAILURE(status) || iter->next(status) != CollationElementIterator::NULLORDER) {
+ errln("Error: After setting offset to end of the string, we should "
+ "be at the end of the backwards iteration");
+ }
// Run all the way through the iterator, then get the offset
int32_t orderLength = 0;
- int32_t *orders = getOrders(*iter, orderLength);
+ Order *orders = getOrders(*iter, orderLength);
int32_t offset = iter->getOffset();
// Now set the offset back to the beginning and see if it works
CollationElementIterator *pristine = en_us->createCollationElementIterator(test1);
- UErrorCode status = U_ZERO_ERROR;
iter->setOffset(0, status);
assertEqual(*iter, *pristine);
}
- // TODO: try iterating halfway through a messy string.
-
delete pristine;
delete[] orders;
delete iter;
+
+ // setting offset in the middle of a contraction
+ UnicodeString contraction = "change";
+ status = U_ZERO_ERROR;
+ RuleBasedCollator tailored("& a < ch", status);
+ if (U_FAILURE(status)) {
+ errln("Error: in creation of Spanish collator - %s", u_errorName(status));
+ return;
+ }
+ iter = tailored.createCollationElementIterator(contraction);
+ Order *order = getOrders(*iter, orderLength);
+ iter->setOffset(1, status); // sets offset in the middle of ch
+ int32_t order2Length = 0;
+ Order *order2 = getOrders(*iter, order2Length);
+ if (orderLength != order2Length || uprv_memcmp(order, order2, orderLength * sizeof(Order)) != 0) {
+ errln("Error: setting offset in the middle of a contraction should be the same as setting it to the start of the contraction");
+ }
+ delete[] order;
+ delete[] order2;
+ delete iter;
+ contraction = "peache";
+ iter = tailored.createCollationElementIterator(contraction);
+ iter->setOffset(3, status);
+ order = getOrders(*iter, orderLength);
+ iter->setOffset(4, status); // sets offset in the middle of ch
+ order2 = getOrders(*iter, order2Length);
+ if (orderLength != order2Length || uprv_memcmp(order, order2, orderLength * sizeof(Order)) != 0) {
+ errln("Error: setting offset in the middle of a contraction should be the same as setting it to the start of the contraction");
+ }
+ delete[] order;
+ delete[] order2;
+ delete iter;
+ // setting offset in the middle of a surrogate pair
+ UnicodeString surrogate = UNICODE_STRING_SIMPLE("\\ud800\\udc00str").unescape();
+ iter = tailored.createCollationElementIterator(surrogate);
+ order = getOrders(*iter, orderLength);
+ iter->setOffset(1, status); // sets offset in the middle of surrogate
+ order2 = getOrders(*iter, order2Length);
+ if (orderLength != order2Length || uprv_memcmp(order, order2, orderLength * sizeof(Order)) != 0) {
+ errln("Error: setting offset in the middle of a surrogate pair should be the same as setting it to the start of the surrogate pair");
+ }
+ delete[] order;
+ delete[] order2;
+ delete iter;
+ surrogate = UNICODE_STRING_SIMPLE("simple\\ud800\\udc00str").unescape();
+ iter = tailored.createCollationElementIterator(surrogate);
+ iter->setOffset(6, status);
+ order = getOrders(*iter, orderLength);
+ iter->setOffset(7, status); // sets offset in the middle of surrogate
+ order2 = getOrders(*iter, order2Length);
+ if (orderLength != order2Length || uprv_memcmp(order, order2, orderLength * sizeof(Order)) != 0) {
+ errln("Error: setting offset in the middle of a surrogate pair should be the same as setting it to the start of the surrogate pair");
+ }
+ delete[] order;
+ delete[] order2;
+ delete iter;
+ // TODO: try iterating halfway through a messy string.
}
/**
UnicodeString empty("");
iter1->setText(empty, status);
if (U_FAILURE(status)
- || iter1->next(status) != (int32_t)UCOL_NULLORDER) {
+ || iter1->next(status) != (int32_t)CollationElementIterator::NULLORDER) {
errln("Empty string should have no CEs.");
}
((StringCharacterIterator *)chariter)->setText(empty);
iter1->setText(*chariter, status);
if (U_FAILURE(status)
- || iter1->next(status) != (int32_t)UCOL_NULLORDER) {
+ || iter1->next(status) != (int32_t)CollationElementIterator::NULLORDER) {
errln("Empty string should have no CEs.");
}
delete chariter;
order = iter->previous(status);
while (U_SUCCESS(status)
- && iter->previous(status) != (int32_t)UCOL_NULLORDER)
+ && iter->previous(status) != (int32_t)CollationElementIterator::NULLORDER)
{
count ++;
}
|| *iter2 != *iter1) {
errln("CollationElementIterators constructed with the same string data should be the same at the start");
}
- if (iter1->next(status) != (int32_t)UCOL_NULLORDER) {
+ if (iter1->next(status) != (int32_t)CollationElementIterator::NULLORDER) {
errln("Empty string should have no CEs.");
}
- if (iter2->next(status) != (int32_t)UCOL_NULLORDER) {
+ if (iter2->next(status) != (int32_t)CollationElementIterator::NULLORDER) {
errln("Empty string should have no CEs.");
}
delete iter1;
default: name = ""; break;
}
} else {
- errln("Class iterator not instantiated");
+ dataerrln("Class iterator not instantiated");
name = "";
}
}