/*
*******************************************************************************
- * Copyright (C) 2003, International Business Machines Corporation and *
+ * Copyright (C) 2003-2004, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
#include "unicode/coll.h"
#include "unicode/strenum.h"
#include "hash.h"
+#include "uassert.h"
#include "ucol_imp.h" // internal api needed to test ucollator equality
#include "cstring.h" // internal api used to compare locale strings
+void CollationServiceTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par */)
+{
+ if (exec) logln("TestSuite CollationServiceTest: ");
+ switch (index) {
+ TESTCASE(0, TestRegister);
+ TESTCASE(1, TestRegisterFactory);
+ TESTCASE(2, TestSeparateTree);
+ default: name = ""; break;
+ }
+}
+
void CollationServiceTest::TestRegister()
{
- // register a singleton
+#if !UCONFIG_NO_SERVICE
+ // register a singleton
const Locale& FR = Locale::getFrance();
const Locale& US = Locale::getUS();
const Locale US_FOO("en", "US", "FOO");
// ensure original collator's params not touched
Locale loc = frcol->getLocale(ULOC_REQUESTED_LOCALE, status);
if (loc != FR) {
- errln(UnicodeString("fr collator's requested locale changed to ") + loc.getName());
+ errln(UnicodeString("fr collator's requested locale changed to ") + loc.getName());
}
loc = frcol->getLocale(ULOC_VALID_LOCALE, status);
if (loc != FR) {
- errln(UnicodeString("fr collator's valid locale changed to ") + loc.getName());
+ errln(UnicodeString("fr collator's valid locale changed to ") + loc.getName());
}
loc = ncol->getLocale(ULOC_REQUESTED_LOCALE, status);
UnicodeString locName = fu_FU.getName();
StringEnumeration* localeEnum = Collator::getAvailableLocales();
UBool found = FALSE;
- const UnicodeString* locStr;
+ const UnicodeString* locStr, *ls2;
for (locStr = localeEnum->snext(status);
!found && locStr != NULL;
locStr = localeEnum->snext(status)) {
found = TRUE;
}
}
+
+ StringEnumeration *le2 = NULL;
+ localeEnum->reset(status);
+ int32_t i, count;
+ count = localeEnum->count(status);
+ for(i = 0; i < count; ++i) {
+ if(i == count / 2) {
+ le2 = localeEnum->clone();
+ if(le2 == NULL || count != le2->count(status)) {
+ errln("ServiceEnumeration.clone() failed");
+ break;
+ }
+ }
+ if(i >= count / 2) {
+ locStr = localeEnum->snext(status);
+ ls2 = le2->snext(status);
+ if(*locStr != *ls2) {
+ errln("ServiceEnumeration.clone() failed for item %d", i);
+ }
+ } else {
+ localeEnum->snext(status);
+ }
+ }
+
delete localeEnum;
+ delete le2;
if (!found) {
errln("new locale fu_FU not reported as supported locale");
delete ncol; ncol = NULL;
delete fucol; fucol = NULL;
}
+#endif
}
// ------------------
+#if !UCONFIG_NO_SERVICE
struct CollatorInfo {
Locale locale;
Collator* collator;
{
CollatorInfo** p;
for (p = info; *p; ++p) {}
- count = p - info;
+ count = (int32_t)(p - info);
}
~TestFactory() {
};
char TestFactory::gClassID = 0;
+#endif
void CollationServiceTest::TestRegisterFactory(void)
{
+#if !UCONFIG_NO_SERVICE
+ int32_t n1, n2, n3;
Locale fu_FU("fu", "FU", "");
Locale fu_FU_FOO("fu", "FU", "FOO");
fuFUNames->put(fu_FU.getName(), new UnicodeString("ze leetle bunny Fu-Fu"), status);
fuFUNames->put(fu_FU_FOO.getName(), new UnicodeString("zee leetel bunny Foo-Foo"), status);
- fuFUNames->put(Locale::getUS().getName(), new UnicodeString("little bunny Foo Foo"), status);
+ fuFUNames->put(Locale::getDefault().getName(), new UnicodeString("little bunny Foo Foo"), status);
Collator* frcol = Collator::createInstance(Locale::getFrance(), status);
Collator* gecol = Collator::createInstance(Locale::getGermany(), status);
Collator* fucol = Collator::createInstance(fu_FU, status);
{
+ n1 = checkAvailable("before registerFactory");
+
URegistryKey key = Collator::registerFactory(factory, status);
+
+ n2 = checkAvailable("after registerFactory");
+ assertTrue("count after > count before", n2 > n1);
+
Collator* ncol = Collator::createInstance(Locale::getUS(), status);
if (*frcol != *ncol) {
errln("frcoll for en_US failed");
if (*fucol != *ncol) {
errln("collator after unregister does not match original fu_FU");
}
- delete ncol;
+ delete ncol;
+
+ n3 = checkAvailable("after unregister");
+ assertTrue("count after unregister == count before register", n3 == n1);
}
delete fucol;
delete uscol;
+#endif
}
-void CollationServiceTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par */)
-{
- if (exec) logln("TestSuite CollationServiceTest: ");
- switch (index) {
- TESTCASE(0, TestRegister);
- TESTCASE(1, TestRegisterFactory);
- default: name = ""; break;
+/**
+ * Iterate through the given iterator, checking to see that all the strings
+ * in the expected array are present.
+ * @param expected array of strings we expect to see, or NULL
+ * @param expectedCount number of elements of expected, or 0
+ */
+int32_t CollationServiceTest::checkStringEnumeration(const char* msg,
+ StringEnumeration& iter,
+ const char** expected,
+ int32_t expectedCount) {
+ UErrorCode ec = U_ZERO_ERROR;
+ U_ASSERT(expectedCount >= 0 && expectedCount < 31); // [sic] 31 not 32
+ int32_t i = 0, n = iter.count(ec);
+ assertSuccess("count", ec);
+ UnicodeString buf;
+ int32_t seenMask = 0;
+ for (;; ++i) {
+ const UnicodeString* s = iter.snext(ec);
+ if (!assertSuccess("snext", ec) || s == NULL) break;
+ if (i != 0) buf.append(", ");
+ buf.append(*s);
+ // check expected list
+ for (int32_t j=0, bit=1; j<expectedCount; ++j, bit<<=1) {
+ if ((seenMask&bit)==0) {
+ UnicodeString exp(expected[j], (char*)NULL);
+ if (*s == exp) {
+ seenMask |= bit;
+ logln((UnicodeString)"Ok: \"" + exp + "\" seen");
+ }
+ }
+ }
+ }
+ // can't get pesky operator+(const US&, foo) to cooperate; use toString
+#if !UCONFIG_NO_FORMATTING
+ logln(UnicodeString() + msg + " = [" + buf + "] (" + toString(i) + ")");
+#else
+ logln(UnicodeString() + msg + " = [" + buf + "] (??? NO_FORMATTING)");
+#endif
+ assertTrue("count verified", i==n);
+ // did we see all expected strings?
+ if (((1<<expectedCount)-1) != seenMask) {
+ for (int32_t j=0, bit=1; j<expectedCount; ++j, bit<<=1) {
+ if ((seenMask&bit)==0) {
+ errln((UnicodeString)"FAIL: \"" + expected[j] + "\" not seen");
+ }
+ }
}
+ return n;
+}
+
+/**
+ * Check the integrity of the results of Collator::getAvailableLocales().
+ * Return the number of items returned.
+ */
+#if !UCONFIG_NO_SERVICE
+int32_t CollationServiceTest::checkAvailable(const char* msg) {
+ StringEnumeration *iter = Collator::getAvailableLocales();
+ if (!assertTrue("getAvailableLocales != NULL", iter!=NULL)) return -1;
+ int32_t n = checkStringEnumeration(msg, *iter, NULL, 0);
+ delete iter;
+ return n;
+}
+#endif
+
+static const char* KW[] = {
+ "collation"
+};
+static const int32_t KW_COUNT = sizeof(KW)/sizeof(KW[0]);
+
+static const char* KWVAL[] = {
+ "phonebook",
+ "stroke"
+};
+static const int32_t KWVAL_COUNT = sizeof(KWVAL)/sizeof(KWVAL[0]);
+
+void CollationServiceTest::TestSeparateTree() {
+ UErrorCode ec = U_ZERO_ERROR;
+ StringEnumeration *iter = Collator::getKeywords(ec);
+ if (!assertTrue("getKeywords != NULL", iter!=NULL)) return;
+ if (!assertSuccess("getKeywords", ec)) return;
+ checkStringEnumeration("getKeywords", *iter, KW, KW_COUNT);
+ delete iter;
+
+ iter = Collator::getKeywordValues(KW[0], ec);
+ if (!assertTrue("getKeywordValues != NULL", iter!=NULL)) return;
+ if (!assertSuccess("getKeywordValues", ec)) return;
+ checkStringEnumeration("getKeywordValues", *iter, KWVAL, KWVAL_COUNT);
+ delete iter;
+
+ UBool isAvailable;
+ Locale equiv = Collator::getFunctionalEquivalent("collation",
+ Locale::createFromName("fr"),
+ isAvailable, ec);
+ assertSuccess("getFunctionalEquivalent", ec);
+ assertEquals("getFunctionalEquivalent(fr)", "fr", equiv.getName());
+ assertTrue("getFunctionalEquivalent(fr).isAvailable==TRUE",
+ isAvailable == TRUE);
+
+ equiv = Collator::getFunctionalEquivalent("collation",
+ Locale::createFromName("fr_FR"),
+ isAvailable, ec);
+ assertSuccess("getFunctionalEquivalent", ec);
+ assertEquals("getFunctionalEquivalent(fr_FR)", "fr", equiv.getName());
+ assertTrue("getFunctionalEquivalent(fr_FR).isAvailable==TRUE",
+ isAvailable == TRUE);
}
#endif