summaryrefslogtreecommitdiff
path: root/Build/source/libs/icu/icu-4.4/test/intltest/rndmcoll.cpp
blob: be15570c67b313ea3fddb8b72dc2799150083ccc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 ******************************************************************************
 * Copyright (C) 2005-2007, International Business Machines Corporation and   *
 * others. All Rights Reserved.                                               *
 ******************************************************************************
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "rndmcoll.h"
#include "wbnf.h"


#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


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 = ("
    "   '<'"
    " | '<<'"
    " | ';'" 
    " | '<<<'"
    " | ','" 
    " | '='"
    ");"
    "$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| '<'| '&'| '['| ']';"
    "$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};";


void RandomCollatorTest::Test2(){
    // See ticket 5747 about reenabling this test.
    errln("TestWbnf is incorrectly implemented.\nThis test should be modeled to use the existing test frame work for naming tests.\n");
    TestWbnf();
}


void RandomCollatorTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* ){
    if (exec) logln("TestSuite RandomCollatorTest: ");
    switch (index) {
        TESTCASE(0, Test);
        TESTCASE(1, Test2);
        default: name = ""; break;
    }
}

/*
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;
};*/

void RandomCollatorTest::Test(){
    // See ticket 5747 about reenabling this test.
    errln("This test needs to be fixed.\n");

    LanguageGenerator test_rule;
    if (test_rule.parseBNF(collationBNF, "$root", TRUE) != LanguageGenerator::OK){
        errln("The test code itself is wrong.");
        return;
    };

    //TestColltorCompare coll_test;

    static const int CONSTRUCT_RANDOM_COUNT = 1000;
    int i;
    for (i=0; i < CONSTRUCT_RANDOM_COUNT; i++){
        const char * rule = test_rule.next();
        logln("\n-----------------------------------%d\n",i);
        logln(UnicodeString(rule, strlen(rule)));

        UnicodeString newRule(rule);    // potential bug
        UErrorCode status = U_ZERO_ERROR;
        logln(   "===========================================\n");
        fwrite(rule, strlen(rule),1,stdout);
        logln("\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");

        Collator * c = new RuleBasedCollator(newRule,status);

        if (U_FAILURE(status)) {
            errln( "Could not create Collator for the %d(th) generated rule.\n"
                   "Error Name: %s\n"
                   "The rule is ",
                   i, u_errorName(status));
            return;
        }

        delete c;
    }
}

#endif /* #if !UCONFIG_NO_COLLATION */