1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2006, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6
7 /***********************************************************************
8 * Modification history
9 * Date Name Description
10 * 02/14/2001 synwee Added attributes in TestTertiary and
11 * TestSecondary
12 ***********************************************************************/
13
14 #include "unicode/utypes.h"
15
16 #if !UCONFIG_NO_COLLATION
17
18 #include "unicode/coll.h"
19 #include "unicode/tblcoll.h"
20 #include "unicode/unistr.h"
21 #include "unicode/sortkey.h"
22 #include "frcoll.h"
23
24 #include "sfwdchit.h"
25
CollationFrenchTest()26 CollationFrenchTest::CollationFrenchTest()
27 : myCollation(0)
28 {
29 UErrorCode status = U_ZERO_ERROR;
30 myCollation = Collator::createInstance(Locale::getFrance(), status);
31 if(!myCollation || U_FAILURE(status)) {
32 errln(__FILE__ "failed to create! err " + UnicodeString(u_errorName(status)));
33 /* if it wasn't already: */
34 delete myCollation;
35 myCollation = NULL;
36 }
37 }
38
~CollationFrenchTest()39 CollationFrenchTest::~CollationFrenchTest()
40 {
41 delete myCollation;
42 }
43
44 const UChar CollationFrenchTest::testSourceCases[][CollationFrenchTest::MAX_TOKEN_LEN] =
45 {
46 {0x0061/*'a'*/, 0x0062/*'b'*/, 0x0063/*'c'*/, 0x0000},
47 {0x0043/*'C'*/, 0x004f/*'O'*/, 0x0054/*'T'*/, 0x0045/*'E'*/, 0x0000},
48 {0x0063/*'c'*/, 0x006f/*'o'*/, 0x002d/*'-'*/, 0x006f/*'o'*/, 0x0070/*'p'*/, 0x0000},
49 {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0000},
50 {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/, 0x0000},
51 {0x0070/*'p'*/, 0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/, 0x0000},
52 {0x0070/*'p'*/, 0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/, 0x0000},
53 {0x0048/*'H'*/, 0x0065/*'e'*/, 0x006c/*'l'*/, 0x006c/*'l'*/, 0x006f/*'o'*/, 0x0000},
54 {0x01f1, 0x0000},
55 {0xfb00, 0x0000},
56 {0x01fa, 0x0000},
57 {0x0101, 0x0000}
58 };
59
60 const UChar CollationFrenchTest::testTargetCases[][CollationFrenchTest::MAX_TOKEN_LEN] =
61 {
62 {0x0041/*'A'*/, 0x0042/*'B'*/, 0x0043/*'C'*/, 0x0000},
63 {0x0063/*'c'*/, 0x00f4, 0x0074/*'t'*/, 0x0065/*'e'*/, 0x0000},
64 {0x0043/*'C'*/, 0x004f/*'O'*/, 0x004f/*'O'*/, 0x0050/*'P'*/, 0x0000},
65 {0x0070/*'p'*/, 0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x00E9, 0x0000},
66 {0x0070/*'p'*/, 0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x00E9, 0x0000},
67 {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0000},
68 {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/, 0x0000},
69 {0x0068/*'h'*/, 0x0065/*'e'*/, 0x006c/*'l'*/, 0x006c/*'l'*/, 0x004f/*'O'*/, 0x0000},
70 {0x01ee, 0x0000},
71 {0x25ca, 0x0000},
72 {0x00e0, 0x0000},
73 {0x01df, 0x0000}
74 };
75
76 const Collator::EComparisonResult CollationFrenchTest::results[] =
77 {
78 Collator::LESS,
79 Collator::LESS,
80 Collator::LESS, /*Collator::GREATER,*/
81 Collator::LESS,
82 Collator::GREATER,
83 Collator::GREATER,
84 Collator::LESS,
85 Collator::GREATER,
86 Collator::LESS, /*Collator::GREATER,*/
87 Collator::GREATER,
88 Collator::LESS,
89 Collator::LESS
90 };
91
92 // 0x0300 is grave, 0x0301 is acute
93 // the order of elements in this array must be different than the order in CollationEnglishTest
94 const UChar CollationFrenchTest::testAcute[][CollationFrenchTest::MAX_TOKEN_LEN] =
95 {
96 /*00*/ {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0000},
97 /*01*/ {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0000},
98 /*02*/ {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0000},
99 /*03*/ {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0000},
100 /*04*/ {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0000},
101 /*05*/ {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0301, 0x0000},
102 /*06*/ {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0301, 0x0000},
103 /*07*/ {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0301, 0x0000},
104 /*08*/ {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0301, 0x0000},
105 /*09*/ {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0301, 0x0000},
106 /*0a*/ {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0300, 0x0301, 0x0000},
107 /*0b*/ {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0300, 0x0301, 0x0000},
108 /*0c*/ {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0300, 0x0301, 0x0000},
109 /*0d*/ {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0300, 0x0301, 0x0000},
110 /*0e*/ {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0300, 0x0301, 0x0000},
111 /*0f*/ {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0300, 0x0000},
112 /*10*/ {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0300, 0x0000},
113 /*11*/ {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0300, 0x0000},
114 /*12*/ {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0300, 0x0000},
115 /*13*/ {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0300, 0x0000},
116 /*14*/ {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0301, 0x0300, 0x0000},
117 /*15*/ {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0301, 0x0300, 0x0000},
118 /*16*/ {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0301, 0x0300, 0x0000},
119 /*17*/ {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0301, 0x0300, 0x0000},
120 /*18*/ {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0301, 0x0300, 0x0000}
121 };
122
123 const UChar CollationFrenchTest::testBugs[][CollationFrenchTest::MAX_TOKEN_LEN] =
124 {
125 {0x0061/*'a'*/, 0x000},
126 {0x0041/*'A'*/, 0x000},
127 {0x0065/*'e'*/, 0x000},
128 {0x0045/*'E'*/, 0x000},
129 {0x00e9, 0x000},
130 {0x00e8, 0x000},
131 {0x00ea, 0x000},
132 {0x00eb, 0x000},
133 {0x0065/*'e'*/, 0x0061/*'a'*/, 0x000},
134 {0x0078/*'x'*/, 0x000}
135 };
136
TestTertiary()137 void CollationFrenchTest::TestTertiary(/* char* par */)
138 {
139 int32_t i = 0;
140 UErrorCode status = U_ZERO_ERROR;
141 myCollation->setStrength(Collator::TERTIARY);
142 myCollation->setAttribute(UCOL_FRENCH_COLLATION, UCOL_ON, status);
143 myCollation->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, status);
144 if (U_FAILURE(status)) {
145 errln("Error setting attribute in French collator");
146 }
147 else
148 {
149 for (i = 0; i < 12 ; i++)
150 {
151 doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
152 }
153 }
154 }
155
TestSecondary()156 void CollationFrenchTest::TestSecondary(/* char* par */)
157 {
158 //test acute and grave ordering
159 int32_t i = 0;
160 int32_t j;
161 Collator::EComparisonResult expected;
162 UErrorCode status = U_ZERO_ERROR;
163 //myCollation->setAttribute(UCOL_FRENCH_COLLATION, UCOL_ON, status);
164 myCollation->setStrength(Collator::SECONDARY);
165 if (U_FAILURE(status))
166 errln("Error setting attribute in French collator");
167 else
168 {
169 const int32_t testAcuteSize = (int32_t)(sizeof(testAcute) / sizeof(testAcute[0]));
170 for (i = 0; i < testAcuteSize; i++)
171 {
172 for (j = 0; j < testAcuteSize; j++)
173 {
174 if (i < j)
175 expected = Collator::LESS;
176 else if (i == j)
177 expected = Collator::EQUAL;
178 else // (i > j)
179 expected = Collator::GREATER;
180 doTest(myCollation, testAcute[i], testAcute[j], expected );
181 }
182 }
183 }
184 }
185
TestExtra()186 void CollationFrenchTest::TestExtra(/* char* par */)
187 {
188 int32_t i, j;
189 myCollation->setStrength(Collator::TERTIARY);
190 for (i = 0; i < 9 ; i++)
191 {
192 for (j = i + 1; j < 10; j += 1)
193 {
194 doTest(myCollation, testBugs[i], testBugs[j], Collator::LESS);
195 }
196 }
197 }
198
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)199 void CollationFrenchTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
200 {
201 if (exec) logln("TestSuite CollationFrenchTest: ");
202
203 if((!myCollation) && exec) {
204 errln(__FILE__ " cannot test - failed to create collator.");
205 name = "some test";
206 return;
207 }
208
209 switch (index) {
210 case 0: name = "TestSecondary"; if (exec) TestSecondary(/* par */); break;
211 case 1: name = "TestTertiary"; if (exec) TestTertiary(/* par */); break;
212 case 2: name = "TestExtra"; if (exec) TestExtra(/* par */); break;
213 default: name = ""; break;
214 }
215 }
216
217 #endif /* #if !UCONFIG_NO_COLLATION */
218