2005-07-28 20:25:44 +00:00
|
|
|
/*
|
2005-08-02 17:55:57 +00:00
|
|
|
******************************************************************************
|
2006-03-23 04:09:01 +00:00
|
|
|
* Copyright (C) 2005-2006, International Business Machines Corporation and *
|
2005-08-02 17:55:57 +00:00
|
|
|
* others. All Rights Reserved. *
|
|
|
|
******************************************************************************
|
2005-07-28 20:25:44 +00:00
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
2005-08-02 17:55:57 +00:00
|
|
|
#include "rndmcoll.h"
|
2005-12-15 04:09:09 +00:00
|
|
|
#include "wbnf.h"
|
|
|
|
|
2005-07-28 20:25:44 +00:00
|
|
|
|
2005-08-02 17:55:57 +00:00
|
|
|
#if !UCONFIG_NO_COLLATION
|
|
|
|
|
|
|
|
//Raymond: Following comments are copied from Java implementation
|
|
|
|
//
|
|
|
|
// each rule can be:
|
|
|
|
// "[" command "]"
|
|
|
|
// "& [" position "]"
|
|
|
|
// "&" before chars
|
|
|
|
// relation "[variable top]"
|
|
|
|
// relation (chars "|")? chars ("/" chars)?
|
|
|
|
// plus, a reset must come before a relation
|
|
|
|
|
2005-12-15 04:09:09 +00:00
|
|
|
|
2005-08-02 17:55:57 +00:00
|
|
|
static const char collationBNF[] =
|
|
|
|
"$s = ' '? 50%;"
|
|
|
|
"$crlf = '\r\n';"
|
|
|
|
|
|
|
|
"$alternateOptions = non'-'ignorable | shifted;"
|
|
|
|
"$onoff = on | off;"
|
|
|
|
"$caseFirstOptions = off | upper | lower;"
|
|
|
|
"$strengthOptions = '1' | '2' | '3' | '4' | 'I';"
|
|
|
|
"$commandList = '['"
|
|
|
|
" ( alternate ' ' $alternateOptions"
|
|
|
|
" | backwards' 2'"
|
|
|
|
" | normalization ' ' $onoff "
|
|
|
|
" | caseLevel ' ' $onoff "
|
|
|
|
" | hiraganaQ ' ' $onoff"
|
|
|
|
" | caseFirst ' ' $caseFirstOptions"
|
|
|
|
" | strength ' ' $strengthOptions"
|
|
|
|
" ) ']';"
|
|
|
|
"$command = $commandList $crlf;"
|
|
|
|
|
|
|
|
"$ignorableTypes = (tertiary | secondary | primary) ' ' ignorable;"
|
|
|
|
"$allTypes = variable | regular | implicit | trailing | $ignorableTypes;"
|
|
|
|
"$positionList = '[' (first | last) ' ' $allTypes ']';"
|
|
|
|
|
|
|
|
"$beforeList = '[before ' ('1' | '2' | '3') ']';"
|
|
|
|
|
|
|
|
"$relationList = ("
|
|
|
|
" '<'"
|
|
|
|
" | '<<'"
|
|
|
|
" | ';'"
|
|
|
|
" | '<<<'"
|
|
|
|
" | ','"
|
|
|
|
" | '='"
|
|
|
|
");"
|
2005-12-15 04:09:09 +00:00
|
|
|
"$string = $chars{1,5}~@;"
|
|
|
|
"$chars = a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z| '<'| '&'| '['| ']';"
|
2005-08-02 17:55:57 +00:00
|
|
|
"$rel1 = '[variable top]' $s;"
|
|
|
|
"$p1 = ($string $s '|' $s)? 25%;"
|
|
|
|
"$p2 = ('/' $s $string $s)? 25%;"
|
|
|
|
"$rel2 = $p1 $string $s $p2;"
|
|
|
|
"$relation = $relationList $s ($rel1 | $rel2) $crlf;"
|
|
|
|
|
|
|
|
"$reset = '&' $s ($beforeList $s)? 10% ($positionList 1% | $string 10%) $crlf;"
|
|
|
|
"$mostRules = $command 1% | $reset 5% | $relation 25%;"
|
|
|
|
"$root = $command{0,5} $reset $mostRules{1,20};";
|
|
|
|
|
|
|
|
|
2006-07-25 23:02:40 +00:00
|
|
|
static const UVersionInfo ICU_37 = {3,7,0,0};
|
|
|
|
|
2005-08-02 17:55:57 +00:00
|
|
|
void RandomCollatorTest::Test2(){
|
2006-07-25 23:02:40 +00:00
|
|
|
if(!isICUVersionAtLeast(ICU_37)){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
errln("TestWbnf is incorrectly implemented.\nThis test should be modeled to use the existing test frame work for naming tests.\n");
|
|
|
|
}
|
2005-12-15 04:09:09 +00:00
|
|
|
TestWbnf();
|
2005-08-02 17:55:57 +00:00
|
|
|
}
|
2005-07-28 20:25:44 +00:00
|
|
|
|
|
|
|
|
2005-08-04 16:49:11 +00:00
|
|
|
void RandomCollatorTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* ){
|
2005-07-28 20:25:44 +00:00
|
|
|
if (exec) logln("TestSuite RandomCollatorTest: ");
|
|
|
|
switch (index) {
|
2005-08-02 17:55:57 +00:00
|
|
|
TESTCASE(0, Test);
|
2006-07-25 23:02:40 +00:00
|
|
|
TESTCASE(1, Test2);
|
2005-07-28 20:25:44 +00:00
|
|
|
default: name = ""; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-23 04:09:01 +00:00
|
|
|
/*
|
|
|
|
class TestColltorCompare{
|
|
|
|
public:
|
|
|
|
UBool operator()(Collator &coll, int count = 1000){
|
|
|
|
UnicodeString a(test_string.get_a_string());
|
|
|
|
UnicodeString b(test_string.get_a_string());
|
|
|
|
UnicodeString c(test_string.get_a_string());
|
|
|
|
do{
|
|
|
|
if (check_transitivity(coll, a, b, c)){
|
|
|
|
a = b;
|
|
|
|
b = c;
|
|
|
|
c = UnicodeString(test_string.get_a_string());
|
|
|
|
}
|
|
|
|
}while(count-- >= 0 );
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
TestColltorCompare():test_string("$s = $c{1,8};", "$s", "$c", new Magic_SelectOneChar("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ[]&<")){
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
UBool check_transitivity(const Collator & coll, const UnicodeString &a, const UnicodeString &b, const UnicodeString &c){
|
|
|
|
int ab = coll.compare(a,b), ba = coll.compare(b,a);
|
|
|
|
int bc = coll.compare(b,c), cb = coll.compare(c,b);
|
|
|
|
int ca = coll.compare(c,a), ac = coll.compare(a,c);
|
|
|
|
// a
|
|
|
|
// / \
|
|
|
|
// b - c
|
|
|
|
//
|
|
|
|
if (//counter-clockwise, maximum
|
|
|
|
(ab >=0 && bc >=0 && ac <0)
|
|
|
|
||(bc >=0 && ca >=0 && ba <0)
|
|
|
|
||(ca >=0 && ab >=0 && cb <0)
|
|
|
|
|
|
|
|
//counter-clockwise, minimum
|
|
|
|
||(ab <=0 && bc <=0 && ca >0)
|
|
|
|
||(bc <=0 && ca <=0 && ba >0)
|
|
|
|
||(ca <=0 && ab <=0 && cb >0)
|
|
|
|
){
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
LanguageGenerator test_string;
|
|
|
|
};*/
|
|
|
|
|
2005-07-28 20:25:44 +00:00
|
|
|
void RandomCollatorTest::Test(){
|
2006-07-25 23:02:40 +00:00
|
|
|
if(!isICUVersionAtLeast(ICU_37)){
|
2005-12-15 04:09:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2006-07-25 23:02:40 +00:00
|
|
|
else {
|
|
|
|
errln("This test needs to be fixed.\n");
|
|
|
|
}
|
2005-12-15 04:09:09 +00:00
|
|
|
LanguageGenerator test_rule;
|
|
|
|
if (test_rule.parseBNF(collationBNF, "$root", TRUE) != LanguageGenerator::OK){
|
|
|
|
errln("The test code itself is wrong.");
|
|
|
|
return;
|
|
|
|
};
|
2005-07-28 20:25:44 +00:00
|
|
|
|
2006-03-23 04:09:01 +00:00
|
|
|
//TestColltorCompare coll_test;
|
2005-07-28 20:25:44 +00:00
|
|
|
|
2005-12-15 04:09:09 +00:00
|
|
|
static const int CONSTRUCT_RANDOM_COUNT = 1000;
|
2005-08-02 21:47:28 +00:00
|
|
|
int i;
|
|
|
|
for (i=0; i < CONSTRUCT_RANDOM_COUNT; i++){
|
2005-12-15 04:09:09 +00:00
|
|
|
const char * rule = test_rule.next();
|
2005-07-28 20:25:44 +00:00
|
|
|
logln("\n-----------------------------------%d\n",i);
|
|
|
|
logln(UnicodeString(rule, strlen(rule)));
|
|
|
|
|
|
|
|
UnicodeString newRule(rule); // potential bug
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2005-12-15 04:09:09 +00:00
|
|
|
logln( "===========================================\n");
|
|
|
|
fwrite(rule, strlen(rule),1,stdout);
|
|
|
|
logln("\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
|
|
|
|
|
2005-07-28 20:25:44 +00:00
|
|
|
Collator * c = new RuleBasedCollator(newRule,status);
|
|
|
|
|
|
|
|
if (U_FAILURE(status)) {
|
2005-12-15 04:09:09 +00:00
|
|
|
errln( "Could not create Collator for the %d(th) generated rule.\n"
|
|
|
|
"Error Name: %s\n"
|
|
|
|
"The rule is ",
|
|
|
|
i, u_errorName(status));
|
2005-07-28 20:25:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-02 17:55:57 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_COLLATION */
|
2005-07-28 20:25:44 +00:00
|
|
|
|