2002-04-30 23:03:51 +00:00
|
|
|
/********************************************************************
|
|
|
|
* COPYRIGHT:
|
2010-10-19 21:48:04 +00:00
|
|
|
* Copyright (c) 1997-2010, International Business Machines Corporation and
|
2002-04-30 23:03:51 +00:00
|
|
|
* others. All Rights Reserved.
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* IntlTestCollator is the medium level test class for everything in the directory "collate".
|
|
|
|
*/
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* Modification history
|
|
|
|
* Date Name Description
|
|
|
|
* 02/14/2001 synwee Compare with cintltst and commented away tests
|
|
|
|
* that are not run.
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
2002-09-21 00:43:14 +00:00
|
|
|
|
2009-08-04 21:09:17 +00:00
|
|
|
#if !UCONFIG_NO_COLLATION && !UCONFIG_NO_FILE_IO
|
2002-09-21 00:43:14 +00:00
|
|
|
|
2002-04-30 23:03:51 +00:00
|
|
|
#include "unicode/uchar.h"
|
2004-03-23 06:37:36 +00:00
|
|
|
#include "unicode/tstdtmod.h"
|
2002-04-30 23:03:51 +00:00
|
|
|
#include "cstring.h"
|
|
|
|
#include "ucol_tok.h"
|
|
|
|
#include "tscoll.h"
|
|
|
|
#include "dadrcoll.h"
|
|
|
|
|
2002-08-07 17:50:29 +00:00
|
|
|
U_CDECL_BEGIN
|
2002-08-04 06:25:06 +00:00
|
|
|
static void U_CALLCONV deleteSeqElement(void *elem) {
|
|
|
|
delete((SeqElement *)elem);
|
|
|
|
}
|
2002-08-07 17:50:29 +00:00
|
|
|
U_CDECL_END
|
2002-08-04 06:25:06 +00:00
|
|
|
|
2002-04-30 23:03:51 +00:00
|
|
|
DataDrivenCollatorTest::DataDrivenCollatorTest()
|
2002-08-04 06:25:06 +00:00
|
|
|
: seq(StringCharacterIterator("")),
|
|
|
|
status(U_ZERO_ERROR),
|
|
|
|
sequences(status)
|
2002-04-30 23:03:51 +00:00
|
|
|
{
|
2004-03-23 06:37:36 +00:00
|
|
|
driver = TestDataModule::getTestDataModule("DataDrivenCollationTest", *this, status);
|
2002-08-04 06:25:06 +00:00
|
|
|
sequences.setDeleter(deleteSeqElement);
|
2005-06-28 16:40:46 +00:00
|
|
|
UCA = (RuleBasedCollator*)Collator::createInstance("root", status);
|
2002-04-30 23:03:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DataDrivenCollatorTest::~DataDrivenCollatorTest()
|
|
|
|
{
|
2002-05-10 01:26:46 +00:00
|
|
|
delete driver;
|
2005-06-28 16:40:46 +00:00
|
|
|
delete UCA;
|
2002-04-30 23:03:51 +00:00
|
|
|
}
|
|
|
|
|
2002-06-17 21:31:41 +00:00
|
|
|
void DataDrivenCollatorTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par */)
|
2002-04-30 23:03:51 +00:00
|
|
|
{
|
2002-06-08 23:31:36 +00:00
|
|
|
if(driver != NULL) {
|
|
|
|
if (exec)
|
|
|
|
{
|
|
|
|
logln("TestSuite Collator: ");
|
2002-06-04 08:06:05 +00:00
|
|
|
}
|
2002-06-08 23:31:36 +00:00
|
|
|
const DataMap *info = NULL;
|
|
|
|
TestData *testData = driver->createTestData(index, status);
|
|
|
|
if(U_SUCCESS(status)) {
|
|
|
|
name = testData->getName();
|
|
|
|
if(testData->getInfo(info, status)) {
|
|
|
|
log(info->getString("Description", status));
|
|
|
|
}
|
|
|
|
if(exec) {
|
|
|
|
log(name);
|
|
|
|
logln("---");
|
|
|
|
logln("");
|
2002-08-04 06:25:06 +00:00
|
|
|
processTest(testData);
|
2002-06-08 23:31:36 +00:00
|
|
|
}
|
|
|
|
delete testData;
|
|
|
|
} else {
|
|
|
|
name = "";
|
2002-04-30 23:03:51 +00:00
|
|
|
}
|
2002-06-04 08:06:05 +00:00
|
|
|
} else {
|
2009-06-12 19:34:21 +00:00
|
|
|
dataerrln("collate/DataDrivenTest data not initialized!");
|
2002-06-04 08:06:05 +00:00
|
|
|
name = "";
|
2002-04-30 23:03:51 +00:00
|
|
|
}
|
2002-06-04 08:06:05 +00:00
|
|
|
|
|
|
|
|
2002-04-30 23:03:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
2002-08-04 06:25:06 +00:00
|
|
|
DataDrivenCollatorTest::setTestSequence(const UnicodeString &setSequence, SeqElement &el) {
|
2002-04-30 23:03:51 +00:00
|
|
|
seq.setText(setSequence);
|
2002-08-04 06:25:06 +00:00
|
|
|
return getNextInSequence(el);
|
2002-04-30 23:03:51 +00:00
|
|
|
}
|
|
|
|
|
2002-05-01 18:33:04 +00:00
|
|
|
// Parses the sequence to be tested
|
2002-04-30 23:03:51 +00:00
|
|
|
UBool
|
2002-08-04 06:25:06 +00:00
|
|
|
DataDrivenCollatorTest::getNextInSequence(SeqElement &el) {
|
|
|
|
el.source.truncate(0);
|
2002-04-30 23:03:51 +00:00
|
|
|
UBool quoted = FALSE;
|
|
|
|
UBool quotedsingle = FALSE;
|
|
|
|
UChar32 currChar = 0;
|
|
|
|
|
|
|
|
while(currChar != CharacterIterator::DONE) {
|
|
|
|
currChar= seq.next32PostInc();
|
|
|
|
if(!quoted) {
|
|
|
|
if(u_isWhitespace(currChar)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch(currChar) {
|
|
|
|
case CharacterIterator::DONE:
|
|
|
|
break;
|
|
|
|
case 0x003C /* < */:
|
2003-03-19 13:21:51 +00:00
|
|
|
el.relation = Collator::LESS;
|
2002-04-30 23:03:51 +00:00
|
|
|
currChar = CharacterIterator::DONE;
|
|
|
|
break;
|
|
|
|
case 0x003D /* = */:
|
2003-03-19 13:21:51 +00:00
|
|
|
el.relation = Collator::EQUAL;
|
2002-04-30 23:03:51 +00:00
|
|
|
currChar = CharacterIterator::DONE;
|
|
|
|
break;
|
|
|
|
case 0x003E /* > */:
|
2003-03-19 13:21:51 +00:00
|
|
|
el.relation = Collator::GREATER;
|
2002-04-30 23:03:51 +00:00
|
|
|
currChar = CharacterIterator::DONE;
|
|
|
|
break;
|
|
|
|
case 0x0027 /* ' */: /* very basic quoting */
|
|
|
|
quoted = TRUE;
|
|
|
|
quotedsingle = FALSE;
|
|
|
|
break;
|
|
|
|
case 0x005c /* \ */: /* single quote */
|
|
|
|
quoted = TRUE;
|
|
|
|
quotedsingle = TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
2002-08-04 06:25:06 +00:00
|
|
|
el.source.append(currChar);
|
2002-04-30 23:03:51 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(currChar == CharacterIterator::DONE) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
errln("Quote in sequence not closed!");
|
|
|
|
return FALSE;
|
|
|
|
} else if(currChar == 0x0027) {
|
|
|
|
quoted = FALSE;
|
|
|
|
} else {
|
2002-08-04 06:25:06 +00:00
|
|
|
el.source.append(currChar);
|
2002-04-30 23:03:51 +00:00
|
|
|
}
|
|
|
|
if(quotedsingle) {
|
|
|
|
quoted = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return seq.hasNext();
|
|
|
|
}
|
|
|
|
|
2002-05-01 18:33:04 +00:00
|
|
|
// Reads the options string and sets appropriate attributes in collator
|
2002-04-30 23:03:51 +00:00
|
|
|
void
|
2002-08-04 06:25:06 +00:00
|
|
|
DataDrivenCollatorTest::processArguments(Collator *col, const UChar *start, int32_t optLen) {
|
2002-05-01 18:33:04 +00:00
|
|
|
const UChar *end = start+optLen;
|
2002-04-30 23:03:51 +00:00
|
|
|
UColAttribute attrib;
|
|
|
|
UColAttributeValue value;
|
|
|
|
|
2002-05-10 01:26:46 +00:00
|
|
|
if(optLen == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-04-30 23:03:51 +00:00
|
|
|
start = ucol_tok_getNextArgument(start, end, &attrib, &value, &status);
|
|
|
|
while(start != NULL) {
|
|
|
|
if(U_SUCCESS(status)) {
|
|
|
|
col->setAttribute(attrib, value, status);
|
|
|
|
}
|
|
|
|
start = ucol_tok_getNextArgument(start, end, &attrib, &value, &status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-28 16:40:46 +00:00
|
|
|
void
|
2002-08-04 06:25:06 +00:00
|
|
|
DataDrivenCollatorTest::processTest(TestData *testData) {
|
2002-04-30 23:03:51 +00:00
|
|
|
Collator *col = NULL;
|
2002-06-04 08:06:05 +00:00
|
|
|
const UChar *arguments = NULL;
|
|
|
|
int32_t argLen = 0;
|
|
|
|
const DataMap *settings = NULL;
|
|
|
|
const DataMap *currentCase = NULL;
|
|
|
|
UErrorCode intStatus = U_ZERO_ERROR;
|
|
|
|
UnicodeString testSetting;
|
|
|
|
while(testData->nextSettings(settings, status)) {
|
2002-07-02 22:37:47 +00:00
|
|
|
intStatus = U_ZERO_ERROR;
|
2002-06-04 08:06:05 +00:00
|
|
|
// try to get a locale
|
|
|
|
testSetting = settings->getString("TestLocale", intStatus);
|
|
|
|
if(U_SUCCESS(intStatus)) {
|
|
|
|
char localeName[256];
|
|
|
|
testSetting.extract(0, testSetting.length(), localeName, "");
|
|
|
|
col = Collator::createInstance(localeName, status);
|
|
|
|
if(U_SUCCESS(status)) {
|
|
|
|
logln("Testing collator for locale "+testSetting);
|
|
|
|
} else {
|
|
|
|
errln("Unable to instantiate collator for locale "+testSetting);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// if no locale, try from rules
|
|
|
|
intStatus = U_ZERO_ERROR;
|
|
|
|
testSetting = settings->getString("Rules", intStatus);
|
|
|
|
if(U_SUCCESS(intStatus)) {
|
|
|
|
col = new RuleBasedCollator(testSetting, status);
|
|
|
|
if(U_SUCCESS(status)) {
|
|
|
|
logln("Testing collator for rules "+testSetting);
|
|
|
|
} else {
|
2010-10-19 21:48:04 +00:00
|
|
|
errln("Unable to instantiate collator for rules "+testSetting+" - "+u_errorName(status));
|
2002-06-04 08:06:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errln("No collator definition!");
|
2010-10-19 21:48:04 +00:00
|
|
|
return;
|
2002-06-04 08:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
2005-06-28 16:40:46 +00:00
|
|
|
|
|
|
|
int32_t cloneSize = 0;
|
|
|
|
uint8_t* cloneBuf = NULL;
|
|
|
|
RuleBasedCollator* clone = NULL;
|
|
|
|
if(col != NULL){
|
|
|
|
RuleBasedCollator* rbc = (RuleBasedCollator*)col;
|
|
|
|
cloneSize = rbc->cloneBinary(NULL, 0, intStatus);
|
2005-07-06 21:27:21 +00:00
|
|
|
intStatus = U_ZERO_ERROR;
|
2005-06-28 16:40:46 +00:00
|
|
|
cloneBuf = (uint8_t*) malloc(cloneSize);
|
|
|
|
cloneSize = rbc->cloneBinary(cloneBuf, cloneSize, intStatus);
|
|
|
|
clone = new RuleBasedCollator(cloneBuf, cloneSize, UCA, intStatus);
|
|
|
|
if(U_FAILURE(intStatus)){
|
|
|
|
errln("Could not clone the RuleBasedCollator. Error: %s", u_errorName(intStatus));
|
|
|
|
intStatus= U_ZERO_ERROR;
|
|
|
|
}
|
2002-06-04 08:06:05 +00:00
|
|
|
// get attributes
|
|
|
|
testSetting = settings->getString("Arguments", intStatus);
|
|
|
|
if(U_SUCCESS(intStatus)) {
|
|
|
|
logln("Arguments: "+testSetting);
|
|
|
|
argLen = testSetting.length();
|
|
|
|
arguments = testSetting.getBuffer();
|
2002-08-04 06:25:06 +00:00
|
|
|
processArguments(col, arguments, argLen);
|
2005-06-28 16:40:46 +00:00
|
|
|
if(clone != NULL){
|
|
|
|
processArguments(clone, arguments, argLen);
|
|
|
|
}
|
2002-08-04 06:25:06 +00:00
|
|
|
if(U_FAILURE(status)) {
|
2002-06-04 08:06:05 +00:00
|
|
|
errln("Couldn't process arguments");
|
|
|
|
break;
|
|
|
|
}
|
2002-07-02 22:37:47 +00:00
|
|
|
} else {
|
|
|
|
intStatus = U_ZERO_ERROR;
|
2002-06-04 08:06:05 +00:00
|
|
|
}
|
|
|
|
// Start the processing
|
|
|
|
while(testData->nextCase(currentCase, status)) {
|
|
|
|
UnicodeString sequence = currentCase->getString("sequence", status);
|
|
|
|
if(U_SUCCESS(status)) {
|
2002-08-04 06:25:06 +00:00
|
|
|
processSequence(col, sequence);
|
2005-06-28 16:40:46 +00:00
|
|
|
if(clone != NULL){
|
|
|
|
processSequence(clone, sequence);
|
|
|
|
}
|
2002-06-04 08:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errln("Couldn't instantiate a collator!");
|
|
|
|
}
|
2005-06-28 16:40:46 +00:00
|
|
|
delete clone;
|
|
|
|
free(cloneBuf);
|
2002-06-04 08:06:05 +00:00
|
|
|
delete col;
|
2002-07-02 22:37:47 +00:00
|
|
|
col = NULL;
|
2002-06-04 08:06:05 +00:00
|
|
|
}
|
2002-04-30 23:03:51 +00:00
|
|
|
}
|
|
|
|
|
2005-06-28 16:40:46 +00:00
|
|
|
|
2002-04-30 23:03:51 +00:00
|
|
|
void
|
2002-08-04 06:25:06 +00:00
|
|
|
DataDrivenCollatorTest::processSequence(Collator* col, const UnicodeString &sequence) {
|
2003-03-19 13:21:51 +00:00
|
|
|
Collator::EComparisonResult relation = Collator::EQUAL;
|
2002-04-30 23:03:51 +00:00
|
|
|
UBool hasNext;
|
2002-08-04 06:25:06 +00:00
|
|
|
SeqElement *source = NULL;
|
|
|
|
SeqElement *target = NULL;
|
|
|
|
int32_t j = 0;
|
|
|
|
|
|
|
|
sequences.removeAllElements();
|
2002-04-30 23:03:51 +00:00
|
|
|
|
2002-08-04 06:25:06 +00:00
|
|
|
target = new SeqElement();
|
|
|
|
|
|
|
|
setTestSequence(sequence, *target);
|
|
|
|
sequences.addElement(target, status);
|
2002-04-30 23:03:51 +00:00
|
|
|
|
|
|
|
do {
|
2003-03-19 13:21:51 +00:00
|
|
|
relation = Collator::EQUAL;
|
2002-08-04 06:25:06 +00:00
|
|
|
target = new SeqElement();
|
|
|
|
hasNext = getNextInSequence(*target);
|
|
|
|
for(j = sequences.size(); j > 0; j--) {
|
|
|
|
source = (SeqElement *)sequences.elementAt(j-1);
|
2003-03-19 13:21:51 +00:00
|
|
|
if(relation == Collator::EQUAL && source->relation != Collator::EQUAL) {
|
2002-08-04 06:25:06 +00:00
|
|
|
relation = source->relation;
|
|
|
|
}
|
|
|
|
doTest(col, source->source, target->source, relation);
|
|
|
|
}
|
|
|
|
sequences.addElement(target, status);
|
2002-04-30 23:03:51 +00:00
|
|
|
source = target;
|
|
|
|
} while(hasNext);
|
|
|
|
}
|
|
|
|
|
2002-09-21 00:43:14 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_COLLATION */
|