2000-01-15 02:00:06 +00:00
|
|
|
/********************************************************************
|
2002-08-12 20:12:25 +00:00
|
|
|
* COPYRIGHT:
|
2006-04-02 19:07:50 +00:00
|
|
|
* Copyright (c) 1997-2006, International Business Machines Corporation and
|
2000-01-15 02:00:06 +00:00
|
|
|
* others. All Rights Reserved.
|
|
|
|
********************************************************************/
|
1999-08-16 21:50:52 +00:00
|
|
|
//===============================================================================
|
|
|
|
//
|
|
|
|
// File apitest.cpp
|
|
|
|
//
|
2002-08-12 20:12:25 +00:00
|
|
|
//
|
1999-08-16 21:50:52 +00:00
|
|
|
//
|
|
|
|
// Created by: Helena Shih
|
|
|
|
//
|
|
|
|
// Modification History:
|
|
|
|
//
|
|
|
|
// Date Name Description
|
|
|
|
// 2/5/97 aliu Added streamIn and streamOut methods. Added
|
|
|
|
// constructor which reads RuleBasedCollator object from
|
|
|
|
// a binary file. Added writeToFile method which streams
|
|
|
|
// RuleBasedCollator out to a binary file. The streamIn
|
|
|
|
// and streamOut methods use istream and ostream objects
|
|
|
|
// in binary mode.
|
|
|
|
// 6/30/97 helena Added tests for CollationElementIterator::setText, getOffset
|
2002-08-12 20:12:25 +00:00
|
|
|
// setOffset and DecompositionIterator::getOffset, setOffset.
|
1999-08-16 21:50:52 +00:00
|
|
|
// DecompositionIterator is made public so add class scope
|
|
|
|
// testing.
|
|
|
|
// 02/10/98 damiba Added test for compare(UnicodeString&, UnicodeString&, int32_t)
|
|
|
|
//===============================================================================
|
|
|
|
|
2002-09-21 00:43:14 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_COLLATION
|
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/coll.h"
|
|
|
|
#include "unicode/tblcoll.h"
|
|
|
|
#include "unicode/coleitr.h"
|
|
|
|
#include "unicode/sortkey.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "apicoll.h"
|
2000-06-29 23:28:46 +00:00
|
|
|
#include "unicode/chariter.h"
|
|
|
|
#include "unicode/schriter.h"
|
2001-06-26 17:44:23 +00:00
|
|
|
#include "unicode/ustring.h"
|
2006-08-09 22:16:47 +00:00
|
|
|
#include "unicode/ucol.h"
|
2000-06-29 23:28:46 +00:00
|
|
|
|
2000-12-12 19:40:58 +00:00
|
|
|
#include "sfwdchit.h"
|
2001-03-14 00:24:02 +00:00
|
|
|
#include "cmemory.h"
|
2004-12-03 20:22:01 +00:00
|
|
|
#include <stdlib.h>
|
2000-12-12 19:40:58 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
2000-05-18 22:08:39 +00:00
|
|
|
CollationAPITest::doAssert(UBool condition, const char *message)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
if (!condition) {
|
|
|
|
errln(UnicodeString("ERROR : ") + message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-11-19 21:37:56 +00:00
|
|
|
#ifdef U_USE_COLLATION_OBSOLETE_2_6
|
2001-03-14 00:24:02 +00:00
|
|
|
/*
|
|
|
|
* Test Collator::createInstance(... version...) for some locale. Called by TestProperty().
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
TestOpenVersion(IntlTest &test, const Locale &locale) {
|
|
|
|
UVersionInfo version1, version2;
|
|
|
|
Collator *collator1, *collator2;
|
|
|
|
UErrorCode errorCode;
|
|
|
|
|
|
|
|
errorCode=U_ZERO_ERROR;
|
|
|
|
collator1=Collator::createInstance(locale, errorCode);
|
|
|
|
if(U_SUCCESS(errorCode)) {
|
|
|
|
/* get the current version */
|
|
|
|
collator1->getVersion(version1);
|
|
|
|
delete collator1;
|
|
|
|
|
|
|
|
/* try to get that same version again */
|
|
|
|
collator2=Collator::createInstance(locale, version1, errorCode);
|
|
|
|
if(U_SUCCESS(errorCode)) {
|
|
|
|
collator2->getVersion(version2);
|
|
|
|
if(0!=uprv_memcmp(version1, version2, sizeof(UVersionInfo))) {
|
|
|
|
test.errln("error: Collator::createInstance(\"%s\", (%s collator)->getVersion()) returns a different collator\n", locale.getName(), locale.getName());
|
|
|
|
}
|
|
|
|
delete collator2;
|
|
|
|
} else {
|
|
|
|
test.errln("error: Collator::createInstance(\"%s\", (%s collator)->getVersion()) fails: %s\n", locale.getName(), locale.getName(), u_errorName(errorCode));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-11-19 21:37:56 +00:00
|
|
|
#endif
|
2001-03-14 00:24:02 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// Collator Class Properties
|
|
|
|
// ctor, dtor, createInstance, compare, getStrength/setStrength
|
|
|
|
// getDecomposition/setDecomposition, getDisplayName
|
|
|
|
void
|
2000-08-23 19:11:16 +00:00
|
|
|
CollationAPITest::TestProperty(/* char* par */)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode success = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
Collator *col = 0;
|
2002-08-12 20:12:25 +00:00
|
|
|
/*
|
2002-02-28 22:16:27 +00:00
|
|
|
All the collations have the same version in an ICU
|
|
|
|
version.
|
|
|
|
ICU 2.0 currVersionArray = {0x18, 0xC0, 0x02, 0x02};
|
2002-03-28 17:52:19 +00:00
|
|
|
ICU 2.1 currVersionArray = {0x19, 0x00, 0x03, 0x03};
|
2002-07-20 04:38:14 +00:00
|
|
|
ICU 2.2 currVersionArray = {0x21, 0x40, 0x04, 0x04};
|
2003-05-01 17:45:08 +00:00
|
|
|
ICU 2.4 currVersionArray = {0x21, 0x40, 0x04, 0x04};
|
2003-08-23 01:46:02 +00:00
|
|
|
ICU 2.6 currVersionArray = {0x21, 0x40, 0x03, 0x03};
|
2005-06-24 23:09:31 +00:00
|
|
|
ICU 2.8 currVersionArray = {0x29, 0x80, 0x00, 0x04};
|
2006-06-16 05:46:38 +00:00
|
|
|
ICU 3.4 currVersionArray = {0x31, 0xC0, 0x00, 0x04};
|
2002-02-28 22:16:27 +00:00
|
|
|
*/
|
2006-06-16 05:46:38 +00:00
|
|
|
UVersionInfo currVersionArray = {0x31, 0xC0, 0x00, 0x05};
|
2000-05-15 19:49:14 +00:00
|
|
|
UVersionInfo versionArray;
|
2000-08-09 23:39:15 +00:00
|
|
|
int i = 0;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
logln("The property tests begin : ");
|
|
|
|
logln("Test ctors : ");
|
2002-08-21 00:16:30 +00:00
|
|
|
col = Collator::createInstance(Locale::getEnglish(), success);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(success))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
errln("Default Collator creation failed.");
|
|
|
|
return;
|
|
|
|
}
|
2000-05-15 19:49:14 +00:00
|
|
|
|
2002-02-28 22:16:27 +00:00
|
|
|
col->getVersion(versionArray);
|
2000-08-09 23:39:15 +00:00
|
|
|
for (i=0; i<4; ++i) {
|
2002-02-28 22:16:27 +00:00
|
|
|
if (versionArray[i] != currVersionArray[i]) {
|
2002-08-12 20:12:25 +00:00
|
|
|
errln("Testing ucol_getVersion() - unexpected result: %d.%d.%d.%d",
|
2002-02-28 22:16:27 +00:00
|
|
|
versionArray[0], versionArray[1], versionArray[2], versionArray[3]);
|
2000-08-09 23:39:15 +00:00
|
|
|
break;
|
|
|
|
}
|
2000-05-15 19:49:14 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-03-19 13:21:51 +00:00
|
|
|
doAssert((col->compare("ab", "abc") == Collator::LESS), "ab < abc comparison failed");
|
|
|
|
doAssert((col->compare("ab", "AB") == Collator::LESS), "ab < AB comparison failed");
|
|
|
|
doAssert((col->compare("blackbird", "black-bird") == Collator::GREATER), "black-bird > blackbird comparison failed");
|
|
|
|
doAssert((col->compare("black bird", "black-bird") == Collator::LESS), "black bird > black-bird comparison failed");
|
|
|
|
doAssert((col->compare("Hello", "hello") == Collator::GREATER), "Hello > hello comparison failed");
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*start of update [Bertrand A. D. 02/10/98]*/
|
2003-03-19 13:21:51 +00:00
|
|
|
doAssert((col->compare("ab", "abc", 2) == Collator::EQUAL), "ab = abc with length 2 comparison failed");
|
|
|
|
doAssert((col->compare("ab", "AB", 2) == Collator::LESS), "ab < AB with length 2 comparison failed");
|
|
|
|
doAssert((col->compare("ab", "Aa", 1) == Collator::LESS), "ab < Aa with length 1 comparison failed");
|
|
|
|
doAssert((col->compare("ab", "Aa", 2) == Collator::GREATER), "ab > Aa with length 2 comparison failed");
|
|
|
|
doAssert((col->compare("black-bird", "blackbird", 5) == Collator::EQUAL), "black-bird = blackbird with length of 5 comparison failed");
|
|
|
|
doAssert((col->compare("black bird", "black-bird", 10) == Collator::LESS), "black bird < black-bird with length 10 comparison failed");
|
|
|
|
doAssert((col->compare("Hello", "hello", 5) == Collator::GREATER), "Hello > hello with length 5 comparison failed");
|
1999-08-16 21:50:52 +00:00
|
|
|
/*end of update [Bertrand A. D. 02/10/98]*/
|
|
|
|
|
|
|
|
|
|
|
|
logln("Test ctors ends.");
|
|
|
|
logln("testing Collator::getStrength() method ...");
|
|
|
|
doAssert((col->getStrength() == Collator::TERTIARY), "collation object has the wrong strength");
|
|
|
|
doAssert((col->getStrength() != Collator::PRIMARY), "collation object's strength is primary difference");
|
2001-06-26 17:44:23 +00:00
|
|
|
|
2002-08-12 20:12:25 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
logln("testing Collator::setStrength() method ...");
|
|
|
|
col->setStrength(Collator::SECONDARY);
|
|
|
|
doAssert((col->getStrength() != Collator::TERTIARY), "collation object's strength is secondary difference");
|
|
|
|
doAssert((col->getStrength() != Collator::PRIMARY), "collation object's strength is primary difference");
|
|
|
|
doAssert((col->getStrength() == Collator::SECONDARY), "collation object has the wrong strength");
|
|
|
|
|
|
|
|
UnicodeString name;
|
|
|
|
|
|
|
|
logln("Get display name for the US English collation in German : ");
|
2002-03-26 23:18:57 +00:00
|
|
|
logln(Collator::getDisplayName(Locale::getUS(), Locale::getGerman(), name));
|
1999-08-16 21:50:52 +00:00
|
|
|
doAssert((name == UnicodeString("Englisch (Vereinigte Staaten)")), "getDisplayName failed");
|
|
|
|
|
|
|
|
logln("Get display name for the US English collation in English : ");
|
2002-08-12 20:12:25 +00:00
|
|
|
logln(Collator::getDisplayName(Locale::getUS(), Locale::getEnglish(), name));
|
1999-08-16 21:50:52 +00:00
|
|
|
doAssert((name == UnicodeString("English (United States)")), "getDisplayName failed");
|
2001-03-21 18:26:34 +00:00
|
|
|
#if 0
|
|
|
|
// weiv : this test is bogus if we're running on any machine that has different default locale than English.
|
|
|
|
// Therefore, it is banned!
|
2000-06-29 23:28:46 +00:00
|
|
|
logln("Get display name for the US English in default locale language : ");
|
2002-08-12 20:12:25 +00:00
|
|
|
logln(Collator::getDisplayName(Locale::US, name));
|
2001-03-21 18:26:34 +00:00
|
|
|
doAssert((name == UnicodeString("English (United States)")), "getDisplayName failed if this is an English machine");
|
|
|
|
#endif
|
1999-08-16 21:50:52 +00:00
|
|
|
delete col; col = 0;
|
2002-08-12 20:12:25 +00:00
|
|
|
RuleBasedCollator *rcol = (RuleBasedCollator *)Collator::createInstance("da_DK",
|
2001-03-23 00:57:44 +00:00
|
|
|
success);
|
|
|
|
doAssert(rcol->getRules().length() != 0, "da_DK rules does not have length 0");
|
|
|
|
delete rcol;
|
|
|
|
|
2002-08-21 00:16:30 +00:00
|
|
|
col = Collator::createInstance(Locale::getFrench(), success);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(success))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
errln("Creating French collation failed.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
col->setStrength(Collator::PRIMARY);
|
|
|
|
logln("testing Collator::getStrength() method again ...");
|
|
|
|
doAssert((col->getStrength() != Collator::TERTIARY), "collation object has the wrong strength");
|
|
|
|
doAssert((col->getStrength() == Collator::PRIMARY), "collation object's strength is not primary difference");
|
2002-08-12 20:12:25 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
logln("testing French Collator::setStrength() method ...");
|
|
|
|
col->setStrength(Collator::TERTIARY);
|
|
|
|
doAssert((col->getStrength() == Collator::TERTIARY), "collation object's strength is not tertiary difference");
|
|
|
|
doAssert((col->getStrength() != Collator::PRIMARY), "collation object's strength is primary difference");
|
|
|
|
doAssert((col->getStrength() != Collator::SECONDARY), "collation object's strength is secondary difference");
|
|
|
|
|
|
|
|
logln("Create junk collation: ");
|
|
|
|
Locale abcd("ab", "CD", "");
|
1999-10-07 00:07:53 +00:00
|
|
|
success = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
Collator *junk = 0;
|
|
|
|
junk = Collator::createInstance(abcd, success);
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(success))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
errln("Junk collation creation failed, should at least return default.");
|
|
|
|
delete col;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete col;
|
2001-03-22 00:06:51 +00:00
|
|
|
col = Collator::createInstance(success);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(success))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
errln("Creating default collator failed.");
|
|
|
|
delete junk;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert(((RuleBasedCollator *)col)->getRules() == ((RuleBasedCollator *)junk)->getRules(),
|
2001-03-15 02:53:05 +00:00
|
|
|
"The default collation should be returned.");
|
2002-03-26 23:18:57 +00:00
|
|
|
Collator *frCol = Collator::createInstance(Locale::getFrance(), success);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(success))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
errln("Creating French collator failed.");
|
|
|
|
delete col; delete junk;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
doAssert((*frCol != *junk), "The junk is the same as the French collator.");
|
|
|
|
Collator *aFrCol = frCol->clone();
|
|
|
|
doAssert((*frCol == *aFrCol), "The cloning of a French collator failed.");
|
|
|
|
logln("Collator property test ended.");
|
|
|
|
|
|
|
|
delete col;
|
|
|
|
delete frCol;
|
|
|
|
delete aFrCol;
|
|
|
|
delete junk;
|
2001-03-14 00:24:02 +00:00
|
|
|
|
2003-11-19 21:37:56 +00:00
|
|
|
#ifdef U_USE_COLLATION_OBSOLETE_2_6
|
2001-03-14 00:24:02 +00:00
|
|
|
/* test Collator::createInstance(...version...) */
|
|
|
|
TestOpenVersion(*this, "");
|
|
|
|
TestOpenVersion(*this, "da");
|
|
|
|
TestOpenVersion(*this, "fr");
|
|
|
|
TestOpenVersion(*this, "ja");
|
|
|
|
|
|
|
|
/* try some bogus version */
|
|
|
|
versionArray[0]=0;
|
|
|
|
versionArray[1]=0x99;
|
|
|
|
versionArray[2]=0xc7;
|
|
|
|
versionArray[3]=0xfe;
|
|
|
|
col=Collator::createInstance(Locale(), versionArray, success);
|
|
|
|
if(U_SUCCESS(success)) {
|
|
|
|
errln("error: ucol_openVersion(bogus version) succeeded");
|
|
|
|
delete col;
|
|
|
|
}
|
2003-11-19 21:37:56 +00:00
|
|
|
#endif
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-03-23 00:57:44 +00:00
|
|
|
void
|
|
|
|
CollationAPITest::TestRuleBasedColl()
|
|
|
|
{
|
2001-03-29 01:48:16 +00:00
|
|
|
RuleBasedCollator *col1, *col2, *col3, *col4;
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
|
|
|
|
UnicodeString ruleset1("&9 < a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E");
|
|
|
|
UnicodeString ruleset2("&9 < a, A < b, B < c, C < d, D, e, E");
|
|
|
|
|
|
|
|
col1 = new RuleBasedCollator(ruleset1, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
errln("RuleBased Collator creation failed.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logln("PASS: RuleBased Collator creation passed\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
col2 = new RuleBasedCollator(ruleset2, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
errln("RuleBased Collator creation failed.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logln("PASS: RuleBased Collator creation passed\n");
|
|
|
|
}
|
|
|
|
|
2001-06-26 17:44:23 +00:00
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
Locale locale("aa", "AA");
|
|
|
|
col3 = (RuleBasedCollator *)Collator::createInstance(locale, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
errln("Fallback Collator creation failed.: %s\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logln("PASS: Fallback Collator creation passed\n");
|
|
|
|
}
|
|
|
|
delete col3;
|
|
|
|
|
2001-03-29 01:48:16 +00:00
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
col3 = (RuleBasedCollator *)Collator::createInstance(status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
errln("Default Collator creation failed.: %s\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logln("PASS: Default Collator creation passed\n");
|
|
|
|
}
|
2001-03-23 00:57:44 +00:00
|
|
|
|
2001-03-29 01:48:16 +00:00
|
|
|
UnicodeString rule1 = col1->getRules();
|
|
|
|
UnicodeString rule2 = col2->getRules();
|
|
|
|
UnicodeString rule3 = col3->getRules();
|
|
|
|
|
|
|
|
doAssert(rule1 != rule2, "Default collator getRules failed");
|
|
|
|
doAssert(rule2 != rule3, "Default collator getRules failed");
|
|
|
|
doAssert(rule1 != rule3, "Default collator getRules failed");
|
2002-08-12 20:12:25 +00:00
|
|
|
|
2001-03-29 01:48:16 +00:00
|
|
|
col4 = new RuleBasedCollator(rule2, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
errln("RuleBased Collator creation failed.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString rule4 = col4->getRules();
|
|
|
|
doAssert(rule2 == rule4, "Default collator getRules failed");
|
2001-06-26 17:44:23 +00:00
|
|
|
int32_t length4 = 0;
|
|
|
|
uint8_t *clonedrule4 = col4->cloneRuleData(length4, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
errln("Cloned rule data failed.\n");
|
|
|
|
return;
|
|
|
|
}
|
2001-03-29 01:48:16 +00:00
|
|
|
|
2002-07-29 21:04:18 +00:00
|
|
|
// free(clonedrule4); BAD API!!!!
|
|
|
|
uprv_free(clonedrule4);
|
|
|
|
|
|
|
|
|
2001-03-29 01:48:16 +00:00
|
|
|
delete col1;
|
|
|
|
delete col2;
|
|
|
|
delete col3;
|
|
|
|
delete col4;
|
2001-03-23 00:57:44 +00:00
|
|
|
}
|
|
|
|
|
2002-02-13 02:57:21 +00:00
|
|
|
void
|
|
|
|
CollationAPITest::TestRules()
|
|
|
|
{
|
|
|
|
RuleBasedCollator *coll;
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
UnicodeString rules;
|
|
|
|
|
2002-08-12 20:12:25 +00:00
|
|
|
coll = (RuleBasedCollator *)Collator::createInstance(Locale::getEnglish(), status);
|
2002-02-13 02:57:21 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
errln("English Collator creation failed.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logln("PASS: RuleBased Collator creation passed\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
coll->getRules(UCOL_TAILORING_ONLY, rules);
|
2004-10-13 00:44:55 +00:00
|
|
|
if (rules.length() != 0x0a) {
|
2004-10-01 21:07:02 +00:00
|
|
|
errln("English tailored rules failed - length is 0x%x expected 0x%x", rules.length(), 0x0e);
|
2002-02-13 02:57:21 +00:00
|
|
|
}
|
2002-08-12 20:12:25 +00:00
|
|
|
|
2002-02-13 02:57:21 +00:00
|
|
|
coll->getRules(UCOL_FULL_RULES, rules);
|
|
|
|
if (rules.length() < 0) {
|
|
|
|
errln("English full rules failed");
|
|
|
|
}
|
|
|
|
delete coll;
|
|
|
|
}
|
|
|
|
|
2001-03-23 00:57:44 +00:00
|
|
|
void
|
|
|
|
CollationAPITest::TestDecomposition() {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
Collator *en_US = Collator::createInstance("en_US", status),
|
|
|
|
*el_GR = Collator::createInstance("el_GR", status),
|
|
|
|
*vi_VN = Collator::createInstance("vi_VN", status);
|
|
|
|
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
errln("ERROR: collation creation failed.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-08-21 19:09:33 +00:00
|
|
|
/* there is no reason to have canonical decomposition in en_US OR default locale */
|
|
|
|
if (vi_VN->getAttribute(UCOL_NORMALIZATION_MODE, status) != UCOL_ON)
|
|
|
|
{
|
2004-10-01 21:07:02 +00:00
|
|
|
errln("ERROR: vi_VN collation did not have canonical decomposition for normalization!\n");
|
2002-08-21 19:09:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (el_GR->getAttribute(UCOL_NORMALIZATION_MODE, status) != UCOL_ON)
|
|
|
|
{
|
2004-10-01 21:07:02 +00:00
|
|
|
errln("ERROR: el_GR collation did not have canonical decomposition for normalization!\n");
|
2002-08-21 19:09:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (en_US->getAttribute(UCOL_NORMALIZATION_MODE, status) != UCOL_OFF)
|
|
|
|
{
|
2004-10-01 21:07:02 +00:00
|
|
|
errln("ERROR: en_US collation had canonical decomposition for normalization!\n");
|
2002-08-21 19:09:33 +00:00
|
|
|
}
|
2001-03-23 00:57:44 +00:00
|
|
|
|
|
|
|
delete en_US;
|
|
|
|
delete el_GR;
|
|
|
|
delete vi_VN;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CollationAPITest::TestSafeClone() {
|
2001-03-29 01:48:16 +00:00
|
|
|
static const int CLONETEST_COLLATOR_COUNT = 3;
|
|
|
|
Collator *someCollators [CLONETEST_COLLATOR_COUNT];
|
|
|
|
Collator *col;
|
|
|
|
UErrorCode err = U_ZERO_ERROR;
|
|
|
|
int index;
|
|
|
|
|
|
|
|
UnicodeString test1("abCda");
|
|
|
|
UnicodeString test2("abcda");
|
|
|
|
|
|
|
|
/* one default collator & two complex ones */
|
2002-01-24 19:45:08 +00:00
|
|
|
someCollators[0] = Collator::createInstance("en_US", err);
|
2001-03-29 01:48:16 +00:00
|
|
|
someCollators[1] = Collator::createInstance("ko", err);
|
|
|
|
someCollators[2] = Collator::createInstance("ja_JP", err);
|
2003-06-06 04:54:34 +00:00
|
|
|
if(U_FAILURE(err)) {
|
|
|
|
errln("Couldn't instantiate collators. Error: %s", u_errorName(err));
|
|
|
|
delete someCollators[0];
|
|
|
|
delete someCollators[1];
|
|
|
|
delete someCollators[2];
|
|
|
|
return;
|
|
|
|
}
|
2001-03-29 01:48:16 +00:00
|
|
|
|
|
|
|
/* change orig & clone & make sure they are independent */
|
|
|
|
|
|
|
|
for (index = 0; index < CLONETEST_COLLATOR_COUNT; index++)
|
|
|
|
{
|
|
|
|
col = someCollators[index]->safeClone();
|
|
|
|
if (col == 0) {
|
|
|
|
errln("SafeClone of collator should not return null\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
col->setStrength(Collator::TERTIARY);
|
|
|
|
someCollators[index]->setStrength(Collator::PRIMARY);
|
|
|
|
col->setAttribute(UCOL_CASE_LEVEL, UCOL_OFF, err);
|
|
|
|
someCollators[index]->setAttribute(UCOL_CASE_LEVEL, UCOL_OFF, err);
|
|
|
|
|
|
|
|
doAssert(col->greater(test1, test2), "Result should be \"abCda\" >>> \"abcda\" ");
|
|
|
|
doAssert(someCollators[index]->equals(test1, test2), "Result should be \"abcda\" == \"abCda\"");
|
|
|
|
delete col;
|
|
|
|
delete someCollators[index];
|
2001-03-23 00:57:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
2000-08-23 19:11:16 +00:00
|
|
|
CollationAPITest::TestHashCode(/* char* par */)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
logln("hashCode tests begin.");
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode success = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
Collator *col1 = 0;
|
2002-08-21 00:16:30 +00:00
|
|
|
col1 = Collator::createInstance(Locale::getEnglish(), success);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(success))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
errln("Default collation creation failed.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Collator *col2 = 0;
|
|
|
|
Locale dk("da", "DK", "");
|
|
|
|
col2 = Collator::createInstance(dk, success);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(success))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
errln("Danish collation creation failed.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Collator *col3 = 0;
|
2002-08-21 00:16:30 +00:00
|
|
|
col3 = Collator::createInstance(Locale::getEnglish(), success);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(success))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
errln("2nd default collation creation failed.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
logln("Collator::hashCode() testing ...");
|
2002-08-12 20:12:25 +00:00
|
|
|
|
|
|
|
doAssert(col1->hashCode() != col2->hashCode(), "Hash test1 result incorrect" );
|
|
|
|
doAssert(!(col1->hashCode() == col2->hashCode()), "Hash test2 result incorrect" );
|
|
|
|
doAssert(col1->hashCode() == col3->hashCode(), "Hash result not equal" );
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
logln("hashCode tests end.");
|
|
|
|
delete col1;
|
|
|
|
delete col2;
|
2001-03-23 00:57:44 +00:00
|
|
|
|
|
|
|
UnicodeString test1("Abcda");
|
|
|
|
UnicodeString test2("abcda");
|
2002-08-12 20:12:25 +00:00
|
|
|
|
2001-03-23 00:57:44 +00:00
|
|
|
CollationKey sortk1, sortk2, sortk3;
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2002-08-12 20:12:25 +00:00
|
|
|
|
2001-03-23 00:57:44 +00:00
|
|
|
col3->getCollationKey(test1, sortk1, status);
|
2002-08-12 20:12:25 +00:00
|
|
|
col3->getCollationKey(test2, sortk2, status);
|
|
|
|
col3->getCollationKey(test2, sortk3, status);
|
|
|
|
|
|
|
|
doAssert(sortk1.hashCode() != sortk2.hashCode(), "Hash test1 result incorrect");
|
2001-03-23 00:57:44 +00:00
|
|
|
doAssert(sortk2.hashCode() == sortk3.hashCode(), "Hash result not equal" );
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
delete col3;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// CollationKey -- Tests the CollationKey methods
|
|
|
|
//
|
|
|
|
void
|
2000-08-23 19:11:16 +00:00
|
|
|
CollationAPITest::TestCollationKey(/* char* par */)
|
2002-08-12 20:12:25 +00:00
|
|
|
{
|
1999-08-16 21:50:52 +00:00
|
|
|
logln("testing CollationKey begins...");
|
|
|
|
Collator *col = 0;
|
2000-06-29 23:28:46 +00:00
|
|
|
UErrorCode success=U_ZERO_ERROR;
|
2002-08-21 00:16:30 +00:00
|
|
|
col = Collator::createInstance(Locale::getEnglish(), success);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(success))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
errln("Default collation creation failed.");
|
|
|
|
return;
|
|
|
|
}
|
2003-06-06 04:54:34 +00:00
|
|
|
col->setStrength(Collator::TERTIARY);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
CollationKey sortk1, sortk2;
|
|
|
|
UnicodeString test1("Abcda"), test2("abcda");
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode key1Status = U_ZERO_ERROR, key2Status = U_ZERO_ERROR;
|
2001-06-26 17:44:23 +00:00
|
|
|
|
|
|
|
logln("Testing weird arguments");
|
|
|
|
col->getCollationKey(NULL, 0, sortk1, key1Status);
|
|
|
|
// key gets reset here
|
|
|
|
int32_t length;
|
|
|
|
sortk1.getByteArray(length);
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert(sortk1.isBogus() == FALSE && length == 0,
|
2001-06-26 17:44:23 +00:00
|
|
|
"Empty string should return an empty collation key");
|
|
|
|
// bogus key returned here
|
|
|
|
key1Status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
col->getCollationKey(NULL, 0, sortk1, key1Status);
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert(sortk1.getByteArray(length) == NULL && length == 0,
|
2001-06-26 17:44:23 +00:00
|
|
|
"Error code should return bogus collation key");
|
|
|
|
|
|
|
|
key1Status = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
logln("Use tertiary comparison level testing ....");
|
|
|
|
|
2001-06-26 17:44:23 +00:00
|
|
|
col->getCollationKey(test1, sortk1, key1Status);
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert((sortk1.compareTo(col->getCollationKey(test2, sortk2, key2Status)))
|
2003-03-19 13:21:51 +00:00
|
|
|
== Collator::GREATER,
|
1999-08-16 21:50:52 +00:00
|
|
|
"Result should be \"Abcda\" >>> \"abcda\"");
|
|
|
|
|
|
|
|
CollationKey sortk3(sortk2), sortkNew, sortkEmpty;
|
|
|
|
|
2002-08-12 20:12:25 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
sortkNew = sortk1;
|
|
|
|
doAssert((sortk1 != sortk2), "The sort keys should be different");
|
|
|
|
doAssert((sortk1.hashCode() != sortk2.hashCode()), "sort key hashCode() failed");
|
|
|
|
doAssert((sortk2 == sortk3), "The sort keys should be the same");
|
|
|
|
doAssert((sortk1 == sortkNew), "The sort keys assignment failed");
|
|
|
|
doAssert((sortk1.hashCode() == sortkNew.hashCode()), "sort key hashCode() failed");
|
|
|
|
doAssert((sortkNew != sortk3), "The sort keys should be different");
|
2003-03-19 13:21:51 +00:00
|
|
|
doAssert(sortk1.compareTo(sortk3) == Collator::GREATER, "Result should be \"Abcda\" >>> \"abcda\"");
|
|
|
|
doAssert(sortk2.compareTo(sortk3) == Collator::EQUAL, "Result should be \"abcda\" == \"abcda\"");
|
|
|
|
doAssert(sortkEmpty.compareTo(sortk1) == Collator::LESS, "Result should be (empty key) <<< \"Abcda\"");
|
|
|
|
doAssert(sortk1.compareTo(sortkEmpty) == Collator::GREATER, "Result should be \"Abcda\" >>> (empty key)");
|
|
|
|
doAssert(sortkEmpty.compareTo(sortkEmpty) == Collator::EQUAL, "Result should be (empty key) == (empty key)");
|
2003-06-09 04:57:01 +00:00
|
|
|
doAssert(sortk1.compareTo(sortk3, success) == UCOL_GREATER, "Result should be \"Abcda\" >>> \"abcda\"");
|
|
|
|
doAssert(sortk2.compareTo(sortk3, success) == UCOL_EQUAL, "Result should be \"abcda\" == \"abcda\"");
|
|
|
|
doAssert(sortkEmpty.compareTo(sortk1, success) == UCOL_LESS, "Result should be (empty key) <<< \"Abcda\"");
|
|
|
|
doAssert(sortk1.compareTo(sortkEmpty, success) == UCOL_GREATER, "Result should be \"Abcda\" >>> (empty key)");
|
|
|
|
doAssert(sortkEmpty.compareTo(sortkEmpty, success) == UCOL_EQUAL, "Result should be (empty key) == (empty key)");
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
int32_t cnt1, cnt2, cnt3, cnt4;
|
|
|
|
|
2003-05-01 01:31:28 +00:00
|
|
|
const uint8_t* byteArray1 = sortk1.getByteArray(cnt1);
|
|
|
|
const uint8_t* byteArray2 = sortk2.getByteArray(cnt2);
|
2002-08-12 20:12:25 +00:00
|
|
|
|
|
|
|
/*
|
2001-04-20 22:27:24 +00:00
|
|
|
this is a bad test since it is dependent on the version of uca data,
|
|
|
|
which changes
|
|
|
|
will remove it.
|
2002-08-12 20:12:25 +00:00
|
|
|
const char sortk2_compat[] = {
|
2001-04-20 22:27:24 +00:00
|
|
|
// this is a 1.8 sortkey
|
2001-03-23 00:57:44 +00:00
|
|
|
0x17, 0x19, 0x1B, 0x1D, 0x17, 0x01, 0x08, 0x01, 0x08, 0x00
|
|
|
|
};
|
2001-04-20 22:27:24 +00:00
|
|
|
*/
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
const uint8_t* byteArray3 = 0;
|
|
|
|
byteArray3 = sortk1.getByteArray(cnt3);
|
|
|
|
|
|
|
|
const uint8_t* byteArray4 = 0;
|
|
|
|
byteArray4 = sortk2.getByteArray(cnt4);
|
|
|
|
|
|
|
|
CollationKey sortk4(byteArray1, cnt1), sortk5(byteArray2, cnt2);
|
|
|
|
CollationKey sortk6(byteArray3, cnt3), sortk7(byteArray4, cnt4);
|
|
|
|
|
2001-04-20 22:27:24 +00:00
|
|
|
/*
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert(memcmp(byteArray2, sortk2_compat, strlen(sortk2_compat)) == 0,
|
2001-03-23 00:57:44 +00:00
|
|
|
"Binary format for 'abcda' sortkey different!");
|
2001-04-20 22:27:24 +00:00
|
|
|
*/
|
2003-03-19 13:21:51 +00:00
|
|
|
doAssert(sortk1.compareTo(sortk4) == Collator::EQUAL, "CollationKey::toByteArray(sortk1) Failed.");
|
|
|
|
doAssert(sortk2.compareTo(sortk5) == Collator::EQUAL, "CollationKey::toByteArray(sortk2) Failed.");
|
|
|
|
doAssert(sortk4.compareTo(sortk5) == Collator::GREATER, "sortk4 >>> sortk5 Failed");
|
|
|
|
doAssert(sortk1.compareTo(sortk6) == Collator::EQUAL, "CollationKey::getByteArray(sortk1) Failed.");
|
|
|
|
doAssert(sortk2.compareTo(sortk7) == Collator::EQUAL, "CollationKey::getByteArray(sortk2) Failed.");
|
|
|
|
doAssert(sortk6.compareTo(sortk7) == Collator::GREATER, "sortk6 >>> sortk7 Failed");
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
logln("Equality tests : ");
|
|
|
|
doAssert(sortk1 == sortk4, "sortk1 == sortk4 Failed.");
|
|
|
|
doAssert(sortk2 == sortk5, "sortk2 == sortk5 Failed.");
|
|
|
|
doAssert(sortk1 != sortk5, "sortk1 != sortk5 Failed.");
|
|
|
|
doAssert(sortk1 == sortk6, "sortk1 == sortk6 Failed.");
|
|
|
|
doAssert(sortk2 == sortk7, "sortk2 == sortk7 Failed.");
|
|
|
|
doAssert(sortk1 != sortk7, "sortk1 != sortk7 Failed.");
|
|
|
|
|
2002-03-25 22:56:46 +00:00
|
|
|
byteArray1 = 0;
|
|
|
|
byteArray2 = 0;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
sortk3 = sortk1;
|
|
|
|
doAssert(sortk1 == sortk3, "sortk1 = sortk3 assignment Failed.");
|
|
|
|
doAssert(sortk2 != sortk3, "sortk2 != sortk3 Failed.");
|
|
|
|
logln("testing sortkey ends...");
|
2001-03-23 00:57:44 +00:00
|
|
|
|
2001-03-23 02:11:22 +00:00
|
|
|
col->setStrength(Collator::SECONDARY);
|
2001-03-23 00:57:44 +00:00
|
|
|
doAssert(col->getCollationKey(test1, sortk1, key1Status).compareTo(
|
2002-08-12 20:12:25 +00:00
|
|
|
col->getCollationKey(test2, sortk2, key2Status))
|
2003-03-19 13:21:51 +00:00
|
|
|
== Collator::EQUAL,
|
2001-03-23 00:57:44 +00:00
|
|
|
"Result should be \"Abcda\" == \"abcda\"");
|
1999-08-16 21:50:52 +00:00
|
|
|
delete col;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// Tests the CollatorElementIterator class.
|
|
|
|
// ctor, RuleBasedCollator::createCollationElementIterator(), operator==, operator!=
|
2002-08-12 20:12:25 +00:00
|
|
|
//
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
2000-08-23 19:11:16 +00:00
|
|
|
CollationAPITest::TestElemIter(/* char* par */)
|
2002-08-12 20:12:25 +00:00
|
|
|
{
|
1999-08-16 21:50:52 +00:00
|
|
|
logln("testing sortkey begins...");
|
|
|
|
Collator *col = 0;
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode success = U_ZERO_ERROR;
|
2002-08-21 00:16:30 +00:00
|
|
|
col = Collator::createInstance(Locale::getEnglish(), success);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(success))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
errln("Default collation creation failed.");
|
|
|
|
return;
|
|
|
|
}
|
2002-08-12 20:12:25 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString testString1("XFILE What subset of all possible test cases has the highest probability of detecting the most errors?");
|
2001-03-21 22:13:30 +00:00
|
|
|
UnicodeString testString2("Xf_ile What subset of all possible test cases has the lowest probability of detecting the least errors?");
|
1999-08-16 21:50:52 +00:00
|
|
|
logln("Constructors and comparison testing....");
|
|
|
|
CollationElementIterator *iterator1 = ((RuleBasedCollator*)col)->createCollationElementIterator(testString1);
|
2002-08-12 20:12:25 +00:00
|
|
|
|
2000-06-29 23:28:46 +00:00
|
|
|
CharacterIterator *chariter=new StringCharacterIterator(testString1);
|
|
|
|
CollationElementIterator *coliter=((RuleBasedCollator*)col)->createCollationElementIterator(*chariter);
|
2002-08-12 20:12:25 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// copy ctor
|
2001-03-23 00:57:44 +00:00
|
|
|
CollationElementIterator *iterator2 = ((RuleBasedCollator*)col)->createCollationElementIterator(testString1);
|
1999-08-16 21:50:52 +00:00
|
|
|
CollationElementIterator *iterator3 = ((RuleBasedCollator*)col)->createCollationElementIterator(testString2);
|
2001-03-23 00:57:44 +00:00
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t offset = iterator1->getOffset();
|
2001-03-28 01:39:36 +00:00
|
|
|
if (offset != 0) {
|
|
|
|
errln("Error in getOffset for collation element iterator\n");
|
|
|
|
return;
|
|
|
|
}
|
2001-03-23 00:57:44 +00:00
|
|
|
iterator1->setOffset(6, success);
|
|
|
|
if (U_FAILURE(success)) {
|
|
|
|
errln("Error in setOffset for collation element iterator\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
iterator1->setOffset(0, success);
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t order1, order2, order3;
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert((*iterator1 == *iterator2), "The two iterators should be the same");
|
1999-08-16 21:50:52 +00:00
|
|
|
doAssert((*iterator1 != *iterator3), "The two iterators should be different");
|
2002-08-12 20:12:25 +00:00
|
|
|
|
2000-06-29 23:28:46 +00:00
|
|
|
doAssert((*coliter == *iterator1), "The two iterators should be the same");
|
|
|
|
doAssert((*coliter == *iterator2), "The two iterators should be the same");
|
|
|
|
doAssert((*coliter != *iterator3), "The two iterators should be different");
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
order1 = iterator1->next(success);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(success))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
errln("Somehow ran out of memory stepping through the iterator.");
|
|
|
|
return;
|
|
|
|
}
|
2002-08-12 20:12:25 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
doAssert((*iterator1 != *iterator2), "The first iterator advance failed");
|
2001-03-23 00:57:44 +00:00
|
|
|
order2 = iterator2->getOffset();
|
|
|
|
doAssert((order1 != order2), "The order result should not be the same");
|
1999-08-16 21:50:52 +00:00
|
|
|
order2 = iterator2->next(success);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(success))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
errln("Somehow ran out of memory stepping through the iterator.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert((*iterator1 == *iterator2), "The second iterator advance failed");
|
1999-08-16 21:50:52 +00:00
|
|
|
doAssert((order1 == order2), "The order result should be the same");
|
|
|
|
order3 = iterator3->next(success);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(success))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
errln("Somehow ran out of memory stepping through the iterator.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert((CollationElementIterator::primaryOrder(order1) ==
|
1999-08-16 21:50:52 +00:00
|
|
|
CollationElementIterator::primaryOrder(order3)), "The primary orders should be the same");
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert((CollationElementIterator::secondaryOrder(order1) ==
|
1999-08-16 21:50:52 +00:00
|
|
|
CollationElementIterator::secondaryOrder(order3)), "The secondary orders should be the same");
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert((CollationElementIterator::tertiaryOrder(order1) ==
|
1999-08-16 21:50:52 +00:00
|
|
|
CollationElementIterator::tertiaryOrder(order3)), "The tertiary orders should be the same");
|
|
|
|
|
|
|
|
order1 = iterator1->next(success); order3 = iterator3->next(success);
|
2002-08-12 20:12:25 +00:00
|
|
|
if (U_FAILURE(success))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
errln("Somehow ran out of memory stepping through the iterator.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert((CollationElementIterator::primaryOrder(order1) ==
|
1999-08-16 21:50:52 +00:00
|
|
|
CollationElementIterator::primaryOrder(order3)), "The primary orders should be identical");
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert((CollationElementIterator::tertiaryOrder(order1) !=
|
1999-08-16 21:50:52 +00:00
|
|
|
CollationElementIterator::tertiaryOrder(order3)), "The tertiary orders should be different");
|
|
|
|
|
2002-08-12 20:12:25 +00:00
|
|
|
order1 = iterator1->next(success);
|
2001-03-15 02:53:05 +00:00
|
|
|
order3 = iterator3->next(success);
|
2002-06-13 18:35:05 +00:00
|
|
|
/* NO! Secondary orders of two CEs are not related, especially in the case of '_' vs 'I' */
|
|
|
|
/*
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert((CollationElementIterator::secondaryOrder(order1) !=
|
2001-03-21 22:13:30 +00:00
|
|
|
CollationElementIterator::secondaryOrder(order3)), "The secondary orders should not be the same");
|
2002-06-13 18:35:05 +00:00
|
|
|
*/
|
1999-08-16 21:50:52 +00:00
|
|
|
doAssert((order1 != CollationElementIterator::NULLORDER), "Unexpected end of iterator reached");
|
|
|
|
|
|
|
|
iterator1->reset(); iterator2->reset(); iterator3->reset();
|
|
|
|
order1 = iterator1->next(success);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(success))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
errln("Somehow ran out of memory stepping through the iterator.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
doAssert((*iterator1 != *iterator2), "The first iterator advance failed");
|
|
|
|
|
|
|
|
order2 = iterator2->next(success);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(success))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
errln("Somehow ran out of memory stepping through the iterator.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
doAssert((*iterator1 == *iterator2), "The second iterator advance failed");
|
|
|
|
doAssert((order1 == order2), "The order result should be the same");
|
|
|
|
|
|
|
|
order3 = iterator3->next(success);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(success))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
errln("Somehow ran out of memory stepping through the iterator.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert((CollationElementIterator::primaryOrder(order1) ==
|
1999-08-16 21:50:52 +00:00
|
|
|
CollationElementIterator::primaryOrder(order3)), "The primary orders should be the same");
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert((CollationElementIterator::secondaryOrder(order1) ==
|
1999-08-16 21:50:52 +00:00
|
|
|
CollationElementIterator::secondaryOrder(order3)), "The secondary orders should be the same");
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert((CollationElementIterator::tertiaryOrder(order1) ==
|
1999-08-16 21:50:52 +00:00
|
|
|
CollationElementIterator::tertiaryOrder(order3)), "The tertiary orders should be the same");
|
|
|
|
|
|
|
|
order1 = iterator1->next(success); order2 = iterator2->next(success); order3 = iterator3->next(success);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(success))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
errln("Somehow ran out of memory stepping through the iterator.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert((CollationElementIterator::primaryOrder(order1) ==
|
1999-08-16 21:50:52 +00:00
|
|
|
CollationElementIterator::primaryOrder(order3)), "The primary orders should be identical");
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert((CollationElementIterator::tertiaryOrder(order1) !=
|
1999-08-16 21:50:52 +00:00
|
|
|
CollationElementIterator::tertiaryOrder(order3)), "The tertiary orders should be different");
|
|
|
|
|
|
|
|
order1 = iterator1->next(success); order3 = iterator3->next(success);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(success))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
errln("Somehow ran out of memory stepping through the iterator.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-06-13 18:35:05 +00:00
|
|
|
/* NO! Secondary orders of two CEs are not related, especially in the case of '_' vs 'I' */
|
|
|
|
/*
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert((CollationElementIterator::secondaryOrder(order1) !=
|
2001-03-21 22:13:30 +00:00
|
|
|
CollationElementIterator::secondaryOrder(order3)), "The secondary orders should not be the same");
|
2002-06-13 18:35:05 +00:00
|
|
|
*/
|
1999-08-16 21:50:52 +00:00
|
|
|
doAssert((order1 != CollationElementIterator::NULLORDER), "Unexpected end of iterator reached");
|
|
|
|
doAssert((*iterator2 != *iterator3), "The iterators should be different");
|
|
|
|
|
2002-08-12 20:12:25 +00:00
|
|
|
|
2000-06-29 23:28:46 +00:00
|
|
|
//test error values
|
|
|
|
success=U_UNSUPPORTED_ERROR;
|
|
|
|
Collator *colerror=NULL;
|
2002-08-21 00:16:30 +00:00
|
|
|
colerror=Collator::createInstance(Locale::getEnglish(), success);
|
2000-06-29 23:28:46 +00:00
|
|
|
if (colerror != 0 || success == U_ZERO_ERROR){
|
|
|
|
errln("Error: createInstance(UErrorCode != U_ZERO_ERROR) should just return and not create an instance\n");
|
|
|
|
}
|
|
|
|
int32_t position=coliter->previous(success);
|
|
|
|
if(position != CollationElementIterator::NULLORDER){
|
|
|
|
errln((UnicodeString)"Expected NULLORDER got" + position);
|
|
|
|
}
|
|
|
|
coliter->reset();
|
|
|
|
coliter->setText(*chariter, success);
|
|
|
|
if(!U_FAILURE(success)){
|
|
|
|
errln("Expeceted error");
|
|
|
|
}
|
|
|
|
iterator1->setText((UnicodeString)"hello there", success);
|
|
|
|
if(!U_FAILURE(success)){
|
|
|
|
errln("Expeceted error");
|
|
|
|
}
|
2002-08-12 20:12:25 +00:00
|
|
|
|
2000-06-29 23:28:46 +00:00
|
|
|
delete chariter;
|
|
|
|
delete coliter;
|
1999-08-16 21:50:52 +00:00
|
|
|
delete iterator1;
|
|
|
|
delete iterator2;
|
|
|
|
delete iterator3;
|
|
|
|
delete col;
|
|
|
|
|
2002-08-12 20:12:25 +00:00
|
|
|
|
2000-06-29 23:28:46 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
logln("testing CollationElementIterator ends...");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test RuleBasedCollator ctor, dtor, operator==, operator!=, clone, copy, and getRules
|
|
|
|
void
|
2000-08-23 19:11:16 +00:00
|
|
|
CollationAPITest::TestOperators(/* char* par */)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode success = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString ruleset1("< a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E");
|
|
|
|
UnicodeString ruleset2("< a, A < b, B < c, C < d, D, e, E");
|
|
|
|
RuleBasedCollator *col1 = new RuleBasedCollator(ruleset1, success);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(success)) {
|
1999-08-16 21:50:52 +00:00
|
|
|
errln("RuleBasedCollator creation failed.");
|
|
|
|
return;
|
|
|
|
}
|
1999-10-07 00:07:53 +00:00
|
|
|
success = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
RuleBasedCollator *col2 = new RuleBasedCollator(ruleset2, success);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(success)) {
|
1999-08-16 21:50:52 +00:00
|
|
|
errln("The RuleBasedCollator constructor failed when building with the 2nd rule set.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
logln("The operator tests begin : ");
|
|
|
|
logln("testing operator==, operator!=, clone methods ...");
|
|
|
|
doAssert((*col1 != *col2), "The two different table collations compared equal");
|
|
|
|
*col1 = *col2;
|
|
|
|
doAssert((*col1 == *col2), "Collator objects not equal after assignment (operator=)");
|
2002-08-12 20:12:25 +00:00
|
|
|
|
1999-10-07 00:07:53 +00:00
|
|
|
success = U_ZERO_ERROR;
|
2002-08-21 00:16:30 +00:00
|
|
|
Collator *col3 = Collator::createInstance(Locale::getEnglish(), success);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(success)) {
|
1999-08-16 21:50:52 +00:00
|
|
|
errln("Default collation creation failed.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
doAssert((*col1 != *col3), "The two different table collations compared equal");
|
|
|
|
Collator* col4 = col1->clone();
|
|
|
|
Collator* col5 = col3->clone();
|
|
|
|
doAssert((*col1 == *col4), "Cloned collation objects not equal");
|
|
|
|
doAssert((*col3 != *col4), "Two different table collations compared equal");
|
|
|
|
doAssert((*col3 == *col5), "Cloned collation objects not equal");
|
|
|
|
doAssert((*col4 != *col5), "Two cloned collations compared equal");
|
|
|
|
|
|
|
|
const UnicodeString& defRules = ((RuleBasedCollator*)col3)->getRules();
|
|
|
|
RuleBasedCollator* col6 = new RuleBasedCollator(defRules, success);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(success)) {
|
1999-08-16 21:50:52 +00:00
|
|
|
errln("Creating default collation with rules failed.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
doAssert((((RuleBasedCollator*)col3)->getRules() == col6->getRules()), "Default collator getRules failed");
|
|
|
|
|
2000-06-29 23:28:46 +00:00
|
|
|
success = U_ZERO_ERROR;
|
|
|
|
RuleBasedCollator *col7 = new RuleBasedCollator(ruleset2, Collator::TERTIARY, success);
|
|
|
|
if (U_FAILURE(success)) {
|
|
|
|
errln("The RuleBasedCollator constructor failed when building with the 2nd rule set with tertiary strength.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
success = U_ZERO_ERROR;
|
2002-08-21 19:09:33 +00:00
|
|
|
RuleBasedCollator *col8 = new RuleBasedCollator(ruleset2, UCOL_OFF, success);
|
2000-06-29 23:28:46 +00:00
|
|
|
if (U_FAILURE(success)) {
|
|
|
|
errln("The RuleBasedCollator constructor failed when building with the 2nd rule set with Normalizer::NO_OP.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
success = U_ZERO_ERROR;
|
2002-08-21 19:09:33 +00:00
|
|
|
RuleBasedCollator *col9 = new RuleBasedCollator(ruleset2, Collator::PRIMARY, UCOL_ON, success);
|
2000-06-29 23:28:46 +00:00
|
|
|
if (U_FAILURE(success)) {
|
|
|
|
errln("The RuleBasedCollator constructor failed when building with the 2nd rule set with tertiary strength and Normalizer::NO_OP.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// doAssert((*col7 == *col8), "The two equal table collations compared different");
|
|
|
|
doAssert((*col7 != *col9), "The two different table collations compared equal");
|
|
|
|
doAssert((*col8 != *col9), "The two different table collations compared equal");
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
logln("operator tests ended.");
|
|
|
|
delete col1;
|
|
|
|
delete col2;
|
|
|
|
delete col3;
|
|
|
|
delete col4;
|
|
|
|
delete col5;
|
|
|
|
delete col6;
|
2000-06-29 23:28:46 +00:00
|
|
|
delete col7;
|
|
|
|
delete col8;
|
|
|
|
delete col9;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// test clone and copy
|
|
|
|
void
|
2000-08-23 19:11:16 +00:00
|
|
|
CollationAPITest::TestDuplicate(/* char* par */)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2002-08-21 00:16:30 +00:00
|
|
|
Collator *col1 = Collator::createInstance(Locale::getEnglish(), status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(status)) {
|
1999-08-16 21:50:52 +00:00
|
|
|
logln("Default collator creation failed.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Collator *col2 = col1->clone();
|
|
|
|
doAssert((*col1 == *col2), "Cloned object is not equal to the orginal");
|
2006-04-02 19:07:50 +00:00
|
|
|
UnicodeString *ruleset = new UnicodeString("< a, A < b, B < c, C < d, D, e, E");
|
|
|
|
RuleBasedCollator *col3 = new RuleBasedCollator(*ruleset, status);
|
1999-08-16 21:50:52 +00:00
|
|
|
doAssert((*col1 != *col3), "Cloned object is equal to some dummy");
|
|
|
|
*col3 = *((RuleBasedCollator*)col1);
|
|
|
|
doAssert((*col1 == *col3), "Copied object is not equal to the orginal");
|
2006-04-02 19:07:50 +00:00
|
|
|
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
logln("Collation tailoring failed.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UCollationResult res;
|
|
|
|
UnicodeString first((UChar)0x0061);
|
|
|
|
UnicodeString second((UChar)0x0062);
|
|
|
|
UnicodeString copiedEnglishRules(((RuleBasedCollator*)col1)->getRules());
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
delete col1;
|
2006-04-02 19:07:50 +00:00
|
|
|
delete ruleset;
|
|
|
|
|
|
|
|
// Try using the cloned collators after deleting the original data
|
|
|
|
res = col2->compare(first, second, status);
|
|
|
|
if(res != UCOL_LESS) {
|
|
|
|
errln("a should be less then b after tailoring");
|
|
|
|
}
|
|
|
|
if (((RuleBasedCollator*)col2)->getRules() != copiedEnglishRules) {
|
|
|
|
errln(UnicodeString("English rule difference. ")
|
|
|
|
+ copiedEnglishRules + UnicodeString("\ngetRules=") + ((RuleBasedCollator*)col2)->getRules());
|
|
|
|
}
|
|
|
|
res = col3->compare(first, second, status);
|
|
|
|
if(res != UCOL_LESS) {
|
|
|
|
errln("a should be less then b after tailoring");
|
|
|
|
}
|
|
|
|
if (col3->getRules() != copiedEnglishRules) {
|
|
|
|
errln(UnicodeString("English rule difference. ")
|
|
|
|
+ copiedEnglishRules + UnicodeString("\ngetRules=") + col3->getRules());
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
delete col2;
|
|
|
|
delete col3;
|
2002-08-12 20:12:25 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
void
|
2000-08-23 19:11:16 +00:00
|
|
|
CollationAPITest::TestCompare(/* char* par */)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
logln("The compare tests begin : ");
|
|
|
|
Collator *col = 0;
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode success = U_ZERO_ERROR;
|
2002-08-21 00:16:30 +00:00
|
|
|
col = Collator::createInstance(Locale::getEnglish(), success);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(success)) {
|
1999-08-16 21:50:52 +00:00
|
|
|
errln("Default collation creation failed.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
UnicodeString test1("Abcda"), test2("abcda");
|
|
|
|
logln("Use tertiary comparison level testing ....");
|
2002-08-12 20:12:25 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
doAssert((!col->equals(test1, test2) ), "Result should be \"Abcda\" != \"abcda\"");
|
|
|
|
doAssert((col->greater(test1, test2) ), "Result should be \"Abcda\" >>> \"abcda\"");
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert((col->greaterOrEqual(test1, test2) ), "Result should be \"Abcda\" >>> \"abcda\"");
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
col->setStrength(Collator::SECONDARY);
|
|
|
|
logln("Use secondary comparison level testing ....");
|
2002-08-12 20:12:25 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
doAssert((col->equals(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
|
|
|
|
doAssert((!col->greater(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert((col->greaterOrEqual(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
col->setStrength(Collator::PRIMARY);
|
|
|
|
logln("Use primary comparison level testing ....");
|
2002-08-12 20:12:25 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
doAssert((col->equals(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
|
|
|
|
doAssert((!col->greater(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert((col->greaterOrEqual(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");
|
2003-05-16 19:52:44 +00:00
|
|
|
|
|
|
|
// Test different APIs
|
|
|
|
const UChar* t1 = test1.getBuffer();
|
|
|
|
int32_t t1Len = test1.length();
|
|
|
|
const UChar* t2 = test2.getBuffer();
|
|
|
|
int32_t t2Len = test2.length();
|
|
|
|
|
|
|
|
doAssert((col->compare(test1, test2) == Collator::EQUAL), "Problem");
|
|
|
|
doAssert((col->compare(test1, test2, success) == UCOL_EQUAL), "Problem");
|
|
|
|
doAssert((col->compare(t1, t1Len, t2, t2Len) == Collator::EQUAL), "Problem");
|
|
|
|
doAssert((col->compare(t1, t1Len, t2, t2Len, success) == UCOL_EQUAL), "Problem");
|
|
|
|
doAssert((col->compare(test1, test2, t1Len) == Collator::EQUAL), "Problem");
|
|
|
|
doAssert((col->compare(test1, test2, t1Len, success) == UCOL_EQUAL), "Problem");
|
|
|
|
|
|
|
|
col->setAttribute(UCOL_STRENGTH, UCOL_TERTIARY, success);
|
|
|
|
doAssert((col->compare(test1, test2) == Collator::GREATER), "Problem");
|
|
|
|
doAssert((col->compare(test1, test2, success) == UCOL_GREATER), "Problem");
|
|
|
|
doAssert((col->compare(t1, t1Len, t2, t2Len) == Collator::GREATER), "Problem");
|
|
|
|
doAssert((col->compare(t1, t1Len, t2, t2Len, success) == UCOL_GREATER), "Problem");
|
|
|
|
doAssert((col->compare(test1, test2, t1Len) == Collator::GREATER), "Problem");
|
|
|
|
doAssert((col->compare(test1, test2, t1Len, success) == UCOL_GREATER), "Problem");
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
logln("The compare tests end.");
|
|
|
|
delete col;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-08-23 19:11:16 +00:00
|
|
|
CollationAPITest::TestGetAll(/* char* par */)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
int32_t count;
|
|
|
|
const Locale* list = Collator::getAvailableLocales(count);
|
|
|
|
for (int32_t i = 0; i < count; ++i) {
|
|
|
|
UnicodeString locName, dispName;
|
2002-08-12 20:12:25 +00:00
|
|
|
log("Locale name: ");
|
2000-04-15 21:28:17 +00:00
|
|
|
log(list[i].getName());
|
1999-08-16 21:50:52 +00:00
|
|
|
log(" , the display name is : ");
|
|
|
|
logln(list[i].getDisplayName(dispName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-26 17:44:23 +00:00
|
|
|
void CollationAPITest::TestSortKey()
|
|
|
|
{
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2002-08-12 20:12:25 +00:00
|
|
|
/*
|
2001-06-26 17:44:23 +00:00
|
|
|
this is supposed to open default date format, but later on it treats
|
2002-08-12 20:12:25 +00:00
|
|
|
it like it is "en_US"
|
2001-06-26 17:44:23 +00:00
|
|
|
- very bad if you try to run the tests on machine where default
|
2002-08-12 20:12:25 +00:00
|
|
|
locale is NOT "en_US"
|
2001-06-26 17:44:23 +00:00
|
|
|
*/
|
2002-08-21 00:16:30 +00:00
|
|
|
Collator *col = Collator::createInstance(Locale::getEnglish(), status);
|
2001-06-26 17:44:23 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
errln("ERROR: Default collation creation failed.: %s\n", u_errorName(status));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (col->getStrength() != Collator::TERTIARY)
|
|
|
|
{
|
|
|
|
errln("ERROR: default collation did not have UCOL_DEFAULT_STRENGTH !\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Need to use identical strength */
|
|
|
|
col->setAttribute(UCOL_STRENGTH, UCOL_IDENTICAL, status);
|
|
|
|
|
2002-08-12 20:12:25 +00:00
|
|
|
uint8_t key2compat[] = {
|
2006-06-16 05:46:38 +00:00
|
|
|
/* 3.6 key, from UCA 5.0 */
|
|
|
|
0x29, 0x2b, 0x2d, 0x2f, 0x29, 0x01,
|
|
|
|
0x09, 0x01, 0x09, 0x01, 0x28, 0x01,
|
|
|
|
0x92, 0x93, 0x94, 0x95, 0x92, 0x00
|
|
|
|
|
2005-05-19 06:46:30 +00:00
|
|
|
/* 3.4 key, from UCA 4.1 */
|
2006-06-16 05:46:38 +00:00
|
|
|
/*
|
2005-05-19 06:46:30 +00:00
|
|
|
0x28, 0x2a, 0x2c, 0x2e, 0x28, 0x01,
|
|
|
|
0x09, 0x01, 0x09, 0x01, 0x27, 0x01,
|
|
|
|
0x92, 0x93, 0x94, 0x95, 0x92, 0x00
|
2006-06-16 05:46:38 +00:00
|
|
|
*/
|
2005-05-19 06:46:30 +00:00
|
|
|
/* 2.6.1 key */
|
|
|
|
/*
|
2003-08-23 01:46:02 +00:00
|
|
|
0x26, 0x28, 0x2A, 0x2C, 0x26, 0x01,
|
|
|
|
0x09, 0x01, 0x09, 0x01, 0x25, 0x01,
|
|
|
|
0x92, 0x93, 0x94, 0x95, 0x92, 0x00
|
2005-05-19 06:46:30 +00:00
|
|
|
*/
|
2002-06-13 18:35:05 +00:00
|
|
|
/* 2.2 key */
|
2003-08-23 01:46:02 +00:00
|
|
|
/*
|
2002-08-12 20:12:25 +00:00
|
|
|
0x1D, 0x1F, 0x21, 0x23, 0x1D, 0x01,
|
|
|
|
0x09, 0x01, 0x09, 0x01, 0x1C, 0x01,
|
2002-06-13 18:35:05 +00:00
|
|
|
0x92, 0x93, 0x94, 0x95, 0x92, 0x00
|
2003-08-23 01:46:02 +00:00
|
|
|
*/
|
2001-10-01 02:55:57 +00:00
|
|
|
/* 2.0 key */
|
2002-06-13 18:35:05 +00:00
|
|
|
/*
|
2002-08-12 20:12:25 +00:00
|
|
|
0x19, 0x1B, 0x1D, 0x1F, 0x19,
|
2001-10-02 16:51:27 +00:00
|
|
|
0x01, 0x09, 0x01, 0x09, 0x01,
|
2002-08-12 20:12:25 +00:00
|
|
|
0x18, 0x01,
|
|
|
|
0x92, 0x93, 0x94, 0x95, 0x92,
|
2001-10-01 02:55:57 +00:00
|
|
|
0x00
|
2002-06-13 18:35:05 +00:00
|
|
|
*/
|
2001-06-26 17:44:23 +00:00
|
|
|
/* 1.8.1 key.*/
|
2001-10-01 02:55:57 +00:00
|
|
|
/*
|
2002-08-12 20:12:25 +00:00
|
|
|
0x19, 0x1B, 0x1D, 0x1F, 0x19,
|
|
|
|
0x01, 0x0A, 0x01, 0x0A, 0x01,
|
|
|
|
0x92, 0x93, 0x94, 0x95, 0x92,
|
2001-06-26 17:44:23 +00:00
|
|
|
0x00
|
2001-10-01 02:55:57 +00:00
|
|
|
*/
|
2001-06-26 17:44:23 +00:00
|
|
|
};
|
2002-08-12 20:12:25 +00:00
|
|
|
|
|
|
|
UChar test1[6] = {0x41, 0x62, 0x63, 0x64, 0x61, 0},
|
|
|
|
test2[6] = {0x61, 0x62, 0x63, 0x64, 0x61, 0},
|
2001-06-26 17:44:23 +00:00
|
|
|
test3[6] = {0x61, 0x62, 0x63, 0x64, 0x61, 0};
|
|
|
|
|
|
|
|
uint8_t sortkey1[64];
|
|
|
|
uint8_t sortkey2[64];
|
|
|
|
uint8_t sortkey3[64];
|
|
|
|
|
|
|
|
logln("Use tertiary comparison level testing ....\n");
|
|
|
|
|
|
|
|
CollationKey key1;
|
|
|
|
col->getCollationKey(test1, u_strlen(test1), key1, status);
|
|
|
|
|
|
|
|
CollationKey key2;
|
|
|
|
col->getCollationKey(test2, u_strlen(test2), key2, status);
|
|
|
|
|
|
|
|
CollationKey key3;
|
|
|
|
col->getCollationKey(test3, u_strlen(test3), key3, status);
|
|
|
|
|
2003-03-19 13:21:51 +00:00
|
|
|
doAssert(key1.compareTo(key2) == Collator::GREATER,
|
2001-06-26 17:44:23 +00:00
|
|
|
"Result should be \"Abcda\" > \"abcda\"");
|
2003-03-19 13:21:51 +00:00
|
|
|
doAssert(key2.compareTo(key1) == Collator::LESS,
|
2001-06-26 17:44:23 +00:00
|
|
|
"Result should be \"abcda\" < \"Abcda\"");
|
2003-03-19 13:21:51 +00:00
|
|
|
doAssert(key2.compareTo(key3) == Collator::EQUAL,
|
2001-06-26 17:44:23 +00:00
|
|
|
"Result should be \"abcda\" == \"abcda\"");
|
|
|
|
|
|
|
|
int32_t keylength = 0;
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert(strcmp((const char *)(key2.getByteArray(keylength)),
|
|
|
|
(const char *)key2compat) == 0,
|
2001-06-26 17:44:23 +00:00
|
|
|
"Binary format for 'abcda' sortkey different!");
|
|
|
|
|
|
|
|
col->getSortKey(test1, sortkey1, 64);
|
|
|
|
col->getSortKey(test2, sortkey2, 64);
|
|
|
|
col->getSortKey(test3, sortkey3, 64);
|
|
|
|
|
|
|
|
const uint8_t *tempkey = key1.getByteArray(keylength);
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert(memcmp(tempkey, sortkey1, keylength) == 0,
|
2001-06-26 17:44:23 +00:00
|
|
|
"Test1 string should have the same collation key and sort key");
|
|
|
|
tempkey = key2.getByteArray(keylength);
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert(memcmp(tempkey, sortkey2, keylength) == 0,
|
2001-06-26 17:44:23 +00:00
|
|
|
"Test2 string should have the same collation key and sort key");
|
|
|
|
tempkey = key3.getByteArray(keylength);
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert(memcmp(tempkey, sortkey3, keylength) == 0,
|
2001-06-26 17:44:23 +00:00
|
|
|
"Test3 string should have the same collation key and sort key");
|
|
|
|
|
|
|
|
col->getSortKey(test1, 5, sortkey1, 64);
|
|
|
|
col->getSortKey(test2, 5, sortkey2, 64);
|
|
|
|
col->getSortKey(test3, 5, sortkey3, 64);
|
|
|
|
|
|
|
|
tempkey = key1.getByteArray(keylength);
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert(memcmp(tempkey, sortkey1, keylength) == 0,
|
2001-06-26 17:44:23 +00:00
|
|
|
"Test1 string should have the same collation key and sort key");
|
|
|
|
tempkey = key2.getByteArray(keylength);
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert(memcmp(tempkey, sortkey2, keylength) == 0,
|
2001-06-26 17:44:23 +00:00
|
|
|
"Test2 string should have the same collation key and sort key");
|
|
|
|
tempkey = key3.getByteArray(keylength);
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert(memcmp(tempkey, sortkey3, keylength) == 0,
|
2001-06-26 17:44:23 +00:00
|
|
|
"Test3 string should have the same collation key and sort key");
|
|
|
|
|
|
|
|
UnicodeString strtest1(test1);
|
|
|
|
col->getSortKey(strtest1, sortkey1, 64);
|
|
|
|
UnicodeString strtest2(test2);
|
|
|
|
col->getSortKey(strtest2, sortkey2, 64);
|
|
|
|
UnicodeString strtest3(test3);
|
|
|
|
col->getSortKey(strtest3, sortkey3, 64);
|
|
|
|
|
|
|
|
tempkey = key1.getByteArray(keylength);
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert(memcmp(tempkey, sortkey1, keylength) == 0,
|
2001-06-26 17:44:23 +00:00
|
|
|
"Test1 string should have the same collation key and sort key");
|
|
|
|
tempkey = key2.getByteArray(keylength);
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert(memcmp(tempkey, sortkey2, keylength) == 0,
|
2001-06-26 17:44:23 +00:00
|
|
|
"Test2 string should have the same collation key and sort key");
|
|
|
|
tempkey = key3.getByteArray(keylength);
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert(memcmp(tempkey, sortkey3, keylength) == 0,
|
2001-06-26 17:44:23 +00:00
|
|
|
"Test3 string should have the same collation key and sort key");
|
|
|
|
|
|
|
|
logln("Use secondary comparision level testing ...\n");
|
|
|
|
col->setStrength(Collator::SECONDARY);
|
2002-08-12 20:12:25 +00:00
|
|
|
|
2001-06-26 17:44:23 +00:00
|
|
|
col->getCollationKey(test1, u_strlen(test1), key1, status);
|
|
|
|
col->getCollationKey(test2, u_strlen(test2), key2, status);
|
|
|
|
col->getCollationKey(test3, u_strlen(test3), key3, status);
|
2002-08-12 20:12:25 +00:00
|
|
|
|
2003-03-19 13:21:51 +00:00
|
|
|
doAssert(key1.compareTo(key2) == Collator::EQUAL,
|
2001-06-26 17:44:23 +00:00
|
|
|
"Result should be \"Abcda\" == \"abcda\"");
|
2003-03-19 13:21:51 +00:00
|
|
|
doAssert(key2.compareTo(key3) == Collator::EQUAL,
|
2001-06-26 17:44:23 +00:00
|
|
|
"Result should be \"abcda\" == \"abcda\"");
|
|
|
|
|
|
|
|
tempkey = key2.getByteArray(keylength);
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert(memcmp(tempkey, key2compat, keylength - 1) == 0,
|
2001-06-26 17:44:23 +00:00
|
|
|
"Binary format for 'abcda' sortkey different!");
|
|
|
|
|
|
|
|
col->getSortKey(test1, sortkey1, 64);
|
|
|
|
col->getSortKey(test2, sortkey2, 64);
|
|
|
|
col->getSortKey(test3, sortkey3, 64);
|
|
|
|
|
|
|
|
tempkey = key1.getByteArray(keylength);
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert(memcmp(tempkey, sortkey1, keylength) == 0,
|
2001-06-26 17:44:23 +00:00
|
|
|
"Test1 string should have the same collation key and sort key");
|
|
|
|
tempkey = key2.getByteArray(keylength);
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert(memcmp(tempkey, sortkey2, keylength) == 0,
|
2001-06-26 17:44:23 +00:00
|
|
|
"Test2 string should have the same collation key and sort key");
|
|
|
|
tempkey = key3.getByteArray(keylength);
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert(memcmp(tempkey, sortkey3, keylength) == 0,
|
2001-06-26 17:44:23 +00:00
|
|
|
"Test3 string should have the same collation key and sort key");
|
|
|
|
|
|
|
|
col->getSortKey(test1, 5, sortkey1, 64);
|
|
|
|
col->getSortKey(test2, 5, sortkey2, 64);
|
|
|
|
col->getSortKey(test3, 5, sortkey3, 64);
|
|
|
|
|
|
|
|
tempkey = key1.getByteArray(keylength);
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert(memcmp(tempkey, sortkey1, keylength) == 0,
|
2001-06-26 17:44:23 +00:00
|
|
|
"Test1 string should have the same collation key and sort key");
|
|
|
|
tempkey = key2.getByteArray(keylength);
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert(memcmp(tempkey, sortkey2, keylength) == 0,
|
2001-06-26 17:44:23 +00:00
|
|
|
"Test2 string should have the same collation key and sort key");
|
|
|
|
tempkey = key3.getByteArray(keylength);
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert(memcmp(tempkey, sortkey3, keylength) == 0,
|
2001-06-26 17:44:23 +00:00
|
|
|
"Test3 string should have the same collation key and sort key");
|
|
|
|
|
|
|
|
col->getSortKey(strtest1, sortkey1, 64);
|
|
|
|
col->getSortKey(strtest2, sortkey2, 64);
|
|
|
|
col->getSortKey(strtest3, sortkey3, 64);
|
|
|
|
|
|
|
|
tempkey = key1.getByteArray(keylength);
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert(memcmp(tempkey, sortkey1, keylength) == 0,
|
2001-06-26 17:44:23 +00:00
|
|
|
"Test1 string should have the same collation key and sort key");
|
|
|
|
tempkey = key2.getByteArray(keylength);
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert(memcmp(tempkey, sortkey2, keylength) == 0,
|
2001-06-26 17:44:23 +00:00
|
|
|
"Test2 string should have the same collation key and sort key");
|
|
|
|
tempkey = key3.getByteArray(keylength);
|
2002-08-12 20:12:25 +00:00
|
|
|
doAssert(memcmp(tempkey, sortkey3, keylength) == 0,
|
2001-06-26 17:44:23 +00:00
|
|
|
"Test3 string should have the same collation key and sort key");
|
|
|
|
|
|
|
|
logln("testing sortkey ends...");
|
|
|
|
delete col;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CollationAPITest::TestMaxExpansion()
|
|
|
|
{
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
UChar ch = 0;
|
2003-08-23 01:46:02 +00:00
|
|
|
UChar32 unassigned = 0xEFFFD;
|
2001-06-26 17:44:23 +00:00
|
|
|
uint32_t sorder = 0;
|
|
|
|
uint32_t temporder = 0;
|
|
|
|
|
|
|
|
UnicodeString rule("&a < ab < c/aba < d < z < ch");
|
|
|
|
RuleBasedCollator coll(rule, status);
|
2003-06-06 04:54:34 +00:00
|
|
|
if(U_FAILURE(status)) {
|
|
|
|
errln("Collator creation failed with error %s", u_errorName(status));
|
|
|
|
return;
|
|
|
|
}
|
2001-06-26 17:44:23 +00:00
|
|
|
UnicodeString str(ch);
|
2002-08-12 20:12:25 +00:00
|
|
|
CollationElementIterator *iter =
|
2001-06-26 17:44:23 +00:00
|
|
|
coll.createCollationElementIterator(str);
|
|
|
|
|
|
|
|
while (ch < 0xFFFF && U_SUCCESS(status)) {
|
|
|
|
int count = 1;
|
|
|
|
uint32_t order;
|
|
|
|
int32_t size = 0;
|
|
|
|
|
|
|
|
ch ++;
|
|
|
|
|
|
|
|
str.setCharAt(0, ch);
|
|
|
|
iter->setText(str, status);
|
|
|
|
order = iter->previous(status);
|
|
|
|
|
|
|
|
/* thai management */
|
|
|
|
if (order == 0)
|
|
|
|
order = iter->previous(status);
|
|
|
|
|
2004-01-16 00:59:24 +00:00
|
|
|
while (U_SUCCESS(status) && iter->previous(status) != UCOL_NULLORDER) {
|
2001-06-26 17:44:23 +00:00
|
|
|
count ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = coll.getMaxExpansion(order);
|
|
|
|
if (U_FAILURE(status) || size < count) {
|
|
|
|
errln("Failure at codepoint %d, maximum expansion count < %d\n",
|
|
|
|
ch, count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* testing for exact max expansion */
|
|
|
|
ch = 0;
|
|
|
|
while (ch < 0x61) {
|
|
|
|
uint32_t order;
|
|
|
|
int32_t size;
|
|
|
|
str.setCharAt(0, ch);
|
|
|
|
iter->setText(str, status);
|
|
|
|
order = iter->previous(status);
|
|
|
|
size = coll.getMaxExpansion(order);
|
|
|
|
if (U_FAILURE(status) || size != 1) {
|
|
|
|
errln("Failure at codepoint %d, maximum expansion count < %d\n",
|
|
|
|
ch, 1);
|
|
|
|
}
|
|
|
|
ch ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
ch = 0x63;
|
2002-08-12 20:12:25 +00:00
|
|
|
str.setTo(ch);
|
2001-06-26 17:44:23 +00:00
|
|
|
iter->setText(str, status);
|
|
|
|
temporder = iter->previous(status);
|
|
|
|
|
|
|
|
if (U_FAILURE(status) || coll.getMaxExpansion(temporder) != 3) {
|
|
|
|
errln("Failure at codepoint %d, maximum expansion count != %d\n",
|
|
|
|
ch, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
ch = 0x64;
|
2002-08-12 20:12:25 +00:00
|
|
|
str.setTo(ch);
|
2001-06-26 17:44:23 +00:00
|
|
|
iter->setText(str, status);
|
|
|
|
temporder = iter->previous(status);
|
|
|
|
|
|
|
|
if (U_FAILURE(status) || coll.getMaxExpansion(temporder) != 1) {
|
|
|
|
errln("Failure at codepoint %d, maximum expansion count != %d\n",
|
|
|
|
ch, 3);
|
|
|
|
}
|
|
|
|
|
2003-08-23 01:46:02 +00:00
|
|
|
str.setTo(unassigned);
|
2001-06-26 17:44:23 +00:00
|
|
|
iter->setText(str, status);
|
|
|
|
sorder = iter->previous(status);
|
|
|
|
|
|
|
|
if (U_FAILURE(status) || coll.getMaxExpansion(sorder) != 2) {
|
|
|
|
errln("Failure at supplementary codepoints, maximum expansion count < %d\n",
|
|
|
|
2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* testing jamo */
|
|
|
|
ch = 0x1165;
|
|
|
|
str.setTo(ch);
|
|
|
|
iter->setText(str, status);
|
|
|
|
temporder = iter->previous(status);
|
|
|
|
if (U_FAILURE(status) || coll.getMaxExpansion(temporder) > 3) {
|
|
|
|
errln("Failure at codepoint %d, maximum expansion count > %d\n",
|
|
|
|
ch, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete iter;
|
|
|
|
|
|
|
|
/* testing special jamo &a<\u1160 */
|
|
|
|
rule = CharsToUnicodeString("\\u0026\\u0071\\u003c\\u1165\\u002f\\u0071\\u0071\\u0071\\u0071");
|
2002-08-12 20:12:25 +00:00
|
|
|
|
2001-06-26 17:44:23 +00:00
|
|
|
RuleBasedCollator jamocoll(rule, status);
|
|
|
|
iter = jamocoll.createCollationElementIterator(str);
|
|
|
|
temporder = iter->previous(status);
|
|
|
|
if (U_FAILURE(status) || iter->getMaxExpansion(temporder) != 6) {
|
|
|
|
errln("Failure at codepoint %d, maximum expansion count > %d\n",
|
|
|
|
ch, 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CollationAPITest::TestDisplayName()
|
|
|
|
{
|
|
|
|
UErrorCode error = U_ZERO_ERROR;
|
|
|
|
Collator *coll = Collator::createInstance("en_US", error);
|
|
|
|
if (U_FAILURE(error)) {
|
|
|
|
errln("Failure creating english collator");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
UnicodeString name;
|
|
|
|
UnicodeString result;
|
2002-03-26 23:18:57 +00:00
|
|
|
coll->getDisplayName(Locale::getCanadaFrench(), result);
|
|
|
|
Locale::getCanadaFrench().getDisplayName(name);
|
2001-06-26 17:44:23 +00:00
|
|
|
if (result.compare(name)) {
|
|
|
|
errln("Failure getting the correct name for locale en_US");
|
|
|
|
}
|
|
|
|
|
2002-08-21 00:16:30 +00:00
|
|
|
coll->getDisplayName(Locale::getSimplifiedChinese(), result);
|
|
|
|
Locale::getSimplifiedChinese().getDisplayName(name);
|
2001-06-26 17:44:23 +00:00
|
|
|
if (result.compare(name)) {
|
|
|
|
errln("Failure getting the correct name for locale zh_SG");
|
|
|
|
}
|
|
|
|
delete coll;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CollationAPITest::TestAttribute()
|
|
|
|
{
|
|
|
|
UErrorCode error = U_ZERO_ERROR;
|
|
|
|
Collator *coll = Collator::createInstance(error);
|
2002-08-12 20:12:25 +00:00
|
|
|
|
2001-06-26 17:44:23 +00:00
|
|
|
if (U_FAILURE(error)) {
|
|
|
|
errln("Creation of default collator failed");
|
|
|
|
return;
|
|
|
|
}
|
2002-08-12 20:12:25 +00:00
|
|
|
|
2001-06-26 17:44:23 +00:00
|
|
|
coll->setAttribute(UCOL_FRENCH_COLLATION, UCOL_OFF, error);
|
|
|
|
if (coll->getAttribute(UCOL_FRENCH_COLLATION, error) != UCOL_OFF ||
|
|
|
|
U_FAILURE(error)) {
|
|
|
|
errln("Setting and retrieving of the french collation failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
coll->setAttribute(UCOL_FRENCH_COLLATION, UCOL_ON, error);
|
|
|
|
if (coll->getAttribute(UCOL_FRENCH_COLLATION, error) != UCOL_ON ||
|
|
|
|
U_FAILURE(error)) {
|
|
|
|
errln("Setting and retrieving of the french collation failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
coll->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, error);
|
|
|
|
if (coll->getAttribute(UCOL_ALTERNATE_HANDLING, error) != UCOL_SHIFTED ||
|
|
|
|
U_FAILURE(error)) {
|
|
|
|
errln("Setting and retrieving of the alternate handling failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
coll->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, error);
|
|
|
|
if (coll->getAttribute(UCOL_ALTERNATE_HANDLING, error) != UCOL_NON_IGNORABLE ||
|
|
|
|
U_FAILURE(error)) {
|
|
|
|
errln("Setting and retrieving of the alternate handling failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
coll->setAttribute(UCOL_CASE_FIRST, UCOL_LOWER_FIRST, error);
|
|
|
|
if (coll->getAttribute(UCOL_CASE_FIRST, error) != UCOL_LOWER_FIRST ||
|
|
|
|
U_FAILURE(error)) {
|
|
|
|
errln("Setting and retrieving of the case first attribute failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
coll->setAttribute(UCOL_CASE_FIRST, UCOL_UPPER_FIRST, error);
|
|
|
|
if (coll->getAttribute(UCOL_CASE_FIRST, error) != UCOL_UPPER_FIRST ||
|
|
|
|
U_FAILURE(error)) {
|
|
|
|
errln("Setting and retrieving of the case first attribute failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
coll->setAttribute(UCOL_CASE_LEVEL, UCOL_ON, error);
|
|
|
|
if (coll->getAttribute(UCOL_CASE_LEVEL, error) != UCOL_ON ||
|
|
|
|
U_FAILURE(error)) {
|
|
|
|
errln("Setting and retrieving of the case level attribute failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
coll->setAttribute(UCOL_CASE_LEVEL, UCOL_OFF, error);
|
|
|
|
if (coll->getAttribute(UCOL_CASE_LEVEL, error) != UCOL_OFF ||
|
|
|
|
U_FAILURE(error)) {
|
|
|
|
errln("Setting and retrieving of the case level attribute failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
coll->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, error);
|
|
|
|
if (coll->getAttribute(UCOL_NORMALIZATION_MODE, error) != UCOL_ON ||
|
|
|
|
U_FAILURE(error)) {
|
|
|
|
errln("Setting and retrieving of the normalization on/off attribute failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
coll->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_OFF, error);
|
|
|
|
if (coll->getAttribute(UCOL_NORMALIZATION_MODE, error) != UCOL_OFF ||
|
|
|
|
U_FAILURE(error)) {
|
|
|
|
errln("Setting and retrieving of the normalization on/off attribute failed");
|
|
|
|
}
|
2002-08-12 20:12:25 +00:00
|
|
|
|
2001-06-26 17:44:23 +00:00
|
|
|
coll->setAttribute(UCOL_STRENGTH, UCOL_PRIMARY, error);
|
|
|
|
if (coll->getAttribute(UCOL_STRENGTH, error) != UCOL_PRIMARY ||
|
|
|
|
U_FAILURE(error)) {
|
|
|
|
errln("Setting and retrieving of the collation strength failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
coll->setAttribute(UCOL_STRENGTH, UCOL_SECONDARY, error);
|
|
|
|
if (coll->getAttribute(UCOL_STRENGTH, error) != UCOL_SECONDARY ||
|
|
|
|
U_FAILURE(error)) {
|
|
|
|
errln("Setting and retrieving of the collation strength failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
coll->setAttribute(UCOL_STRENGTH, UCOL_TERTIARY, error);
|
|
|
|
if (coll->getAttribute(UCOL_STRENGTH, error) != UCOL_TERTIARY ||
|
|
|
|
U_FAILURE(error)) {
|
|
|
|
errln("Setting and retrieving of the collation strength failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
coll->setAttribute(UCOL_STRENGTH, UCOL_QUATERNARY, error);
|
|
|
|
if (coll->getAttribute(UCOL_STRENGTH, error) != UCOL_QUATERNARY ||
|
|
|
|
U_FAILURE(error)) {
|
|
|
|
errln("Setting and retrieving of the collation strength failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
coll->setAttribute(UCOL_STRENGTH, UCOL_IDENTICAL, error);
|
|
|
|
if (coll->getAttribute(UCOL_STRENGTH, error) != UCOL_IDENTICAL ||
|
|
|
|
U_FAILURE(error)) {
|
|
|
|
errln("Setting and retrieving of the collation strength failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
delete coll;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-06-26 22:25:23 +00:00
|
|
|
void CollationAPITest::TestVariableTopSetting() {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
|
|
|
|
UChar vt[256] = { 0 };
|
|
|
|
|
|
|
|
Collator *coll = Collator::createInstance(status);
|
2003-06-06 04:54:34 +00:00
|
|
|
if(U_FAILURE(status)) {
|
|
|
|
delete coll;
|
|
|
|
errln("Collator creation failed with error %s", u_errorName(status));
|
|
|
|
return;
|
|
|
|
}
|
2001-06-26 22:25:23 +00:00
|
|
|
|
|
|
|
uint32_t oldVarTop = coll->getVariableTop(status);
|
|
|
|
|
|
|
|
vt[0] = 0x0041;
|
|
|
|
|
|
|
|
uint32_t newVarTop = coll->setVariableTop(vt, 1, status);
|
|
|
|
|
|
|
|
if((newVarTop & 0xFFFF0000) != (coll->getVariableTop(status) & 0xFFFF0000)) {
|
|
|
|
errln("Didn't set vartop properly\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
coll->setVariableTop(oldVarTop, status);
|
|
|
|
|
|
|
|
uint32_t newerVarTop = coll->setVariableTop(UnicodeString(vt, 1), status);
|
|
|
|
|
|
|
|
if((newVarTop & 0xFFFF0000) != (newerVarTop & 0xFFFF0000)) {
|
|
|
|
errln("Didn't set vartop properly from UnicodeString!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
delete coll;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2002-02-28 21:51:25 +00:00
|
|
|
void CollationAPITest::TestGetLocale() {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
const char *rules = "&a<x<y<z";
|
|
|
|
UChar rlz[256] = {0};
|
|
|
|
|
2002-03-13 06:02:54 +00:00
|
|
|
Collator *coll = NULL;
|
|
|
|
Locale locale;
|
2002-02-28 21:51:25 +00:00
|
|
|
|
|
|
|
int32_t i = 0;
|
|
|
|
|
2002-03-29 19:42:30 +00:00
|
|
|
static const struct {
|
|
|
|
const char* requestedLocale;
|
|
|
|
const char* validLocale;
|
|
|
|
const char* actualLocale;
|
2002-03-13 06:02:54 +00:00
|
|
|
} testStruct[] = {
|
|
|
|
{ "sr_YU", "sr_YU", "root" },
|
|
|
|
{ "sh_YU", "sh_YU", "sh" },
|
|
|
|
{ "en_US_CALIFORNIA", "en_US", "root" },
|
|
|
|
{ "fr_FR_NONEXISTANT", "fr_FR", "fr" }
|
|
|
|
};
|
|
|
|
|
2002-04-04 20:06:49 +00:00
|
|
|
u_unescape(rules, rlz, 256);
|
|
|
|
|
2002-03-13 06:02:54 +00:00
|
|
|
/* test opening collators for different locales */
|
2002-04-04 20:06:49 +00:00
|
|
|
for(i = 0; i<(int32_t)(sizeof(testStruct)/sizeof(testStruct[0])); i++) {
|
2002-03-13 06:02:54 +00:00
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
coll = Collator::createInstance(testStruct[i].requestedLocale, status);
|
|
|
|
if(U_FAILURE(status)) {
|
|
|
|
log("Failed to open collator for %s with %s\n", testStruct[i].requestedLocale, u_errorName(status));
|
|
|
|
delete coll;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
locale = coll->getLocale(ULOC_REQUESTED_LOCALE, status);
|
|
|
|
if(locale != testStruct[i].requestedLocale) {
|
2002-03-29 18:59:00 +00:00
|
|
|
log("[Coll %s]: Error in requested locale, expected %s, got %s\n", testStruct[i].requestedLocale, testStruct[i].requestedLocale, locale.getName());
|
2002-02-28 21:51:25 +00:00
|
|
|
}
|
2002-03-13 06:02:54 +00:00
|
|
|
locale = coll->getLocale(ULOC_VALID_LOCALE, status);
|
|
|
|
if(locale != testStruct[i].validLocale) {
|
2002-03-29 18:59:00 +00:00
|
|
|
log("[Coll %s]: Error in valid locale, expected %s, got %s\n", testStruct[i].requestedLocale, testStruct[i].validLocale, locale.getName());
|
2002-03-13 06:02:54 +00:00
|
|
|
}
|
|
|
|
locale = coll->getLocale(ULOC_ACTUAL_LOCALE, status);
|
|
|
|
if(locale != testStruct[i].actualLocale) {
|
2002-03-29 18:59:00 +00:00
|
|
|
log("[Coll %s]: Error in actual locale, expected %s, got %s\n", testStruct[i].requestedLocale, testStruct[i].actualLocale, locale.getName());
|
2002-02-28 21:51:25 +00:00
|
|
|
}
|
|
|
|
delete coll;
|
|
|
|
}
|
|
|
|
|
2002-03-13 06:02:54 +00:00
|
|
|
/* completely non-existant locale for collator should get a default collator */
|
|
|
|
{
|
2003-06-23 23:16:10 +00:00
|
|
|
Collator *defaultColl = Collator::createInstance((const Locale)NULL, status);
|
2002-03-13 06:02:54 +00:00
|
|
|
coll = Collator::createInstance("blahaha", status);
|
2003-06-06 04:54:34 +00:00
|
|
|
if(U_FAILURE(status)) {
|
|
|
|
log("Failed to open collator with %s\n", u_errorName(status));
|
|
|
|
delete coll;
|
|
|
|
delete defaultColl;
|
|
|
|
return;
|
|
|
|
}
|
2002-03-13 06:02:54 +00:00
|
|
|
if(coll->getLocale(ULOC_REQUESTED_LOCALE, status) != "blahaha") {
|
|
|
|
log("Nonexisting locale didn't preserve the requested locale\n");
|
|
|
|
}
|
|
|
|
if(coll->getLocale(ULOC_VALID_LOCALE, status) !=
|
|
|
|
defaultColl->getLocale(ULOC_VALID_LOCALE, status)) {
|
|
|
|
log("Valid locale for nonexisting locale locale collator differs "
|
|
|
|
"from valid locale for default collator\n");
|
|
|
|
}
|
2002-08-12 20:12:25 +00:00
|
|
|
if(coll->getLocale(ULOC_ACTUAL_LOCALE, status) !=
|
2002-03-13 06:02:54 +00:00
|
|
|
defaultColl->getLocale(ULOC_ACTUAL_LOCALE, status)) {
|
|
|
|
log("Actual locale for nonexisting locale locale collator differs "
|
|
|
|
"from actual locale for default collator\n");
|
|
|
|
}
|
|
|
|
delete coll;
|
|
|
|
delete defaultColl;
|
|
|
|
}
|
|
|
|
|
2002-08-12 20:12:25 +00:00
|
|
|
|
2002-03-13 06:02:54 +00:00
|
|
|
|
|
|
|
/* collator instantiated from rules should have all three locales NULL */
|
2002-02-28 21:51:25 +00:00
|
|
|
coll = new RuleBasedCollator(rlz, status);
|
2002-03-13 06:02:54 +00:00
|
|
|
locale = coll->getLocale(ULOC_REQUESTED_LOCALE, status);
|
|
|
|
if(!locale.isBogus()) {
|
2002-03-29 18:59:00 +00:00
|
|
|
log("For collator instantiated from rules, requested locale %s is not bogus\n", locale.getName());
|
2002-03-13 06:02:54 +00:00
|
|
|
}
|
|
|
|
locale = coll->getLocale(ULOC_VALID_LOCALE, status);
|
|
|
|
if(!locale.isBogus()) {
|
2002-03-29 18:59:00 +00:00
|
|
|
log("For collator instantiated from rules, valid locale %s is not bogus\n", locale.getName());
|
2002-03-13 06:02:54 +00:00
|
|
|
}
|
|
|
|
locale = coll->getLocale(ULOC_ACTUAL_LOCALE, status);
|
|
|
|
if(!locale.isBogus()) {
|
2002-03-29 18:59:00 +00:00
|
|
|
log("For collator instantiated from rules, actual locale %s is not bogus\n", locale.getName());
|
2002-02-28 21:51:25 +00:00
|
|
|
}
|
|
|
|
delete coll;
|
|
|
|
}
|
|
|
|
|
2002-08-12 20:12:25 +00:00
|
|
|
struct teststruct {
|
|
|
|
const char *original;
|
|
|
|
uint8_t key[256];
|
2002-08-14 00:38:03 +00:00
|
|
|
};
|
2002-08-10 00:12:23 +00:00
|
|
|
|
|
|
|
|
2002-08-14 00:38:03 +00:00
|
|
|
|
|
|
|
U_CDECL_BEGIN
|
|
|
|
static int U_CALLCONV
|
|
|
|
compare_teststruct(const void *string1, const void *string2) {
|
2002-08-10 00:12:23 +00:00
|
|
|
return(strcmp((const char *)((struct teststruct *)string1)->key, (const char *)((struct teststruct *)string2)->key));
|
|
|
|
}
|
2002-08-14 00:38:03 +00:00
|
|
|
U_CDECL_END
|
2002-08-10 00:12:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
void CollationAPITest::TestBounds(void) {
|
2002-08-12 20:12:25 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2002-08-10 00:12:23 +00:00
|
|
|
|
2002-08-12 20:12:25 +00:00
|
|
|
Collator *coll = Collator::createInstance(Locale("sh"), status);
|
2003-06-06 04:54:34 +00:00
|
|
|
if(U_FAILURE(status)) {
|
|
|
|
delete coll;
|
|
|
|
errln("Collator creation failed with %s", u_errorName(status));
|
|
|
|
return;
|
|
|
|
}
|
2002-08-10 00:12:23 +00:00
|
|
|
|
2002-08-12 20:12:25 +00:00
|
|
|
uint8_t sortkey[512], lower[512], upper[512];
|
|
|
|
UChar buffer[512];
|
|
|
|
|
|
|
|
const char *test[] = {
|
|
|
|
"John Smith",
|
|
|
|
"JOHN SMITH",
|
|
|
|
"john SMITH",
|
|
|
|
"j\\u00F6hn sm\\u00EFth",
|
|
|
|
"J\\u00F6hn Sm\\u00EFth",
|
|
|
|
"J\\u00D6HN SM\\u00CFTH",
|
|
|
|
"john smithsonian",
|
|
|
|
"John Smithsonian"
|
|
|
|
};
|
2002-08-10 00:12:23 +00:00
|
|
|
|
2002-08-12 20:12:25 +00:00
|
|
|
static struct teststruct tests[] = {
|
2002-08-15 22:04:18 +00:00
|
|
|
{"\\u010CAKI MIHALJ", {0}},
|
|
|
|
{"\\u010CAKI MIHALJ", {0}},
|
|
|
|
{"\\u010CAKI PIRO\\u0160KA", {0}},
|
|
|
|
{"\\u010CABAI ANDRIJA", {0}},
|
|
|
|
{"\\u010CABAI LAJO\\u0160", {0}},
|
|
|
|
{"\\u010CABAI MARIJA", {0}},
|
|
|
|
{"\\u010CABAI STEVAN", {0}},
|
|
|
|
{"\\u010CABAI STEVAN", {0}},
|
|
|
|
{"\\u010CABARKAPA BRANKO", {0}},
|
|
|
|
{"\\u010CABARKAPA MILENKO", {0}},
|
|
|
|
{"\\u010CABARKAPA MIROSLAV", {0}},
|
|
|
|
{"\\u010CABARKAPA SIMO", {0}},
|
|
|
|
{"\\u010CABARKAPA STANKO", {0}},
|
|
|
|
{"\\u010CABARKAPA TAMARA", {0}},
|
|
|
|
{"\\u010CABARKAPA TOMA\\u0160", {0}},
|
|
|
|
{"\\u010CABDARI\\u0106 NIKOLA", {0}},
|
|
|
|
{"\\u010CABDARI\\u0106 ZORICA", {0}},
|
|
|
|
{"\\u010CABI NANDOR", {0}},
|
|
|
|
{"\\u010CABOVI\\u0106 MILAN", {0}},
|
|
|
|
{"\\u010CABRADI AGNEZIJA", {0}},
|
|
|
|
{"\\u010CABRADI IVAN", {0}},
|
|
|
|
{"\\u010CABRADI JELENA", {0}},
|
|
|
|
{"\\u010CABRADI LJUBICA", {0}},
|
|
|
|
{"\\u010CABRADI STEVAN", {0}},
|
|
|
|
{"\\u010CABRDA MARTIN", {0}},
|
|
|
|
{"\\u010CABRILO BOGDAN", {0}},
|
|
|
|
{"\\u010CABRILO BRANISLAV", {0}},
|
|
|
|
{"\\u010CABRILO LAZAR", {0}},
|
|
|
|
{"\\u010CABRILO LJUBICA", {0}},
|
|
|
|
{"\\u010CABRILO SPASOJA", {0}},
|
|
|
|
{"\\u010CADE\\u0160 ZDENKA", {0}},
|
|
|
|
{"\\u010CADESKI BLAGOJE", {0}},
|
|
|
|
{"\\u010CADOVSKI VLADIMIR", {0}},
|
|
|
|
{"\\u010CAGLJEVI\\u0106 TOMA", {0}},
|
|
|
|
{"\\u010CAGOROVI\\u0106 VLADIMIR", {0}},
|
|
|
|
{"\\u010CAJA VANKA", {0}},
|
|
|
|
{"\\u010CAJI\\u0106 BOGOLJUB", {0}},
|
|
|
|
{"\\u010CAJI\\u0106 BORISLAV", {0}},
|
|
|
|
{"\\u010CAJI\\u0106 RADOSLAV", {0}},
|
|
|
|
{"\\u010CAK\\u0160IRAN MILADIN", {0}},
|
|
|
|
{"\\u010CAKAN EUGEN", {0}},
|
|
|
|
{"\\u010CAKAN EVGENIJE", {0}},
|
|
|
|
{"\\u010CAKAN IVAN", {0}},
|
|
|
|
{"\\u010CAKAN JULIJAN", {0}},
|
|
|
|
{"\\u010CAKAN MIHAJLO", {0}},
|
|
|
|
{"\\u010CAKAN STEVAN", {0}},
|
|
|
|
{"\\u010CAKAN VLADIMIR", {0}},
|
|
|
|
{"\\u010CAKAN VLADIMIR", {0}},
|
|
|
|
{"\\u010CAKAN VLADIMIR", {0}},
|
|
|
|
{"\\u010CAKARA ANA", {0}},
|
|
|
|
{"\\u010CAKAREVI\\u0106 MOMIR", {0}},
|
|
|
|
{"\\u010CAKAREVI\\u0106 NEDELJKO", {0}},
|
|
|
|
{"\\u010CAKI \\u0160ANDOR", {0}},
|
|
|
|
{"\\u010CAKI AMALIJA", {0}},
|
|
|
|
{"\\u010CAKI ANDRA\\u0160", {0}},
|
|
|
|
{"\\u010CAKI LADISLAV", {0}},
|
|
|
|
{"\\u010CAKI LAJO\\u0160", {0}},
|
|
|
|
{"\\u010CAKI LASLO", {0}}
|
2002-08-12 20:12:25 +00:00
|
|
|
};
|
2002-08-10 00:12:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2002-08-12 20:12:25 +00:00
|
|
|
int32_t i = 0, j = 0, k = 0, buffSize = 0, skSize = 0, lowerSize = 0, upperSize = 0;
|
|
|
|
int32_t arraySize = sizeof(tests)/sizeof(tests[0]);
|
2002-08-10 00:12:23 +00:00
|
|
|
|
2002-08-12 20:12:25 +00:00
|
|
|
for(i = 0; i<arraySize; i++) {
|
|
|
|
buffSize = u_unescape(tests[i].original, buffer, 512);
|
|
|
|
skSize = coll->getSortKey(buffer, buffSize, tests[i].key, 512);
|
|
|
|
}
|
2002-08-10 00:12:23 +00:00
|
|
|
|
2002-08-12 20:12:25 +00:00
|
|
|
qsort(tests, arraySize, sizeof(struct teststruct), compare_teststruct);
|
|
|
|
|
|
|
|
for(i = 0; i < arraySize-1; i++) {
|
|
|
|
for(j = i+1; j < arraySize; j++) {
|
|
|
|
lowerSize = coll->getBound(tests[i].key, -1, UCOL_BOUND_LOWER, 1, lower, 512, status);
|
|
|
|
upperSize = coll->getBound(tests[j].key, -1, UCOL_BOUND_UPPER, 1, upper, 512, status);
|
|
|
|
for(k = i; k <= j; k++) {
|
|
|
|
if(strcmp((const char *)lower, (const char *)tests[k].key) > 0) {
|
|
|
|
errln("Problem with lower! j = %i (%s vs %s)", k, tests[k].original, tests[i].original);
|
|
|
|
}
|
|
|
|
if(strcmp((const char *)upper, (const char *)tests[k].key) <= 0) {
|
|
|
|
errln("Problem with upper! j = %i (%s vs %s)", k, tests[k].original, tests[j].original);
|
|
|
|
}
|
|
|
|
}
|
2002-08-10 00:12:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-12 20:12:25 +00:00
|
|
|
for(i = 0; i<(int32_t)(sizeof(test)/sizeof(test[0])); i++) {
|
|
|
|
buffSize = u_unescape(test[i], buffer, 512);
|
|
|
|
skSize = coll->getSortKey(buffer, buffSize, sortkey, 512);
|
|
|
|
lowerSize = ucol_getBound(sortkey, skSize, UCOL_BOUND_LOWER, 1, lower, 512, &status);
|
|
|
|
upperSize = ucol_getBound(sortkey, skSize, UCOL_BOUND_UPPER_LONG, 1, upper, 512, &status);
|
2002-08-15 22:04:18 +00:00
|
|
|
for(j = i+1; j<(int32_t)(sizeof(test)/sizeof(test[0])); j++) {
|
2002-08-12 20:12:25 +00:00
|
|
|
buffSize = u_unescape(test[j], buffer, 512);
|
|
|
|
skSize = coll->getSortKey(buffer, buffSize, sortkey, 512);
|
|
|
|
if(strcmp((const char *)lower, (const char *)sortkey) > 0) {
|
|
|
|
errln("Problem with lower! i = %i, j = %i (%s vs %s)", i, j, test[i], test[j]);
|
|
|
|
}
|
|
|
|
if(strcmp((const char *)upper, (const char *)sortkey) <= 0) {
|
|
|
|
errln("Problem with upper! i = %i, j = %i (%s vs %s)", i, j, test[i], test[j]);
|
|
|
|
}
|
|
|
|
}
|
2002-08-10 00:12:23 +00:00
|
|
|
}
|
2002-08-12 20:12:25 +00:00
|
|
|
delete coll;
|
2002-08-10 00:12:23 +00:00
|
|
|
}
|
|
|
|
|
2002-09-17 06:27:51 +00:00
|
|
|
|
|
|
|
void CollationAPITest::TestGetTailoredSet()
|
|
|
|
{
|
|
|
|
struct {
|
2002-11-12 01:37:48 +00:00
|
|
|
const char *rules;
|
|
|
|
const char *tests[20];
|
2002-09-17 06:27:51 +00:00
|
|
|
int32_t testsize;
|
|
|
|
} setTest[] = {
|
|
|
|
{ "&a < \\u212b", { "\\u212b", "A\\u030a", "\\u00c5" }, 3},
|
|
|
|
{ "& S < \\u0161 <<< \\u0160", { "\\u0161", "s\\u030C", "\\u0160", "S\\u030C" }, 4}
|
|
|
|
};
|
|
|
|
|
2002-12-19 19:00:19 +00:00
|
|
|
uint32_t i = 0, j = 0;
|
2002-09-17 06:27:51 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
|
|
|
|
RuleBasedCollator *coll = NULL;
|
2002-12-19 19:00:19 +00:00
|
|
|
UnicodeString buff;
|
2002-09-17 06:27:51 +00:00
|
|
|
UnicodeSet *set = NULL;
|
|
|
|
|
|
|
|
for(i = 0; i < sizeof(setTest)/sizeof(setTest[0]); i++) {
|
|
|
|
buff = UnicodeString(setTest[i].rules, "").unescape();
|
|
|
|
coll = new RuleBasedCollator(buff, status);
|
|
|
|
if(U_SUCCESS(status)) {
|
|
|
|
set = coll->getTailoredSet(status);
|
|
|
|
if(set->size() != setTest[i].testsize) {
|
|
|
|
errln("Tailored set size different (%d) than expected (%d)", set->size(), setTest[i].testsize);
|
|
|
|
}
|
2002-12-19 19:00:19 +00:00
|
|
|
for(j = 0; j < (uint32_t)setTest[i].testsize; j++) {
|
2002-09-17 06:27:51 +00:00
|
|
|
buff = UnicodeString(setTest[i].tests[j], "").unescape();
|
|
|
|
if(!set->contains(buff)) {
|
|
|
|
errln("Tailored set doesn't contain %s... It should", setTest[i].tests[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete set;
|
|
|
|
} else {
|
|
|
|
errln("Couldn't open collator with rules %s\n", setTest[i].rules);
|
|
|
|
}
|
|
|
|
delete coll;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-21 21:43:03 +00:00
|
|
|
void CollationAPITest::TestUClassID()
|
|
|
|
{
|
2003-06-02 23:34:58 +00:00
|
|
|
char id = *((char *)RuleBasedCollator::getStaticClassID());
|
|
|
|
if (id != 0) {
|
|
|
|
errln("Static class id for RuleBasedCollator should be 0");
|
|
|
|
}
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
RuleBasedCollator *coll
|
|
|
|
= (RuleBasedCollator *)Collator::createInstance(status);
|
2003-06-06 04:54:34 +00:00
|
|
|
if(U_FAILURE(status)) {
|
|
|
|
delete coll;
|
|
|
|
errln("Collator creation failed with %s", u_errorName(status));
|
|
|
|
return;
|
|
|
|
}
|
2003-06-02 23:34:58 +00:00
|
|
|
id = *((char *)coll->getDynamicClassID());
|
|
|
|
if (id != 0) {
|
|
|
|
errln("Dynamic class id for RuleBasedCollator should be 0");
|
|
|
|
}
|
|
|
|
id = *((char *)CollationKey::getStaticClassID());
|
|
|
|
if (id != 0) {
|
|
|
|
errln("Static class id for CollationKey should be 0");
|
|
|
|
}
|
|
|
|
CollationKey *key = new CollationKey();
|
|
|
|
id = *((char *)key->getDynamicClassID());
|
|
|
|
if (id != 0) {
|
|
|
|
errln("Dynamic class id for CollationKey should be 0");
|
|
|
|
}
|
|
|
|
id = *((char *)CollationElementIterator::getStaticClassID());
|
|
|
|
if (id != 0) {
|
|
|
|
errln("Static class id for CollationElementIterator should be 0");
|
|
|
|
}
|
|
|
|
UnicodeString str("testing");
|
|
|
|
CollationElementIterator *iter = coll->createCollationElementIterator(str);
|
|
|
|
id = *((char *)iter->getDynamicClassID());
|
|
|
|
if (id != 0) {
|
|
|
|
errln("Dynamic class id for CollationElementIterator should be 0");
|
|
|
|
}
|
|
|
|
delete key;
|
|
|
|
delete iter;
|
|
|
|
delete coll;
|
2002-11-21 21:43:03 +00:00
|
|
|
}
|
|
|
|
|
2003-05-12 22:46:56 +00:00
|
|
|
class TestCollator : public Collator
|
2002-12-10 06:02:50 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual Collator* clone(void) const;
|
2003-05-01 23:23:52 +00:00
|
|
|
|
2003-06-02 23:34:58 +00:00
|
|
|
// dang, markus says we can't use 'using' in ICU. I hate doing this for
|
|
|
|
// deprecated methods...
|
2003-05-01 23:23:52 +00:00
|
|
|
|
2003-06-02 23:34:58 +00:00
|
|
|
// using Collator::compare;
|
|
|
|
|
|
|
|
virtual EComparisonResult compare(const UnicodeString& source,
|
2003-05-01 23:23:52 +00:00
|
|
|
const UnicodeString& target) const
|
2003-06-02 23:34:58 +00:00
|
|
|
{
|
|
|
|
return Collator::compare(source, target);
|
|
|
|
}
|
2003-05-01 23:23:52 +00:00
|
|
|
|
2003-06-02 23:34:58 +00:00
|
|
|
virtual EComparisonResult compare(const UnicodeString& source,
|
2002-12-10 06:02:50 +00:00
|
|
|
const UnicodeString& target,
|
2003-05-01 23:23:52 +00:00
|
|
|
int32_t length) const
|
2003-06-02 23:34:58 +00:00
|
|
|
{
|
|
|
|
return Collator::compare(source, target, length);
|
|
|
|
}
|
2003-05-01 23:23:52 +00:00
|
|
|
|
2003-06-02 23:34:58 +00:00
|
|
|
virtual EComparisonResult compare(const UChar* source,
|
|
|
|
int32_t sourceLength,
|
|
|
|
const UChar* target,
|
|
|
|
int32_t targetLength) const
|
|
|
|
{
|
|
|
|
return Collator::compare(source, sourceLength, target, targetLength);
|
|
|
|
}
|
2003-05-01 23:23:52 +00:00
|
|
|
|
|
|
|
|
2003-06-02 23:34:58 +00:00
|
|
|
virtual UCollationResult compare(const UnicodeString& source,
|
2003-05-01 23:23:52 +00:00
|
|
|
const UnicodeString& target,
|
2003-06-02 23:34:58 +00:00
|
|
|
UErrorCode& status) const;
|
|
|
|
virtual UCollationResult compare(const UnicodeString& source,
|
2003-05-01 23:23:52 +00:00
|
|
|
const UnicodeString& target,
|
|
|
|
int32_t length,
|
2003-06-02 23:34:58 +00:00
|
|
|
UErrorCode& status) const;
|
|
|
|
virtual UCollationResult compare(const UChar* source,
|
|
|
|
int32_t sourceLength,
|
|
|
|
const UChar* target,
|
|
|
|
int32_t targetLength,
|
|
|
|
UErrorCode& status) const;
|
|
|
|
virtual CollationKey& getCollationKey(const UnicodeString& source,
|
2002-12-10 06:02:50 +00:00
|
|
|
CollationKey& key,
|
|
|
|
UErrorCode& status) const;
|
2003-06-02 23:34:58 +00:00
|
|
|
virtual CollationKey& getCollationKey(const UChar*source,
|
2002-12-10 06:02:50 +00:00
|
|
|
int32_t sourceLength,
|
|
|
|
CollationKey& key,
|
|
|
|
UErrorCode& status) const;
|
2003-06-02 23:34:58 +00:00
|
|
|
virtual int32_t hashCode(void) const;
|
|
|
|
virtual const Locale getLocale(ULocDataLocaleType type,
|
|
|
|
UErrorCode& status) const;
|
|
|
|
virtual ECollationStrength getStrength(void) const;
|
|
|
|
virtual void setStrength(ECollationStrength newStrength);
|
|
|
|
virtual UClassID getDynamicClassID(void) const;
|
|
|
|
virtual void getVersion(UVersionInfo info) const;
|
|
|
|
virtual void setAttribute(UColAttribute attr, UColAttributeValue value,
|
2002-12-10 06:02:50 +00:00
|
|
|
UErrorCode &status);
|
2003-06-02 23:34:58 +00:00
|
|
|
virtual UColAttributeValue getAttribute(UColAttribute attr,
|
2002-12-10 06:02:50 +00:00
|
|
|
UErrorCode &status);
|
2003-06-02 23:34:58 +00:00
|
|
|
virtual uint32_t setVariableTop(const UChar *varTop, int32_t len,
|
|
|
|
UErrorCode &status);
|
|
|
|
virtual uint32_t setVariableTop(const UnicodeString varTop,
|
|
|
|
UErrorCode &status);
|
|
|
|
virtual void setVariableTop(const uint32_t varTop, UErrorCode &status);
|
|
|
|
virtual uint32_t getVariableTop(UErrorCode &status) const;
|
|
|
|
virtual Collator* safeClone(void);
|
|
|
|
virtual int32_t getSortKey(const UnicodeString& source,
|
2002-12-10 06:02:50 +00:00
|
|
|
uint8_t* result,
|
|
|
|
int32_t resultLength) const;
|
|
|
|
virtual int32_t getSortKey(const UChar*source, int32_t sourceLength,
|
|
|
|
uint8_t*result, int32_t resultLength) const;
|
|
|
|
virtual UnicodeSet *getTailoredSet(UErrorCode &status) const;
|
2003-06-02 23:34:58 +00:00
|
|
|
virtual UBool operator!=(const Collator& other) const;
|
|
|
|
virtual void setLocales(const Locale& requestedLocale, const Locale& validLocale);
|
2003-06-11 01:01:40 +00:00
|
|
|
TestCollator() : Collator() {};
|
|
|
|
TestCollator(UCollationStrength collationStrength,
|
|
|
|
UNormalizationMode decompositionMode) : Collator(collationStrength, decompositionMode) {};
|
2002-12-10 06:02:50 +00:00
|
|
|
};
|
|
|
|
|
2003-06-02 23:34:58 +00:00
|
|
|
inline UBool TestCollator::operator!=(const Collator& other) const {
|
|
|
|
return Collator::operator!=(other);
|
2002-12-10 07:51:40 +00:00
|
|
|
}
|
|
|
|
|
2002-12-10 06:02:50 +00:00
|
|
|
#define returnEComparisonResult(data) \
|
2003-06-02 23:34:58 +00:00
|
|
|
if (data < 0) return Collator::LESS;\
|
|
|
|
if (data > 0) return Collator::GREATER;\
|
|
|
|
return Collator::EQUAL;
|
2002-12-10 06:02:50 +00:00
|
|
|
|
|
|
|
Collator* TestCollator::clone() const
|
|
|
|
{
|
2003-06-02 23:34:58 +00:00
|
|
|
return new TestCollator();
|
2002-12-10 06:02:50 +00:00
|
|
|
}
|
|
|
|
|
2003-05-01 23:23:52 +00:00
|
|
|
UCollationResult TestCollator::compare(const UnicodeString& source,
|
|
|
|
const UnicodeString& target,
|
2003-06-02 23:34:58 +00:00
|
|
|
UErrorCode& status) const
|
2002-12-10 06:02:50 +00:00
|
|
|
{
|
2003-05-15 21:06:44 +00:00
|
|
|
if(U_SUCCESS(status)) {
|
2003-05-01 23:23:52 +00:00
|
|
|
return UCollationResult(source.compare(target));
|
2003-05-15 21:06:44 +00:00
|
|
|
} else {
|
|
|
|
return UCOL_EQUAL;
|
|
|
|
}
|
2002-12-10 06:02:50 +00:00
|
|
|
}
|
|
|
|
|
2003-05-01 23:23:52 +00:00
|
|
|
UCollationResult TestCollator::compare(const UnicodeString& source,
|
2002-12-10 06:02:50 +00:00
|
|
|
const UnicodeString& target,
|
2003-05-01 23:23:52 +00:00
|
|
|
int32_t length,
|
2003-06-02 23:34:58 +00:00
|
|
|
UErrorCode& status) const
|
2002-12-10 06:02:50 +00:00
|
|
|
{
|
2003-05-15 21:06:44 +00:00
|
|
|
if(U_SUCCESS(status)) {
|
|
|
|
return UCollationResult(source.compare(0, length, target));
|
|
|
|
} else {
|
|
|
|
return UCOL_EQUAL;
|
|
|
|
}
|
2002-12-10 06:02:50 +00:00
|
|
|
}
|
|
|
|
|
2003-05-01 23:23:52 +00:00
|
|
|
UCollationResult TestCollator::compare(const UChar* source,
|
2003-06-02 23:34:58 +00:00
|
|
|
int32_t sourceLength,
|
|
|
|
const UChar* target,
|
|
|
|
int32_t targetLength,
|
|
|
|
UErrorCode& status) const
|
2002-12-10 06:02:50 +00:00
|
|
|
{
|
2003-06-02 23:34:58 +00:00
|
|
|
UnicodeString s(source, sourceLength);
|
|
|
|
UnicodeString t(target, targetLength);
|
|
|
|
return compare(s, t, status);
|
2002-12-10 06:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CollationKey& TestCollator::getCollationKey(const UnicodeString& source,
|
|
|
|
CollationKey& key,
|
|
|
|
UErrorCode& status) const
|
|
|
|
{
|
2003-06-02 23:34:58 +00:00
|
|
|
char temp[100];
|
|
|
|
int length = 100;
|
|
|
|
length = source.extract(temp, length, NULL, status);
|
|
|
|
temp[length] = 0;
|
|
|
|
CollationKey tempkey((uint8_t*)temp, length);
|
|
|
|
key = tempkey;
|
|
|
|
return key;
|
2002-12-10 06:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CollationKey& TestCollator::getCollationKey(const UChar*source,
|
|
|
|
int32_t sourceLength,
|
|
|
|
CollationKey& key,
|
|
|
|
UErrorCode& status) const
|
|
|
|
{
|
2003-06-02 23:34:58 +00:00
|
|
|
//s tack allocation used since collationkey does not keep the unicodestring
|
|
|
|
UnicodeString str(source, sourceLength);
|
|
|
|
return getCollationKey(str, key, status);
|
2002-12-10 06:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t TestCollator::getSortKey(const UnicodeString& source, uint8_t* result,
|
2003-06-02 23:34:58 +00:00
|
|
|
int32_t resultLength) const
|
2002-12-10 06:02:50 +00:00
|
|
|
{
|
2003-06-02 23:34:58 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
int32_t length = source.extract((char *)result, resultLength, NULL,
|
|
|
|
status);
|
|
|
|
result[length] = 0;
|
|
|
|
return length;
|
2002-12-10 06:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t TestCollator::getSortKey(const UChar*source, int32_t sourceLength,
|
2003-06-02 23:34:58 +00:00
|
|
|
uint8_t*result, int32_t resultLength) const
|
2002-12-10 06:02:50 +00:00
|
|
|
{
|
2003-06-02 23:34:58 +00:00
|
|
|
UnicodeString str(source, sourceLength);
|
|
|
|
return getSortKey(str, result, resultLength);
|
2002-12-10 06:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t TestCollator::hashCode() const
|
|
|
|
{
|
2003-06-02 23:34:58 +00:00
|
|
|
return 0;
|
2002-12-10 06:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const Locale TestCollator::getLocale(ULocDataLocaleType type,
|
2003-06-02 23:34:58 +00:00
|
|
|
UErrorCode& status) const
|
2002-12-10 06:02:50 +00:00
|
|
|
{
|
2002-12-19 19:00:19 +00:00
|
|
|
// api not used, this is to make the compiler happy
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
type = ULOC_DATA_LOCALE_TYPE_LIMIT;
|
|
|
|
}
|
|
|
|
return NULL;
|
2002-12-10 06:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Collator::ECollationStrength TestCollator::getStrength() const
|
|
|
|
{
|
2003-06-02 23:34:58 +00:00
|
|
|
return TERTIARY;
|
2002-12-10 06:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCollator::setStrength(Collator::ECollationStrength newStrength)
|
|
|
|
{
|
2002-12-19 19:00:19 +00:00
|
|
|
// api not used, this is to make the compiler happy
|
|
|
|
newStrength = TERTIARY;
|
2002-12-10 06:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UClassID TestCollator::getDynamicClassID(void) const
|
|
|
|
{
|
2003-06-02 23:34:58 +00:00
|
|
|
return 0;
|
2002-12-10 06:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCollator::getVersion(UVersionInfo info) const
|
|
|
|
{
|
2002-12-19 19:00:19 +00:00
|
|
|
// api not used, this is to make the compiler happy
|
|
|
|
memset(info, 0, U_MAX_VERSION_LENGTH);
|
2002-12-10 06:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCollator::setAttribute(UColAttribute attr, UColAttributeValue value,
|
2002-12-19 19:00:19 +00:00
|
|
|
UErrorCode &status)
|
2002-12-10 06:02:50 +00:00
|
|
|
{
|
2002-12-19 19:00:19 +00:00
|
|
|
// api not used, this is to make the compiler happy
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
attr = UCOL_ATTRIBUTE_COUNT;
|
|
|
|
value = UCOL_OFF;
|
|
|
|
}
|
2002-12-10 06:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UColAttributeValue TestCollator::getAttribute(UColAttribute attr,
|
|
|
|
UErrorCode &status)
|
|
|
|
{
|
2002-12-19 19:00:19 +00:00
|
|
|
// api not used, this is to make the compiler happy
|
|
|
|
if (U_FAILURE(status) || attr == UCOL_ATTRIBUTE_COUNT) {
|
|
|
|
return UCOL_OFF;
|
|
|
|
}
|
2003-06-02 23:34:58 +00:00
|
|
|
return UCOL_DEFAULT;
|
2002-12-10 06:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t TestCollator::setVariableTop(const UChar *varTop, int32_t len,
|
2003-06-02 23:34:58 +00:00
|
|
|
UErrorCode &status)
|
2002-12-10 06:02:50 +00:00
|
|
|
{
|
2002-12-19 19:00:19 +00:00
|
|
|
// api not used, this is to make the compiler happy
|
|
|
|
if (U_SUCCESS(status) && (varTop == 0 || len < -1)) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
}
|
2003-06-02 23:34:58 +00:00
|
|
|
return 0;
|
2002-12-10 06:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t TestCollator::setVariableTop(const UnicodeString varTop,
|
|
|
|
UErrorCode &status)
|
|
|
|
{
|
2003-06-02 23:34:58 +00:00
|
|
|
// api not used, this is to make the compiler happy
|
2002-12-19 19:00:19 +00:00
|
|
|
if (U_SUCCESS(status) && varTop.length() == 0) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
}
|
2003-06-02 23:34:58 +00:00
|
|
|
return 0;
|
2002-12-10 06:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestCollator::setVariableTop(const uint32_t varTop, UErrorCode &status)
|
|
|
|
{
|
2002-12-19 19:00:19 +00:00
|
|
|
// api not used, this is to make the compiler happy
|
|
|
|
if (U_SUCCESS(status) && varTop == 0) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
}
|
2002-12-10 06:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t TestCollator::getVariableTop(UErrorCode &status) const
|
|
|
|
{
|
2002-12-19 19:00:19 +00:00
|
|
|
|
|
|
|
// api not used, this is to make the compiler happy
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2003-06-02 23:34:58 +00:00
|
|
|
return (uint32_t)(0xFFFFFFFFu);
|
2002-12-10 06:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Collator* TestCollator::safeClone(void)
|
|
|
|
{
|
2003-06-02 23:34:58 +00:00
|
|
|
return new TestCollator();
|
2002-12-10 06:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeSet * TestCollator::getTailoredSet(UErrorCode &status) const
|
|
|
|
{
|
2003-06-02 23:34:58 +00:00
|
|
|
return Collator::getTailoredSet(status);
|
2002-12-10 06:02:50 +00:00
|
|
|
}
|
|
|
|
|
2003-05-19 18:32:53 +00:00
|
|
|
void TestCollator::setLocales(const Locale& requestedLocale, const Locale& validLocale)
|
|
|
|
{
|
2003-06-02 23:34:58 +00:00
|
|
|
Collator::setLocales(requestedLocale, validLocale);
|
2003-05-19 18:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-10 06:02:50 +00:00
|
|
|
void CollationAPITest::TestSubclass()
|
|
|
|
{
|
2003-06-02 23:34:58 +00:00
|
|
|
TestCollator col1;
|
2002-12-10 06:02:50 +00:00
|
|
|
TestCollator col2;
|
2002-12-10 07:51:40 +00:00
|
|
|
doAssert(col1 != col2, "2 instance of TestCollator should be different");
|
2003-06-02 23:34:58 +00:00
|
|
|
if (col1.hashCode() != col2.hashCode()) {
|
2002-12-10 06:02:50 +00:00
|
|
|
errln("Every TestCollator has the same hashcode");
|
|
|
|
}
|
|
|
|
UnicodeString abc("abc", 3);
|
|
|
|
UnicodeString bcd("bcd", 3);
|
|
|
|
if (col1.compare(abc, bcd) != abc.compare(bcd)) {
|
|
|
|
errln("TestCollator compare should be the same as the default "
|
|
|
|
"string comparison");
|
|
|
|
}
|
|
|
|
CollationKey key;
|
2003-06-02 23:34:58 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
col1.getCollationKey(abc, key, status);
|
|
|
|
int32_t length = 0;
|
|
|
|
const char* bytes = (const char *)key.getByteArray(length);
|
2003-05-01 23:23:52 +00:00
|
|
|
UnicodeString keyarray(bytes, length, NULL, status);
|
2002-12-10 06:02:50 +00:00
|
|
|
if (abc != keyarray) {
|
|
|
|
errln("TestCollator collationkey API is returning wrong values");
|
|
|
|
}
|
|
|
|
|
2003-06-02 23:34:58 +00:00
|
|
|
UnicodeSet expectedset(0, 0x10FFFF);
|
|
|
|
UnicodeSet *defaultset = col1.getTailoredSet(status);
|
|
|
|
if (!defaultset->containsAll(expectedset)
|
|
|
|
|| !expectedset.containsAll(*defaultset)) {
|
|
|
|
errln("Error: expected default tailoring to be 0 to 0x10ffff");
|
|
|
|
}
|
|
|
|
delete defaultset;
|
2003-05-19 18:32:53 +00:00
|
|
|
|
2003-06-02 23:34:58 +00:00
|
|
|
// use base class implementation
|
|
|
|
Locale loc1 = Locale::getGermany();
|
|
|
|
Locale loc2 = Locale::getFrance();
|
|
|
|
col1.setLocales(loc1, loc2); // default implementation has no effect
|
2003-05-19 18:32:53 +00:00
|
|
|
|
2003-06-02 23:34:58 +00:00
|
|
|
UnicodeString displayName;
|
|
|
|
col1.getDisplayName(loc1, loc2, displayName); // de_DE collator in fr_FR locale
|
2003-06-11 01:01:40 +00:00
|
|
|
|
|
|
|
TestCollator col3(UCOL_TERTIARY, UNORM_NONE);
|
|
|
|
UnicodeString a("a");
|
|
|
|
UnicodeString b("b");
|
2003-06-12 01:37:25 +00:00
|
|
|
Collator::EComparisonResult result = Collator::EComparisonResult(a.compare(b));
|
2003-06-11 01:01:40 +00:00
|
|
|
if(col1.compare(a, b) != result) {
|
|
|
|
errln("Collator doesn't give default result");
|
|
|
|
}
|
|
|
|
if(col1.compare(a, b, 1) != result) {
|
|
|
|
errln("Collator doesn't give default result");
|
|
|
|
}
|
|
|
|
if(col1.compare(a.getBuffer(), a.length(), b.getBuffer(), b.length()) != result) {
|
|
|
|
errln("Collator doesn't give default result");
|
|
|
|
}
|
2002-12-10 06:02:50 +00:00
|
|
|
}
|
|
|
|
|
2004-06-03 19:26:16 +00:00
|
|
|
void CollationAPITest::TestNULLCharTailoring()
|
|
|
|
{
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
UChar buf[256] = {0};
|
|
|
|
int32_t len = u_unescape("&a < '\\u0000'", buf, 256);
|
2004-06-03 22:50:09 +00:00
|
|
|
UnicodeString first((UChar)0x0061);
|
2004-06-03 22:50:39 +00:00
|
|
|
UnicodeString second((UChar)0);
|
2004-06-03 19:26:16 +00:00
|
|
|
RuleBasedCollator *coll = new RuleBasedCollator(UnicodeString(buf, len), status);
|
|
|
|
if(U_FAILURE(status)) {
|
|
|
|
errln("Failed to open collator");
|
|
|
|
}
|
|
|
|
UCollationResult res = coll->compare(first, second, status);
|
|
|
|
if(res != UCOL_LESS) {
|
|
|
|
errln("a should be less then NULL after tailoring");
|
|
|
|
}
|
|
|
|
delete coll;
|
|
|
|
}
|
|
|
|
|
2006-08-09 22:16:47 +00:00
|
|
|
void CollationAPITest::TestClone() {
|
|
|
|
logln("\ninit c0");
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
RuleBasedCollator* c0 = (RuleBasedCollator*)Collator::createInstance(status);
|
|
|
|
c0->setStrength(Collator::TERTIARY);
|
|
|
|
dump("c0", c0, status);
|
|
|
|
|
|
|
|
logln("\ninit c1");
|
|
|
|
RuleBasedCollator* c1 = (RuleBasedCollator*)Collator::createInstance(status);
|
|
|
|
c1->setStrength(Collator::TERTIARY);
|
|
|
|
UColAttributeValue val = c1->getAttribute(UCOL_CASE_FIRST, status);
|
|
|
|
if(val == UCOL_LOWER_FIRST){
|
|
|
|
c1->setAttribute(UCOL_CASE_FIRST, UCOL_UPPER_FIRST, status);
|
|
|
|
}else{
|
|
|
|
c1->setAttribute(UCOL_CASE_FIRST, UCOL_LOWER_FIRST, status);
|
|
|
|
}
|
|
|
|
dump("c0", c0, status);
|
|
|
|
dump("c1", c1, status);
|
|
|
|
|
|
|
|
logln("\ninit c2");
|
|
|
|
RuleBasedCollator* c2 = (RuleBasedCollator*)c1->clone();
|
|
|
|
val = c2->getAttribute(UCOL_CASE_FIRST, status);
|
|
|
|
if(val == UCOL_LOWER_FIRST){
|
|
|
|
c2->setAttribute(UCOL_CASE_FIRST, UCOL_UPPER_FIRST, status);
|
|
|
|
}else{
|
|
|
|
c2->setAttribute(UCOL_CASE_FIRST, UCOL_LOWER_FIRST, status);
|
|
|
|
}
|
|
|
|
if(U_FAILURE(status)){
|
|
|
|
errln("set and get attributes of collator failed. %s\n", u_errorName(status));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dump("c0", c0, status);
|
|
|
|
dump("c1", c1, status);
|
|
|
|
dump("c2", c2, status);
|
|
|
|
if(*c1 == *c2){
|
|
|
|
errln("The cloned objects refer to same data");
|
|
|
|
}
|
2006-08-11 02:48:23 +00:00
|
|
|
delete c0;
|
|
|
|
delete c1;
|
|
|
|
delete c2;
|
2006-08-09 22:16:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CollationAPITest::dump(UnicodeString msg, RuleBasedCollator* c, UErrorCode& status) {
|
|
|
|
const char* bigone = "One";
|
|
|
|
const char* littleone = "one";
|
|
|
|
|
|
|
|
logln(msg + " " + c->compare(bigone, littleone) +
|
|
|
|
" s: " + c->getStrength() +
|
|
|
|
" u: " + c->getAttribute(UCOL_CASE_FIRST, status));
|
|
|
|
}
|
2000-08-23 19:11:16 +00:00
|
|
|
void CollationAPITest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par */)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
if (exec) logln("TestSuite CollationAPITest: ");
|
|
|
|
switch (index) {
|
2000-08-23 19:11:16 +00:00
|
|
|
case 0: name = "TestProperty"; if (exec) TestProperty(/* par */); break;
|
|
|
|
case 1: name = "TestOperators"; if (exec) TestOperators(/* par */); break;
|
|
|
|
case 2: name = "TestDuplicate"; if (exec) TestDuplicate(/* par */); break;
|
|
|
|
case 3: name = "TestCompare"; if (exec) TestCompare(/* par */); break;
|
|
|
|
case 4: name = "TestHashCode"; if (exec) TestHashCode(/* par */); break;
|
|
|
|
case 5: name = "TestCollationKey"; if (exec) TestCollationKey(/* par */); break;
|
|
|
|
case 6: name = "TestElemIter"; if (exec) TestElemIter(/* par */); break;
|
|
|
|
case 7: name = "TestGetAll"; if (exec) TestGetAll(/* par */); break;
|
2001-03-23 00:57:44 +00:00
|
|
|
case 8: name = "TestRuleBasedColl"; if (exec) TestRuleBasedColl(/* par */); break;
|
|
|
|
case 9: name = "TestDecomposition"; if (exec) TestDecomposition(/* par */); break;
|
|
|
|
case 10: name = "TestSafeClone"; if (exec) TestSafeClone(/* par */); break;
|
2001-06-26 17:44:23 +00:00
|
|
|
case 11: name = "TestSortKey"; if (exec) TestSortKey(); break;
|
2002-03-22 23:53:57 +00:00
|
|
|
case 12: name = "TestMaxExpansion"; if (exec) TestMaxExpansion(); break;
|
2001-06-26 17:44:23 +00:00
|
|
|
case 13: name = "TestDisplayName"; if (exec) TestDisplayName(); break;
|
|
|
|
case 14: name = "TestAttribute"; if (exec) TestAttribute(); break;
|
2001-06-26 22:25:23 +00:00
|
|
|
case 15: name = "TestVariableTopSetting"; if (exec) TestVariableTopSetting(); break;
|
2002-02-13 02:57:21 +00:00
|
|
|
case 16: name = "TestRules"; if (exec) TestRules(); break;
|
2002-02-28 21:51:25 +00:00
|
|
|
case 17: name = "TestGetLocale"; if (exec) TestGetLocale(); break;
|
2002-08-10 00:12:23 +00:00
|
|
|
case 18: name = "TestBounds"; if (exec) TestBounds(); break;
|
2002-09-17 06:27:51 +00:00
|
|
|
case 19: name = "TestGetTailoredSet"; if (exec) TestGetTailoredSet(); break;
|
2002-11-21 21:43:03 +00:00
|
|
|
case 20: name = "TestUClassID"; if (exec) TestUClassID(); break;
|
2003-06-02 23:34:58 +00:00
|
|
|
case 21: name = "TestSubclass"; if (exec) TestSubclass(); break;
|
2004-06-03 19:26:16 +00:00
|
|
|
case 22: name = "TestNULLCharTailoring"; if (exec) TestNULLCharTailoring(); break;
|
2006-08-09 22:16:47 +00:00
|
|
|
case 23: name = "TestClone"; if (exec) TestClone(); break;
|
1999-08-16 21:50:52 +00:00
|
|
|
default: name = ""; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-21 00:43:14 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_COLLATION */
|