1 /********************************************************************
3 * Copyright (c) 1997-2016, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /********************************************************************************
10 * Modification History:
12 * Madhu Katragadda Ported for C API
13 *********************************************************************************/
15 * CollationFrenchTest is a third level test class. This tests the locale
16 * specific primary, secondary and tertiary rules. For example, the ignorable
17 * character '-' in string "black-bird". The en_US locale uses the default
18 * collation rules as its sorting sequence.
23 #include "unicode/utypes.h"
25 #if !UCONFIG_NO_COLLATION
27 #include "unicode/ucol.h"
28 #include "unicode/uloc.h"
34 #include "unicode/ustring.h"
37 static UCollator
*myCollation
;
38 const static UChar testSourceCases
[][MAX_TOKEN_LEN
] =
40 {0x0061/*'a'*/, 0x0062/*'b'*/, 0x0063/*'c'*/, 0x0000},
41 {0x0043/*'C'*/, 0x004f/*'O'*/, 0x0054/*'T'*/, 0x0045/*'E'*/, 0x0000},
42 {0x0063/*'c'*/, 0x006f/*'o'*/, 0x002d/*'-'*/, 0x006f/*'o'*/, 0x0070/*'p'*/, 0x0000},
43 {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0000},
44 {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/, 0x0000},
45 {0x0070/*'p'*/, 0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/, 0x0000},
46 {0x0070/*'p'*/, 0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/, 0x0000},
47 {0x0048/*'H'*/, 0x0065/*'e'*/, 0x006c/*'l'*/, 0x006c/*'l'*/, 0x006f/*'o'*/, 0x0000},
54 const static UChar testTargetCases
[][MAX_TOKEN_LEN
] =
56 {0x0041/*'A'*/, 0x0042/*'B'*/, 0x0043/*'C'*/, 0x0000},
57 {0x0063/*'c'*/, 0x00f4, 0x0074/*'t'*/, 0x0065/*'e'*/, 0x0000},
58 {0x0043/*'C'*/, 0x004f/*'O'*/, 0x004f/*'O'*/, 0x0050/*'P'*/, 0x0000},
59 {0x0070/*'p'*/, 0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x00E9, 0x0000},
60 {0x0070/*'p'*/, 0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x00E9, 0x0000},
61 {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0000},
62 {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/, 0x0000},
63 {0x0068/*'h'*/, 0x0065/*'e'*/, 0x006c/*'l'*/, 0x006c/*'l'*/, 0x004f/*'O'*/, 0x0000},
70 const static UCollationResult results
[] =
74 UCOL_LESS
, /*UCOL_GREATER,*/
80 UCOL_LESS
, /*UCOL_GREATER,*/
86 /* 0x0300 is grave, 0x0301 is acute*/
87 /* the order of elements in this array must be different than the order in CollationEnglishTest*/
88 const static UChar testAcute
[][MAX_TOKEN_LEN
] =
90 /*00*/ {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0000},
91 /*01*/ {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0000},
92 /*02*/ {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0000},
93 /*03*/ {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0000},
94 /*04*/ {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0000},
95 /*05*/ {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0301, 0x0000},
96 /*06*/ {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0301, 0x0000},
97 /*07*/ {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0301, 0x0000},
98 /*08*/ {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0301, 0x0000},
99 /*09*/ {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0301, 0x0000},
100 /*0a*/ {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0300, 0x0301, 0x0000},
101 /*0b*/ {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0300, 0x0301, 0x0000},
102 /*0c*/ {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0300, 0x0301, 0x0000},
103 /*0d*/ {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0300, 0x0301, 0x0000},
104 /*0e*/ {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0300, 0x0301, 0x0000},
105 /*0f*/ {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0300, 0x0000},
106 /*10*/ {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0300, 0x0000},
107 /*11*/ {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0300, 0x0000},
108 /*12*/ {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0300, 0x0000},
109 /*13*/ {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0300, 0x0000},
110 /*14*/ {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0301, 0x0300, 0x0000},
111 /*15*/ {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0301, 0x0300, 0x0000},
112 /*16*/ {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0301, 0x0300, 0x0000},
113 /*17*/ {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0301, 0x0300, 0x0000},
114 /*18*/ {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0301, 0x0300, 0x0000}
117 const static UChar testBugs
[][MAX_TOKEN_LEN
] =
119 {0x0061/*'a'*/, 0x000},
120 {0x0041/*'A'*/, 0x000},
121 {0x0065/*'e'*/, 0x000},
122 {0x0045/*'E'*/, 0x000},
127 {0x0065/*'e'*/, 0x0061/*'a'*/, 0x000},
128 {0x0078/*'x'*/, 0x000}
132 static void TestGetSortKey(void);
135 void addFrenchCollTest(TestNode
** root
)
137 addTest(root
, &TestSecondary
, "tscoll/cfrtst/TestSecondary");
138 addTest(root
, &TestTertiary
, "tscoll/cfrtst/TestTertiary");
139 addTest(root
, &TestExtra
, "tscoll/cfrtst/TestExtra");
140 addTest(root
, &TestGetSortKey
, "tscoll/cfrtst/TestGetSortKey");
144 static void TestTertiary( )
148 UErrorCode status
= U_ZERO_ERROR
;
149 myCollation
= ucol_open("fr_CA", &status
);
150 if(U_FAILURE(status
) || !myCollation
){
151 log_err_status(status
, "ERROR: in creation of rule based collator: %s\n", myErrorName(status
));
155 ucol_setAttribute(myCollation
, UCOL_ALTERNATE_HANDLING
, UCOL_SHIFTED
, &status
);
156 if(U_FAILURE(status
)){
157 log_err("ERROR: in creation of rule based collator: %s\n", myErrorName(status
));
160 log_verbose("Testing fr_CA Collation with Tertiary strength\n");
161 ucol_setStrength(myCollation
, UCOL_QUATERNARY
);
162 for (i
= 0; i
< 12 ; i
++)
164 doTest(myCollation
, testSourceCases
[i
], testTargetCases
[i
], results
[i
]);
166 ucol_close(myCollation
);
169 static void TestSecondary()
171 int32_t i
,j
, testAcuteSize
;
172 UCollationResult expected
=UCOL_EQUAL
;
173 UErrorCode status
= U_ZERO_ERROR
;
174 myCollation
= ucol_open("fr_CA", &status
);
175 if(U_FAILURE(status
)){
176 log_err_status(status
, "ERROR: in creation of rule based collator: %s\n", myErrorName(status
));
179 ucol_setAttribute(myCollation
, UCOL_STRENGTH
, UCOL_SECONDARY
, &status
);
180 if(U_FAILURE(status
)){
181 log_err("ERROR: in creation of rule based collator: %s\n", myErrorName(status
));
184 log_verbose("Testing fr_CA Collation with Secondary strength\n");
185 /*test acute and grave ordering (compare to french collation)*/
186 testAcuteSize
= UPRV_LENGTHOF(testAcute
);
187 for (i
= 0; i
< testAcuteSize
; i
++)
189 for (j
= 0; j
< testAcuteSize
; j
++)
191 if (i
< j
) expected
= UCOL_LESS
;
192 if (i
== j
) expected
= UCOL_EQUAL
;
193 if (i
> j
) expected
= UCOL_GREATER
;
194 doTest(myCollation
, testAcute
[i
], testAcute
[j
], expected
);
197 ucol_close(myCollation
);
200 static void TestExtra()
203 UErrorCode status
= U_ZERO_ERROR
;
204 myCollation
= ucol_open("fr_CA", &status
);
205 if(U_FAILURE(status
)){
206 log_err_status(status
, "ERROR: in creation of rule based collator: %s\n", myErrorName(status
));
209 log_verbose("Testing fr_CA Collation extra with secondary strength\n");
210 ucol_setStrength(myCollation
, UCOL_TERTIARY
);
211 for (i
= 0; i
< 9 ; i
++)
213 for (j
= i
+ 1; j
< 10; j
+= 1)
215 doTest(myCollation
, testBugs
[i
], testBugs
[j
], UCOL_LESS
);
218 ucol_close(myCollation
);
221 static void TestGetSortKey() {
222 /* This is meant to test a buffer reallocation crash while using
223 French secondary sorting with a large buffer.
224 The fact that Japanese characters are used is irrelevant. */
225 static const UChar pucUTF16
[] = {
226 0x3049,0x30b9,0x3088,0xfffd,0xfffd,0x308f,0xfffd,0x3042,
227 0xfffd,0xfffd,0x305e,0xfffd,0x30b6,0x30bb,0x305b,0x30b1,
228 0x3050,0x30af,0x304e,0x30bd,0xfffd,0x30c6,0xfffd,0xfffd,
229 0x30e1,0xfffd,0xfffd,0x30d9,0xfffd,0x3092,0x3075,0x304a,
230 0x3074,0x3070,0x30f5,0x30c4,0x306e,0x30df,0x3053,0xfffd,
231 0x30a6,0x30b6,0x30e0,0xfffd,0x30bc,0x30ef,0x3087,0x30cc,
232 0x305f,0x30de,0xfffd,0x3090,0x3063,0x30dc,0x30b6,0x30b9,
233 0x30d2,0x3072,0x3061,0xfffd,0xfffd,0xfffd,0x307b,0x3092,
234 0x30a5,0x30a9,0x30b1,0x30e7,0xfffd,0xfffd,0xfffd,0xfffd,
235 0xfffd,0x305e,0xfffd,0x30c7,0x30ae,0x305b,0x308b,0x30c0,
236 0x30f5,0xfffd,0xfffd,0xfffd,0x307d,0x304e,0xfffd,0xfffd,
237 0x30c0,0x30c8,0x306f,0x307a,0x30dd,0x30e4,0x3084,0xfffd,
238 0x308c,0x30f1,0xfffd,0x30c6,0xfffd,0x307a,0xfffd,0x3052,
239 0x3056,0x305d,0x30b7,0xfffd,0x305b,0x30b0,0x30b9,0xfffd,
240 0x30b2,0x306d,0x3044,0xfffd,0x3073,0xfffd,0x30be,0x30cf,
241 0x3080,0xfffd,0x30a8,0x30f5,0x30a5,0x30c7,0x307c,0xfffd,
242 0x30d1,0x305f,0x30b2,0xfffd,0x3053,0x30ca,0xfffd,0x30dd,
243 0x3058,0x30c0,0x305d,0x30e1,0xfffd,0x30bb,0x305f,0x30d1,
244 0x30f2,0x3058,0x3086,0x30ce,0x30db,0x30cb,0x30e9,0xfffd,
245 0x308c,0xfffd,0xfffd,0x30af,0x30c4,0x3076,0x304c,0x30f5,
246 0x30e8,0x308c,0xfffd,0x30e2,0x3073,0x30a3,0x304e,0x30ea,
247 0xfffd,0x304f,0xfffd,0x306c,0x3044,0xfffd,0xfffd,0x30c9,
248 0xfffd,0x30f5,0xfffd,0xfffd,0xfffd,0x30eb,0x30a8,0xfffd,
249 0x306d,0x307d,0x30d8,0x3069,0xfffd,0xfffd,0x3086,0x30a9,
250 0xfffd,0x3076,0x30e9,0x30cc,0x3074,0x30e0,0xfffd,0xfffd,
251 0xfffd,0x30f0,0x3086,0x30ac,0x3076,0x3068,0x30c7,0xfffd,
252 0x30b7,0x30d2,0x3048,0x308e,0x30e8,0x30d9,0x30ce,0x30d0,
253 0x308b,0x30ee,0x30e6,0x3079,0x30f3,0x30af,0xfffd,0x3079,
254 0xfffd,0xfffd,0x30ca,0x30bf,0xfffd,0x30b5,0xfffd,0xfffd,
255 0x3093,0xfffd,0x30ba,0xfffd,0x3076,0x3047,0x304a,0xfffd,
256 0xfffd,0x3086,0xfffd,0x3081,0xfffd,0x30f6,0x3066,0xfffd,
257 0xfffd,0x30b6,0x30ef,0x30e2,0x30bf,0xfffd,0x3053,0x304a,
258 0xfffd,0xfffd,0x304a,0x30e8,0xfffd,0x30e2,0xfffd,0xfffd,
259 0x305c,0x3081,0x30c6,0xfffd,0x3091,0x3046,0x306a,0x3059,
260 0xfffd,0xfffd,0x30dd,0x30d1,0x308a,0x30ee,0xfffd,0xfffd,
261 0x308a,0x3042,0x30da,0xfffd,0x3064,0x30ef,0x305c,0x306b,
262 0xfffd,0x30ca,0x3085,0x3067,0x30ea,0x30c2,0x30c8,0xfffd,
263 0x30f5,0xfffd,0xfffd,0xfffd,0x30ca,0xfffd,0x3050,0x30f1,
264 0x3050,0x3053,0x3072,0xfffd,0xfffd,0xfffd,0x3074,0xfffd,
265 0x304b,0x30dd,0x306d,0xfffd,0x3049,0x30a1,0x30cc,0x30de,
266 0x30ae,0x307b,0x308a,0xfffd,0x3065,0xfffd,0xfffd,0x30c0,
267 0xfffd,0x3048,0x30dc,0x304f,0x3085,0x3059,0x304b,0x30d3,
268 0x30eb,0x30a4,0x3073,0xfffd,0x30ba,0x308f,0x30a7,0x30c3,
269 0x3074,0x30cf,0x306c,0x3053,0x30c0,0xfffd,0x3066,0xfffd,
270 0x308f,0xfffd,0x30b5,0xfffd,0x3092,0x30c4,0xfffd,0x30d6,
271 0x3056,0x30ad,0x30d2,0x30ba,0xfffd,0x30e6,0x304c,0x3088,
272 0x30b6,0x3048,0x3077,0x30d1,0xfffd,0x3050,0xfffd,0x3042,
273 0xfffd,0xfffd,0x308f,0xfffd,0x30c1,0xfffd,0x3074,0x3061,
274 0x3056,0x30e5,0xfffd,0xfffd,0x3057,0xfffd,0xfffd,0xfffd,
275 0xfffd,0x30bd,0x30b3,0x30ee,0xfffd,0x30f2,0x3084,0x3050,
276 0xfffd,0x30e7,0xfffd,0xfffd,0x3060,0x3049,0x30f2,0x30ad,
277 0x30bf,0x30f1,0x30a2,0xfffd,0x30af,0xfffd,0x3060,0x30a1,
278 0x30e9,0x30c3,0xfffd,0x3072,0x3093,0x3070,0xfffd,0x308f,
279 0x3060,0xfffd,0x3067,0x306f,0x3082,0x308b,0x3051,0xfffd,
280 0x3058,0xfffd,0xfffd,0x30a8,0x3051,0x3054,0x30ad,0x30f0,
281 0x3053,0xfffd,0x30e1,0x30d7,0x308d,0x307f,0x30be,0x30b0,
282 0xfffd,0x30db,0xfffd,0x30d1,0xfffd,0x3054,0x30a5,0xfffd,
283 0x306a,0xfffd,0x305c,0xfffd,0x3052,0x3088,0xfffd,0x306e,
284 0xfffd,0x30a9,0x30a1,0x30b4,0x3083,0x30bd,0xfffd,0xfffd,
285 0x306a,0x3070,0x30cd,0xfffd,0x3072,0x30ed,0x30c6,0x30be,
286 0x30c4,0x305e,0x30b3,0x30e1,0x308a,0xfffd,0x305b,0xfffd,
287 0x3042,0x3088,0xfffd,0x304c,0xfffd,0x3089,0x3071,0xfffd,
288 0xfffd,0x30c6,0x3062,0x3079,0xfffd,0x304b,0x304a,0xfffd,
289 0x30ad,0x3045,0x3045,0x3087,0xfffd,0x306a,0x308b,0x0000,
290 0x30bd,0x3065,0x30b8,0x3086,0x30d3,0x3076,0xfffd,0xfffd,
291 0x308f,0x3053,0x307c,0x3053,0x3084,0x30ae,0x30c4,0x3045,
292 0x30a8,0x30d0,0x30e1,0x308c,0x30e6,0x30b7,0xfffd,0xfffd,
293 0xfffd,0x3046,0x305f,0xfffd,0x3086,0x30ab,0xfffd,0xfffd,
294 0x30c8,0xfffd,0x30a1,0x3052,0x3059,0xfffd,0x30a4,0xfffd,
295 0xfffd,0x308c,0x3085,0x30ab,0x30b5,0x3091,0x30bf,0x30e3,
296 0xfffd,0xfffd,0x3087,0xfffd,0x30f6,0x3051,0x30bd,0x3092,
297 0x3063,0xfffd,0x30a9,0x3063,0x306e,0xfffd,0xfffd,0xfffd,
298 0x306c,0xfffd,0x307e,0x30ad,0x3077,0x30c2,0x30e9,0x30d5,
299 0xfffd,0xfffd,0x30c6,0x305c,0xfffd,0xfffd,0x3089,0xfffd,
300 0x3048,0x30cb,0x308c,0xfffd,0xfffd,0x3044,0xfffd,0x3080,
301 0x3063,0x3079,0xfffd,0x308a,0x30cb,0x3042,0x3057,0xfffd,
302 0x307c,0x30c1,0x30a8,0x30cf,0xfffd,0x3083,0xfffd,0xfffd,
303 0x306c,0xfffd,0x305e,0x3092,0xfffd,0x30dc,0x30b0,0x3081,
304 0x30e3,0x30f0,0x304e,0x30cc,0x308e,0x30c4,0x30ad
307 UErrorCode status
= U_ZERO_ERROR
;
308 UCollator
*pCollator
;
309 int32_t lenActualSortKey
;
310 uint8_t pucSortKey
[4096];
311 static const int32_t LENSORTKEY
= (int32_t)sizeof(pucSortKey
);
313 ucol_prepareShortStringOpen("LFR_AN_CX_EX_FO_HX_NX_S3", 0, NULL
, &status
);
315 pCollator
= ucol_openFromShortString("LFR_AN_CX_EX_FO_HX_NX_S3", 0, NULL
, &status
);
317 if (U_FAILURE(status
)) {
318 log_data_err("error opening collator -> %s. (Are you missing data?)\n", u_errorName(status
));
322 lenActualSortKey
= ucol_getSortKey(pCollator
,
323 (const UChar
*)pucUTF16
,
324 UPRV_LENGTHOF(pucUTF16
),
328 if (lenActualSortKey
> LENSORTKEY
) {
329 log_err("sort key too big for original buffer. Got: %d Expected: %d\n", lenActualSortKey
, LENSORTKEY
);
332 /* If the test didn't crash, then the test succeeded. */
333 ucol_close(pCollator
);
336 #endif /* #if !UCONFIG_NO_COLLATION */