X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/b75a7d8f3b4adbae880cab104ce2c6a50eee4db2..f59164e3d128c7675a4d3934206346a3384e53a5:/icuSources/test/intltest/itercoll.cpp diff --git a/icuSources/test/intltest/itercoll.cpp b/icuSources/test/intltest/itercoll.cpp index 8596b2a7..7af192cf 100644 --- a/icuSources/test/intltest/itercoll.cpp +++ b/icuSources/test/intltest/itercoll.cpp @@ -1,6 +1,6 @@ /******************************************************************** - * COPYRIGHT: - * Copyright (c) 1997-2003, International Business Machines Corporation and + * COPYRIGHT: + * Copyright (c) 1997-2016, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ @@ -18,8 +18,6 @@ #include "unicode/uchar.h" #include "cmemory.h" -#define ARRAY_LENGTH(array) (sizeof array / sizeof array[0]) - static UErrorCode status = U_ZERO_ERROR; CollationIteratorTest::CollationIteratorTest() @@ -30,7 +28,7 @@ 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; } @@ -173,10 +171,22 @@ void CollationIteratorTest::TestPrevious(/* char* par */) 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(); @@ -190,7 +200,6 @@ void CollationIteratorTest::TestOffset(/* char* par */) // 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); @@ -203,11 +212,66 @@ void CollationIteratorTest::TestOffset(/* char* par */) 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. } /** @@ -263,13 +327,13 @@ void CollationIteratorTest::TestSetText(/* char* par */) 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; @@ -303,7 +367,7 @@ void CollationIteratorTest::TestMaxExpansion(/* char* par */) order = iter->previous(status); while (U_SUCCESS(status) - && iter->previous(status) != (int32_t)UCOL_NULLORDER) + && iter->previous(status) != (int32_t)CollationElementIterator::NULLORDER) { count ++; } @@ -510,10 +574,10 @@ void CollationIteratorTest::TestConstructors() || *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; @@ -628,7 +692,7 @@ void CollationIteratorTest::runIndexedTest(int32_t index, UBool exec, const char default: name = ""; break; } } else { - errln("Class iterator not instantiated"); + dataerrln("Class iterator not instantiated"); name = ""; } }