1/********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2009, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6
7#include "unicode/utypes.h"
8
9#if !UCONFIG_NO_COLLATION
10
11#include "unicode/coll.h"
12#include "unicode/tblcoll.h"
13#include "unicode/unistr.h"
14#include "unicode/sortkey.h"
15#include "allcoll.h"
16#include "sfwdchit.h"
17#include "../cintltst/calldata.h"
18
19
20CollationDummyTest::CollationDummyTest()
21: myCollation(0)
22{
23    /*UErrorCode status = U_ZERO_ERROR;
24    UnicodeString rules(TRUE, DEFAULTRULEARRAY, sizeof(DEFAULTRULEARRAY)/sizeof(DEFAULTRULEARRAY[0]));
25    UnicodeString newRules("& C < ch, cH, Ch, CH & Five, 5 & Four, 4 & one, 1 & Ampersand; '&' & Two, 2 ");
26    rules += newRules;
27    myCollation = new RuleBasedCollator(rules, status);
28    */
29
30    UErrorCode status = U_ZERO_ERROR;
31    UnicodeString ruleset("& C < ch, cH, Ch, CH & Five, 5 & Four, 4 & one, 1 & Ampersand; '&' & Two, 2 ");
32    if (myCollation != NULL)
33    {
34      delete myCollation;
35    }
36    myCollation = new RuleBasedCollator(ruleset, status);
37    if(U_FAILURE(status)){
38        errcheckln(status, "ERROR: in creation of rule based collator from ruleset - %s", u_errorName(status));
39        delete myCollation;
40        myCollation = 0;
41    }
42}
43
44CollationDummyTest::~CollationDummyTest()
45{
46    delete myCollation;
47}
48
49const Collator::EComparisonResult CollationDummyTest::results[] = {
50    Collator::LESS,
51    Collator::LESS, /*Collator::GREATER,*/
52    Collator::LESS,
53    Collator::LESS,
54    Collator::LESS,
55    Collator::LESS,
56    Collator::LESS,
57    Collator::GREATER,
58    Collator::GREATER,
59    Collator::LESS,                                     /*  10 */
60    Collator::GREATER,
61    Collator::LESS,
62    Collator::GREATER,
63    Collator::GREATER,
64    Collator::LESS,
65    Collator::LESS,
66    Collator::LESS,
67    /*  test primary > 17 */
68    Collator::EQUAL,
69    Collator::EQUAL,
70    Collator::EQUAL,                                    /*  20 */
71    Collator::LESS,
72    Collator::LESS,
73    Collator::EQUAL,
74    Collator::EQUAL,
75    Collator::EQUAL,
76    Collator::LESS,
77    /*  test secondary > 26 */
78    Collator::EQUAL,
79    Collator::EQUAL,
80    Collator::EQUAL,
81    Collator::EQUAL,
82    Collator::EQUAL,                                    /*  30 */
83    Collator::EQUAL,
84    Collator::LESS,
85    Collator::EQUAL,                                     /*  34 */
86    Collator::EQUAL,
87    Collator::EQUAL,
88    Collator::LESS
89};
90
91
92void CollationDummyTest::TestTertiary(/* char* par */)
93{
94    int32_t i = 0;
95    myCollation->setStrength(Collator::TERTIARY);
96    for (i = 0; i < 17 ; i++)
97    {
98        doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
99    }
100}
101void CollationDummyTest::TestPrimary(/* char* par */)
102{
103    /* problem in strcollinc for unfinshed contractions */
104    UErrorCode status = U_ZERO_ERROR;
105
106    myCollation->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, status);
107    myCollation->setStrength(Collator::PRIMARY);
108
109    if (U_FAILURE(status))
110    {
111      errln("Failure in setting attribute for normalization mode\n");
112    }
113
114    for (int i = 17; i < 26 ; i++)
115    {
116        doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
117    }
118}
119
120void CollationDummyTest::TestSecondary(/* char* par */)
121{
122    int32_t i;
123    myCollation->setStrength(Collator::SECONDARY);
124    for (i = 26; i < 34; i++)
125    {
126        doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
127    }
128}
129
130void CollationDummyTest::TestExtra(/* char* par */)
131{
132    int32_t i, j;
133    myCollation->setStrength(Collator::TERTIARY);
134    for (i = 0; i < COUNT_TEST_CASES-1; i++)
135    {
136        for (j = i + 1; j < COUNT_TEST_CASES; j += 1)
137        {
138            doTest(myCollation, testCases[i], testCases[j], Collator::LESS);
139        }
140    }
141}
142
143void CollationDummyTest::TestIdentical()
144{
145    int32_t i;
146    myCollation->setStrength(Collator::IDENTICAL);
147    for (i= 34; i<37; i++)
148    {
149        doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
150    }
151}
152
153void CollationDummyTest::TestJB581(void)
154{
155    UErrorCode status = U_ZERO_ERROR;
156
157    UnicodeString source("THISISATEST.");
158    UnicodeString target("Thisisatest.");
159
160    Collator *coll = Collator::createInstance("en_US", status);
161    if (U_FAILURE(status)){
162        errln("ERROR: Failed to create the collator for : en_US\n");
163        return;
164    }
165
166    Collator::EComparisonResult result = coll->compare(source, target);
167    /* result is 1, secondary differences only for ignorable space characters*/
168    if (result != 1)
169    {
170        errln("Comparing two strings with only secondary differences in C failed.\n");
171    }
172    /* To compare them with just primary differences */
173    coll->setStrength(Collator::PRIMARY);
174    result = coll->compare(source, target);
175    /* result is 0 */
176    if (result != 0)
177    {
178        errln("Comparing two strings with no differences in C failed.\n");
179    }
180    /* Now, do the same comparison with keys */
181    CollationKey sourceKeyOut,
182      targetKeyOut;
183    coll->getCollationKey(source, sourceKeyOut, status);
184    coll->getCollationKey(target, targetKeyOut, status);
185    result = sourceKeyOut.compareTo(targetKeyOut);
186    if (result != 0)
187    {
188        errln("Comparing two strings with sort keys in C failed.\n");
189    }
190    delete coll;
191}
192
193void CollationDummyTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
194{
195    if (exec) logln("TestSuite CollationDummyTest: ");
196    if(myCollation) {
197      switch (index) {
198          case 0: name = "TestPrimary";   if (exec)   TestPrimary(/* par */); break;
199          case 1: name = "TestSecondary"; if (exec)   TestSecondary(/* par */); break;
200          case 2: name = "TestTertiary";  if (exec)   TestTertiary(/* par */); break;
201          case 3: name = "TestExtra";     if (exec)   TestExtra(/* par */); break;
202          case 4: name = "TestIdentical"; if (exec)   TestIdentical(/* par */); break;
203          case 5: name = "TestJB581";     if (exec)   TestJB581(/* par */); break;
204          default: name = ""; break;
205      }
206    } else {
207      dataerrln("Collator couldn't be instantiated!");
208      name = "";
209    }
210}
211
212#endif /* #if !UCONFIG_NO_COLLATION */
213