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