1/*
2**********************************************************************
3* Copyright (c) 2002-2005, International Business Machines
4* Corporation and others.  All Rights Reserved.
5**********************************************************************
6**********************************************************************
7*/
8/**
9 * This Program tests the performance of ICU's Normalization engine against Windows
10 * to run it use the command like
11 *
12 * c:\normperf.exe -s C:\work\ICUCupertinoRep\icu4c\collation-perf-data  -i 10 -p 15 -f TestNames_Asian.txt -u -e UTF-8  -l
13 */
14#include "normperf.h"
15#include "uoptions.h"
16#include <stdio.h>
17
18#define LENGTHOF(array) (sizeof(array)/sizeof((array)[0]))
19
20UPerfFunction* NormalizerPerformanceTest::runIndexedTest(int32_t index, UBool exec,const char* &name, char* par) {
21    switch (index) {
22        TESTCASE(0,TestICU_NFC_NFD_Text);
23        TESTCASE(1,TestICU_NFC_NFC_Text);
24        TESTCASE(2,TestICU_NFC_Orig_Text);
25
26        TESTCASE(3,TestICU_NFD_NFD_Text);
27        TESTCASE(4,TestICU_NFD_NFC_Text);
28        TESTCASE(5,TestICU_NFD_Orig_Text);
29
30        TESTCASE(6,TestICU_FCD_NFD_Text);
31        TESTCASE(7,TestICU_FCD_NFC_Text);
32        TESTCASE(8,TestICU_FCD_Orig_Text);
33
34        TESTCASE(9,TestWin_NFC_NFD_Text);
35        TESTCASE(10,TestWin_NFC_NFC_Text);
36        TESTCASE(11,TestWin_NFC_Orig_Text);
37
38        TESTCASE(12,TestWin_NFD_NFD_Text);
39        TESTCASE(13,TestWin_NFD_NFC_Text);
40        TESTCASE(14,TestWin_NFD_Orig_Text);
41
42        TESTCASE(15,TestQC_NFC_NFD_Text);
43        TESTCASE(16,TestQC_NFC_NFC_Text);
44        TESTCASE(17,TestQC_NFC_Orig_Text);
45
46        TESTCASE(18,TestQC_NFD_NFD_Text);
47        TESTCASE(19,TestQC_NFD_NFC_Text);
48        TESTCASE(20,TestQC_NFD_Orig_Text);
49
50        TESTCASE(21,TestQC_FCD_NFD_Text);
51        TESTCASE(22,TestQC_FCD_NFC_Text);
52        TESTCASE(23,TestQC_FCD_Orig_Text);
53
54        TESTCASE(24,TestIsNormalized_NFC_NFD_Text);
55        TESTCASE(25,TestIsNormalized_NFC_NFC_Text);
56        TESTCASE(26,TestIsNormalized_NFC_Orig_Text);
57
58        TESTCASE(27,TestIsNormalized_NFD_NFD_Text);
59        TESTCASE(28,TestIsNormalized_NFD_NFC_Text);
60        TESTCASE(29,TestIsNormalized_NFD_Orig_Text);
61
62        TESTCASE(30,TestIsNormalized_FCD_NFD_Text);
63        TESTCASE(31,TestIsNormalized_FCD_NFC_Text);
64        TESTCASE(32,TestIsNormalized_FCD_Orig_Text);
65
66        default:
67            name = "";
68            return NULL;
69    }
70    return NULL;
71
72}
73
74void NormalizerPerformanceTest::normalizeInput(ULine* dest,const UChar* src ,int32_t srcLen,UNormalizationMode mode, int32_t options){
75    int32_t reqLen = 0;
76    UErrorCode status = U_ZERO_ERROR;
77    for(;;){
78        /* pure pre-flight */
79        reqLen=unorm_normalize(src,srcLen,mode, options,NULL,0,&status);
80        if(status==U_BUFFER_OVERFLOW_ERROR){
81            status=U_ZERO_ERROR;
82            dest->name = new UChar[reqLen+1];
83            reqLen= unorm_normalize(src,srcLen,mode, options,dest->name,reqLen+1,&status);
84            dest->len=reqLen;
85            break;
86        }else if(U_FAILURE(status)){
87            printf("Could not normalize input. Error: %s", u_errorName(status));
88        }
89    }
90}
91UChar* NormalizerPerformanceTest::normalizeInput(int32_t& len, const UChar* src ,int32_t srcLen,UNormalizationMode mode, int32_t options){
92    int32_t reqLen = 0;
93    UErrorCode status = U_ZERO_ERROR;
94    UChar* dest = NULL;
95    for(;;){
96        /* pure pre-flight */
97        reqLen=unorm_normalize(src,srcLen,mode, options,NULL,0,&status);
98        if(status==U_BUFFER_OVERFLOW_ERROR){
99            status=U_ZERO_ERROR;
100            dest = new UChar[reqLen+1];
101            reqLen= unorm_normalize(src,srcLen,mode, options,dest,reqLen+1,&status);
102            len=reqLen;
103            break;
104        }else if(U_FAILURE(status)){
105            printf("Could not normalize input. Error: %s", u_errorName(status));
106            return NULL;
107        }
108    }
109    return dest;
110}
111
112static UOption cmdLineOptions[]={
113    UOPTION_DEF("options", 'o', UOPT_OPTIONAL_ARG)
114};
115
116NormalizerPerformanceTest::NormalizerPerformanceTest(int32_t argc, const char* argv[], UErrorCode& status)
117: UPerfTest(argc,argv,status), options(0) {
118    NFDBuffer = NULL;
119    NFCBuffer = NULL;
120    NFDBufferLen = 0;
121    NFCBufferLen = 0;
122    NFDFileLines = NULL;
123    NFCFileLines = NULL;
124
125    if(status== U_ILLEGAL_ARGUMENT_ERROR){
126       fprintf(stderr,gUsageString, "normperf");
127       return;
128    }
129
130    if(U_FAILURE(status)){
131        fprintf(stderr, "FAILED to create UPerfTest object. Error: %s\n", u_errorName(status));
132        return;
133    }
134
135    _remainingArgc = u_parseArgs(_remainingArgc, (char **)argv, (int32_t)(LENGTHOF(cmdLineOptions)), cmdLineOptions);
136    if(cmdLineOptions[0].doesOccur && cmdLineOptions[0].value!=NULL) {
137        options=(int32_t)strtol(cmdLineOptions[0].value, NULL, 16);
138    }
139
140    if(line_mode){
141        ULine* filelines = getLines(status);
142        if(U_FAILURE(status)){
143            fprintf(stderr, "FAILED to read lines from file and create UPerfTest object. Error: %s\n", u_errorName(status));
144            return;
145        }
146        NFDFileLines = new ULine[numLines];
147        NFCFileLines = new ULine[numLines];
148
149        for(int32_t i=0;i<numLines;i++){
150            normalizeInput(&NFDFileLines[i],filelines[i].name,filelines[i].len,UNORM_NFD, options);
151            normalizeInput(&NFCFileLines[i],filelines[i].name,filelines[i].len,UNORM_NFC, options);
152
153        }
154    }else if(bulk_mode){
155        int32_t srcLen = 0;
156        const UChar* src = getBuffer(srcLen,status);
157        NFDBufferLen = 0;
158        NFCBufferLen = 0;
159
160        if(U_FAILURE(status)){
161            fprintf(stderr, "FAILED to read buffer from file and create UPerfTest object. Error: %s\n", u_errorName(status));
162            return;
163        }
164
165        NFDBuffer = normalizeInput(NFDBufferLen,src,srcLen,UNORM_NFD, options);
166        NFCBuffer = normalizeInput(NFCBufferLen,src,srcLen,UNORM_NFC, options);
167    }
168
169}
170
171NormalizerPerformanceTest::~NormalizerPerformanceTest(){
172    delete[] NFDFileLines;
173    delete[] NFCFileLines;
174    delete[] NFDBuffer;
175    delete[] NFCBuffer;
176}
177
178// Test NFC Performance
179UPerfFunction* NormalizerPerformanceTest::TestICU_NFC_NFD_Text(){
180    if(line_mode){
181        NormPerfFunction* func= new NormPerfFunction(ICUNormNFC, options,NFDFileLines,numLines, uselen);
182        return func;
183    }else{
184        NormPerfFunction* func= new NormPerfFunction(ICUNormNFC, options,NFDBuffer, NFDBufferLen, uselen);
185        return func;
186    }
187}
188UPerfFunction* NormalizerPerformanceTest::TestICU_NFC_NFC_Text(){
189    if(line_mode){
190        NormPerfFunction* func = new NormPerfFunction(ICUNormNFC, options,NFCFileLines,numLines, uselen);
191        return func;
192    }else{
193        NormPerfFunction* func= new NormPerfFunction(ICUNormNFC, options,NFCBuffer, NFCBufferLen, uselen);
194        return func;
195    }
196}
197UPerfFunction* NormalizerPerformanceTest::TestICU_NFC_Orig_Text(){
198    if(line_mode){
199        NormPerfFunction* func = new NormPerfFunction(ICUNormNFC, options,lines,numLines, uselen);
200        return func;
201    }else{
202        NormPerfFunction* func = new NormPerfFunction(ICUNormNFC, options,buffer, bufferLen, uselen);
203        return func;
204    }
205}
206
207// Test NFD Performance
208UPerfFunction* NormalizerPerformanceTest::TestICU_NFD_NFD_Text(){
209    if(line_mode){
210        NormPerfFunction* func = new NormPerfFunction(ICUNormNFD, options,NFDFileLines,numLines, uselen);
211        return func;
212    }else{
213        NormPerfFunction* func = new NormPerfFunction(ICUNormNFD, options,NFDBuffer,NFDBufferLen, uselen);
214        return func;
215    }
216}
217UPerfFunction* NormalizerPerformanceTest::TestICU_NFD_NFC_Text(){
218    if(line_mode){
219        NormPerfFunction* func = new NormPerfFunction(ICUNormNFD, options,NFCFileLines,numLines, uselen);
220        return func;
221    }else{
222        NormPerfFunction* func = new NormPerfFunction(ICUNormNFD, options,NFCBuffer,NFCBufferLen, uselen);
223        return func;
224    }
225}
226UPerfFunction* NormalizerPerformanceTest::TestICU_NFD_Orig_Text(){
227    if(line_mode){
228        NormPerfFunction* func = new NormPerfFunction(ICUNormNFD, options,lines,numLines, uselen);
229        return func;
230    }else{
231        NormPerfFunction* func = new NormPerfFunction(ICUNormNFD, options,buffer,bufferLen, uselen);
232        return func;
233    }
234}
235
236// Test FCD Performance
237UPerfFunction* NormalizerPerformanceTest::TestICU_FCD_NFD_Text(){
238    if(line_mode){
239        NormPerfFunction* func = new NormPerfFunction(ICUNormFCD, options,NFDFileLines,numLines, uselen);
240        return func;
241    }else{
242        NormPerfFunction* func = new NormPerfFunction(ICUNormFCD, options,NFDBuffer,NFDBufferLen, uselen);
243        return func;
244    }
245
246}
247UPerfFunction* NormalizerPerformanceTest::TestICU_FCD_NFC_Text(){
248    if(line_mode){
249        NormPerfFunction* func = new NormPerfFunction(ICUNormFCD, options,NFCFileLines,numLines, uselen);
250        return func;
251    }else{
252        NormPerfFunction* func = new NormPerfFunction(ICUNormFCD, options,NFCBuffer,NFCBufferLen, uselen);
253        return func;
254    }
255}
256UPerfFunction* NormalizerPerformanceTest::TestICU_FCD_Orig_Text(){
257    if(line_mode){
258        NormPerfFunction* func = new NormPerfFunction(ICUNormFCD, options,lines,numLines, uselen);
259        return func;
260    }else{
261        NormPerfFunction* func = new NormPerfFunction(ICUNormFCD, options,buffer,bufferLen, uselen);
262        return func;
263    }
264}
265
266// Test Win NFC Performance
267UPerfFunction* NormalizerPerformanceTest::TestWin_NFC_NFD_Text(){
268    if(line_mode){
269        NormPerfFunction* func = new NormPerfFunction(WinNormNFC, options,NFDFileLines,numLines, uselen);
270        return func;
271    }else{
272        NormPerfFunction* func = new NormPerfFunction(WinNormNFC, options,NFDBuffer,NFDBufferLen, uselen);
273        return func;
274    }
275}
276UPerfFunction* NormalizerPerformanceTest::TestWin_NFC_NFC_Text(){
277    if(line_mode){
278        NormPerfFunction* func = new NormPerfFunction(WinNormNFC, options,NFCFileLines,numLines, uselen);
279        return func;
280    }else{
281        NormPerfFunction* func = new NormPerfFunction(WinNormNFC, options,NFCBuffer,NFCBufferLen, uselen);
282        return func;
283    }
284}
285UPerfFunction* NormalizerPerformanceTest::TestWin_NFC_Orig_Text(){
286    if(line_mode){
287        NormPerfFunction* func = new NormPerfFunction(WinNormNFC, options,lines,numLines, uselen);
288        return func;
289    }else{
290        NormPerfFunction* func = new NormPerfFunction(WinNormNFC, options,buffer,bufferLen, uselen);
291        return func;
292    }
293}
294
295// Test Win NFD Performance
296UPerfFunction* NormalizerPerformanceTest::TestWin_NFD_NFD_Text(){
297    if(line_mode){
298        NormPerfFunction* func = new NormPerfFunction(WinNormNFD, options,NFDFileLines,numLines, uselen);
299        return func;
300    }else{
301        NormPerfFunction* func = new NormPerfFunction(WinNormNFD, options,NFDBuffer,NFDBufferLen, uselen);
302        return func;
303    }
304}
305UPerfFunction* NormalizerPerformanceTest::TestWin_NFD_NFC_Text(){
306    if(line_mode){
307        NormPerfFunction* func = new NormPerfFunction(WinNormNFD, options,NFCFileLines,numLines, uselen);
308        return func;
309    }else{
310        NormPerfFunction* func = new NormPerfFunction(WinNormNFD, options,NFCBuffer,NFCBufferLen, uselen);
311        return func;
312    }
313}
314UPerfFunction* NormalizerPerformanceTest::TestWin_NFD_Orig_Text(){
315    if(line_mode){
316        NormPerfFunction* func = new NormPerfFunction(WinNormNFD, options,lines,numLines, uselen);
317        return func;
318    }else{
319        NormPerfFunction* func = new NormPerfFunction(WinNormNFD, options,buffer,bufferLen, uselen);
320        return func;
321    }
322}
323
324// Test Quick Check Performance
325UPerfFunction* NormalizerPerformanceTest::TestQC_NFC_NFD_Text(){
326    if(line_mode){
327        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUQuickCheck,NFDFileLines, numLines, UNORM_NFC, options,uselen);
328        return func;
329    }else{
330        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUQuickCheck,NFDBuffer, NFDBufferLen, UNORM_NFC, options,uselen);
331        return func;
332    }
333}
334UPerfFunction* NormalizerPerformanceTest::TestQC_NFC_NFC_Text(){
335    if(line_mode){
336        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUQuickCheck,NFCFileLines, numLines, UNORM_NFC, options,uselen);
337        return func;
338    }else{
339        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUQuickCheck,NFCBuffer, NFCBufferLen, UNORM_NFC, options,uselen);
340        return func;
341    }
342}
343UPerfFunction* NormalizerPerformanceTest::TestQC_NFC_Orig_Text(){
344    if(line_mode){
345        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUQuickCheck,lines, numLines, UNORM_NFC, options,uselen);
346        return func;
347    }else{
348        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUQuickCheck,buffer, bufferLen, UNORM_NFC, options,uselen);
349        return func;
350    }
351}
352
353UPerfFunction* NormalizerPerformanceTest::TestQC_NFD_NFD_Text(){
354    if(line_mode){
355        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUQuickCheck,NFDFileLines, numLines, UNORM_NFD, options,uselen);
356        return func;
357    }else{
358        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUQuickCheck,NFDBuffer, NFDBufferLen, UNORM_NFD, options,uselen);
359        return func;
360    }
361}
362UPerfFunction* NormalizerPerformanceTest::TestQC_NFD_NFC_Text(){
363    if(line_mode){
364        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUQuickCheck,NFCFileLines, numLines, UNORM_NFD, options,uselen);
365        return func;
366    }else{
367        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUQuickCheck,NFCBuffer, NFCBufferLen, UNORM_NFD, options,uselen);
368        return func;
369    }
370}
371UPerfFunction* NormalizerPerformanceTest::TestQC_NFD_Orig_Text(){
372    if(line_mode){
373        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUQuickCheck,lines, numLines, UNORM_NFD, options,uselen);
374        return func;
375    }else{
376        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUQuickCheck,buffer, bufferLen, UNORM_NFD, options,uselen);
377        return func;
378    }
379}
380
381UPerfFunction* NormalizerPerformanceTest::TestQC_FCD_NFD_Text(){
382    if(line_mode){
383        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUQuickCheck,NFDFileLines, numLines, UNORM_FCD, options,uselen);
384        return func;
385    }else{
386        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUQuickCheck,NFDBuffer, NFDBufferLen, UNORM_FCD, options,uselen);
387        return func;
388    }
389}
390UPerfFunction* NormalizerPerformanceTest::TestQC_FCD_NFC_Text(){
391    if(line_mode){
392        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUQuickCheck,NFCFileLines, numLines, UNORM_FCD, options,uselen);
393        return func;
394    }else{
395        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUQuickCheck,NFCBuffer, NFCBufferLen, UNORM_FCD, options,uselen);
396        return func;
397    }
398}
399UPerfFunction* NormalizerPerformanceTest::TestQC_FCD_Orig_Text(){
400    if(line_mode){
401        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUQuickCheck,lines, numLines, UNORM_FCD, options,uselen);
402        return func;
403    }else{
404        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUQuickCheck,buffer, bufferLen, UNORM_FCD, options,uselen);
405        return func;
406    }
407}
408
409// Test isNormalized Performance
410UPerfFunction* NormalizerPerformanceTest::TestIsNormalized_NFC_NFD_Text(){
411    if(line_mode){
412        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUIsNormalized,NFDFileLines, numLines, UNORM_NFC, options,uselen);
413        return func;
414    }else{
415        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUIsNormalized,NFDBuffer, NFDBufferLen, UNORM_NFC, options,uselen);
416        return func;
417    }
418}
419UPerfFunction* NormalizerPerformanceTest::TestIsNormalized_NFC_NFC_Text(){
420    if(line_mode){
421        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUIsNormalized,NFCFileLines, numLines, UNORM_NFC, options,uselen);
422        return func;
423    }else{
424        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUIsNormalized,NFCBuffer, NFCBufferLen, UNORM_NFC, options,uselen);
425        return func;
426    }
427}
428UPerfFunction* NormalizerPerformanceTest::TestIsNormalized_NFC_Orig_Text(){
429    if(line_mode){
430        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUIsNormalized,lines, numLines, UNORM_NFC, options,uselen);
431        return func;
432    }else{
433        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUIsNormalized,buffer, bufferLen, UNORM_NFC, options,uselen);
434        return func;
435    }
436}
437
438UPerfFunction* NormalizerPerformanceTest::TestIsNormalized_NFD_NFD_Text(){
439    if(line_mode){
440        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUIsNormalized,NFDFileLines, numLines, UNORM_NFD, options,uselen);
441        return func;
442    }else{
443        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUIsNormalized,NFDBuffer, NFDBufferLen, UNORM_NFD, options,uselen);
444        return func;
445    }
446}
447UPerfFunction* NormalizerPerformanceTest::TestIsNormalized_NFD_NFC_Text(){
448    if(line_mode){
449        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUIsNormalized,NFCFileLines, numLines, UNORM_NFD, options,uselen);
450        return func;
451    }else{
452        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUIsNormalized,NFCBuffer, NFCBufferLen, UNORM_NFD, options,uselen);
453        return func;
454    }
455}
456UPerfFunction* NormalizerPerformanceTest::TestIsNormalized_NFD_Orig_Text(){
457    if(line_mode){
458        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUIsNormalized,lines, numLines, UNORM_NFD, options,uselen);
459        return func;
460    }else{
461        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUIsNormalized,buffer, bufferLen, UNORM_NFD, options,uselen);
462        return func;
463    }
464}
465
466UPerfFunction* NormalizerPerformanceTest::TestIsNormalized_FCD_NFD_Text(){
467    if(line_mode){
468        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUIsNormalized,NFDFileLines, numLines, UNORM_FCD, options,uselen);
469        return func;
470    }else{
471        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUIsNormalized,NFDBuffer, NFDBufferLen, UNORM_FCD, options,uselen);
472        return func;
473    }
474}
475UPerfFunction* NormalizerPerformanceTest::TestIsNormalized_FCD_NFC_Text(){
476    if(line_mode){
477        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUIsNormalized,NFCFileLines, numLines, UNORM_FCD, options,uselen);
478        return func;
479    }else{
480        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUIsNormalized,NFCBuffer, NFCBufferLen, UNORM_FCD, options,uselen);
481        return func;
482    }
483}
484UPerfFunction* NormalizerPerformanceTest::TestIsNormalized_FCD_Orig_Text(){
485    if(line_mode){
486        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUIsNormalized,lines, numLines, UNORM_FCD, options,uselen);
487        return func;
488    }else{
489        QuickCheckPerfFunction* func = new QuickCheckPerfFunction(ICUIsNormalized,buffer, bufferLen, UNORM_FCD, options,uselen);
490        return func;
491    }
492}
493
494int main(int argc, const char* argv[]){
495    UErrorCode status = U_ZERO_ERROR;
496    NormalizerPerformanceTest test(argc, argv, status);
497    if(U_FAILURE(status)){
498        return status;
499    }
500    if(test.run()==FALSE){
501        fprintf(stderr,"FAILED: Tests could not be run please check the arguments.\n");
502        return -1;
503    }
504    return 0;
505}
506