]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/perf/howExpensiveIs/howExpensiveIs.cpp
ICU-57132.0.1.tar.gz
[apple/icu.git] / icuSources / test / perf / howExpensiveIs / howExpensiveIs.cpp
1 /*
2 **********************************************************************
3 * Copyright (c) 2011-2016,International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 */
7 #include <stdio.h>
8 #include <string.h>
9
10 #include "cmemory.h"
11 #include "sieve.h"
12 #include "unicode/utimer.h"
13 #include "udbgutil.h"
14 #include "unicode/ustring.h"
15 #include "unicode/decimfmt.h"
16 #include "unicode/udat.h"
17 U_NAMESPACE_USE
18
19 #if U_PLATFORM_IMPLEMENTS_POSIX
20 #include <unistd.h>
21
22 static void usage(const char *prog) {
23 fprintf(stderr, "Usage: %s [ -f outfile.xml ] [ -t 'TestName' ]\n", prog);
24 }
25 #endif
26
27 void runTests(void);
28
29 #ifndef ITERATIONS
30 #define ITERATIONS 5
31 #endif
32
33 #ifndef TEST_LOCALE
34 #define TEST_LOCALE "en_US"
35 #endif
36
37 FILE *out = NULL;
38 UErrorCode setupStatus = U_ZERO_ERROR;
39 const char *outName = NULL;
40 int listmode = 0;
41 const char *testName = NULL;
42 const char *progname = NULL;
43 int errflg = 0;
44 int testhit = 0;
45
46 int testMatch(const char *aName) {
47 if(testName==NULL) return 1;
48 int len = strlen(testName);
49 if(testName[len-1]=='*') {
50 return strncmp(testName,aName,len-1);
51 } else {
52 return strcmp(testName,aName);
53 }
54 }
55
56 int main(int argc, char * const * argv){
57 #if U_DEBUG
58 fprintf(stderr,"%s: warning: U_DEBUG is on.\n", argv[0]);
59 #endif
60 #if U_DEBUG
61 {
62 double m;
63 double s = uprv_getSieveTime(&m);
64 fprintf(stderr, "** Standard sieve time: %.9fs +/- %.9fs (%d iterations)\n", s,m, (int)U_LOTS_OF_TIMES);
65 }
66 #endif
67
68 #if U_PLATFORM_IMPLEMENTS_POSIX
69 int c;
70 //extern int optind;
71 extern char *optarg;
72 while((c=getopt(argc,argv,"lf:t:")) != EOF) {
73 switch(c) {
74 case 'f':
75 outName = optarg;
76 break;
77 case 'l':
78 listmode++;
79 break;
80 case 't':
81 testName = optarg;
82 break;
83 case '?':
84 errflg++;
85 }
86 if(errflg) {
87 usage(progname);
88 return 0;
89 }
90 }
91 /* for ( ; optind < argc; optind++) { ... argv[optind] } */
92 #else
93 if(argc==2) {
94 outName = argv[1];
95 } else if(argc>2) {
96 fprintf(stderr, "Err: usage: %s [ output-file.xml ]\n", argv[0]);
97 }
98 #endif
99
100 if(listmode && outName != NULL ) {
101 fprintf(stderr, "Warning: no output when list mode\n");
102 outName=NULL;
103 }
104
105 if(outName != NULL) {
106
107
108 out=fopen(outName,"w");
109 if(out==NULL) {
110 fprintf(stderr,"Err: can't open %s for writing.\n", outName);
111 return 1;
112 } else {
113 fprintf(stderr, "# writing results to %s\n", outName);
114 }
115 fprintf(out, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
116 fprintf(out, "<tests icu=\"%s\">\n", U_ICU_VERSION);
117 fprintf(out, "<!-- %s -->\n", U_COPYRIGHT_STRING);
118 } else {
119 fprintf(stderr, "# (no output)\n");
120 }
121
122 if(listmode && testName!=NULL) {
123 fprintf(stderr, "ERR: no -l mode when specific test with -t\n");
124 usage(progname);
125 return 1;
126 }
127
128
129 runTests();
130
131
132 if(out!=NULL) {
133 #ifndef SKIP_INFO
134 udbg_writeIcuInfo(out);
135 #endif
136 fprintf(out, "</tests>\n");
137 fclose(out);
138 }
139
140 if(U_FAILURE(setupStatus)) {
141 fprintf(stderr, "Error in tests: %s\n", u_errorName(setupStatus));
142 return 1;
143 }
144
145 return 0;
146 }
147
148 class HowExpensiveTest {
149 public:
150 virtual ~HowExpensiveTest(){}
151 protected:
152 HowExpensiveTest(const char *name, const char *file, int32_t line) : fName(name), fFile(file), fLine(line) {}
153 protected:
154 /**
155 * @return number of iterations
156 */
157 virtual int32_t run() = 0;
158 virtual void warmup() { run(); }
159 public:
160 virtual const char *getName() { return fName; }
161 public:
162 virtual int32_t runTest(double *subTime) {
163 UTimer a,b;
164 utimer_getTime(&a);
165 int32_t iter = run();
166 utimer_getTime(&b);
167 *subTime = utimer_getDeltaSeconds(&a,&b);
168 return iter;
169 }
170
171 virtual int32_t runTests(double *subTime, double *marginOfError) {
172 warmup(); /* warmup */
173 double times[ITERATIONS];
174 int subIterations = 0;
175 for(int i=0;i<ITERATIONS;i++) {
176 subIterations = runTest(&times[i]);
177 #if U_DEBUG
178 fprintf(stderr, "trial: %d/%d = %.9fs\n", i, ITERATIONS,times[i]);
179 fflush(stderr);
180 #endif
181 }
182 uint32_t iterations = ITERATIONS;
183 *subTime = uprv_getMeanTime(times,&iterations,marginOfError);
184 return subIterations;
185 }
186 public:
187 const char *fName;
188 const char *fFile;
189 int32_t fLine;
190 int32_t fIterations;
191 };
192
193 void runTestOn(HowExpensiveTest &t) {
194 if(U_FAILURE(setupStatus)) return; // silently
195 const char *tn = t.getName();
196 if(testName!=NULL && testMatch(tn)) return; // skipped.
197 if(listmode) {
198 fprintf(stderr, "%s:%d:\t%s\n", t.fFile, t.fLine, t.getName());
199 testhit++;
200 return;
201 } else {
202 fprintf(stderr, "%s:%d: Running: %s\n", t.fFile, t.fLine, t.getName());
203 testhit++;
204 }
205 double sieveTime = uprv_getSieveTime(NULL);
206 double st;
207 double me;
208
209 fflush(stdout);
210 fflush(stderr);
211 int32_t iter = t.runTests(&st,&me);
212 if(U_FAILURE(setupStatus)) {
213 fprintf(stderr, "Error in tests: %s\n", u_errorName(setupStatus));
214 return;
215 }
216 fflush(stdout);
217 fflush(stderr);
218
219 double stn = st/sieveTime;
220
221 printf("%s\t%.9f\t%.9f +/- %.9f, @ %d iter\n", t.getName(),stn,st,me,iter);
222
223 if(out!=NULL) {
224 fprintf(out, " <test name=\"%s\" standardizedTime=\"%f\" realDuration=\"%f\" marginOfError=\"%f\" iterations=\"%d\" />\n",
225 tn,stn,st,me,iter);
226 fflush(out);
227 }
228 }
229
230 /* ------------------- test code here --------------------- */
231
232 class SieveTest : public HowExpensiveTest {
233 public:
234 virtual ~SieveTest(){}
235 SieveTest():HowExpensiveTest("SieveTest",__FILE__,__LINE__){}
236 virtual int32_t run(){return 0;} // dummy
237 int32_t runTest(double *subTime) {
238 *subTime = uprv_getSieveTime(NULL);
239 return U_LOTS_OF_TIMES;
240 }
241 virtual int32_t runTests(double *subTime, double *marginOfError) {
242 *subTime = uprv_getSieveTime(marginOfError);
243 return U_LOTS_OF_TIMES;
244 }
245 };
246
247
248 /* ------- NumParseTest ------------- */
249 #include "unicode/unum.h"
250 /* open and close tests */
251 #define OCName(svc,ub,testn,suffix,n) testn ## svc ## ub ## suffix ## n
252 #define OCStr(svc,ub,suffix,n) "Test_" # svc # ub # suffix # n
253 #define OCRun(svc,ub,suffix) svc ## ub ## suffix
254 // TODO: run away screaming
255 #define OpenCloseTest(n, svc,suffix,c,a,d) class OCName(svc,_,Test_,suffix,n) : public HowExpensiveTest { public: OCName(svc,_,Test_,suffix,n)():HowExpensiveTest(OCStr(svc,_,suffix,n),__FILE__,__LINE__) c int32_t run() { int32_t i; for(i=0;i<U_LOTS_OF_TIMES;i++){ OCRun(svc,_,close) ( OCRun(svc,_,suffix) a ); } return i; } void warmup() { OCRun(svc,_,close) ( OCRun(svc,_,suffix) a); } virtual ~ OCName(svc,_,Test_,suffix,n) () d };
256 #define QuickTest(n,c,r,d) class n : public HowExpensiveTest { public: n():HowExpensiveTest(#n,__FILE__,__LINE__) c int32_t run() r virtual ~n () d };
257
258 class NumTest : public HowExpensiveTest {
259 private:
260 double fExpect;
261 UNumberFormat *fFmt;
262 UnicodeString fPat;
263 UnicodeString fString;
264 const UChar *fStr;
265 int32_t fLen;
266 const char *fFile;
267 int fLine;
268 const char *fCPat;
269 const char *fCStr;
270 char name[100];
271 public:
272 virtual const char *getName() {
273 if(name[0]==0) {
274 sprintf(name,"%s:p=|%s|,str=|%s|",getClassName(),fCPat,fCStr);
275 }
276 return name;
277 }
278 protected:
279 virtual UNumberFormat* initFmt() {
280 return unum_open(UNUM_PATTERN_DECIMAL, fPat.getTerminatedBuffer(), -1, TEST_LOCALE, 0, &setupStatus);
281 }
282 virtual const char *getClassName() {
283 return "NumTest";
284 }
285 public:
286 NumTest(const char *pat, const char *num, double expect, const char *FILE, int LINE)
287 : HowExpensiveTest("(n/a)",FILE, LINE),
288 fExpect(expect),
289 fFmt(0),
290 fPat(pat, -1, US_INV),
291 fString(num,-1,US_INV),
292 fStr(fString.getTerminatedBuffer()),
293 fLen(u_strlen(fStr)),
294 fFile(FILE),
295 fLine(LINE),
296 fCPat(pat),
297 fCStr(num)
298 {
299 name[0]=0;
300 }
301 void warmup() {
302 fFmt = initFmt();
303 if(U_SUCCESS(setupStatus)) {
304 double trial = unum_parseDouble(fFmt,fStr,fLen, NULL, &setupStatus);
305 if(U_SUCCESS(setupStatus) && trial!=fExpect) {
306 setupStatus = U_INTERNAL_PROGRAM_ERROR;
307 printf("%s:%d: warmup() %s got %.8f expected %.8f\n",
308 fFile,fLine,getName(),trial,fExpect);
309 }
310 }
311 }
312 int32_t run() {
313 double trial=0.0;
314 int i;
315 for(i=0;i<U_LOTS_OF_TIMES;i++){
316 trial = unum_parse(fFmt,fStr,fLen, NULL, &setupStatus);
317 }
318 return i;
319 }
320 virtual ~NumTest(){}
321 };
322
323 #define DO_NumTest(p,n,x) { NumTest t(p,n,x,__FILE__,__LINE__); runTestOn(t); }
324
325
326 class AttrNumTest : public NumTest
327 {
328 private:
329 UNumberFormatAttribute fAttr;
330 int32_t fAttrValue;
331 char name2[100];
332 protected:
333 virtual const char *getClassName() {
334 sprintf(name2,"AttrNumTest:%d=%d", fAttr,fAttrValue);
335 return name2;
336 }
337 public:
338 AttrNumTest(const char *pat, const char *num, double expect, const char *FILE, int LINE, UNumberFormatAttribute attr, int32_t newValue)
339 : NumTest(pat,num,expect,FILE,LINE),
340 fAttr(attr),
341 fAttrValue(newValue)
342 {
343 }
344 virtual UNumberFormat* initFmt() {
345 UNumberFormat *fmt = NumTest::initFmt();
346 unum_setAttribute(fmt, fAttr,fAttrValue);
347 return fmt;
348 }
349 };
350
351 #define DO_AttrNumTest(p,n,x,a,v) { AttrNumTest t(p,n,x,__FILE__,__LINE__,a,v); runTestOn(t); }
352
353
354 class NOXNumTest : public NumTest
355 {
356 private:
357 UNumberFormatAttribute fAttr;
358 int32_t fAttrValue;
359 char name2[100];
360 protected:
361 virtual const char *getClassName() {
362 sprintf(name2,"NOXNumTest:%d=%d", fAttr,fAttrValue);
363 return name2;
364 }
365 public:
366 NOXNumTest(const char *pat, const char *num, double expect, const char *FILE, int LINE /*, UNumberFormatAttribute attr, int32_t newValue */)
367 : NumTest(pat,num,expect,FILE,LINE) /* ,
368 fAttr(attr),
369 fAttrValue(newValue) */
370 {
371 }
372 virtual UNumberFormat* initFmt() {
373 UNumberFormat *fmt = NumTest::initFmt();
374 //unum_setAttribute(fmt, fAttr,fAttrValue);
375 return fmt;
376 }
377 };
378
379 #define DO_NOXNumTest(p,n,x) { NOXNumTest t(p,n,x,__FILE__,__LINE__); runTestOn(t); }
380
381 #define DO_TripleNumTest(p,n,x) DO_AttrNumTest(p,n,x,UNUM_PARSE_ALL_INPUT,UNUM_YES) \
382 DO_AttrNumTest(p,n,x,UNUM_PARSE_ALL_INPUT,UNUM_NO) \
383 DO_AttrNumTest(p,n,x,UNUM_PARSE_ALL_INPUT,UNUM_MAYBE)
384
385
386 class NumFmtTest : public HowExpensiveTest {
387 private:
388 double fExpect;
389 UNumberFormat *fFmt;
390 UnicodeString fPat;
391 UnicodeString fString;
392 const UChar *fStr;
393 int32_t fLen;
394 const char *fFile;
395 int fLine;
396 const char *fCPat;
397 const char *fCStr;
398 char name[100];
399 public:
400 virtual const char *getName() {
401 if(name[0]==0) {
402 sprintf(name,"%s:p=|%s|,str=|%s|",getClassName(),fCPat,fCStr);
403 }
404 return name;
405 }
406 protected:
407 virtual UNumberFormat* initFmt() {
408 return unum_open(UNUM_PATTERN_DECIMAL, fPat.getTerminatedBuffer(), -1, TEST_LOCALE, 0, &setupStatus);
409 }
410 virtual const char *getClassName() {
411 return "NumFmtTest";
412 }
413 public:
414 NumFmtTest(const char *pat, const char *num, double expect, const char *FILE, int LINE)
415 : HowExpensiveTest("(n/a)",FILE, LINE),
416 fExpect(expect),
417 fFmt(0),
418 fPat(pat, -1, US_INV),
419 fString(num,-1,US_INV),
420 fStr(fString.getTerminatedBuffer()),
421 fLen(u_strlen(fStr)),
422 fFile(FILE),
423 fLine(LINE),
424 fCPat(pat),
425 fCStr(num)
426 {
427 name[0]=0;
428 }
429 void warmup() {
430 fFmt = initFmt();
431 UChar buf[100];
432 if(U_SUCCESS(setupStatus)) {
433 int32_t trial = unum_formatDouble(fFmt,fExpect, buf, 100, NULL, &setupStatus);
434 if(!U_SUCCESS(setupStatus)
435 || trial!=fLen
436 ||trial<=0
437 || u_strncmp(fStr,buf,trial) ) {
438 char strBuf[200];
439 u_strToUTF8(strBuf,200,NULL,buf,trial+1,&setupStatus);
440 printf("%s:%d: warmup() %s got %s expected %s, err %s\n",
441 fFile,fLine,getName(),strBuf,fCStr, u_errorName(setupStatus));
442 setupStatus = U_INTERNAL_PROGRAM_ERROR;
443 }
444 }
445 }
446 int32_t run() {
447 int32_t trial;
448 int i;
449 UChar buf[100];
450 if(U_SUCCESS(setupStatus)) {
451 for(i=0;i<U_LOTS_OF_TIMES;i++){
452 trial = unum_formatDouble(fFmt,fExpect, buf, 100, NULL, &setupStatus);
453 }
454 }
455 return i;
456 }
457 virtual ~NumFmtTest(){}
458 };
459
460 #define DO_NumFmtTest(p,n,x) { NumFmtTest t(p,n,x,__FILE__,__LINE__); runTestOn(t); }
461
462 class NumFmtInt64Test : public HowExpensiveTest {
463 public:
464 enum EMode {
465 kDefault,
466 kPattern,
467 kApplyPattern,
468 kGroupOff,
469 kApplyGroupOff
470 };
471 private:
472 EMode fMode;
473 int64_t fExpect;
474 UNumberFormat *fFmt;
475 UnicodeString fPat;
476 UnicodeString fString;
477 const UChar *fStr;
478 int32_t fLen;
479 const char *fFile;
480 int fLine;
481 const char *fCPat;
482 const char *fCStr;
483 char name[100];
484 public:
485 virtual const char *getName() {
486 if(name[0]==0) {
487 sprintf(name,"%s:p=|%s|,str=|%s|",getClassName(),fCPat,fCStr);
488 }
489 return name;
490 }
491 protected:
492 virtual UNumberFormat* initFmt() {
493 switch(fMode) {
494 case kPattern:
495 return unum_open(UNUM_PATTERN_DECIMAL, fPat.getTerminatedBuffer(), -1, TEST_LOCALE, 0, &setupStatus);
496 case kApplyPattern:
497 {
498 UNumberFormat *fmt = unum_open(UNUM_DECIMAL, NULL, -1, TEST_LOCALE, 0, &setupStatus);
499 unum_applyPattern(fmt, FALSE, fPat.getTerminatedBuffer(), -1, NULL, &setupStatus);
500 return fmt;
501 }
502 case kGroupOff:
503 {
504 UNumberFormat *fmt = unum_open(UNUM_PATTERN_DECIMAL, fPat.getTerminatedBuffer(), -1, TEST_LOCALE, 0, &setupStatus);
505 unum_setAttribute(fmt, UNUM_GROUPING_USED, UNUM_NO);
506 return fmt;
507 }
508 case kApplyGroupOff:
509 {
510 UNumberFormat *fmt = unum_open(UNUM_DECIMAL, NULL, -1, TEST_LOCALE, 0, &setupStatus);
511 unum_applyPattern(fmt, FALSE, fPat.getTerminatedBuffer(), -1, NULL, &setupStatus);
512 unum_setAttribute(fmt, UNUM_GROUPING_USED, UNUM_NO);
513 return fmt;
514 }
515 default:
516 case kDefault:
517 return unum_open(UNUM_DEFAULT, NULL, -1, TEST_LOCALE, 0, &setupStatus);
518 }
519 }
520 virtual const char *getClassName() {
521 switch(fMode) {
522 case EMode::kDefault:
523 return "NumFmtInt64Test (default)";
524 case EMode::kPattern:
525 return "NumFmtInt64Test (pattern)";
526 case EMode::kApplyPattern:
527 return "NumFmtInt64Test (applypattern)";
528 case EMode::kGroupOff:
529 return "NumFmtInt64Test (pattern, group=off)";
530 case EMode::kApplyGroupOff:
531 return "NumFmtInt64Test (applypattern, group=off)";
532 default:
533 return "NumFmtInt64Test (? ? ?)";
534 }
535 }
536 public:
537 NumFmtInt64Test(const char *pat, const char *num, int64_t expect, const char *FILE, int LINE, EMode mode)
538 : HowExpensiveTest("(n/a)",FILE, LINE),
539 fMode(mode),
540 fExpect(expect),
541 fFmt(0),
542 fPat(pat, -1, US_INV),
543 fString(num,-1,US_INV),
544 fStr(fString.getTerminatedBuffer()),
545 fLen(u_strlen(fStr)),
546 fFile(FILE),
547 fLine(LINE),
548 fCPat(pat),
549 fCStr(num)
550 {
551 name[0]=0;
552 }
553 void warmup() {
554 fFmt = initFmt();
555 UChar buf[100];
556 if(U_SUCCESS(setupStatus)) {
557 int32_t trial = unum_formatInt64(fFmt,fExpect, buf, 100, NULL, &setupStatus);
558 if(!U_SUCCESS(setupStatus)
559 || trial!=fLen
560 ||trial<=0
561 || u_strncmp(fStr,buf,trial) ) {
562 char strBuf[200];
563 u_strToUTF8(strBuf,200,NULL,buf,trial+1,&setupStatus);
564 printf("%s:%d: warmup() %s got %s (len %d) expected %s (len %d), err %s\n",
565 fFile,fLine,getName(),strBuf,trial,fCStr,fLen, u_errorName(setupStatus));
566 setupStatus = U_INTERNAL_PROGRAM_ERROR;
567 }
568 }
569 }
570 int32_t run() {
571 int32_t trial;
572 int i;
573 UChar buf[100];
574 if(U_SUCCESS(setupStatus)) {
575 for(i=0;i<U_LOTS_OF_TIMES;i++){
576 trial = unum_formatInt64(fFmt,fExpect, buf, 100, NULL, &setupStatus);
577 }
578 }
579 return i;
580 }
581 virtual ~NumFmtInt64Test(){}
582 };
583
584 /**
585 * unum_open .. with pattern, == new DecimalFormat(pattern)
586 */
587 #define DO_NumFmtInt64Test(p,n,x) { NumFmtInt64Test t(p,n,x,__FILE__,__LINE__,NumFmtInt64Test::EMode::kPattern); runTestOn(t); }
588 /**
589 * unum_open(UNUM_DECIMAL), then
590 */
591 #define DO_NumFmtInt64Test_apply(p,n,x) { NumFmtInt64Test t(p,n,x,__FILE__,__LINE__,NumFmtInt64Test::EMode::kApplyPattern); runTestOn(t); }
592
593 #define DO_NumFmtInt64Test_default(p,n,x) { NumFmtInt64Test t(p,n,x,__FILE__,__LINE__,NumFmtInt64Test::EMode::kDefault); runTestOn(t); }
594 #define DO_NumFmtInt64Test_gr0(p,n,x) { NumFmtInt64Test t(p,n,x,__FILE__,__LINE__,NumFmtInt64Test::EMode::kGroupOff); runTestOn(t); }
595 #define DO_NumFmtInt64Test_applygr0(p,n,x) { NumFmtInt64Test t(p,n,x,__FILE__,__LINE__,NumFmtInt64Test::EMode::kApplyGroupOff); runTestOn(t); }
596
597
598 class NumFmtStringPieceTest : public HowExpensiveTest {
599 private:
600 const StringPiece &fExpect;
601 UNumberFormat *fFmt;
602 UnicodeString fPat;
603 UnicodeString fString;
604 const UChar *fStr;
605 int32_t fLen;
606 const char *fFile;
607 int fLine;
608 const char *fCPat;
609 const char *fCStr;
610 char name[100];
611 public:
612 virtual const char *getName() {
613 if(name[0]==0) {
614 sprintf(name,"%s:p=|%s|,str=|%s|,sp=|%s|",getClassName(),fCPat,fCStr, fExpect.data());
615 }
616 return name;
617 }
618 protected:
619 virtual UNumberFormat* initFmt() {
620 DecimalFormat *d = new DecimalFormat(setupStatus);
621 UParseError pe;
622 d->applyPattern(fPat, pe, setupStatus);
623 return (UNumberFormat*) d;
624 }
625 virtual const char *getClassName() {
626 return "NumFmtStringPieceTest";
627 }
628 public:
629 NumFmtStringPieceTest(const char *pat, const char *num, const StringPiece& expect, const char *FILE, int LINE)
630 : HowExpensiveTest("(n/a)",FILE, LINE),
631 fExpect(expect),
632 fFmt(0),
633 fPat(pat, -1, US_INV),
634 fString(num,-1,US_INV),
635 fStr(fString.getTerminatedBuffer()),
636 fLen(u_strlen(fStr)),
637 fFile(FILE),
638 fLine(LINE),
639 fCPat(pat),
640 fCStr(num)
641 {
642 name[0]=0;
643 }
644 void warmup() {
645 fFmt = initFmt();
646 UnicodeString buf;
647 if(U_SUCCESS(setupStatus)) {
648 buf.remove();
649 ((const DecimalFormat*)fFmt)->format(fExpect, buf, NULL, setupStatus);
650 if(!U_SUCCESS(setupStatus)
651 || fString!=buf
652 ) {
653 char strBuf[200];
654 u_strToUTF8(strBuf,200,NULL,buf.getTerminatedBuffer(),buf.length()+1,&setupStatus);
655 printf("%s:%d: warmup() %s got %s (len %d) expected %s (len %d), err %s\n",
656 fFile,fLine,getName(),strBuf,buf.length(),fCStr,fLen, u_errorName(setupStatus));
657 setupStatus = U_INTERNAL_PROGRAM_ERROR;
658 }
659 }
660 }
661
662 int32_t run() {
663 #if U_DEBUG
664 int32_t trial;
665 #endif
666 int i=0;
667 UnicodeString buf;
668 if(U_SUCCESS(setupStatus)) {
669 for(i=0;i<U_LOTS_OF_TIMES;i++){
670 buf.remove();
671 ((const DecimalFormat*)fFmt)->format(fExpect, buf, NULL, setupStatus);
672 }
673 }
674 return i;
675 }
676 virtual ~NumFmtStringPieceTest(){}
677 };
678
679 #define DO_NumFmtStringPieceTest(p,n,x) { NumFmtStringPieceTest t(p,n,x,__FILE__,__LINE__); runTestOn(t); }
680
681 // TODO: move, scope.
682 static UChar pattern[] = { 0x23 }; // '#'
683 static UChar strdot[] = { '2', '.', '0', 0 };
684 static UChar strspc[] = { '2', ' ', 0 };
685 static UChar strgrp[] = {'2',',','2','2','2', 0 };
686 static UChar strbeng[] = {0x09E8,0x09E8,0x09E8,0x09E8, 0 };
687
688 UNumberFormat *NumParseTest_fmt;
689
690 // TODO: de-uglify.
691 QuickTest(NumParseTest,{ static UChar pattern[] = { 0x23 }; NumParseTest_fmt = unum_open(UNUM_PATTERN_DECIMAL, pattern, 1, TEST_LOCALE, 0, &setupStatus); },{ int32_t i; static UChar str[] = { 0x31 };double val; for(i=0;i<U_LOTS_OF_TIMES;i++) { val=unum_parse(NumParseTest_fmt,str,1,NULL,&setupStatus); } return i; },{unum_close(NumParseTest_fmt);})
692
693 QuickTest(NumParseTestdot,{ static UChar pattern[] = { 0x23 }; NumParseTest_fmt = unum_open(UNUM_PATTERN_DECIMAL, pattern, 1, TEST_LOCALE, 0, &setupStatus); },{ int32_t i; double val; for(i=0;i<U_LOTS_OF_TIMES;i++) { val=unum_parse(NumParseTest_fmt,strdot,1,NULL,&setupStatus); } return i; },{unum_close(NumParseTest_fmt);})
694 QuickTest(NumParseTestspc,{ static UChar pattern[] = { 0x23 }; NumParseTest_fmt = unum_open(UNUM_PATTERN_DECIMAL, pattern, 1, TEST_LOCALE, 0, &setupStatus); },{ int32_t i; double val; for(i=0;i<U_LOTS_OF_TIMES;i++) { val=unum_parse(NumParseTest_fmt,strspc,1,NULL,&setupStatus); } return i; },{unum_close(NumParseTest_fmt);})
695 QuickTest(NumParseTestgrp,{ static UChar pattern[] = { 0x23 }; NumParseTest_fmt = unum_open(UNUM_PATTERN_DECIMAL, pattern, 1, TEST_LOCALE, 0, &setupStatus); },{ int32_t i; double val; for(i=0;i<U_LOTS_OF_TIMES;i++) { val=unum_parse(NumParseTest_fmt,strgrp,-1,NULL,&setupStatus); } return i; },{unum_close(NumParseTest_fmt);})
696
697 QuickTest(NumParseTestbeng,{ static UChar pattern[] = { 0x23 }; NumParseTest_fmt = unum_open(UNUM_PATTERN_DECIMAL, pattern, 1, TEST_LOCALE, 0, &setupStatus); },{ int32_t i; double val; for(i=0;i<U_LOTS_OF_TIMES;i++) { val=unum_parse(NumParseTest_fmt,strbeng,-1,NULL,&setupStatus); } return i; },{unum_close(NumParseTest_fmt);})
698
699 UDateFormat *DateFormatTest_fmt = NULL;
700 UDate sometime = 100000000.0;
701 UChar onekbuf[1024];
702 const int32_t onekbuf_len = UPRV_LENGTHOF(onekbuf);
703
704
705 QuickTest(DateFormatTestBasic, \
706 { \
707 DateFormatTest_fmt = udat_open(UDAT_DEFAULT, UDAT_DEFAULT, NULL, NULL, -1, NULL, -1, &setupStatus); \
708 }, \
709 { \
710 int i; \
711 for(i=0;i<U_LOTS_OF_TIMES;i++) \
712 { \
713 udat_format(DateFormatTest_fmt, sometime, onekbuf, onekbuf_len, NULL, &setupStatus); \
714 } \
715 return i; \
716 }, \
717 { \
718 udat_close(DateFormatTest_fmt); \
719 } \
720 )
721
722
723 QuickTest(NullTest,{},{int j=U_LOTS_OF_TIMES;while(--j);return U_LOTS_OF_TIMES;},{})
724
725 #if 0
726 #include <time.h>
727
728 QuickTest(RandomTest,{},{timespec ts; ts.tv_sec=rand()%4; int j=U_LOTS_OF_TIMES;while(--j) { ts.tv_nsec=100000+(rand()%10000)*1000000; nanosleep(&ts,NULL); return j;} return U_LOTS_OF_TIMES;},{})
729 #endif
730
731 OpenCloseTest(pattern,unum,open,{},(UNUM_PATTERN_DECIMAL,pattern,1,TEST_LOCALE,0,&setupStatus),{})
732 OpenCloseTest(default,unum,open,{},(UNUM_DEFAULT,NULL,-1,TEST_LOCALE,0,&setupStatus),{})
733 #if !UCONFIG_NO_CONVERSION
734 #include "unicode/ucnv.h"
735 OpenCloseTest(gb18030,ucnv,open,{},("gb18030",&setupStatus),{})
736 #endif
737 #include "unicode/ures.h"
738 OpenCloseTest(root,ures,open,{},(NULL,"root",&setupStatus),{})
739
740 void runTests() {
741 {
742 SieveTest t;
743 runTestOn(t);
744 }
745 #if 0
746 {
747 RandomTest t;
748 runTestOn(t);
749 }
750 #endif
751 {
752 NullTest t;
753 runTestOn(t);
754 }
755
756 #ifndef SKIP_DATEFMT_TESTS
757 {
758 DateFormatTestBasic t;
759 runTestOn(t);
760 }
761 #endif
762
763 #ifndef SKIP_NUMPARSE_TESTS
764 {
765 // parse tests
766
767 DO_NumTest("#","0",0.0);
768 DO_NumTest("#","2.0",2.0);
769 DO_NumTest("#","2 ",2);
770 DO_NumTest("#","-2 ",-2);
771 DO_NumTest("+#","+2",2);
772 DO_NumTest("#,###.0","2222.0",2222.0);
773 DO_NumTest("#.0","1.000000000000000000000000000000000000000000000000000000000000000000000000000000",1.0);
774 DO_NumTest("#","123456",123456);
775
776 // attr
777 #ifdef HAVE_UNUM_MAYBE
778 DO_AttrNumTest("#","0",0.0,UNUM_PARSE_ALL_INPUT,UNUM_YES);
779 DO_AttrNumTest("#","0",0.0,UNUM_PARSE_ALL_INPUT,UNUM_NO);
780 DO_AttrNumTest("#","0",0.0,UNUM_PARSE_ALL_INPUT,UNUM_MAYBE);
781 DO_TripleNumTest("#","2.0",2.0);
782 DO_AttrNumTest("#.0","1.000000000000000000000000000000000000000000000000000000000000000000000000000000",1.0,UNUM_PARSE_ALL_INPUT,UNUM_NO);
783 #endif
784
785
786 // { NumParseTestgrp t; runTestOn(t); }
787 { NumParseTestbeng t; runTestOn(t); }
788
789 }
790 #endif
791
792 #ifndef SKIP_NUMFORMAT_TESTS
793 // format tests
794 {
795
796 DO_NumFmtInt64Test("0000","0001",1);
797 DO_NumFmtInt64Test("0000","0000",0);
798 StringPiece sp3456("3456");
799 DO_NumFmtStringPieceTest("0000","3456",sp3456);
800 DO_NumFmtStringPieceTest("#","3456",sp3456);
801 StringPiece sp3("3");
802 DO_NumFmtStringPieceTest("0000","0003",sp3);
803 DO_NumFmtStringPieceTest("#","3",sp3);
804 StringPiece spn3("-3");
805 DO_NumFmtStringPieceTest("0000","-0003",spn3);
806 DO_NumFmtStringPieceTest("#","-3",spn3);
807 StringPiece spPI("123.456");
808 DO_NumFmtStringPieceTest("#.0000","123.4560",spPI);
809 DO_NumFmtStringPieceTest("#.00","123.46",spPI);
810
811 DO_NumFmtTest("#","0",0.0);
812 DO_NumFmtTest("#","12345",12345);
813 DO_NumFmtTest("#","-2",-2);
814 DO_NumFmtTest("+#","+2",2);
815
816 DO_NumFmtInt64Test("#","-682",-682);
817 DO_NumFmtInt64Test("#","0",0);
818 DO_NumFmtInt64Test("#","12345",12345);
819 DO_NumFmtInt64Test("#,###","12,345",12345);
820 DO_NumFmtInt64Test("#","1234",1234);
821 DO_NumFmtInt64Test("#","123",123);
822 DO_NumFmtInt64Test("#,###","123",123);
823 DO_NumFmtInt64Test_apply("#","123",123);
824 DO_NumFmtInt64Test_apply("#","12345",12345);
825 DO_NumFmtInt64Test_apply("#,###","123",123);
826 DO_NumFmtInt64Test_apply("#,###","12,345",12345);
827 DO_NumFmtInt64Test_default("","123",123);
828 DO_NumFmtInt64Test_default("","12,345",12345);
829 DO_NumFmtInt64Test_applygr0("#","123",123);
830 DO_NumFmtInt64Test_applygr0("#","12345",12345);
831 DO_NumFmtInt64Test_applygr0("#,###","123",123);
832 DO_NumFmtInt64Test_applygr0("#,###","12345",12345);
833 DO_NumFmtInt64Test_gr0("#","123",123);
834 DO_NumFmtInt64Test_gr0("#","12345",12345);
835 DO_NumFmtInt64Test_gr0("#,###","123",123);
836 DO_NumFmtInt64Test_gr0("#,###","12345",12345);
837 DO_NumFmtInt64Test("#","-2",-2);
838 DO_NumFmtInt64Test("+#","+2",2);
839 }
840
841 #ifndef SKIP_NUM_OPEN_TEST
842 {
843 Test_unum_opendefault t;
844 runTestOn(t);
845 }
846 {
847 Test_unum_openpattern t;
848 runTestOn(t);
849 }
850 #endif
851
852 #endif /* skip numformat tests */
853 #if !UCONFIG_NO_CONVERSION
854 {
855 Test_ucnv_opengb18030 t;
856 runTestOn(t);
857 }
858 #endif
859 {
860 Test_ures_openroot t;
861 runTestOn(t);
862 }
863
864 if(testhit==0) {
865 fprintf(stderr, "ERROR: no tests matched.\n");
866 }
867 }