]> git.saurik.com Git - apple/icu.git/blame - icuSources/tools/ctestfw/ctest.c
ICU-64260.0.1.tar.gz
[apple/icu.git] / icuSources / tools / ctestfw / ctest.c
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
b75a7d8f 3/*
73c04bcf 4********************************************************************************
b75a7d8f 5*
b331163b 6* Copyright (C) 1996-2014, International Business Machines
b75a7d8f
A
7* Corporation and others. All Rights Reserved.
8*
73c04bcf 9********************************************************************************
b75a7d8f
A
10*/
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <assert.h>
15#include <stdarg.h>
729e4ab9 16#include <ctype.h>
b75a7d8f 17
374ca955 18#include "unicode/utrace.h"
46f4442e 19#include "unicode/uclean.h"
729e4ab9 20#include "putilimp.h"
57a6839d 21#include "udbgutil.h"
374ca955 22
b75a7d8f
A
23/* NOTES:
24 3/20/1999 srl - strncpy called w/o setting nulls at the end
25 */
26
27#define MAXTESTNAME 128
28#define MAXTESTS 512
29#define MAX_TEST_LOG 4096
30
729e4ab9
A
31/**
32 * How may columns to indent the 'OK' markers.
33 */
34#define FLAG_INDENT 45
35/**
36 * How many lines of scrollage can go by before we need to remind the user what the test is.
37 */
38#define PAGE_SIZE_LIMIT 25
39
40#ifndef SHOW_TIMES
41#define SHOW_TIMES 1
42#endif
43
b75a7d8f
A
44struct TestNode
45{
b75a7d8f
A
46 void (*test)(void);
47 struct TestNode* sibling;
48 struct TestNode* child;
46f4442e 49 char name[1]; /* This is dynamically allocated off the end with malloc. */
b75a7d8f
A
50};
51
52
53static const struct TestNode* currentTest;
54
55typedef enum { RUNTESTS, SHOWTESTS } TestMode;
56#define TEST_SEPARATOR '/'
57
58#ifndef C_TEST_IMPL
59#define C_TEST_IMPL
60#endif
61
62#include "unicode/ctest.h"
63
64static char ERROR_LOG[MAX_TEST_LOG][MAXTESTNAME];
65
66/* Local prototypes */
67static TestNode* addTestNode( TestNode *root, const char *name );
68
46f4442e 69static TestNode *createTestNode(const char* name, int32_t nameLen);
b75a7d8f
A
70
71static int strncmp_nullcheck( const char* s1,
72 const char* s2,
73 int n );
74
75static void getNextLevel( const char* name,
76 int* nameLen,
77 const char** nextName );
78
729e4ab9
A
79static void iterateTestsWithLevel( const TestNode *root, int depth,
80 const TestNode** nodeList,
b75a7d8f
A
81 TestMode mode);
82
83static void help ( const char *argv0 );
84
85/**
86 * Do the work of logging an error. Doesn't increase the error count.
87 *
88 * @prefix optional prefix prepended to message, or NULL.
89 * @param pattern printf style pattern
90 * @param ap vprintf style arg list
91 */
92static void vlog_err(const char *prefix, const char *pattern, va_list ap);
b75a7d8f 93static void vlog_verbose(const char *prefix, const char *pattern, va_list ap);
57a6839d 94static UBool vlog_knownIssue(const char *ticket, const char *pattern, va_list ap);
b75a7d8f 95
729e4ab9
A
96/**
97 * Log test structure, with indent
98 * @param pattern printf pattern
99 */
100static void log_testinfo_i(const char *pattern, ...);
101
102/**
103 * Log test structure, NO indent
104 * @param pattern printf pattern
105 */
106static void log_testinfo(const char *pattern, ...);
107
b75a7d8f
A
108/* If we need to make the framework multi-thread safe
109 we need to pass around the following vars
110*/
111static int ERRONEOUS_FUNCTION_COUNT = 0;
112static int ERROR_COUNT = 0; /* Count of errors from all tests. */
729e4ab9 113static int ONE_ERROR = 0; /* were there any other errors? */
b75a7d8f
A
114static int DATA_ERROR_COUNT = 0; /* count of data related errors or warnings */
115static int INDENT_LEVEL = 0;
57a6839d
A
116static UBool NO_KNOWN = FALSE;
117static void *knownList = NULL;
118static char gTestName[1024] = "";
729e4ab9
A
119static UBool ON_LINE = FALSE; /* are we on the top line with our test name? */
120static UBool HANGING_OUTPUT = FALSE; /* did the user leave us without a trailing \n ? */
121static int GLOBAL_PRINT_COUNT = 0; /* global count of printouts */
b75a7d8f
A
122int REPEAT_TESTS_INIT = 0; /* Was REPEAT_TESTS initialized? */
123int REPEAT_TESTS = 1; /* Number of times to run the test */
124int VERBOSITY = 0; /* be No-verbose by default */
125int ERR_MSG =1; /* error messages will be displayed by default*/
126int QUICK = 1; /* Skip some of the slower tests? */
127int WARN_ON_MISSING_DATA = 0; /* Reduce data errs to warnings? */
374ca955 128UTraceLevel ICU_TRACE = UTRACE_OFF; /* ICU tracing level */
46f4442e
A
129size_t MINIMUM_MEMORY_SIZE_FAILURE = (size_t)-1; /* Minimum library memory allocation window that will fail. */
130size_t MAXIMUM_MEMORY_SIZE_FAILURE = (size_t)-1; /* Maximum library memory allocation window that will fail. */
729e4ab9
A
131static const char *ARGV_0 = "[ALL]";
132static const char *XML_FILE_NAME=NULL;
133static char XML_PREFIX[256];
57a6839d 134static const char *SUMMARY_FILE = NULL;
729e4ab9 135FILE *XML_FILE = NULL;
b75a7d8f
A
136/*-------------------------------------------*/
137
138/* strncmp that also makes sure there's a \0 at s2[0] */
139static int strncmp_nullcheck( const char* s1,
140 const char* s2,
141 int n )
142{
143 if (((int)strlen(s2) >= n) && s2[n] != 0) {
144 return 3; /* null check fails */
145 }
146 else {
147 return strncmp ( s1, s2, n );
148 }
149}
150
151static void getNextLevel( const char* name,
152 int* nameLen,
153 const char** nextName )
154{
155 /* Get the next component of the name */
156 *nextName = strchr(name, TEST_SEPARATOR);
157
158 if( *nextName != 0 )
159 {
160 char n[255];
161 *nameLen = (int)((*nextName) - name);
162 (*nextName)++; /* skip '/' */
163 strncpy(n, name, *nameLen);
164 n[*nameLen] = 0;
165 /*printf("->%s-< [%d] -> [%s]\n", name, *nameLen, *nextName);*/
166 }
167 else {
168 *nameLen = (int)strlen(name);
169 }
170}
171
46f4442e 172static TestNode *createTestNode(const char* name, int32_t nameLen)
b75a7d8f
A
173{
174 TestNode *newNode;
175
46f4442e 176 newNode = (TestNode*)malloc(sizeof(TestNode) + (nameLen + 1));
b75a7d8f 177
b75a7d8f
A
178 newNode->test = NULL;
179 newNode->sibling = NULL;
180 newNode->child = NULL;
181
46f4442e
A
182 strncpy( newNode->name, name, nameLen );
183 newNode->name[nameLen] = 0;
184
b75a7d8f
A
185 return newNode;
186}
187
374ca955
A
188void T_CTEST_EXPORT2
189cleanUpTestTree(TestNode *tn)
190{
b75a7d8f
A
191 if(tn->child != NULL) {
192 cleanUpTestTree(tn->child);
193 }
194 if(tn->sibling != NULL) {
195 cleanUpTestTree(tn->sibling);
196 }
197
198 free(tn);
729e4ab9 199
b75a7d8f
A
200}
201
202
374ca955
A
203void T_CTEST_EXPORT2
204addTest(TestNode** root,
205 TestFunctionPtr test,
206 const char* name )
b75a7d8f
A
207{
208 TestNode *newNode;
209
210 /*if this is the first Test created*/
211 if (*root == NULL)
46f4442e 212 *root = createTestNode("", 0);
b75a7d8f
A
213
214 newNode = addTestNode( *root, name );
215 assert(newNode != 0 );
216 /* printf("addTest: nreName = %s\n", newNode->name );*/
217
218 newNode->test = test;
219}
220
221/* non recursive insert function */
222static TestNode *addTestNode ( TestNode *root, const char *name )
223{
224 const char* nextName;
225 TestNode *nextNode, *curNode;
226 int nameLen; /* length of current 'name' */
227
228 /* remove leading slash */
229 if ( *name == TEST_SEPARATOR )
230 name++;
231
232 curNode = root;
233
234 for(;;)
235 {
236 /* Start with the next child */
237 nextNode = curNode->child;
238
239 getNextLevel ( name, &nameLen, &nextName );
240
241 /* printf("* %s\n", name );*/
242
243 /* if nextNode is already null, then curNode has no children
244 -- add them */
245 if( nextNode == NULL )
246 {
247 /* Add all children of the node */
248 do
249 {
b75a7d8f 250 /* Get the next component of the name */
46f4442e 251 getNextLevel(name, &nameLen, &nextName);
b75a7d8f
A
252
253 /* update curName to have the next name segment */
46f4442e 254 curNode->child = createTestNode(name, nameLen);
b75a7d8f
A
255 /* printf("*** added %s\n", curNode->child->name );*/
256 curNode = curNode->child;
257 name = nextName;
258 }
259 while( name != NULL );
260
261 return curNode;
262 }
263
264 /* Search across for the name */
265 while (strncmp_nullcheck ( name, nextNode->name, nameLen) != 0 )
266 {
267 curNode = nextNode;
268 nextNode = nextNode -> sibling;
269
270 if ( nextNode == NULL )
271 {
272 /* Did not find 'name' on this level. */
46f4442e 273 nextNode = createTestNode(name, nameLen);
b75a7d8f
A
274 curNode->sibling = nextNode;
275 break;
276 }
277 }
278
279 /* nextNode matches 'name' */
280
281 if (nextName == NULL) /* end of the line */
282 {
283 return nextNode;
284 }
285
286 /* Loop again with the next item */
287 name = nextName;
288 curNode = nextNode;
289 }
290}
291
729e4ab9
A
292/**
293 * Log the time taken. May not output anything.
294 * @param deltaTime change in time
295 */
296void T_CTEST_EXPORT2 str_timeDelta(char *str, UDate deltaTime) {
297 if (deltaTime > 110000.0 ) {
298 double mins = uprv_floor(deltaTime/60000.0);
299 sprintf(str, "[(%.0fm %.1fs)]", mins, (deltaTime-(mins*60000.0))/1000.0);
300 } else if (deltaTime > 1500.0) {
301 sprintf(str, "((%.1fs))", deltaTime/1000.0);
302 } else if(deltaTime>900.0) {
303 sprintf(str, "( %.2fs )", deltaTime/1000.0);
304 } else if(deltaTime > 5.0) {
305 sprintf(str, " (%.0fms) ", deltaTime);
306 } else {
307 str[0]=0; /* at least terminate it. */
308 }
309}
310
311static void print_timeDelta(UDate deltaTime) {
312 char str[256];
313 str_timeDelta(str, deltaTime);
314 if(str[0]) {
315 printf("%s", str);
316 }
317}
318
319/**
320 * Run or list tests (according to mode) in a subtree.
321 *
322 * @param root root of the subtree to operate on
323 * @param depth The depth of this tree (0=root)
324 * @param nodeList an array of MAXTESTS depth that's used for keeping track of where we are. nodeList[depth] points to the 'parent' at depth depth.
325 * @param mode what mode we are operating in.
326 */
b75a7d8f 327static void iterateTestsWithLevel ( const TestNode* root,
729e4ab9
A
328 int depth,
329 const TestNode** nodeList,
b75a7d8f
A
330 TestMode mode)
331{
332 int i;
b75a7d8f
A
333
334 char pathToFunction[MAXTESTNAME] = "";
335 char separatorString[2] = { TEST_SEPARATOR, '\0'};
729e4ab9
A
336#if SHOW_TIMES
337 UDate allStartTime = -1, allStopTime = -1;
338#endif
339
340 if(depth<2) {
341 allStartTime = uprv_getRawUTCtime();
342 }
b75a7d8f
A
343
344 if ( root == NULL )
345 return;
346
729e4ab9
A
347 /* record the current root node, and increment depth. */
348 nodeList[depth++] = root;
349 /* depth is now the depth of root's children. */
b75a7d8f 350
729e4ab9
A
351 /* Collect the 'path' to the current subtree. */
352 for ( i=0;i<(depth-1);i++ )
b75a7d8f 353 {
729e4ab9 354 strcat(pathToFunction, nodeList[i]->name);
b75a7d8f
A
355 strcat(pathToFunction, separatorString);
356 }
729e4ab9
A
357 strcat(pathToFunction, nodeList[i]->name); /* including 'root' */
358
359 /* print test name and space. */
360 INDENT_LEVEL = depth-1;
361 if(root->name[0]) {
362 log_testinfo_i("%s ", root->name);
363 } else {
364 log_testinfo_i("(%s) ", ARGV_0);
365 }
366 ON_LINE = TRUE; /* we are still on the line with the test name */
b75a7d8f 367
b75a7d8f 368
729e4ab9
A
369 if ( (mode == RUNTESTS) &&
370 (root->test != NULL)) /* if root is a leaf node, run it */
b75a7d8f
A
371 {
372 int myERROR_COUNT = ERROR_COUNT;
729e4ab9
A
373 int myGLOBAL_PRINT_COUNT = GLOBAL_PRINT_COUNT;
374#if SHOW_TIMES
375 UDate startTime, stopTime;
376 char timeDelta[256];
377 char timeSeconds[256];
378#else
379 const char timeDelta[] = "(unknown)";
380 const char timeSeconds[] = "0.000";
381#endif
b75a7d8f 382 currentTest = root;
729e4ab9
A
383 INDENT_LEVEL = depth; /* depth of subitems */
384 ONE_ERROR=0;
385 HANGING_OUTPUT=FALSE;
386#if SHOW_TIMES
387 startTime = uprv_getRawUTCtime();
388#endif
57a6839d 389 strcpy(gTestName, pathToFunction);
729e4ab9
A
390 root->test(); /* PERFORM THE TEST ************************/
391#if SHOW_TIMES
392 stopTime = uprv_getRawUTCtime();
393#endif
394 if(HANGING_OUTPUT) {
395 log_testinfo("\n");
396 HANGING_OUTPUT=FALSE;
397 }
398 INDENT_LEVEL = depth-1; /* depth of root */
b75a7d8f 399 currentTest = NULL;
729e4ab9
A
400 if((ONE_ERROR>0)&&(ERROR_COUNT==0)) {
401 ERROR_COUNT++; /* There was an error without a newline */
b75a7d8f 402 }
729e4ab9
A
403 ONE_ERROR=0;
404
405#if SHOW_TIMES
406 str_timeDelta(timeDelta, stopTime-startTime);
407 sprintf(timeSeconds, "%f", (stopTime-startTime)/1000.0);
408#endif
409 ctest_xml_testcase(pathToFunction, pathToFunction, timeSeconds, (myERROR_COUNT!=ERROR_COUNT)?"error":NULL);
410
411 if (myERROR_COUNT != ERROR_COUNT) {
412 log_testinfo_i("} ---[%d ERRORS in %s] ", ERROR_COUNT - myERROR_COUNT, pathToFunction);
413 strcpy(ERROR_LOG[ERRONEOUS_FUNCTION_COUNT++], pathToFunction);
414 } else {
415 if(!ON_LINE) { /* had some output */
416 int spaces = FLAG_INDENT-(depth-1);
417 log_testinfo_i("} %*s[OK] ", spaces, "---");
418 if((GLOBAL_PRINT_COUNT-myGLOBAL_PRINT_COUNT)>PAGE_SIZE_LIMIT) {
419 log_testinfo(" %s ", pathToFunction); /* in case they forgot. */
420 }
421 } else {
422 /* put -- out at 30 sp. */
3d1f044b 423 int spaces = FLAG_INDENT - ((int)strlen(root->name) + depth);
729e4ab9
A
424 if(spaces<0) spaces=0;
425 log_testinfo(" %*s[OK] ", spaces,"---");
426 }
427 }
428
429#if SHOW_TIMES
430 if(timeDelta[0]) printf("%s", timeDelta);
431#endif
432
433 ON_LINE = TRUE; /* we are back on-line */
b75a7d8f
A
434 }
435
729e4ab9 436 INDENT_LEVEL = depth-1; /* root */
b75a7d8f
A
437
438 /* we want these messages to be at 0 indent. so just push the indent level breifly. */
729e4ab9
A
439 if(mode==SHOWTESTS) {
440 log_testinfo("---%s%c\n",pathToFunction, nodeList[i]->test?' ':TEST_SEPARATOR );
441 }
b75a7d8f 442
729e4ab9
A
443 INDENT_LEVEL = depth;
444
445 if(root->child) {
446 int myERROR_COUNT = ERROR_COUNT;
447 int myGLOBAL_PRINT_COUNT = GLOBAL_PRINT_COUNT;
448 if(mode!=SHOWTESTS) {
449 INDENT_LEVEL=depth-1;
450 log_testinfo("{\n");
451 INDENT_LEVEL=depth;
452 }
453
454 iterateTestsWithLevel ( root->child, depth, nodeList, mode );
455
456 if(mode!=SHOWTESTS) {
457 INDENT_LEVEL=depth-1;
458 log_testinfo_i("} "); /* TODO: summarize subtests */
459 if((depth>1) && (ERROR_COUNT > myERROR_COUNT)) {
460 log_testinfo("[%d %s in %s] ", ERROR_COUNT-myERROR_COUNT, (ERROR_COUNT-myERROR_COUNT)==1?"error":"errors", pathToFunction);
461 } else if((GLOBAL_PRINT_COUNT-myGLOBAL_PRINT_COUNT)>PAGE_SIZE_LIMIT || (depth<1)) {
462 if(pathToFunction[0]) {
463 log_testinfo(" %s ", pathToFunction); /* in case they forgot. */
464 } else {
465 log_testinfo(" / (%s) ", ARGV_0);
466 }
467 }
b75a7d8f 468
729e4ab9
A
469 ON_LINE=TRUE;
470 }
471 }
472 depth--;
473
474#if SHOW_TIMES
475 if(depth<2) {
476 allStopTime = uprv_getRawUTCtime();
477 print_timeDelta(allStopTime-allStartTime);
478 }
479#endif
480
481 if(mode!=SHOWTESTS && ON_LINE) {
482 log_testinfo("\n");
483 }
b75a7d8f 484
729e4ab9
A
485 if ( depth != 0 ) { /* DO NOT iterate over siblings of the root. TODO: why not? */
486 iterateTestsWithLevel ( root->sibling, depth, nodeList, mode );
487 }
b75a7d8f
A
488}
489
490
491
374ca955
A
492void T_CTEST_EXPORT2
493showTests ( const TestNode *root )
b75a7d8f
A
494{
495 /* make up one for them */
729e4ab9 496 const TestNode *nodeList[MAXTESTS];
b75a7d8f
A
497
498 if (root == NULL)
499 log_err("TEST CAN'T BE FOUND!");
500
729e4ab9 501 iterateTestsWithLevel ( root, 0, nodeList, SHOWTESTS );
b75a7d8f
A
502
503}
504
374ca955
A
505void T_CTEST_EXPORT2
506runTests ( const TestNode *root )
b75a7d8f
A
507{
508 int i;
729e4ab9 509 const TestNode *nodeList[MAXTESTS];
b75a7d8f
A
510 /* make up one for them */
511
512
513 if (root == NULL)
514 log_err("TEST CAN'T BE FOUND!\n");
515
516 ERRONEOUS_FUNCTION_COUNT = ERROR_COUNT = 0;
729e4ab9 517 iterateTestsWithLevel ( root, 0, nodeList, RUNTESTS );
b75a7d8f
A
518
519 /*print out result summary*/
520
729e4ab9
A
521 ON_LINE=FALSE; /* just in case */
522
57a6839d
A
523 if(knownList != NULL) {
524 if( udbg_knownIssue_print(knownList) ) {
525 fprintf(stdout, "(To run suppressed tests, use the -K option.) \n\n");
526 }
527 udbg_knownIssue_close(knownList);
b331163b 528 knownList = NULL;
57a6839d
A
529 }
530
b75a7d8f
A
531 if (ERROR_COUNT)
532 {
729e4ab9
A
533 fprintf(stdout,"\nSUMMARY:\n");
534 fflush(stdout);
535 fprintf(stdout,"******* [Total error count:\t%d]\n", ERROR_COUNT);
536 fflush(stdout);
537 fprintf(stdout, " Errors in\n");
b75a7d8f 538 for (i=0;i < ERRONEOUS_FUNCTION_COUNT; i++)
729e4ab9 539 fprintf(stdout, "[%s]\n",ERROR_LOG[i]);
57a6839d
A
540 if(SUMMARY_FILE != NULL) {
541 FILE *summf = fopen(SUMMARY_FILE, "w");
542 if(summf!=NULL) {
543 for (i=0;i < ERRONEOUS_FUNCTION_COUNT; i++)
544 fprintf(summf, "%s\n",ERROR_LOG[i]);
545 fclose(summf);
546 }
547 }
b75a7d8f
A
548 }
549 else
550 {
729e4ab9 551 log_testinfo("\n[All tests passed successfully...]\n");
b75a7d8f
A
552 }
553
554 if(DATA_ERROR_COUNT) {
555 if(WARN_ON_MISSING_DATA==0) {
729e4ab9 556 log_testinfo("\t*Note* some errors are data-loading related. If the data used is not the \n"
b75a7d8f
A
557 "\tstock ICU data (i.e some have been added or removed), consider using\n"
558 "\tthe '-w' option to turn these errors into warnings.\n");
559 } else {
729e4ab9 560 log_testinfo("\t*WARNING* some data-loading errors were ignored by the -w option.\n");
b75a7d8f
A
561 }
562 }
563}
564
374ca955
A
565const char* T_CTEST_EXPORT2
566getTestName(void)
b75a7d8f
A
567{
568 if(currentTest != NULL) {
569 return currentTest->name;
570 } else {
571 return NULL;
572 }
573}
574
374ca955
A
575const TestNode* T_CTEST_EXPORT2
576getTest(const TestNode* root, const char* name)
b75a7d8f
A
577{
578 const char* nextName;
579 TestNode *nextNode;
580 const TestNode* curNode;
581 int nameLen; /* length of current 'name' */
582
73c04bcf 583 if (root == NULL) {
b75a7d8f 584 log_err("TEST CAN'T BE FOUND!\n");
73c04bcf
A
585 return NULL;
586 }
b75a7d8f
A
587 /* remove leading slash */
588 if ( *name == TEST_SEPARATOR )
589 name++;
590
591 curNode = root;
592
593 for(;;)
594 {
595 /* Start with the next child */
596 nextNode = curNode->child;
597
598 getNextLevel ( name, &nameLen, &nextName );
599
600 /* printf("* %s\n", name );*/
601
602 /* if nextNode is already null, then curNode has no children
603 -- add them */
604 if( nextNode == NULL )
605 {
606 return NULL;
607 }
608
609 /* Search across for the name */
610 while (strncmp_nullcheck ( name, nextNode->name, nameLen) != 0 )
611 {
612 curNode = nextNode;
613 nextNode = nextNode -> sibling;
614
615 if ( nextNode == NULL )
616 {
617 /* Did not find 'name' on this level. */
618 return NULL;
619 }
620 }
621
622 /* nextNode matches 'name' */
623
624 if (nextName == NULL) /* end of the line */
625 {
626 return nextNode;
627 }
628
629 /* Loop again with the next item */
630 name = nextName;
631 curNode = nextNode;
632 }
633}
634
729e4ab9
A
635/* =========== io functions ======== */
636
637static void go_offline_with_marker(const char *mrk) {
638 UBool wasON_LINE = ON_LINE;
639
640 if(ON_LINE) {
641 log_testinfo(" {\n");
642 ON_LINE=FALSE;
643 }
644
645 if(!HANGING_OUTPUT || wasON_LINE) {
646 if(mrk != NULL) {
647 fputs(mrk, stdout);
648 }
649 }
650}
651
652static void go_offline() {
653 go_offline_with_marker(NULL);
654}
655
656static void go_offline_err() {
657 go_offline();
658}
659
660static void first_line_verbose() {
661 go_offline_with_marker("v");
662}
663
664static void first_line_err() {
665 go_offline_with_marker("!");
666}
667
668static void first_line_info() {
669 go_offline_with_marker("\"");
670}
671
672static void first_line_test() {
673 fputs(" ", stdout);
674}
675
676
b75a7d8f
A
677static void vlog_err(const char *prefix, const char *pattern, va_list ap)
678{
679 if( ERR_MSG == FALSE){
680 return;
681 }
729e4ab9
A
682 fputs("!", stdout); /* col 1 - bang */
683 fprintf(stdout, "%-*s", INDENT_LEVEL,"" );
b75a7d8f 684 if(prefix) {
729e4ab9 685 fputs(prefix, stdout);
b75a7d8f 686 }
729e4ab9
A
687 vfprintf(stdout, pattern, ap);
688 fflush(stdout);
b75a7d8f 689 va_end(ap);
729e4ab9
A
690 if((*pattern==0) || (pattern[strlen(pattern)-1]!='\n')) {
691 HANGING_OUTPUT=1;
692 } else {
693 HANGING_OUTPUT=0;
694 }
695 GLOBAL_PRINT_COUNT++;
b75a7d8f
A
696}
697
57a6839d
A
698static UBool vlog_knownIssue(const char *ticket, const char *pattern, va_list ap)
699{
700 char buf[2048];
701 UBool firstForTicket;
702 UBool firstForWhere;
703
704 if(NO_KNOWN) return FALSE;
705 if(pattern==NULL) pattern="";
706
707 vsprintf(buf, pattern, ap);
708 knownList = udbg_knownIssue_open(knownList, ticket, gTestName, buf,
709 &firstForTicket, &firstForWhere);
710
711 if(firstForTicket || firstForWhere) {
b331163b 712 log_info("(Known issue #%s) %s\n", ticket, buf);
57a6839d 713 } else {
b331163b 714 log_verbose("(Known issue #%s) %s\n", ticket, buf);
57a6839d
A
715 }
716
717 return TRUE;
718}
719
720
374ca955
A
721void T_CTEST_EXPORT2
722vlog_info(const char *prefix, const char *pattern, va_list ap)
b75a7d8f 723{
729e4ab9
A
724 first_line_info();
725 fprintf(stdout, "%-*s", INDENT_LEVEL,"" );
b75a7d8f 726 if(prefix) {
73c04bcf 727 fputs(prefix, stdout);
b75a7d8f
A
728 }
729 vfprintf(stdout, pattern, ap);
730 fflush(stdout);
731 va_end(ap);
729e4ab9
A
732 if((*pattern==0) || (pattern[strlen(pattern)-1]!='\n')) {
733 HANGING_OUTPUT=1;
734 } else {
735 HANGING_OUTPUT=0;
736 }
737 GLOBAL_PRINT_COUNT++;
738}
739/**
740 * Log test structure, with indent
741 */
742static void log_testinfo_i(const char *pattern, ...)
743{
744 va_list ap;
745 first_line_test();
746 fprintf(stdout, "%-*s", INDENT_LEVEL,"" );
747 va_start(ap, pattern);
748 vfprintf(stdout, pattern, ap);
749 fflush(stdout);
750 va_end(ap);
751 GLOBAL_PRINT_COUNT++;
752}
753/**
754 * Log test structure (no ident)
755 */
756static void log_testinfo(const char *pattern, ...)
757{
758 va_list ap;
759 va_start(ap, pattern);
760 first_line_test();
761 vfprintf(stdout, pattern, ap);
762 fflush(stdout);
763 va_end(ap);
764 GLOBAL_PRINT_COUNT++;
b75a7d8f
A
765}
766
729e4ab9 767
b75a7d8f
A
768static void vlog_verbose(const char *prefix, const char *pattern, va_list ap)
769{
770 if ( VERBOSITY == FALSE )
771 return;
772
729e4ab9
A
773 first_line_verbose();
774 fprintf(stdout, "%-*s", INDENT_LEVEL,"" );
b75a7d8f 775 if(prefix) {
73c04bcf 776 fputs(prefix, stdout);
b75a7d8f
A
777 }
778 vfprintf(stdout, pattern, ap);
779 fflush(stdout);
780 va_end(ap);
729e4ab9
A
781 GLOBAL_PRINT_COUNT++;
782 if((*pattern==0) || (pattern[strlen(pattern)-1]!='\n')) {
783 HANGING_OUTPUT=1;
784 } else {
785 HANGING_OUTPUT=0;
786 }
b75a7d8f
A
787}
788
374ca955
A
789void T_CTEST_EXPORT2
790log_err(const char* pattern, ...)
b75a7d8f
A
791{
792 va_list ap;
729e4ab9 793 first_line_err();
b75a7d8f
A
794 if(strchr(pattern, '\n') != NULL) {
795 /*
796 * Count errors only if there is a line feed in the pattern
797 * so that we do not exaggerate our error count.
798 */
799 ++ERROR_COUNT;
729e4ab9
A
800 } else {
801 /* Count at least one error. */
802 ONE_ERROR=1;
b75a7d8f
A
803 }
804 va_start(ap, pattern);
805 vlog_err(NULL, pattern, ap);
806}
807
57a6839d
A
808UBool T_CTEST_EXPORT2
809log_knownIssue(const char *ticket, const char *pattern, ...) {
810 va_list ap;
811 va_start(ap, pattern);
812 return vlog_knownIssue(ticket, pattern, ap);
813}
814
729e4ab9
A
815void T_CTEST_EXPORT2
816log_err_status(UErrorCode status, const char* pattern, ...)
817{
818 va_list ap;
819 va_start(ap, pattern);
820
729e4ab9
A
821 if ((status == U_FILE_ACCESS_ERROR || status == U_MISSING_RESOURCE_ERROR)) {
822 ++DATA_ERROR_COUNT; /* for informational message at the end */
823
824 if (WARN_ON_MISSING_DATA == 0) {
4388f060 825 first_line_err();
729e4ab9
A
826 /* Fatal error. */
827 if (strchr(pattern, '\n') != NULL) {
828 ++ERROR_COUNT;
829 } else {
4388f060 830 ++ONE_ERROR;
729e4ab9
A
831 }
832 vlog_err(NULL, pattern, ap); /* no need for prefix in default case */
833 } else {
834 vlog_info("[DATA] ", pattern, ap);
835 }
836 } else {
4388f060 837 first_line_err();
729e4ab9
A
838 /* Fatal error. */
839 if(strchr(pattern, '\n') != NULL) {
840 ++ERROR_COUNT;
841 } else {
4388f060 842 ++ONE_ERROR;
729e4ab9
A
843 }
844 vlog_err(NULL, pattern, ap); /* no need for prefix in default case */
845 }
846}
847
374ca955
A
848void T_CTEST_EXPORT2
849log_info(const char* pattern, ...)
b75a7d8f
A
850{
851 va_list ap;
852
853 va_start(ap, pattern);
854 vlog_info(NULL, pattern, ap);
855}
856
374ca955
A
857void T_CTEST_EXPORT2
858log_verbose(const char* pattern, ...)
b75a7d8f
A
859{
860 va_list ap;
861
862 va_start(ap, pattern);
863 vlog_verbose(NULL, pattern, ap);
864}
865
866
374ca955
A
867void T_CTEST_EXPORT2
868log_data_err(const char* pattern, ...)
b75a7d8f 869{
46f4442e
A
870 va_list ap;
871 va_start(ap, pattern);
b75a7d8f 872
729e4ab9 873 go_offline_err();
46f4442e 874 ++DATA_ERROR_COUNT; /* for informational message at the end */
b75a7d8f 875
46f4442e
A
876 if(WARN_ON_MISSING_DATA == 0) {
877 /* Fatal error. */
878 if(strchr(pattern, '\n') != NULL) {
879 ++ERROR_COUNT;
880 }
881 vlog_err(NULL, pattern, ap); /* no need for prefix in default case */
882 } else {
729e4ab9 883 vlog_info("[DATA] ", pattern, ap);
b75a7d8f 884 }
b75a7d8f
A
885}
886
887
46f4442e
A
888/*
889 * Tracing functions.
890 */
891static int traceFnNestingDepth = 0;
892U_CDECL_BEGIN
893static void U_CALLCONV TraceEntry(const void *context, int32_t fnNumber) {
894 char buf[500];
895 utrace_format(buf, sizeof(buf), traceFnNestingDepth*3, "%s() enter.\n", utrace_functionName(fnNumber)); buf[sizeof(buf)-1]=0;
896 fputs(buf, stdout);
897 traceFnNestingDepth++;
898}
899
900static void U_CALLCONV TraceExit(const void *context, int32_t fnNumber, const char *fmt, va_list args) { char buf[500];
901
902 if (traceFnNestingDepth>0) {
903 traceFnNestingDepth--;
904 }
905 utrace_format(buf, sizeof(buf), traceFnNestingDepth*3, "%s() ", utrace_functionName(fnNumber)); buf[sizeof(buf)-1]=0;
906 fputs(buf, stdout);
907 utrace_vformat(buf, sizeof(buf), traceFnNestingDepth*3, fmt, args);
908 buf[sizeof(buf)-1]=0;
909 fputs(buf, stdout);
910 putc('\n', stdout);
911}
912
913static void U_CALLCONV TraceData(const void *context, int32_t fnNumber,
914 int32_t level, const char *fmt, va_list args) {
915 char buf[500];
916 utrace_vformat(buf, sizeof(buf), traceFnNestingDepth*3, fmt, args);
917 buf[sizeof(buf)-1]=0;
918 fputs(buf, stdout);
919 putc('\n', stdout);
920}
921
922static void *U_CALLCONV ctest_libMalloc(const void *context, size_t size) {
923 /*if (VERBOSITY) {
924 printf("Allocated %ld\n", (long)size);
925 }*/
926 if (MINIMUM_MEMORY_SIZE_FAILURE <= size && size <= MAXIMUM_MEMORY_SIZE_FAILURE) {
927 return NULL;
928 }
46f4442e
A
929 return malloc(size);
930}
931static void *U_CALLCONV ctest_libRealloc(const void *context, void *mem, size_t size) {
932 /*if (VERBOSITY) {
933 printf("Reallocated %ld\n", (long)size);
934 }*/
935 if (MINIMUM_MEMORY_SIZE_FAILURE <= size && size <= MAXIMUM_MEMORY_SIZE_FAILURE) {
936 /*free(mem);*/ /* Realloc doesn't free on failure. */
937 return NULL;
938 }
46f4442e
A
939 return realloc(mem, size);
940}
941static void U_CALLCONV ctest_libFree(const void *context, void *mem) {
46f4442e
A
942 free(mem);
943}
944
374ca955 945int T_CTEST_EXPORT2
46f4442e 946initArgs( int argc, const char* const argv[], ArgHandlerPtr argHandler, void *context)
b75a7d8f 947{
b75a7d8f 948 int i;
4388f060 949 int argSkip = 0;
b75a7d8f 950
b75a7d8f
A
951 VERBOSITY = FALSE;
952 ERR_MSG = TRUE;
953
729e4ab9
A
954 ARGV_0=argv[0];
955
b75a7d8f
A
956 for( i=1; i<argc; i++)
957 {
958 if ( argv[i][0] == '/' )
959 {
46f4442e
A
960 /* We don't run the tests here. */
961 continue;
962 }
963 else if ((strcmp( argv[i], "-a") == 0) || (strcmp(argv[i],"-all") == 0))
964 {
965 /* We don't run the tests here. */
966 continue;
b75a7d8f
A
967 }
968 else if (strcmp( argv[i], "-v" )==0 || strcmp( argv[i], "-verbose")==0)
969 {
970 VERBOSITY = TRUE;
971 }
972 else if (strcmp( argv[i], "-l" )==0 )
973 {
4388f060 974 /* doList = TRUE; */
b75a7d8f
A
975 }
976 else if (strcmp( argv[i], "-e1") == 0)
977 {
978 QUICK = -1;
979 }
980 else if (strcmp( argv[i], "-e") ==0)
981 {
982 QUICK = 0;
983 }
57a6839d
A
984 else if (strcmp( argv[i], "-K") ==0)
985 {
986 NO_KNOWN = 1;
987 }
988 else if (strncmp( argv[i], "-E",2) ==0)
989 {
990 SUMMARY_FILE=argv[i]+2;
991 }
b75a7d8f
A
992 else if (strcmp( argv[i], "-w") ==0)
993 {
994 WARN_ON_MISSING_DATA = TRUE;
995 }
46f4442e
A
996 else if (strcmp( argv[i], "-m") ==0)
997 {
998 UErrorCode errorCode = U_ZERO_ERROR;
999 if (i+1 < argc) {
1000 char *endPtr = NULL;
1001 i++;
1002 MINIMUM_MEMORY_SIZE_FAILURE = (size_t)strtol(argv[i], &endPtr, 10);
1003 if (endPtr == argv[i]) {
1004 printf("Can't parse %s\n", argv[i]);
1005 help(argv[0]);
1006 return 0;
1007 }
1008 if (*endPtr == '-') {
1009 char *maxPtr = endPtr+1;
1010 endPtr = NULL;
1011 MAXIMUM_MEMORY_SIZE_FAILURE = (size_t)strtol(maxPtr, &endPtr, 10);
1012 if (endPtr == argv[i]) {
1013 printf("Can't parse %s\n", argv[i]);
1014 help(argv[0]);
1015 return 0;
1016 }
1017 }
1018 }
1019 /* Use the default value */
1020 u_setMemoryFunctions(NULL, ctest_libMalloc, ctest_libRealloc, ctest_libFree, &errorCode);
1021 if (U_FAILURE(errorCode)) {
1022 printf("u_setMemoryFunctions returned %s\n", u_errorName(errorCode));
1023 return 0;
1024 }
1025 }
b75a7d8f
A
1026 else if(strcmp( argv[i], "-n") == 0 || strcmp( argv[i], "-no_err_msg") == 0)
1027 {
1028 ERR_MSG = FALSE;
1029 }
1030 else if (strcmp( argv[i], "-r") == 0)
1031 {
1032 if (!REPEAT_TESTS_INIT) {
1033 REPEAT_TESTS++;
1034 }
1035 }
729e4ab9
A
1036 else if (strcmp( argv[i], "-x") == 0)
1037 {
1038 if(++i>=argc) {
1039 printf("* Error: '-x' option requires an argument. usage: '-x outfile.xml'.\n");
1040 return 0;
1041 }
1042 if(ctest_xml_setFileName(argv[i])) { /* set the name */
1043 return 0;
1044 }
1045 }
374ca955
A
1046 else if (strcmp( argv[i], "-t_info") == 0) {
1047 ICU_TRACE = UTRACE_INFO;
1048 }
1049 else if (strcmp( argv[i], "-t_error") == 0) {
1050 ICU_TRACE = UTRACE_ERROR;
1051 }
1052 else if (strcmp( argv[i], "-t_warn") == 0) {
1053 ICU_TRACE = UTRACE_WARNING;
1054 }
1055 else if (strcmp( argv[i], "-t_verbose") == 0) {
1056 ICU_TRACE = UTRACE_VERBOSE;
1057 }
1058 else if (strcmp( argv[i], "-t_oc") == 0) {
1059 ICU_TRACE = UTRACE_OPEN_CLOSE;
1060 }
1061 else if (strcmp( argv[i], "-h" )==0 || strcmp( argv[i], "--help" )==0)
b75a7d8f
A
1062 {
1063 help( argv[0] );
1064 return 0;
1065 }
46f4442e
A
1066 else if (argHandler != NULL && (argSkip = argHandler(i, argc, argv, context)) > 0)
1067 {
1068 i += argSkip - 1;
1069 }
b75a7d8f
A
1070 else
1071 {
1072 printf("* unknown option: %s\n", argv[i]);
1073 help( argv[0] );
46f4442e
A
1074 return 0;
1075 }
1076 }
1077 if (ICU_TRACE != UTRACE_OFF) {
1078 utrace_setFunctions(NULL, TraceEntry, TraceExit, TraceData);
1079 utrace_setLevel(ICU_TRACE);
1080 }
1081
1082 return 1; /* total error count */
1083}
1084
1085int T_CTEST_EXPORT2
1086runTestRequest(const TestNode* root,
1087 int argc,
1088 const char* const argv[])
1089{
1090 /**
1091 * This main will parse the l, v, h, n, and path arguments
1092 */
1093 const TestNode* toRun;
1094 int i;
1095 int doList = FALSE;
1096 int subtreeOptionSeen = FALSE;
1097
1098 int errorCount = 0;
1099
1100 toRun = root;
1101
729e4ab9
A
1102 if(ctest_xml_init(ARGV_0)) {
1103 return 1; /* couldn't fire up XML thing */
1104 }
1105
46f4442e
A
1106 for( i=1; i<argc; i++)
1107 {
1108 if ( argv[i][0] == '/' )
1109 {
1110 printf("Selecting subtree '%s'\n", argv[i]);
1111
1112 if ( argv[i][1] == 0 )
1113 toRun = root;
1114 else
1115 toRun = getTest(root, argv[i]);
1116
1117 if ( toRun == NULL )
1118 {
1119 printf("* Could not find any matching subtree\n");
1120 return -1;
1121 }
1122
729e4ab9
A
1123 ON_LINE=FALSE; /* just in case */
1124
46f4442e
A
1125 if( doList == TRUE)
1126 showTests(toRun);
1127 else
1128 runTests(toRun);
1129
729e4ab9
A
1130 ON_LINE=FALSE; /* just in case */
1131
46f4442e
A
1132 errorCount += ERROR_COUNT;
1133
1134 subtreeOptionSeen = TRUE;
1135 } else if ((strcmp( argv[i], "-a") == 0) || (strcmp(argv[i],"-all") == 0)) {
1136 subtreeOptionSeen=FALSE;
1137 } else if (strcmp( argv[i], "-l") == 0) {
1138 doList = TRUE;
b75a7d8f 1139 }
46f4442e 1140 /* else option already handled by initArgs */
b75a7d8f
A
1141 }
1142
1143 if( subtreeOptionSeen == FALSE) /* no other subtree given, run the default */
1144 {
729e4ab9 1145 ON_LINE=FALSE; /* just in case */
b75a7d8f
A
1146 if( doList == TRUE)
1147 showTests(toRun);
1148 else
1149 runTests(toRun);
729e4ab9 1150 ON_LINE=FALSE; /* just in case */
b75a7d8f
A
1151
1152 errorCount += ERROR_COUNT;
1153 }
1154 else
1155 {
1156 if( ( doList == FALSE ) && ( errorCount > 0 ) )
1157 printf(" Total errors: %d\n", errorCount );
1158 }
1159
1160 REPEAT_TESTS_INIT = 1;
729e4ab9
A
1161
1162 if(ctest_xml_fini()) {
1163 errorCount++;
1164 }
b75a7d8f
A
1165
1166 return errorCount; /* total error count */
1167}
1168
1169/**
1170 * Display program invocation arguments
1171 */
1172
1173static void help ( const char *argv0 )
1174{
374ca955 1175 printf("Usage: %s [ -l ] [ -v ] [ -verbose] [-a] [ -all] [-n] [ -no_err_msg]\n"
46f4442e
A
1176 " [ -h ] [-t_info | -t_error | -t_warn | -t_oc | -t_verbose] [-m n[-q] ]\n"
1177 " [ /path/to/test ]\n",
b75a7d8f 1178 argv0);
374ca955
A
1179 printf(" -l To get a list of test names\n");
1180 printf(" -e to do exhaustive testing\n");
b75a7d8f 1181 printf(" -verbose To turn ON verbosity\n");
374ca955 1182 printf(" -v To turn ON verbosity(same as -verbose)\n");
729e4ab9 1183 printf(" -x file.xml Write junit format output to file.xml\n");
374ca955 1184 printf(" -h To print this message\n");
57a6839d 1185 printf(" -K to turn OFF suppressing known issues\n");
374ca955
A
1186 printf(" -n To turn OFF printing error messages\n");
1187 printf(" -w Don't fail on data-loading errs, just warn. Useful if\n"
b75a7d8f 1188 " user has reduced/changed the common set of ICU data \n");
374ca955 1189 printf(" -t_info | -t_error | -t_warn | -t_oc | -t_verbose Enable ICU tracing\n");
b75a7d8f 1190 printf(" -no_err_msg (same as -n) \n");
46f4442e
A
1191 printf(" -m n[-q] Min-Max memory size that will cause an allocation failure.\n");
1192 printf(" The default is the maximum value of size_t. Max is optional.\n");
1193 printf(" -r Repeat tests after calling u_cleanup \n");
1194 printf(" [/subtest] To run a subtest \n");
b75a7d8f
A
1195 printf(" eg: to run just the utility tests type: cintltest /tsutil) \n");
1196}
1197
729e4ab9
A
1198int32_t T_CTEST_EXPORT2
1199getTestOption ( int32_t testOption ) {
1200 switch (testOption) {
1201 case VERBOSITY_OPTION:
1202 return VERBOSITY;
1203 case WARN_ON_MISSING_DATA_OPTION:
1204 return WARN_ON_MISSING_DATA;
1205 case QUICK_OPTION:
1206 return QUICK;
1207 case REPEAT_TESTS_OPTION:
1208 return REPEAT_TESTS;
1209 case ERR_MSG_OPTION:
1210 return ERR_MSG;
1211 case ICU_TRACE_OPTION:
1212 return ICU_TRACE;
1213 default :
1214 return 0;
1215 }
1216}
1217
1218void T_CTEST_EXPORT2
1219setTestOption ( int32_t testOption, int32_t value) {
1220 if (value == DECREMENT_OPTION_VALUE) {
1221 value = getTestOption(testOption);
1222 --value;
1223 }
1224 switch (testOption) {
1225 case VERBOSITY_OPTION:
1226 VERBOSITY = value;
1227 break;
1228 case WARN_ON_MISSING_DATA_OPTION:
1229 WARN_ON_MISSING_DATA = value;
1230 break;
1231 case QUICK_OPTION:
1232 QUICK = value;
1233 break;
1234 case REPEAT_TESTS_OPTION:
1235 REPEAT_TESTS = value;
1236 break;
1237 case ICU_TRACE_OPTION:
51004dcb 1238 ICU_TRACE = (UTraceLevel)value;
729e4ab9
A
1239 break;
1240 default :
1241 break;
1242 }
1243}
1244
1245
1246/*
1247 * ================== JUnit support ================================
1248 */
1249
1250int32_t
1251T_CTEST_EXPORT2
1252ctest_xml_setFileName(const char *name) {
1253 XML_FILE_NAME=name;
1254 return 0;
1255}
1256
1257
1258int32_t
1259T_CTEST_EXPORT2
1260ctest_xml_init(const char *rootName) {
1261 if(!XML_FILE_NAME) return 0;
1262 XML_FILE = fopen(XML_FILE_NAME,"w");
1263 if(!XML_FILE) {
1264 perror("fopen");
1265 fprintf(stderr," Error: couldn't open XML output file %s\n", XML_FILE_NAME);
1266 return 1;
1267 }
4388f060 1268 while(*rootName&&!isalnum((int)*rootName)) {
729e4ab9
A
1269 rootName++;
1270 }
1271 strcpy(XML_PREFIX,rootName);
1272 {
1273 char *p = XML_PREFIX+strlen(XML_PREFIX);
4388f060 1274 for(p--;*p&&p>XML_PREFIX&&!isalnum((int)*p);p--) {
729e4ab9
A
1275 *p=0;
1276 }
1277 }
1278 /* write prefix */
1279 fprintf(XML_FILE, "<testsuite name=\"%s\">\n", XML_PREFIX);
1280
1281 return 0;
1282}
1283
1284int32_t
1285T_CTEST_EXPORT2
1286ctest_xml_fini(void) {
1287 if(!XML_FILE) return 0;
1288
1289 fprintf(XML_FILE, "</testsuite>\n");
1290 fclose(XML_FILE);
1291 printf(" ( test results written to %s )\n", XML_FILE_NAME);
1292 XML_FILE=0;
1293 return 0;
1294}
1295
1296
1297int32_t
1298T_CTEST_EXPORT2
57a6839d 1299ctest_xml_testcase(const char *classname, const char *name, const char *timeSeconds, const char *failMsg) {
729e4ab9
A
1300 if(!XML_FILE) return 0;
1301
57a6839d 1302 fprintf(XML_FILE, "\t<testcase classname=\"%s:%s\" name=\"%s:%s\" time=\"%s\"", XML_PREFIX, classname, XML_PREFIX, name, timeSeconds);
729e4ab9
A
1303 if(failMsg) {
1304 fprintf(XML_FILE, ">\n\t\t<failure type=\"err\" message=\"%s\"/>\n\t</testcase>\n", failMsg);
1305 } else {
1306 fprintf(XML_FILE, "/>\n");
1307 }
1308
1309 return 0;
1310}
1311
1312