]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/perf/collperf/collperf.cpp
ICU-8.11.2.tar.gz
[apple/icu.git] / icuSources / test / perf / collperf / collperf.cpp
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (C) 2001-2006 IBM, Inc. All Rights Reserved.
4 *
5 ********************************************************************/
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <locale.h>
10 #include <limits.h>
11 #include <string.h>
12 #include "unicode/uperf.h"
13 #include "uoptions.h"
14 #include "unicode/coll.h"
15 #include <unicode/ucoleitr.h>
16
17
18
19 /* To store an array of string<UNIT> in continue space.
20 Since string<UNIT> itself is treated as an array of UNIT, this
21 class will ease our memory management for an array of string<UNIT>.
22 */
23
24 //template<typename UNIT>
25 #define COMPATCT_ARRAY(CompactArrays, UNIT) \
26 struct CompactArrays{\
27 CompactArrays(const CompactArrays & );\
28 CompactArrays & operator=(const CompactArrays & );\
29 int32_t count;/*total number of the strings*/ \
30 int32_t * index;/*relative offset in data*/ \
31 UNIT * data; /*the real space to hold strings*/ \
32 \
33 ~CompactArrays(){free(index);free(data);} \
34 CompactArrays():data(NULL), index(NULL), count(0){ \
35 index = (int32_t *) realloc(index, sizeof(int32_t)); \
36 index[0] = 0; \
37 } \
38 void append_one(int32_t theLen){ /*include terminal NULL*/ \
39 count++; \
40 index = (int32_t *) realloc(index, sizeof(int32_t) * (count + 1)); \
41 index[count] = index[count - 1] + theLen; \
42 data = (UNIT *) realloc(data, sizeof(UNIT) * index[count]); \
43 } \
44 UNIT * last(){return data + index[count - 1];} \
45 UNIT * dataOf(int32_t i){return data + index[i];} \
46 int32_t lengthOf(int i){return index[i+1] - index[i] - 1; } /*exclude terminating NULL*/ \
47 };
48
49 //typedef CompactArrays<UChar> CA_uchar;
50 //typedef CompactArrays<char> CA_char;
51 //typedef CompactArrays<uint8_t> CA_uint8;
52 //typedef CompactArrays<WCHAR> CA_win_wchar;
53
54 COMPATCT_ARRAY(CA_uchar, UChar)
55 COMPATCT_ARRAY(CA_char, char)
56 COMPATCT_ARRAY(CA_uint8, uint8_t)
57 COMPATCT_ARRAY(CA_win_wchar, WCHAR)
58
59
60 struct DataIndex {
61 static DWORD win_langid; // for qsort callback function
62 static UCollator * col; // for qsort callback function
63 uint8_t * icu_key;
64 UChar * icu_data;
65 int32_t icu_data_len;
66 char* posix_key;
67 char* posix_data;
68 int32_t posix_data_len;
69 char* win_key;
70 WCHAR * win_data;
71 int32_t win_data_len;
72 };
73 DWORD DataIndex::win_langid;
74 UCollator * DataIndex::col;
75
76
77
78 class CmdKeyGen : public UPerfFunction {
79 typedef void (CmdKeyGen::* Func)(int32_t);
80 enum{MAX_KEY_LENGTH = 5000};
81 UCollator * col;
82 DWORD win_langid;
83 int32_t count;
84 DataIndex * data;
85 Func fn;
86
87 union { // to save sapce
88 uint8_t icu_key[MAX_KEY_LENGTH];
89 char posix_key[MAX_KEY_LENGTH];
90 WCHAR win_key[MAX_KEY_LENGTH];
91 };
92 public:
93 CmdKeyGen(UErrorCode, UCollator * col,DWORD win_langid, int32_t count, DataIndex * data,Func fn,int32_t)
94 :col(col),win_langid(win_langid), count(count), data(data), fn(fn){}
95
96 virtual long getOperationsPerIteration(){return count;}
97
98 virtual void call(UErrorCode* status){
99 for(int32_t i = 0; i< count; i++){
100 (this->*fn)(i);
101 }
102 }
103
104 void icu_key_null(int32_t i){
105 ucol_getSortKey(col, data[i].icu_data, -1, icu_key, MAX_KEY_LENGTH);
106 }
107
108 void icu_key_len(int32_t i){
109 ucol_getSortKey(col, data[i].icu_data, data[i].icu_data_len, icu_key, MAX_KEY_LENGTH);
110 }
111
112 // pre-generated in CollPerfTest::prepareData(), need not to check error here
113 void win_key_null(int32_t i){
114 //LCMAP_SORTsk 0x00000400 // WC sort sk (normalize)
115 LCMapStringW(win_langid, LCMAP_SORTKEY, data[i].win_data, -1, win_key, MAX_KEY_LENGTH);
116 }
117
118 void win_key_len(int32_t i){
119 LCMapStringW(win_langid, LCMAP_SORTKEY, data[i].win_data, data[i].win_data_len, win_key, MAX_KEY_LENGTH);
120 }
121
122 void posix_key_null(int32_t i){
123 strxfrm(posix_key, data[i].posix_data, MAX_KEY_LENGTH);
124 }
125 };
126
127
128 class CmdIter : public UPerfFunction {
129 typedef void (CmdIter::* Func)(UErrorCode* , int32_t );
130 int32_t count;
131 CA_uchar * data;
132 Func fn;
133 UCollationElements *iter;
134 int32_t exec_count;
135 public:
136 CmdIter(UErrorCode & status, UCollator * col, int32_t count, CA_uchar *data, Func fn, int32_t,int32_t)
137 :count(count), data(data), fn(fn){
138 exec_count = 0;
139 UChar dummytext[] = {0, 0};
140 iter = ucol_openElements(col, NULL, 0, &status);
141 ucol_setText(iter, dummytext, 1, &status);
142 }
143 ~CmdIter(){
144 ucol_closeElements(iter);
145 }
146
147 virtual long getOperationsPerIteration(){return exec_count ? exec_count : 1;}
148
149 virtual void call(UErrorCode* status){
150 exec_count = 0;
151 for(int32_t i = 0; i< count; i++){
152 (this->*fn)(status, i);
153 }
154 }
155
156 void icu_forward_null(UErrorCode* status, int32_t i){
157 ucol_setText(iter, data->dataOf(i), -1, status);
158 while (ucol_next(iter, status) != UCOL_NULLORDER) exec_count++;
159 }
160
161 void icu_forward_len(UErrorCode* status, int32_t i){
162 ucol_setText(iter, data->dataOf(i), data->lengthOf(i) , status);
163 while (ucol_next(iter, status) != UCOL_NULLORDER) exec_count++;
164 }
165
166 void icu_backward_null(UErrorCode* status, int32_t i){
167 ucol_setText(iter, data->dataOf(i), -1, status);
168 while (ucol_previous(iter, status) != UCOL_NULLORDER) exec_count++;
169 }
170
171 void icu_backward_len(UErrorCode* status, int32_t i){
172 ucol_setText(iter, data->dataOf(i), data->lengthOf(i) , status);
173 while (ucol_previous(iter, status) != UCOL_NULLORDER) exec_count++;
174 }
175 };
176
177 class CmdIterAll : public UPerfFunction {
178 typedef void (CmdIterAll::* Func)(UErrorCode* status);
179 int32_t count;
180 UChar * data;
181 Func fn;
182 UCollationElements *iter;
183 int32_t exec_count;
184
185 public:
186 enum CALL {forward_null, forward_len, backward_null, backward_len};
187
188 ~CmdIterAll(){
189 ucol_closeElements(iter);
190 }
191 CmdIterAll(UErrorCode & status, UCollator * col, int32_t count, UChar * data, CALL call,int32_t,int32_t)
192 :count(count),data(data){
193 exec_count = 0;
194 if (call == forward_null || call == backward_null) {
195 iter = ucol_openElements(col, data, -1, &status);
196 } else {
197 iter = ucol_openElements(col, data, count, &status);
198 }
199
200 if (call == forward_null || call == forward_len){
201 fn = icu_forward_all;
202 } else {
203 fn = icu_backward_all;
204 }
205 }
206 virtual long getOperationsPerIteration(){return exec_count ? exec_count : 1;}
207
208 virtual void call(UErrorCode* status){
209 (this->*fn)(status);
210 }
211
212 void icu_forward_all(UErrorCode* status){
213 int strlen = count - 5;
214 int count5 = 5;
215 int strindex = 0;
216 ucol_setOffset(iter, strindex, status);
217 while (TRUE) {
218 if (ucol_next(iter, status) == UCOL_NULLORDER) {
219 break;
220 }
221 exec_count++;
222 count5 --;
223 if (count5 == 0) {
224 strindex += 10;
225 if (strindex > strlen) {
226 break;
227 }
228 ucol_setOffset(iter, strindex, status);
229 count5 = 5;
230 }
231 }
232 }
233
234 void icu_backward_all(UErrorCode* status){
235 int strlen = count;
236 int count5 = 5;
237 int strindex = 5;
238 ucol_setOffset(iter, strindex, status);
239 while (TRUE) {
240 if (ucol_previous(iter, status) == UCOL_NULLORDER) {
241 break;
242 }
243 exec_count++;
244 count5 --;
245 if (count5 == 0) {
246 strindex += 10;
247 if (strindex > strlen) {
248 break;
249 }
250 ucol_setOffset(iter, strindex, status);
251 count5 = 5;
252 }
253 }
254 }
255
256 };
257
258 struct CmdQsort : public UPerfFunction{
259
260 static int q_random(const void * a, const void * b){
261 uint8_t * key_a = ((DataIndex *)a)->icu_key;
262 uint8_t * key_b = ((DataIndex *)b)->icu_key;
263
264 int val_a = 0;
265 int val_b = 0;
266 while (*key_a != 0) {val_a += val_a*37 + *key_a++;}
267 while (*key_b != 0) {val_b += val_b*37 + *key_b++;}
268 return val_a - val_b;
269 }
270
271 #define QCAST() \
272 DataIndex * da = (DataIndex *) a; \
273 DataIndex * db = (DataIndex *) b; \
274 ++exec_count
275
276 static int icu_strcoll_null(const void *a, const void *b){
277 QCAST();
278 return ucol_strcoll(da->col, da->icu_data, -1, db->icu_data, -1) - UCOL_EQUAL;
279 }
280
281 static int icu_strcoll_len(const void *a, const void *b){
282 QCAST();
283 return ucol_strcoll(da->col, da->icu_data, da->icu_data_len, db->icu_data, db->icu_data_len) - UCOL_EQUAL;
284 }
285
286 static int icu_cmpkey (const void *a, const void *b){
287 QCAST();
288 return strcmp((char *) da->icu_key, (char *) db->icu_key);
289 }
290
291 static int win_cmp_null(const void *a, const void *b) {
292 QCAST();
293 //CSTR_LESS_THAN 1
294 //CSTR_EQUAL 2
295 //CSTR_GREATER_THAN 3
296 int t = CompareStringW(da->win_langid, 0, da->win_data, -1, db->win_data, -1);
297 if (t == 0){
298 fprintf(stderr, "CompareStringW error, error number %x\n", GetLastError());
299 exit(-1);
300 } else{
301 return t - CSTR_EQUAL;
302 }
303 }
304
305 static int win_cmp_len(const void *a, const void *b) {
306 QCAST();
307 int t = CompareStringW(da->win_langid, 0, da->win_data, da->win_data_len, db->win_data, db->win_data_len);
308 if (t == 0){
309 fprintf(stderr, "CompareStringW error, error number %x\n", GetLastError());
310 exit(-1);
311 } else{
312 return t - CSTR_EQUAL;
313 }
314 }
315
316 #define QFUNC(name, func, data) \
317 static int name (const void *a, const void *b){ \
318 QCAST(); \
319 return func(da->data, db->data); \
320 }
321
322 QFUNC(posix_strcoll_null, strcoll, posix_data)
323 QFUNC(posix_cmpkey, strcmp, posix_key)
324 QFUNC(win_cmpkey, strcmp, win_key)
325 QFUNC(win_wcscmp, wcscmp, win_data)
326 QFUNC(icu_strcmp, u_strcmp, icu_data)
327 QFUNC(icu_cmpcpo, u_strcmpCodePointOrder, icu_data)
328
329 private:
330 static int32_t exec_count; // potential muilt-thread problem
331
332 typedef int (* Func)(const void *, const void *);
333
334 Func fn;
335 void * base; //Start of target array.
336 int32_t num; //Array size in elements.
337 int32_t width; //Element size in bytes.
338
339 void * backup; //copy source of base
340 public:
341 CmdQsort(UErrorCode & status,void *theBase, int32_t num, int32_t width, Func fn, int32_t,int32_t)
342 :backup(theBase),num(num),width(width),fn(fn){
343 base = malloc(num * width);
344 time_empty(100, &status); // warm memory/cache
345 }
346
347 ~CmdQsort(){
348 free(base);
349 }
350
351 void empty_call(){
352 exec_count = 0;
353 memcpy(base, backup, num * width);
354 }
355
356 double time_empty(int32_t n, UErrorCode* status) {
357 UTimer start, stop;
358 utimer_getTime(&start);
359 while (n-- > 0) {
360 empty_call();
361 }
362 utimer_getTime(&stop);
363 return utimer_getDeltaSeconds(&start,&stop); // ms
364 }
365
366 virtual void call(UErrorCode* status){
367 exec_count = 0;
368 memcpy(base, backup, num * width);
369 qsort(base, num, width, fn);
370 }
371 virtual double time(int32_t n, UErrorCode* status) {
372 double t1 = time_empty(n,status);
373 double t2 = UPerfFunction::time(n, status);
374 return t2-t1;// < 0 ? t2 : t2-t1;
375 }
376
377 virtual long getOperationsPerIteration(){ return exec_count?exec_count:1;}
378 };
379 int32_t CmdQsort::exec_count;
380
381
382 class CmdBinSearch : public UPerfFunction{
383 public:
384 typedef int (CmdBinSearch::* Func)(int, int);
385
386 UCollator * col;
387 DWORD win_langid;
388 int32_t count;
389 DataIndex * rnd;
390 DataIndex * ord;
391 Func fn;
392 int32_t exec_count;
393
394 CmdBinSearch(UErrorCode, UCollator * col,DWORD win_langid,int32_t count,DataIndex * rnd,DataIndex * ord,Func fn)
395 :col(col),win_langid(win_langid), count(count), rnd(rnd), ord(ord), fn(fn),exec_count(0){}
396
397
398 virtual void call(UErrorCode* status){
399 exec_count = 0;
400 for(int32_t i = 0; i< count; i++){ // search all data
401 binary_search(i);
402 }
403 }
404 virtual long getOperationsPerIteration(){ return exec_count?exec_count:1;}
405
406 void binary_search(int32_t random) {
407 int low = 0;
408 int high = count - 1;
409 int guess;
410 int last_guess = -1;
411 int r;
412 while (TRUE) {
413 guess = (high + low)/2;
414 if (last_guess == guess) break; // nothing to search
415
416 r = (this->*fn)(random, guess);
417 exec_count++;
418
419 if (r == 0)
420 return; // found, search end.
421 if (r < 0) {
422 high = guess;
423 } else {
424 low = guess;
425 }
426 last_guess = guess;
427 }
428 }
429
430 int icu_strcoll_null(int32_t i, int32_t j){
431 return ucol_strcoll(col, rnd[i].icu_data, -1, ord[j].icu_data,-1);
432 }
433
434 int icu_strcoll_len(int32_t i, int32_t j){
435 return ucol_strcoll(col, rnd[i].icu_data, rnd[i].icu_data_len, ord[j].icu_data, ord[j].icu_data_len);
436 }
437
438 int icu_cmpkey(int32_t i, int32_t j) {
439 return strcmp( (char *) rnd[i].icu_key, (char *) ord[j].icu_key );
440 }
441
442 int win_cmp_null(int32_t i, int32_t j) {
443 int t = CompareStringW(win_langid, 0, rnd[i].win_data, -1, ord[j].win_data, -1);
444 if (t == 0){
445 fprintf(stderr, "CompareStringW error, error number %x\n", GetLastError());
446 exit(-1);
447 } else{
448 return t - CSTR_EQUAL;
449 }
450 }
451
452 int win_cmp_len(int32_t i, int32_t j) {
453 int t = CompareStringW(win_langid, 0, rnd[i].win_data, rnd[i].win_data_len, ord[j].win_data, ord[j].win_data_len);
454 if (t == 0){
455 fprintf(stderr, "CompareStringW error, error number %x\n", GetLastError());
456 exit(-1);
457 } else{
458 return t - CSTR_EQUAL;
459 }
460 }
461
462 #define BFUNC(name, func, data) \
463 int name(int32_t i, int32_t j) { \
464 return func(rnd[i].data, ord[j].data); \
465 }
466
467 BFUNC(posix_strcoll_null, strcoll, posix_data)
468 BFUNC(posix_cmpkey, strcmp, posix_key)
469 BFUNC(win_cmpkey, strcmp, win_key)
470 BFUNC(win_wcscmp, wcscmp, win_data)
471 BFUNC(icu_strcmp, u_strcmp, icu_data)
472 BFUNC(icu_cmpcpo, u_strcmpCodePointOrder, icu_data)
473 };
474
475 class CollPerfTest : public UPerfTest {
476 public:
477 UCollator * col;
478 DWORD win_langid;
479
480 UChar * icu_data_all;
481 int32_t icu_data_all_len;
482
483 int32_t count;
484 CA_uchar * icu_data;
485 CA_uint8 * icu_key;
486 CA_char * posix_data;
487 CA_char * posix_key;
488 CA_win_wchar * win_data;
489 CA_char * win_key;
490
491 DataIndex * rnd_index; // random by icu key
492 DataIndex * ord_win_data;
493 DataIndex * ord_win_key;
494 DataIndex * ord_posix_data;
495 DataIndex * ord_posix_key;
496 DataIndex * ord_icu_data;
497 DataIndex * ord_icu_key;
498 DataIndex * ord_win_wcscmp;
499 DataIndex * ord_icu_strcmp;
500 DataIndex * ord_icu_cmpcpo;
501
502 virtual ~CollPerfTest(){
503 ucol_close(col);
504 delete [] icu_data_all;
505 delete icu_data;
506 delete icu_key;
507 delete posix_data;
508 delete posix_key;
509 delete win_data;
510 delete win_key;
511 delete[] rnd_index;
512 delete[] ord_win_data;
513 delete[] ord_win_key;
514 delete[] ord_posix_data;
515 delete[] ord_posix_key;
516 delete[] ord_icu_data;
517 delete[] ord_icu_key;
518 delete[] ord_win_wcscmp;
519 delete[] ord_icu_strcmp;
520 delete[] ord_icu_cmpcpo;
521 }
522
523 CollPerfTest(int32_t argc, const char* argv[], UErrorCode& status):UPerfTest(argc, argv, status){
524 col = NULL;
525 icu_data_all = NULL;
526 icu_data = NULL;
527 icu_key = NULL;
528 posix_data = NULL;
529 posix_key = NULL;
530 win_data =NULL;
531 win_key = NULL;
532
533 rnd_index = NULL;
534 ord_win_data= NULL;
535 ord_win_key= NULL;
536 ord_posix_data= NULL;
537 ord_posix_key= NULL;
538 ord_icu_data= NULL;
539 ord_icu_key= NULL;
540 ord_win_wcscmp = NULL;
541 ord_icu_strcmp = NULL;
542 ord_icu_cmpcpo = NULL;
543
544 if (U_FAILURE(status)){
545 return;
546 }
547
548 // Parse additional arguments
549
550 UOption options[] = {
551 UOPTION_DEF("langid", 'i', UOPT_REQUIRES_ARG), // Windows Language ID number.
552 UOPTION_DEF("rulefile", 'r', UOPT_REQUIRES_ARG), // --rulefile <filename>
553 // Collation related arguments. All are optional.
554 // To simplify parsing, two choice arguments are disigned as NO_ARG.
555 // The default value is UPPER word in the comment
556 UOPTION_DEF("c_french", 'f', UOPT_NO_ARG), // --french <on | OFF>
557 UOPTION_DEF("c_alternate", 'a', UOPT_NO_ARG), // --alternate <NON_IGNORE | shifted>
558 UOPTION_DEF("c_casefirst", 'c', UOPT_REQUIRES_ARG), // --casefirst <lower | upper | OFF>
559 UOPTION_DEF("c_caselevel", 'l', UOPT_NO_ARG), // --caselevel <on | OFF>
560 UOPTION_DEF("c_normal", 'n', UOPT_NO_ARG), // --normal <on | OFF>
561 UOPTION_DEF("c_strength", 's', UOPT_REQUIRES_ARG), // --strength <1-5>
562 };
563 int32_t opt_len = (sizeof(options)/sizeof(options[0]));
564 enum {i, r,f,a,c,l,n,s}; // The buffer between the option items' order and their references
565
566 _remainingArgc = u_parseArgs(_remainingArgc, (char**)argv, opt_len, options);
567
568 if (_remainingArgc < 0){
569 status = U_ILLEGAL_ARGUMENT_ERROR;
570 return;
571 }
572
573 if (locale == NULL){
574 locale = "en_US"; // set default locale
575 }
576
577 //#ifdef U_WINDOWS
578 if (options[i].doesOccur) {
579 char *endp;
580 int tmp = strtol(options[i].value, &endp, 0);
581 if (endp == options[i].value) {
582 status = U_ILLEGAL_ARGUMENT_ERROR;
583 return;
584 }
585 win_langid = MAKELCID(tmp, SORT_DEFAULT);
586 } else {
587 win_langid = uloc_getLCID(locale);
588 }
589 //#endif
590
591 // Set up an ICU collator
592 if (options[r].doesOccur) {
593 // TODO: implement it
594 } else {
595 col = ucol_open(locale, &status);
596 if (U_FAILURE(status)) {
597 return;
598 }
599 }
600
601 if (options[f].doesOccur) {
602 ucol_setAttribute(col, UCOL_FRENCH_COLLATION, UCOL_ON, &status);
603 } else {
604 ucol_setAttribute(col, UCOL_FRENCH_COLLATION, UCOL_OFF, &status);
605 }
606
607 if (options[a].doesOccur) {
608 ucol_setAttribute(col, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status);
609 }
610
611 if (options[c].doesOccur) { // strcmp() has i18n encoding problem
612 if (strcmp("lower", options[c].value) == 0){
613 ucol_setAttribute(col, UCOL_CASE_FIRST, UCOL_LOWER_FIRST, &status);
614 } else if (strcmp("upper", options[c].value) == 0) {
615 ucol_setAttribute(col, UCOL_CASE_FIRST, UCOL_UPPER_FIRST, &status);
616 } else {
617 status = U_ILLEGAL_ARGUMENT_ERROR;
618 return;
619 }
620 }
621
622 if (options[l].doesOccur){
623 ucol_setAttribute(col, UCOL_CASE_LEVEL, UCOL_ON, &status);
624 }
625
626 if (options[n].doesOccur){
627 ucol_setAttribute(col, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
628 }
629
630 if (options[s].doesOccur) {
631 char *endp;
632 int tmp = strtol(options[l].value, &endp, 0);
633 if (endp == options[l].value) {
634 status = U_ILLEGAL_ARGUMENT_ERROR;
635 return;
636 }
637 switch (tmp) {
638 case 1: ucol_setAttribute(col, UCOL_STRENGTH, UCOL_PRIMARY, &status); break;
639 case 2: ucol_setAttribute(col, UCOL_STRENGTH, UCOL_SECONDARY, &status); break;
640 case 3: ucol_setAttribute(col, UCOL_STRENGTH, UCOL_TERTIARY, &status); break;
641 case 4: ucol_setAttribute(col, UCOL_STRENGTH, UCOL_QUATERNARY, &status); break;
642 case 5: ucol_setAttribute(col, UCOL_STRENGTH, UCOL_IDENTICAL, &status); break;
643 default: status = U_ILLEGAL_ARGUMENT_ERROR; return;
644 }
645 }
646 prepareData(status);
647 }
648
649 //to avoid use the annoying 'id' in TESTCASE(id,test) macro or the like
650 #define TEST(testname, classname, arg1, arg2, arg3, arg4, arg5, arg6) \
651 if(temp == index) {\
652 name = #testname;\
653 if (exec) {\
654 UErrorCode status = U_ZERO_ERROR;\
655 UPerfFunction * t = new classname(status,arg1, arg2, arg3, arg4, arg5, arg6);\
656 if (U_FAILURE(status)) {\
657 delete t;\
658 return NULL;\
659 } else {\
660 return t;\
661 }\
662 } else {\
663 return NULL;\
664 }\
665 }\
666 temp++\
667
668
669 virtual UPerfFunction* runIndexedTest( /*[in]*/int32_t index, /*[in]*/UBool exec, /*[out]*/const char* &name, /*[in]*/ char* par = NULL ){
670 int temp = 0;
671
672 #define TEST_KEYGEN(testname, func)\
673 TEST(testname, CmdKeyGen, col, win_langid, count, rnd_index, CmdKeyGen::func, 0)
674 TEST_KEYGEN(TestIcu_KeyGen_null, icu_key_null);
675 TEST_KEYGEN(TestIcu_KeyGen_len, icu_key_len);
676 TEST_KEYGEN(TestPosix_KeyGen_null, posix_key_null);
677 TEST_KEYGEN(TestWin_KeyGen_null, win_key_null);
678 TEST_KEYGEN(TestWin_KeyGen_len, win_key_len);
679
680 #define TEST_ITER(testname, func)\
681 TEST(testname, CmdIter, col, count, icu_data, CmdIter::func,0,0)
682 TEST_ITER(TestIcu_ForwardIter_null, icu_forward_null);
683 TEST_ITER(TestIcu_ForwardIter_len, icu_forward_len);
684 TEST_ITER(TestIcu_BackwardIter_null, icu_backward_null);
685 TEST_ITER(TestIcu_BackwardIter_len, icu_backward_len);
686
687 #define TEST_ITER_ALL(testname, func)\
688 TEST(testname, CmdIterAll, col, icu_data_all_len, icu_data_all, CmdIterAll::func,0,0)
689 TEST_ITER_ALL(TestIcu_ForwardIter_all_null, forward_null);
690 TEST_ITER_ALL(TestIcu_ForwardIter_all_len, forward_len);
691 TEST_ITER_ALL(TestIcu_BackwardIter_all_null, backward_null);
692 TEST_ITER_ALL(TestIcu_BackwardIter_all_len, backward_len);
693
694 #define TEST_QSORT(testname, func)\
695 TEST(testname, CmdQsort, rnd_index, count, sizeof(DataIndex), CmdQsort::func,0,0)
696 TEST_QSORT(TestIcu_qsort_strcoll_null, icu_strcoll_null);
697 TEST_QSORT(TestIcu_qsort_strcoll_len, icu_strcoll_len);
698 TEST_QSORT(TestIcu_qsort_usekey, icu_cmpkey);
699 TEST_QSORT(TestPosix_qsort_strcoll_null, posix_strcoll_null);
700 TEST_QSORT(TestPosix_qsort_usekey, posix_cmpkey);
701 TEST_QSORT(TestWin_qsort_CompareStringW_null, win_cmp_null);
702 TEST_QSORT(TestWin_qsort_CompareStringW_len, win_cmp_len);
703 TEST_QSORT(TestWin_qsort_usekey, win_cmpkey);
704
705 #define TEST_BIN(testname, func)\
706 TEST(testname, CmdBinSearch, col, win_langid, count, rnd_index, ord_icu_key,CmdBinSearch::func)
707 TEST_BIN(TestIcu_BinarySearch_strcoll_null, icu_strcoll_null);
708 TEST_BIN(TestIcu_BinarySearch_strcoll_len, icu_strcoll_len);
709 TEST_BIN(TestIcu_BinarySearch_usekey, icu_cmpkey);
710 TEST_BIN(TestIcu_BinarySearch_strcmp, icu_strcmp);
711 TEST_BIN(TestIcu_BinarySearch_cmpCPO, icu_cmpcpo);
712 TEST_BIN(TestPosix_BinarySearch_strcoll_null, posix_strcoll_null);
713 TEST_BIN(TestPosix_BinarySearch_usekey, posix_cmpkey);
714 TEST_BIN(TestWin_BinarySearch_CompareStringW_null, win_cmp_null);
715 TEST_BIN(TestWin_BinarySearch_CompareStringW_len, win_cmp_len);
716 TEST_BIN(TestWin_BinarySearch_usekey, win_cmpkey);
717 TEST_BIN(TestWin_BinarySearch_wcscmp, win_wcscmp);
718
719 name="";
720 return NULL;
721 }
722
723
724
725 void prepareData(UErrorCode& status){
726 if(U_FAILURE(status)) return;
727 if (icu_data) return; // prepared
728
729 icu_data = new CA_uchar();
730
731 // Following code is borrowed from UPerfTest::getLines();
732 const UChar* line=NULL;
733 int32_t len =0;
734 for (;;) {
735 line = ucbuf_readline(ucharBuf,&len,&status);
736 if(line == NULL || U_FAILURE(status)){break;}
737
738 // Refer to the source code of ucbuf_readline()
739 // 1. 'len' includs the line terminal symbols
740 // 2. The length of the line terminal symbols is only one character
741 // 3. The Windows CR LF line terminal symbols will be converted to CR
742
743 if (len == 1) {
744 continue; //skip empty line
745 } else {
746 icu_data->append_one(len);
747 memcpy(icu_data->last(), line, len * sizeof(UChar));
748 icu_data->last()[len -1] = NULL;
749 }
750 }
751 if(U_FAILURE(status)) return;
752
753 // UTF-16 -> UTF-8 conversion.
754 UConverter *conv = ucnv_open("utf-8", &status); // just UTF-8 for now.
755 if (U_FAILURE(status)) return;
756
757 count = icu_data->count;
758
759 icu_data_all_len = icu_data->index[count]; // includes all NULLs
760 icu_data_all_len -= count; // excludes all NULLs
761 icu_data_all_len += 1; // the terminal NULL
762 icu_data_all = new UChar[icu_data_all_len];
763 icu_data_all[icu_data_all_len - 1] = 0; //the terminal NULL
764
765 icu_key = new CA_uint8;
766 win_data = new CA_win_wchar;
767 win_key = new CA_char;
768 posix_data = new CA_char;
769 posix_key = new CA_char;
770 rnd_index = new DataIndex[count];
771 DataIndex::win_langid = win_langid;
772 DataIndex::col = col;
773
774
775 UChar * p = icu_data_all;
776 int32_t s;
777 int32_t t;
778 for (int i=0; i < count; i++) {
779 // ICU all data
780 s = sizeof(UChar) * icu_data->lengthOf(i);
781 memcpy(p, icu_data->dataOf(i), s);
782 p += icu_data->lengthOf(i);
783
784 // ICU data
785
786 // ICU key
787 s = ucol_getSortKey(col, icu_data->dataOf(i), -1,NULL, 0);
788 icu_key->append_one(s);
789 t = ucol_getSortKey(col, icu_data->dataOf(i), -1,icu_key->last(), s);
790 if (t != s) {status = U_INVALID_FORMAT_ERROR;return;}
791
792 // POSIX data
793 s = ucnv_fromUChars(conv,NULL, 0, icu_data->dataOf(i), icu_data->lengthOf(i), &status);
794 if (status == U_BUFFER_OVERFLOW_ERROR || status == U_ZERO_ERROR){
795 status = U_ZERO_ERROR;
796 } else {
797 return;
798 }
799 posix_data->append_one(s + 1); // plus terminal NULL
800 t = ucnv_fromUChars(conv,posix_data->last(), s, icu_data->dataOf(i), icu_data->lengthOf(i), &status);
801 if (U_FAILURE(status)) return;
802 if ( t != s){status = U_INVALID_FORMAT_ERROR;return;}
803 posix_data->last()[s] = 0;
804
805 // POSIX key
806 s = strxfrm(NULL, posix_data->dataOf(i), 0);
807 if (s == INT_MAX){status = U_INVALID_FORMAT_ERROR;return;}
808 posix_key->append_one(s);
809 t = strxfrm(posix_key->last(), posix_data->dataOf(i), s);
810 if (t != s) {status = U_INVALID_FORMAT_ERROR;return;}
811
812 // Win data
813 s = icu_data->lengthOf(i) + 1; // plus terminal NULL
814 win_data->append_one(s);
815 memcpy(win_data->last(), icu_data->dataOf(i), sizeof(WCHAR) * s);
816
817 // Win key
818 s = LCMapStringW(win_langid, LCMAP_SORTKEY, win_data->dataOf(i), win_data->lengthOf(i), NULL,0);
819 if (s == 0) {status = U_INVALID_FORMAT_ERROR;return;}
820 win_key->append_one(s);
821 t = LCMapStringW(win_langid, LCMAP_SORTKEY, win_data->dataOf(i), win_data->lengthOf(i), (WCHAR *)(win_key->last()),s);
822 if (t != s) {status = U_INVALID_FORMAT_ERROR;return;}
823
824 };
825
826 // append_one() will make points shifting, should not merge following code into previous iteration
827 for (int i=0; i < count; i++) {
828 rnd_index[i].icu_key = icu_key->dataOf(i);
829 rnd_index[i].icu_data = icu_data->dataOf(i);
830 rnd_index[i].icu_data_len = icu_data->lengthOf(i);
831 rnd_index[i].posix_key = posix_key->last();
832 rnd_index[i].posix_data = posix_data->dataOf(i);
833 rnd_index[i].posix_data_len = posix_data->lengthOf(i);
834 rnd_index[i].win_key = win_key->dataOf(i);
835 rnd_index[i].win_data = win_data->dataOf(i);
836 rnd_index[i].win_data_len = win_data->lengthOf(i);
837 };
838
839 ucnv_close(conv);
840 qsort(rnd_index, count, sizeof(DataIndex), CmdQsort::q_random);
841
842 #define SORT(data, func) \
843 data = new DataIndex[count];\
844 memcpy(data, rnd_index, count * sizeof(DataIndex));\
845 qsort(data, count, sizeof(DataIndex), CmdQsort::func)
846
847 SORT(ord_icu_data, icu_strcoll_len);
848 SORT(ord_icu_key, icu_cmpkey);
849 SORT(ord_posix_data, posix_strcoll_null);
850 SORT(ord_posix_key, posix_cmpkey);
851 SORT(ord_win_data, win_cmp_len);
852 SORT(ord_win_key, win_cmpkey);
853 SORT(ord_win_wcscmp, win_wcscmp);
854 SORT(ord_icu_strcmp, icu_strcmp);
855 SORT(ord_icu_cmpcpo, icu_cmpcpo);
856 }
857 };
858
859
860 int main(int argc, const char *argv[])
861 {
862
863 UErrorCode status = U_ZERO_ERROR;
864 CollPerfTest test(argc, argv, status);
865
866 if (U_FAILURE(status)){
867 printf("The error is %s\n", u_errorName(status));
868 //TODO: print usage here
869 return status;
870 }
871
872 if (test.run() == FALSE){
873 fprintf(stderr, "FAILED: Tests could not be run please check the "
874 "arguments.\n");
875 return -1;
876 }
877 return 0;
878 }
879