summaryrefslogtreecommitdiff
path: root/Build/source/libs/icu/icu-4.4/test/intltest/mnkytst.cpp
blob: 51aa8113c54df699f91fd17e2caaeefc848bbbba (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/********************************************************************
 * COPYRIGHT: 
 * Copyright (c) 1997-2009, International Business Machines Corporation and
 * others. All Rights Reserved.
 ********************************************************************/

#include "unicode/utypes.h"

#if !UCONFIG_NO_COLLATION

#include "unicode/coll.h"
#include "unicode/tblcoll.h"
#include "unicode/unistr.h"
#include "unicode/sortkey.h"
#include "mnkytst.h"
#include "sfwdchit.h"

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#ifndef MIN
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#endif

#ifndef MAX
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#endif

CollationMonkeyTest::CollationMonkeyTest()
: source("-abcdefghijklmnopqrstuvwxyz#&^$@", ""),
  myCollator(0)
{
    UErrorCode status = U_ZERO_ERROR;
    myCollator = Collator::createInstance("en_US", status);
}

CollationMonkeyTest::~CollationMonkeyTest()
{
    delete myCollator;
}


void 
CollationMonkeyTest::report(UnicodeString& s, UnicodeString& t, int32_t result, int32_t revResult)
{
    if (revResult != -result)
    {
        UnicodeString msg;

        msg += s; 
        msg += " and ";
        msg += t;
        msg += " round trip comparison failed";
        msg += (UnicodeString) " (result " + result + ", reverse Result " + revResult + ")"; 

        errln(msg);
    }
}

int32_t 
CollationMonkeyTest::checkValue(int32_t value)
{
    if (value < 0)
    {
        return -value;
    }

    return value;
}

void CollationMonkeyTest::TestCollationKey(/* char* par */)
{
    if(source.length() == 0) {
        errln(UNICODE_STRING("CollationMonkeyTest::TestCollationKey(): source is empty - ICU_DATA not set or data missing?", 92));
        return;
    }

    srand( (unsigned)time( NULL ) );
    int32_t s = checkValue(rand() % source.length());
    int32_t t = checkValue(rand() % source.length());
    int32_t slen = checkValue((rand() - source.length()) % source.length());
    int32_t tlen = checkValue((rand() - source.length()) % source.length());
    UnicodeString subs, subt;

    source.extract(MIN(s, slen), MAX(s, slen), subs);
    source.extract(MIN(t, tlen), MAX(t, tlen), subt);

    CollationKey collationKey1, collationKey2;
    UErrorCode status1 = U_ZERO_ERROR, status2= U_ZERO_ERROR;

    myCollator->setStrength(Collator::TERTIARY);
    myCollator->getCollationKey(subs, collationKey1, status1);
    myCollator->getCollationKey(subt, collationKey2, status2);
    int32_t result = collationKey1.compareTo(collationKey2);  // Tertiary
    int32_t revResult = collationKey2.compareTo(collationKey1);  // Tertiary
    report( subs, subt, result, revResult);

    myCollator->setStrength(Collator::SECONDARY);
    myCollator->getCollationKey(subs, collationKey1, status1);
    myCollator->getCollationKey(subt, collationKey2, status2);
    result = collationKey1.compareTo(collationKey2);  // Secondary
    revResult = collationKey2.compareTo(collationKey1);   // Secondary
    report( subs, subt, result, revResult);

    myCollator->setStrength(Collator::PRIMARY);
    myCollator->getCollationKey(subs, collationKey1, status1);
    myCollator->getCollationKey(subt, collationKey2, status2);
    result = collationKey1.compareTo(collationKey2);  // Primary
    revResult = collationKey2.compareTo(collationKey1);   // Primary
    report(subs, subt, result, revResult);

    UnicodeString msg;
    UnicodeString addOne(subs);
    addOne += (UChar32)0xE000;

    myCollator->getCollationKey(subs, collationKey1, status1);
    myCollator->getCollationKey(addOne, collationKey2, status2);
    result = collationKey1.compareTo(collationKey2);
    if (result != -1)
    {
        msg += "CollationKey(";
        msg += subs;
        msg += ") .LT. CollationKey(";
        msg += addOne;
        msg += ") Failed.";
        errln(msg);
    }

    msg.remove();
    result = collationKey2.compareTo(collationKey1);
    if (result != 1)
    {
        msg += "CollationKey(";
        msg += addOne;
        msg += ") .GT. CollationKey(";
        msg += subs;
        msg += ") Failed.";
        errln(msg);
    }
}

void 
CollationMonkeyTest::TestCompare(/* char* par */)
{
    if(source.length() == 0) {
        errln(UNICODE_STRING("CollationMonkeyTest::TestCompare(): source is empty - ICU_DATA not set or data missing?", 87));
        return;
    }

    /* Seed the random-number generator with current time so that
     * the numbers will be different every time we run.
     */
    srand( (unsigned)time( NULL ) );
    int32_t s = checkValue(rand() % source.length());
    int32_t t = checkValue(rand() % source.length());
    int32_t slen = checkValue((rand() - source.length()) % source.length());
    int32_t tlen = checkValue((rand() - source.length()) % source.length());
    UnicodeString subs, subt;

    source.extract(MIN(s, slen), MAX(s, slen), subs);
    source.extract(MIN(t, tlen), MAX(t, tlen), subt);

    myCollator->setStrength(Collator::TERTIARY);
    int32_t result = myCollator->compare(subs, subt);  // Tertiary
    int32_t revResult = myCollator->compare(subt, subs);  // Tertiary
    report(subs, subt, result, revResult);

    myCollator->setStrength(Collator::SECONDARY);
    result = myCollator->compare(subs, subt);  // Secondary
    revResult = myCollator->compare(subt, subs);  // Secondary
    report(subs, subt, result, revResult);

    myCollator->setStrength(Collator::PRIMARY);
    result = myCollator->compare(subs, subt);  // Primary
    revResult = myCollator->compare(subt, subs);  // Primary
    report(subs, subt, result, revResult);

    UnicodeString msg;
    UnicodeString addOne(subs);
    addOne += (UChar32)0xE000;

    result = myCollator->compare(subs, addOne);
    if (result != -1)
    {
        msg += "Test : ";
        msg += subs;
        msg += " .LT. ";
        msg += addOne;
        msg += " Failed.";
        errln(msg);
    }

    msg.remove();
    result = myCollator->compare(addOne, subs);
    if (result != 1)
    {
        msg += "Test : ";
        msg += addOne;
        msg += " .GT. ";
        msg += subs;
        msg += " Failed.";
        errln(msg);
    }
}
void CollationMonkeyTest::TestRules(/* char* par */){
    UChar testSourceCases[][10] = {
    {0x0061, 0x0062, 0x007a, 0},
    {0x0061, 0x0062, 0x007a, 0},
    };

    UChar testTargetCases[][10] = {
        {0x0061, 0x0062, 0x00e4, 0},
        {0x0061, 0x0062, 0x0061, 0x0308, 0},
    };
    
    int i=0;
    logln("Demo Test 1 : Create a new table collation with rules \"& z < 0x00e4\"");
    UErrorCode status = U_ZERO_ERROR;
    Collator *col = Collator::createInstance("en_US", status);
    const UnicodeString baseRules = ((RuleBasedCollator*)col)->getRules();
    UnicodeString newRules(" & z < ");
    newRules.append((UChar)0x00e4);
    newRules.insert(0, baseRules);
    RuleBasedCollator *myCollation = new RuleBasedCollator(newRules, status);
    if (U_FAILURE(status)) {
        errln( "Demo Test 1 Table Collation object creation failed.");
        return;
    }
    for(i=0; i<2; i++){
        doTest(myCollation, testSourceCases[i], testTargetCases[i], Collator::LESS);
    }
    delete myCollation;

    logln("Demo Test 2 : Create a new table collation with rules \"& z < a 0x0308\"");
    newRules.remove();
    newRules.append(" & z < a");
    newRules.append((UChar)0x0308);
    newRules.insert(0, baseRules);
    myCollation = new RuleBasedCollator(newRules, status);
    if (U_FAILURE(status)) {
        errln( "Demo Test 1 Table Collation object creation failed.");
        return;
    }
    for(i=0; i<2; i++){
        doTest(myCollation, testSourceCases[i], testTargetCases[i], Collator::LESS);
    }
    delete myCollation;
    delete col;

}

void CollationMonkeyTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
{
    if (exec) logln("TestSuite CollationMonkeyTest: ");
    if(myCollator) {
      switch (index) {
          case 0: name = "TestCompare";   if (exec)   TestCompare(/* par */); break;
          case 1: name = "TestCollationKey"; if (exec)    TestCollationKey(/* par */); break;
          case 2: name = "TestRules"; if (exec) TestRules(/* par */); break;
          default: name = ""; break;
      }
    } else {
      dataerrln("Class collator not instantiated");
      name = "";
    }
}

#endif /* #if !UCONFIG_NO_COLLATION */