1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 1997-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
9 #include "unicode/utypes.h"
11 #if !UCONFIG_NO_COLLATION
13 #include "unicode/coll.h"
14 #include "unicode/tblcoll.h"
15 #include "unicode/unistr.h"
16 #include "unicode/sortkey.h"
20 #include "../cintltst/calldata.h"
23 CollationDummyTest::CollationDummyTest()
26 /*UErrorCode status = U_ZERO_ERROR;
27 UnicodeString rules(TRUE, DEFAULTRULEARRAY, UPRV_LENGTHOF(DEFAULTRULEARRAY));
28 UnicodeString newRules("& C < ch, cH, Ch, CH & Five, 5 & Four, 4 & one, 1 & Ampersand; '&' & Two, 2 ");
30 myCollation = new RuleBasedCollator(rules, status);
33 UErrorCode status
= U_ZERO_ERROR
;
34 UnicodeString
ruleset("& C < ch, cH, Ch, CH & Five, 5 & Four, 4 & one, 1 & Ampersand; '&' & Two, 2 ");
35 if (myCollation
!= NULL
)
39 myCollation
= new RuleBasedCollator(ruleset
, status
);
40 if(U_FAILURE(status
)){
41 errcheckln(status
, "ERROR: in creation of rule based collator from ruleset - %s", u_errorName(status
));
47 CollationDummyTest::~CollationDummyTest()
52 const Collator::EComparisonResult
CollationDummyTest::results
[] = {
54 Collator::LESS
, /*Collator::GREATER,*/
62 Collator::LESS
, /* 10 */
70 /* test primary > 17 */
73 Collator::EQUAL
, /* 20 */
80 /* test secondary > 26 */
85 Collator::EQUAL
, /* 30 */
88 Collator::EQUAL
, /* 34 */
95 void CollationDummyTest::TestTertiary(/* char* par */)
98 myCollation
->setStrength(Collator::TERTIARY
);
99 for (i
= 0; i
< 17 ; i
++)
101 doTest(myCollation
, testSourceCases
[i
], testTargetCases
[i
], results
[i
]);
104 void CollationDummyTest::TestPrimary(/* char* par */)
106 /* problem in strcollinc for unfinshed contractions */
107 UErrorCode status
= U_ZERO_ERROR
;
109 myCollation
->setAttribute(UCOL_NORMALIZATION_MODE
, UCOL_ON
, status
);
110 myCollation
->setStrength(Collator::PRIMARY
);
112 if (U_FAILURE(status
))
114 errln("Failure in setting attribute for normalization mode\n");
117 for (int i
= 17; i
< 26 ; i
++)
119 doTest(myCollation
, testSourceCases
[i
], testTargetCases
[i
], results
[i
]);
123 void CollationDummyTest::TestSecondary(/* char* par */)
126 myCollation
->setStrength(Collator::SECONDARY
);
127 for (i
= 26; i
< 34; i
++)
129 doTest(myCollation
, testSourceCases
[i
], testTargetCases
[i
], results
[i
]);
133 void CollationDummyTest::TestExtra(/* char* par */)
136 myCollation
->setStrength(Collator::TERTIARY
);
137 for (i
= 0; i
< COUNT_TEST_CASES
-1; i
++)
139 for (j
= i
+ 1; j
< COUNT_TEST_CASES
; j
+= 1)
141 doTest(myCollation
, testCases
[i
], testCases
[j
], Collator::LESS
);
146 void CollationDummyTest::TestIdentical()
149 myCollation
->setStrength(Collator::IDENTICAL
);
150 for (i
= 34; i
<37; i
++)
152 doTest(myCollation
, testSourceCases
[i
], testTargetCases
[i
], results
[i
]);
156 void CollationDummyTest::TestJB581(void)
158 UErrorCode status
= U_ZERO_ERROR
;
160 UnicodeString
source("THISISATEST.");
161 UnicodeString
target("Thisisatest.");
163 Collator
*coll
= Collator::createInstance("en_US", status
);
164 if (U_FAILURE(status
)){
165 errln("ERROR: Failed to create the collator for : en_US\n");
169 Collator::EComparisonResult result
= coll
->compare(source
, target
);
170 /* result is 1, secondary differences only for ignorable space characters*/
173 errln("Comparing two strings with only secondary differences in C failed.\n");
175 /* To compare them with just primary differences */
176 coll
->setStrength(Collator::PRIMARY
);
177 result
= coll
->compare(source
, target
);
181 errln("Comparing two strings with no differences in C failed.\n");
183 /* Now, do the same comparison with keys */
184 CollationKey sourceKeyOut
,
186 coll
->getCollationKey(source
, sourceKeyOut
, status
);
187 coll
->getCollationKey(target
, targetKeyOut
, status
);
188 result
= sourceKeyOut
.compareTo(targetKeyOut
);
191 errln("Comparing two strings with sort keys in C failed.\n");
196 void CollationDummyTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
198 if (exec
) logln("TestSuite CollationDummyTest: ");
201 case 0: name
= "TestPrimary"; if (exec
) TestPrimary(/* par */); break;
202 case 1: name
= "TestSecondary"; if (exec
) TestSecondary(/* par */); break;
203 case 2: name
= "TestTertiary"; if (exec
) TestTertiary(/* par */); break;
204 case 3: name
= "TestExtra"; if (exec
) TestExtra(/* par */); break;
205 case 4: name
= "TestIdentical"; if (exec
) TestIdentical(/* par */); break;
206 case 5: name
= "TestJB581"; if (exec
) TestJB581(/* par */); break;
207 default: name
= ""; break;
210 dataerrln("Collator couldn't be instantiated!");
215 #endif /* #if !UCONFIG_NO_COLLATION */