]>
Commit | Line | Data |
---|---|---|
73c04bcf A |
1 | /* |
2 | ****************************************************************************** | |
3 | * Copyright (C) 2005-2006, International Business Machines Corporation and * | |
4 | * others. All Rights Reserved. * | |
5 | ****************************************************************************** | |
6 | */ | |
7 | #include <stdio.h> | |
8 | #include <string.h> | |
9 | #include <stdlib.h> | |
10 | #include <time.h> | |
11 | #include "rndmcoll.h" | |
12 | #include "wbnf.h" | |
13 | ||
14 | ||
15 | #if !UCONFIG_NO_COLLATION | |
16 | ||
17 | //Raymond: Following comments are copied from Java implementation | |
18 | // | |
19 | // each rule can be: | |
20 | // "[" command "]" | |
21 | // "& [" position "]" | |
22 | // "&" before chars | |
23 | // relation "[variable top]" | |
24 | // relation (chars "|")? chars ("/" chars)? | |
25 | // plus, a reset must come before a relation | |
26 | ||
27 | ||
28 | static const char collationBNF[] = | |
29 | "$s = ' '? 50%;" | |
30 | "$crlf = '\r\n';" | |
31 | ||
32 | "$alternateOptions = non'-'ignorable | shifted;" | |
33 | "$onoff = on | off;" | |
34 | "$caseFirstOptions = off | upper | lower;" | |
35 | "$strengthOptions = '1' | '2' | '3' | '4' | 'I';" | |
36 | "$commandList = '['" | |
37 | " ( alternate ' ' $alternateOptions" | |
38 | " | backwards' 2'" | |
39 | " | normalization ' ' $onoff " | |
40 | " | caseLevel ' ' $onoff " | |
41 | " | hiraganaQ ' ' $onoff" | |
42 | " | caseFirst ' ' $caseFirstOptions" | |
43 | " | strength ' ' $strengthOptions" | |
44 | " ) ']';" | |
45 | "$command = $commandList $crlf;" | |
46 | ||
47 | "$ignorableTypes = (tertiary | secondary | primary) ' ' ignorable;" | |
48 | "$allTypes = variable | regular | implicit | trailing | $ignorableTypes;" | |
49 | "$positionList = '[' (first | last) ' ' $allTypes ']';" | |
50 | ||
51 | "$beforeList = '[before ' ('1' | '2' | '3') ']';" | |
52 | ||
53 | "$relationList = (" | |
54 | " '<'" | |
55 | " | '<<'" | |
56 | " | ';'" | |
57 | " | '<<<'" | |
58 | " | ','" | |
59 | " | '='" | |
60 | ");" | |
61 | "$string = $chars{1,5}~@;" | |
62 | "$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| '<'| '&'| '['| ']';" | |
63 | "$rel1 = '[variable top]' $s;" | |
64 | "$p1 = ($string $s '|' $s)? 25%;" | |
65 | "$p2 = ('/' $s $string $s)? 25%;" | |
66 | "$rel2 = $p1 $string $s $p2;" | |
67 | "$relation = $relationList $s ($rel1 | $rel2) $crlf;" | |
68 | ||
69 | "$reset = '&' $s ($beforeList $s)? 10% ($positionList 1% | $string 10%) $crlf;" | |
70 | "$mostRules = $command 1% | $reset 5% | $relation 25%;" | |
71 | "$root = $command{0,5} $reset $mostRules{1,20};"; | |
72 | ||
73 | ||
74 | static const UVersionInfo ICU_37 = {3,7,0,0}; | |
75 | ||
76 | void RandomCollatorTest::Test2(){ | |
77 | if(!isICUVersionAtLeast(ICU_37)){ | |
78 | return; | |
79 | } | |
80 | else { | |
81 | errln("TestWbnf is incorrectly implemented.\nThis test should be modeled to use the existing test frame work for naming tests.\n"); | |
82 | } | |
83 | TestWbnf(); | |
84 | } | |
85 | ||
86 | ||
87 | void RandomCollatorTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* ){ | |
88 | if (exec) logln("TestSuite RandomCollatorTest: "); | |
89 | switch (index) { | |
90 | TESTCASE(0, Test); | |
91 | TESTCASE(1, Test2); | |
92 | default: name = ""; break; | |
93 | } | |
94 | } | |
95 | ||
96 | /* | |
97 | class TestColltorCompare{ | |
98 | public: | |
99 | UBool operator()(Collator &coll, int count = 1000){ | |
100 | UnicodeString a(test_string.get_a_string()); | |
101 | UnicodeString b(test_string.get_a_string()); | |
102 | UnicodeString c(test_string.get_a_string()); | |
103 | do{ | |
104 | if (check_transitivity(coll, a, b, c)){ | |
105 | a = b; | |
106 | b = c; | |
107 | c = UnicodeString(test_string.get_a_string()); | |
108 | } | |
109 | }while(count-- >= 0 ); | |
110 | ||
111 | return FALSE; | |
112 | } | |
113 | TestColltorCompare():test_string("$s = $c{1,8};", "$s", "$c", new Magic_SelectOneChar("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ[]&<")){ | |
114 | } | |
115 | private: | |
116 | UBool check_transitivity(const Collator & coll, const UnicodeString &a, const UnicodeString &b, const UnicodeString &c){ | |
117 | int ab = coll.compare(a,b), ba = coll.compare(b,a); | |
118 | int bc = coll.compare(b,c), cb = coll.compare(c,b); | |
119 | int ca = coll.compare(c,a), ac = coll.compare(a,c); | |
120 | // a | |
121 | // / \ | |
122 | // b - c | |
123 | // | |
124 | if (//counter-clockwise, maximum | |
125 | (ab >=0 && bc >=0 && ac <0) | |
126 | ||(bc >=0 && ca >=0 && ba <0) | |
127 | ||(ca >=0 && ab >=0 && cb <0) | |
128 | ||
129 | //counter-clockwise, minimum | |
130 | ||(ab <=0 && bc <=0 && ca >0) | |
131 | ||(bc <=0 && ca <=0 && ba >0) | |
132 | ||(ca <=0 && ab <=0 && cb >0) | |
133 | ){ | |
134 | return FALSE; | |
135 | } | |
136 | return TRUE; | |
137 | } | |
138 | ||
139 | LanguageGenerator test_string; | |
140 | };*/ | |
141 | ||
142 | void RandomCollatorTest::Test(){ | |
143 | if(!isICUVersionAtLeast(ICU_37)){ | |
144 | return; | |
145 | } | |
146 | else { | |
147 | errln("This test needs to be fixed.\n"); | |
148 | } | |
149 | LanguageGenerator test_rule; | |
150 | if (test_rule.parseBNF(collationBNF, "$root", TRUE) != LanguageGenerator::OK){ | |
151 | errln("The test code itself is wrong."); | |
152 | return; | |
153 | }; | |
154 | ||
155 | //TestColltorCompare coll_test; | |
156 | ||
157 | static const int CONSTRUCT_RANDOM_COUNT = 1000; | |
158 | int i; | |
159 | for (i=0; i < CONSTRUCT_RANDOM_COUNT; i++){ | |
160 | const char * rule = test_rule.next(); | |
161 | logln("\n-----------------------------------%d\n",i); | |
162 | logln(UnicodeString(rule, strlen(rule))); | |
163 | ||
164 | UnicodeString newRule(rule); // potential bug | |
165 | UErrorCode status = U_ZERO_ERROR; | |
166 | logln( "===========================================\n"); | |
167 | fwrite(rule, strlen(rule),1,stdout); | |
168 | logln("\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"); | |
169 | ||
170 | Collator * c = new RuleBasedCollator(newRule,status); | |
171 | ||
172 | if (U_FAILURE(status)) { | |
173 | errln( "Could not create Collator for the %d(th) generated rule.\n" | |
174 | "Error Name: %s\n" | |
175 | "The rule is ", | |
176 | i, u_errorName(status)); | |
177 | return; | |
178 | } | |
179 | ||
180 | delete c; | |
181 | } | |
182 | } | |
183 | ||
184 | #endif /* #if !UCONFIG_NO_COLLATION */ | |
185 |