1 // Copyright (C) 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-2009, 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 "jacoll.h" 18 19 #include "sfwdchit.h" 20 21 CollationKanaTest::CollationKanaTest() 22 : myCollation(0) 23 { 24 UErrorCode status = U_ZERO_ERROR; 25 myCollation = Collator::createInstance(Locale::getJapan(), status); 26 if(!myCollation || U_FAILURE(status)) { 27 errcheckln(status, __FILE__ "failed to create! err " + UnicodeString(u_errorName(status))); 28 /* if it wasn't already: */ 29 delete myCollation; 30 myCollation = NULL; 31 return; 32 } 33 } 34 35 CollationKanaTest::~CollationKanaTest() 36 { 37 delete myCollation; 38 } 39 40 const UChar CollationKanaTest::testSourceCases[][CollationKanaTest::MAX_TOKEN_LEN] = { 41 {0xff9E, 0x0000}, 42 {0x3042, 0x0000}, 43 {0x30A2, 0x0000}, 44 {0x3042, 0x3042, 0x0000}, 45 {0x30A2, 0x30FC, 0x0000}, 46 {0x30A2, 0x30FC, 0x30C8, 0x0000} /* 6 */ 47 }; 48 49 const UChar CollationKanaTest::testTargetCases[][CollationKanaTest::MAX_TOKEN_LEN] = { 50 {0xFF9F, 0x0000}, 51 {0x30A2, 0x0000}, 52 {0x3042, 0x3042, 0x0000}, 53 {0x30A2, 0x30FC, 0x0000}, 54 {0x30A2, 0x30FC, 0x30C8, 0x0000}, 55 {0x3042, 0x3042, 0x3068, 0x0000} /* 6 */ 56 }; 57 58 const Collator::EComparisonResult CollationKanaTest::results[] = { 59 Collator::LESS, 60 Collator::EQUAL, //Collator::LESS, /* Katakanas and Hiraganas are equal on tertiary level(ICU 2.0)*/ 61 Collator::LESS, 62 Collator::GREATER, // Collator::LESS, /* Prolonged sound mark sorts BEFORE equivalent vowel (ICU 2.0)*/ 63 Collator::LESS, 64 Collator::LESS, //Collator::GREATER /* Prolonged sound mark sorts BEFORE equivalent vowel (ICU 2.0)*//* 6 */ 65 }; 66 67 const UChar CollationKanaTest::testBaseCases[][CollationKanaTest::MAX_TOKEN_LEN] = { 68 {0x30AB, 0x0000}, 69 {0x30AB, 0x30AD, 0x0000}, 70 {0x30AD, 0x0000}, 71 {0x30AD, 0x30AD, 0x0000} 72 }; 73 74 const UChar CollationKanaTest::testPlainDakutenHandakutenCases[][CollationKanaTest::MAX_TOKEN_LEN] = { 75 {0x30CF, 0x30AB, 0x0000}, 76 {0x30D0, 0x30AB, 0x0000}, 77 {0x30CF, 0x30AD, 0x0000}, 78 {0x30D0, 0x30AD, 0x0000} 79 }; 80 81 const UChar CollationKanaTest::testSmallLargeCases[][CollationKanaTest::MAX_TOKEN_LEN] = { 82 {0x30C3, 0x30CF, 0x0000}, 83 {0x30C4, 0x30CF, 0x0000}, 84 {0x30C3, 0x30D0, 0x0000}, 85 {0x30C4, 0x30D0, 0x0000} 86 }; 87 88 const UChar CollationKanaTest::testKatakanaHiraganaCases[][CollationKanaTest::MAX_TOKEN_LEN] = { 89 {0x3042, 0x30C3, 0x0000}, 90 {0x30A2, 0x30C3, 0x0000}, 91 {0x3042, 0x30C4, 0x0000}, 92 {0x30A2, 0x30C4, 0x0000} 93 }; 94 95 const UChar CollationKanaTest::testChooonKigooCases[][CollationKanaTest::MAX_TOKEN_LEN] = { 96 /*0*/ {0x30AB, 0x30FC, 0x3042, 0x0000}, 97 /*1*/ {0x30AB, 0x30FC, 0x30A2, 0x0000}, 98 /*2*/ {0x30AB, 0x30A4, 0x3042, 0x0000}, 99 /*3*/ {0x30AB, 0x30A4, 0x30A2, 0x0000}, 100 /*6*/ {0x30AD, 0x30FC, 0x3042, 0x0000}, /* Prolonged sound mark sorts BEFORE equivalent vowel (ICU 2.0)*/ 101 /*7*/ {0x30AD, 0x30FC, 0x30A2, 0x0000}, /* Prolonged sound mark sorts BEFORE equivalent vowel (ICU 2.0)*/ 102 /*4*/ {0x30AD, 0x30A4, 0x3042, 0x0000}, 103 /*5*/ {0x30AD, 0x30A4, 0x30A2, 0x0000}, 104 }; 105 106 void CollationKanaTest::TestTertiary(/* char* par */) 107 { 108 int32_t i = 0; 109 UErrorCode status = U_ZERO_ERROR; 110 myCollation->setStrength(Collator::TERTIARY); 111 /* for one case, strcollinc fails, since it doesn't have good handling of contractions*/ 112 /* normalization is turned off to stop strcollinc from executing */ 113 myCollation->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, status); 114 myCollation->setAttribute(UCOL_CASE_LEVEL, UCOL_ON, status); 115 for (i = 0; i < 6; i++) { 116 doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]); 117 } 118 } 119 120 /* Testing base letters */ 121 void CollationKanaTest::TestBase() 122 { 123 int32_t i; 124 myCollation->setStrength(Collator::PRIMARY); 125 for (i = 0; i < 3 ; i++) 126 doTest(myCollation, testBaseCases[i], testBaseCases[i + 1], Collator::LESS); 127 } 128 129 /* Testing plain, Daku-ten, Handaku-ten letters */ 130 void CollationKanaTest::TestPlainDakutenHandakuten(void) 131 { 132 int32_t i; 133 myCollation->setStrength(Collator::SECONDARY); 134 for (i = 0; i < 3 ; i++) 135 doTest(myCollation, testPlainDakutenHandakutenCases[i], testPlainDakutenHandakutenCases[i + 1], 136 Collator::LESS); 137 } 138 139 /* 140 * Test Small, Large letters 141 */ 142 void CollationKanaTest::TestSmallLarge(void) 143 { 144 int32_t i; 145 UErrorCode status = U_ZERO_ERROR; 146 myCollation->setStrength(Collator::TERTIARY); 147 myCollation->setAttribute(UCOL_CASE_LEVEL, UCOL_ON, status); 148 for (i = 0; i < 3 ; i++) 149 doTest(myCollation, testSmallLargeCases[i], testSmallLargeCases[i + 1], Collator::LESS); 150 } 151 152 /* 153 * Test Katakana, Hiragana letters 154 */ 155 void CollationKanaTest::TestKatakanaHiragana(void) 156 { 157 int32_t i; 158 UErrorCode status = U_ZERO_ERROR; 159 myCollation->setStrength(Collator::QUATERNARY); 160 myCollation->setAttribute(UCOL_CASE_LEVEL, UCOL_ON, status); 161 for (i = 0; i < 3 ; i++) { 162 doTest(myCollation, testKatakanaHiraganaCases[i], testKatakanaHiraganaCases[i + 1], 163 Collator::LESS); 164 } 165 } 166 167 /* 168 * Test Choo-on kigoo 169 */ 170 void CollationKanaTest::TestChooonKigoo(void) 171 { 172 int32_t i; 173 UErrorCode status = U_ZERO_ERROR; 174 myCollation->setStrength(Collator::QUATERNARY); 175 myCollation->setAttribute(UCOL_CASE_LEVEL, UCOL_ON, status); 176 for (i = 0; i < 7 ; i++) { 177 doTest(myCollation, testChooonKigooCases[i], testChooonKigooCases[i + 1], Collator::LESS); 178 } 179 } 180 181 182 void CollationKanaTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ ) 183 { 184 if (exec) logln("TestSuite CollationKanaTest: "); 185 if(myCollation) { 186 switch (index) { 187 case 0: name = "TestTertiary"; if (exec) TestTertiary(/* par */); break; 188 case 1: name = "TestBase"; if (exec) TestBase(/* par */); break; 189 case 2: name = "TestSmallLarge"; if (exec) TestSmallLarge(/* par */); break; 190 case 3: name = "TestTestPlainDakutenHandakuten"; if (exec) TestPlainDakutenHandakuten(/* par */); break; 191 case 4: name = "TestKatakanaHiragana"; if (exec) TestKatakanaHiragana(/* par */); break; 192 case 5: name = "TestChooonKigoo"; if (exec) TestChooonKigoo(/* par */); break; 193 default: name = ""; break; 194 } 195 } else { 196 dataerrln("Collator couldn't be instantiated!"); 197 name = ""; 198 } 199 } 200 201 #endif /* #if !UCONFIG_NO_COLLATION */ 202