2 ********************************************************************************
4 * Copyright (C) 1996-2013, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ********************************************************************************
16 #include "unicode/utrace.h"
17 #include "unicode/uclean.h"
22 3/20/1999 srl - strncpy called w/o setting nulls at the end
25 #define MAXTESTNAME 128
27 #define MAX_TEST_LOG 4096
30 * How may columns to indent the 'OK' markers.
32 #define FLAG_INDENT 45
34 * How many lines of scrollage can go by before we need to remind the user what the test is.
36 #define PAGE_SIZE_LIMIT 25
45 struct TestNode
* sibling
;
46 struct TestNode
* child
;
47 char name
[1]; /* This is dynamically allocated off the end with malloc. */
51 static const struct TestNode
* currentTest
;
53 typedef enum { RUNTESTS
, SHOWTESTS
} TestMode
;
54 #define TEST_SEPARATOR '/'
60 #include "unicode/ctest.h"
62 static char ERROR_LOG
[MAX_TEST_LOG
][MAXTESTNAME
];
64 /* Local prototypes */
65 static TestNode
* addTestNode( TestNode
*root
, const char *name
);
67 static TestNode
*createTestNode(const char* name
, int32_t nameLen
);
69 static int strncmp_nullcheck( const char* s1
,
73 static void getNextLevel( const char* name
,
75 const char** nextName
);
77 static void iterateTestsWithLevel( const TestNode
*root
, int depth
,
78 const TestNode
** nodeList
,
81 static void help ( const char *argv0
);
84 * Do the work of logging an error. Doesn't increase the error count.
86 * @prefix optional prefix prepended to message, or NULL.
87 * @param pattern printf style pattern
88 * @param ap vprintf style arg list
90 static void vlog_err(const char *prefix
, const char *pattern
, va_list ap
);
91 static void vlog_verbose(const char *prefix
, const char *pattern
, va_list ap
);
92 static UBool
vlog_knownIssue(const char *ticket
, const char *pattern
, va_list ap
);
95 * Log test structure, with indent
96 * @param pattern printf pattern
98 static void log_testinfo_i(const char *pattern
, ...);
101 * Log test structure, NO indent
102 * @param pattern printf pattern
104 static void log_testinfo(const char *pattern
, ...);
106 /* If we need to make the framework multi-thread safe
107 we need to pass around the following vars
109 static int ERRONEOUS_FUNCTION_COUNT
= 0;
110 static int ERROR_COUNT
= 0; /* Count of errors from all tests. */
111 static int ONE_ERROR
= 0; /* were there any other errors? */
112 static int DATA_ERROR_COUNT
= 0; /* count of data related errors or warnings */
113 static int INDENT_LEVEL
= 0;
114 static UBool NO_KNOWN
= FALSE
;
115 static void *knownList
= NULL
;
116 static char gTestName
[1024] = "";
117 static UBool ON_LINE
= FALSE
; /* are we on the top line with our test name? */
118 static UBool HANGING_OUTPUT
= FALSE
; /* did the user leave us without a trailing \n ? */
119 static int GLOBAL_PRINT_COUNT
= 0; /* global count of printouts */
120 int REPEAT_TESTS_INIT
= 0; /* Was REPEAT_TESTS initialized? */
121 int REPEAT_TESTS
= 1; /* Number of times to run the test */
122 int VERBOSITY
= 0; /* be No-verbose by default */
123 int ERR_MSG
=1; /* error messages will be displayed by default*/
124 int QUICK
= 1; /* Skip some of the slower tests? */
125 int WARN_ON_MISSING_DATA
= 0; /* Reduce data errs to warnings? */
126 UTraceLevel ICU_TRACE
= UTRACE_OFF
; /* ICU tracing level */
127 size_t MINIMUM_MEMORY_SIZE_FAILURE
= (size_t)-1; /* Minimum library memory allocation window that will fail. */
128 size_t MAXIMUM_MEMORY_SIZE_FAILURE
= (size_t)-1; /* Maximum library memory allocation window that will fail. */
129 static const char *ARGV_0
= "[ALL]";
130 static const char *XML_FILE_NAME
=NULL
;
131 static char XML_PREFIX
[256];
132 static const char *SUMMARY_FILE
= NULL
;
133 FILE *XML_FILE
= NULL
;
134 /*-------------------------------------------*/
136 /* strncmp that also makes sure there's a \0 at s2[0] */
137 static int strncmp_nullcheck( const char* s1
,
141 if (((int)strlen(s2
) >= n
) && s2
[n
] != 0) {
142 return 3; /* null check fails */
145 return strncmp ( s1
, s2
, n
);
149 static void getNextLevel( const char* name
,
151 const char** nextName
)
153 /* Get the next component of the name */
154 *nextName
= strchr(name
, TEST_SEPARATOR
);
159 *nameLen
= (int)((*nextName
) - name
);
160 (*nextName
)++; /* skip '/' */
161 strncpy(n
, name
, *nameLen
);
163 /*printf("->%s-< [%d] -> [%s]\n", name, *nameLen, *nextName);*/
166 *nameLen
= (int)strlen(name
);
170 static TestNode
*createTestNode(const char* name
, int32_t nameLen
)
174 newNode
= (TestNode
*)malloc(sizeof(TestNode
) + (nameLen
+ 1));
176 newNode
->test
= NULL
;
177 newNode
->sibling
= NULL
;
178 newNode
->child
= NULL
;
180 strncpy( newNode
->name
, name
, nameLen
);
181 newNode
->name
[nameLen
] = 0;
187 cleanUpTestTree(TestNode
*tn
)
189 if(tn
->child
!= NULL
) {
190 cleanUpTestTree(tn
->child
);
192 if(tn
->sibling
!= NULL
) {
193 cleanUpTestTree(tn
->sibling
);
202 addTest(TestNode
** root
,
203 TestFunctionPtr test
,
208 /*if this is the first Test created*/
210 *root
= createTestNode("", 0);
212 newNode
= addTestNode( *root
, name
);
213 assert(newNode
!= 0 );
214 /* printf("addTest: nreName = %s\n", newNode->name );*/
216 newNode
->test
= test
;
219 /* non recursive insert function */
220 static TestNode
*addTestNode ( TestNode
*root
, const char *name
)
222 const char* nextName
;
223 TestNode
*nextNode
, *curNode
;
224 int nameLen
; /* length of current 'name' */
226 /* remove leading slash */
227 if ( *name
== TEST_SEPARATOR
)
234 /* Start with the next child */
235 nextNode
= curNode
->child
;
237 getNextLevel ( name
, &nameLen
, &nextName
);
239 /* printf("* %s\n", name );*/
241 /* if nextNode is already null, then curNode has no children
243 if( nextNode
== NULL
)
245 /* Add all children of the node */
248 /* Get the next component of the name */
249 getNextLevel(name
, &nameLen
, &nextName
);
251 /* update curName to have the next name segment */
252 curNode
->child
= createTestNode(name
, nameLen
);
253 /* printf("*** added %s\n", curNode->child->name );*/
254 curNode
= curNode
->child
;
257 while( name
!= NULL
);
262 /* Search across for the name */
263 while (strncmp_nullcheck ( name
, nextNode
->name
, nameLen
) != 0 )
266 nextNode
= nextNode
-> sibling
;
268 if ( nextNode
== NULL
)
270 /* Did not find 'name' on this level. */
271 nextNode
= createTestNode(name
, nameLen
);
272 curNode
->sibling
= nextNode
;
277 /* nextNode matches 'name' */
279 if (nextName
== NULL
) /* end of the line */
284 /* Loop again with the next item */
291 * Log the time taken. May not output anything.
292 * @param deltaTime change in time
294 void T_CTEST_EXPORT2
str_timeDelta(char *str
, UDate deltaTime
) {
295 if (deltaTime
> 110000.0 ) {
296 double mins
= uprv_floor(deltaTime
/60000.0);
297 sprintf(str
, "[(%.0fm %.1fs)]", mins
, (deltaTime
-(mins
*60000.0))/1000.0);
298 } else if (deltaTime
> 1500.0) {
299 sprintf(str
, "((%.1fs))", deltaTime
/1000.0);
300 } else if(deltaTime
>900.0) {
301 sprintf(str
, "( %.2fs )", deltaTime
/1000.0);
302 } else if(deltaTime
> 5.0) {
303 sprintf(str
, " (%.0fms) ", deltaTime
);
305 str
[0]=0; /* at least terminate it. */
309 static void print_timeDelta(UDate deltaTime
) {
311 str_timeDelta(str
, deltaTime
);
318 * Run or list tests (according to mode) in a subtree.
320 * @param root root of the subtree to operate on
321 * @param depth The depth of this tree (0=root)
322 * @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.
323 * @param mode what mode we are operating in.
325 static void iterateTestsWithLevel ( const TestNode
* root
,
327 const TestNode
** nodeList
,
332 char pathToFunction
[MAXTESTNAME
] = "";
333 char separatorString
[2] = { TEST_SEPARATOR
, '\0'};
335 UDate allStartTime
= -1, allStopTime
= -1;
339 allStartTime
= uprv_getRawUTCtime();
345 /* record the current root node, and increment depth. */
346 nodeList
[depth
++] = root
;
347 /* depth is now the depth of root's children. */
349 /* Collect the 'path' to the current subtree. */
350 for ( i
=0;i
<(depth
-1);i
++ )
352 strcat(pathToFunction
, nodeList
[i
]->name
);
353 strcat(pathToFunction
, separatorString
);
355 strcat(pathToFunction
, nodeList
[i
]->name
); /* including 'root' */
357 /* print test name and space. */
358 INDENT_LEVEL
= depth
-1;
360 log_testinfo_i("%s ", root
->name
);
362 log_testinfo_i("(%s) ", ARGV_0
);
364 ON_LINE
= TRUE
; /* we are still on the line with the test name */
367 if ( (mode
== RUNTESTS
) &&
368 (root
->test
!= NULL
)) /* if root is a leaf node, run it */
370 int myERROR_COUNT
= ERROR_COUNT
;
371 int myGLOBAL_PRINT_COUNT
= GLOBAL_PRINT_COUNT
;
373 UDate startTime
, stopTime
;
375 char timeSeconds
[256];
377 const char timeDelta
[] = "(unknown)";
378 const char timeSeconds
[] = "0.000";
381 INDENT_LEVEL
= depth
; /* depth of subitems */
383 HANGING_OUTPUT
=FALSE
;
385 startTime
= uprv_getRawUTCtime();
387 strcpy(gTestName
, pathToFunction
);
388 root
->test(); /* PERFORM THE TEST ************************/
390 stopTime
= uprv_getRawUTCtime();
394 HANGING_OUTPUT
=FALSE
;
396 INDENT_LEVEL
= depth
-1; /* depth of root */
398 if((ONE_ERROR
>0)&&(ERROR_COUNT
==0)) {
399 ERROR_COUNT
++; /* There was an error without a newline */
404 str_timeDelta(timeDelta
, stopTime
-startTime
);
405 sprintf(timeSeconds
, "%f", (stopTime
-startTime
)/1000.0);
407 ctest_xml_testcase(pathToFunction
, pathToFunction
, timeSeconds
, (myERROR_COUNT
!=ERROR_COUNT
)?"error":NULL
);
409 if (myERROR_COUNT
!= ERROR_COUNT
) {
410 log_testinfo_i("} ---[%d ERRORS in %s] ", ERROR_COUNT
- myERROR_COUNT
, pathToFunction
);
411 strcpy(ERROR_LOG
[ERRONEOUS_FUNCTION_COUNT
++], pathToFunction
);
413 if(!ON_LINE
) { /* had some output */
414 int spaces
= FLAG_INDENT
-(depth
-1);
415 log_testinfo_i("} %*s[OK] ", spaces
, "---");
416 if((GLOBAL_PRINT_COUNT
-myGLOBAL_PRINT_COUNT
)>PAGE_SIZE_LIMIT
) {
417 log_testinfo(" %s ", pathToFunction
); /* in case they forgot. */
420 /* put -- out at 30 sp. */
421 int spaces
= FLAG_INDENT
-(strlen(root
->name
)+depth
);
422 if(spaces
<0) spaces
=0;
423 log_testinfo(" %*s[OK] ", spaces
,"---");
428 if(timeDelta
[0]) printf("%s", timeDelta
);
431 ON_LINE
= TRUE
; /* we are back on-line */
434 INDENT_LEVEL
= depth
-1; /* root */
436 /* we want these messages to be at 0 indent. so just push the indent level breifly. */
437 if(mode
==SHOWTESTS
) {
438 log_testinfo("---%s%c\n",pathToFunction
, nodeList
[i
]->test
?' ':TEST_SEPARATOR
);
441 INDENT_LEVEL
= depth
;
444 int myERROR_COUNT
= ERROR_COUNT
;
445 int myGLOBAL_PRINT_COUNT
= GLOBAL_PRINT_COUNT
;
446 if(mode
!=SHOWTESTS
) {
447 INDENT_LEVEL
=depth
-1;
452 iterateTestsWithLevel ( root
->child
, depth
, nodeList
, mode
);
454 if(mode
!=SHOWTESTS
) {
455 INDENT_LEVEL
=depth
-1;
456 log_testinfo_i("} "); /* TODO: summarize subtests */
457 if((depth
>1) && (ERROR_COUNT
> myERROR_COUNT
)) {
458 log_testinfo("[%d %s in %s] ", ERROR_COUNT
-myERROR_COUNT
, (ERROR_COUNT
-myERROR_COUNT
)==1?"error":"errors", pathToFunction
);
459 } else if((GLOBAL_PRINT_COUNT
-myGLOBAL_PRINT_COUNT
)>PAGE_SIZE_LIMIT
|| (depth
<1)) {
460 if(pathToFunction
[0]) {
461 log_testinfo(" %s ", pathToFunction
); /* in case they forgot. */
463 log_testinfo(" / (%s) ", ARGV_0
);
474 allStopTime
= uprv_getRawUTCtime();
475 print_timeDelta(allStopTime
-allStartTime
);
479 if(mode
!=SHOWTESTS
&& ON_LINE
) {
483 if ( depth
!= 0 ) { /* DO NOT iterate over siblings of the root. TODO: why not? */
484 iterateTestsWithLevel ( root
->sibling
, depth
, nodeList
, mode
);
491 showTests ( const TestNode
*root
)
493 /* make up one for them */
494 const TestNode
*nodeList
[MAXTESTS
];
497 log_err("TEST CAN'T BE FOUND!");
499 iterateTestsWithLevel ( root
, 0, nodeList
, SHOWTESTS
);
504 runTests ( const TestNode
*root
)
507 const TestNode
*nodeList
[MAXTESTS
];
508 /* make up one for them */
512 log_err("TEST CAN'T BE FOUND!\n");
514 ERRONEOUS_FUNCTION_COUNT
= ERROR_COUNT
= 0;
515 iterateTestsWithLevel ( root
, 0, nodeList
, RUNTESTS
);
517 /*print out result summary*/
519 ON_LINE
=FALSE
; /* just in case */
521 if(knownList
!= NULL
) {
522 if( udbg_knownIssue_print(knownList
) ) {
523 fprintf(stdout
, "(To run suppressed tests, use the -K option.) \n\n");
525 udbg_knownIssue_close(knownList
);
530 fprintf(stdout
,"\nSUMMARY:\n");
532 fprintf(stdout
,"******* [Total error count:\t%d]\n", ERROR_COUNT
);
534 fprintf(stdout
, " Errors in\n");
535 for (i
=0;i
< ERRONEOUS_FUNCTION_COUNT
; i
++)
536 fprintf(stdout
, "[%s]\n",ERROR_LOG
[i
]);
537 if(SUMMARY_FILE
!= NULL
) {
538 FILE *summf
= fopen(SUMMARY_FILE
, "w");
540 for (i
=0;i
< ERRONEOUS_FUNCTION_COUNT
; i
++)
541 fprintf(summf
, "%s\n",ERROR_LOG
[i
]);
548 log_testinfo("\n[All tests passed successfully...]\n");
551 if(DATA_ERROR_COUNT
) {
552 if(WARN_ON_MISSING_DATA
==0) {
553 log_testinfo("\t*Note* some errors are data-loading related. If the data used is not the \n"
554 "\tstock ICU data (i.e some have been added or removed), consider using\n"
555 "\tthe '-w' option to turn these errors into warnings.\n");
557 log_testinfo("\t*WARNING* some data-loading errors were ignored by the -w option.\n");
562 const char* T_CTEST_EXPORT2
565 if(currentTest
!= NULL
) {
566 return currentTest
->name
;
572 const TestNode
* T_CTEST_EXPORT2
573 getTest(const TestNode
* root
, const char* name
)
575 const char* nextName
;
577 const TestNode
* curNode
;
578 int nameLen
; /* length of current 'name' */
581 log_err("TEST CAN'T BE FOUND!\n");
584 /* remove leading slash */
585 if ( *name
== TEST_SEPARATOR
)
592 /* Start with the next child */
593 nextNode
= curNode
->child
;
595 getNextLevel ( name
, &nameLen
, &nextName
);
597 /* printf("* %s\n", name );*/
599 /* if nextNode is already null, then curNode has no children
601 if( nextNode
== NULL
)
606 /* Search across for the name */
607 while (strncmp_nullcheck ( name
, nextNode
->name
, nameLen
) != 0 )
610 nextNode
= nextNode
-> sibling
;
612 if ( nextNode
== NULL
)
614 /* Did not find 'name' on this level. */
619 /* nextNode matches 'name' */
621 if (nextName
== NULL
) /* end of the line */
626 /* Loop again with the next item */
632 /* =========== io functions ======== */
634 static void go_offline_with_marker(const char *mrk
) {
635 UBool wasON_LINE
= ON_LINE
;
638 log_testinfo(" {\n");
642 if(!HANGING_OUTPUT
|| wasON_LINE
) {
649 static void go_offline() {
650 go_offline_with_marker(NULL
);
653 static void go_offline_err() {
657 static void first_line_verbose() {
658 go_offline_with_marker("v");
661 static void first_line_err() {
662 go_offline_with_marker("!");
665 static void first_line_info() {
666 go_offline_with_marker("\"");
669 static void first_line_test() {
674 static void vlog_err(const char *prefix
, const char *pattern
, va_list ap
)
676 if( ERR_MSG
== FALSE
){
679 fputs("!", stdout
); /* col 1 - bang */
680 fprintf(stdout
, "%-*s", INDENT_LEVEL
,"" );
682 fputs(prefix
, stdout
);
684 vfprintf(stdout
, pattern
, ap
);
687 if((*pattern
==0) || (pattern
[strlen(pattern
)-1]!='\n')) {
692 GLOBAL_PRINT_COUNT
++;
695 static UBool
vlog_knownIssue(const char *ticket
, const char *pattern
, va_list ap
)
698 UBool firstForTicket
;
701 if(NO_KNOWN
) return FALSE
;
702 if(pattern
==NULL
) pattern
="";
704 vsprintf(buf
, pattern
, ap
);
705 knownList
= udbg_knownIssue_open(knownList
, ticket
, gTestName
, buf
,
706 &firstForTicket
, &firstForWhere
);
708 if(firstForTicket
|| firstForWhere
) {
709 log_info("(Known issue #%s) %s", ticket
, buf
);
711 log_verbose("(Known issue #%s) %s", ticket
, buf
);
719 vlog_info(const char *prefix
, const char *pattern
, va_list ap
)
722 fprintf(stdout
, "%-*s", INDENT_LEVEL
,"" );
724 fputs(prefix
, stdout
);
726 vfprintf(stdout
, pattern
, ap
);
729 if((*pattern
==0) || (pattern
[strlen(pattern
)-1]!='\n')) {
734 GLOBAL_PRINT_COUNT
++;
737 * Log test structure, with indent
739 static void log_testinfo_i(const char *pattern
, ...)
743 fprintf(stdout
, "%-*s", INDENT_LEVEL
,"" );
744 va_start(ap
, pattern
);
745 vfprintf(stdout
, pattern
, ap
);
748 GLOBAL_PRINT_COUNT
++;
751 * Log test structure (no ident)
753 static void log_testinfo(const char *pattern
, ...)
756 va_start(ap
, pattern
);
758 vfprintf(stdout
, pattern
, ap
);
761 GLOBAL_PRINT_COUNT
++;
765 static void vlog_verbose(const char *prefix
, const char *pattern
, va_list ap
)
767 if ( VERBOSITY
== FALSE
)
770 first_line_verbose();
771 fprintf(stdout
, "%-*s", INDENT_LEVEL
,"" );
773 fputs(prefix
, stdout
);
775 vfprintf(stdout
, pattern
, ap
);
778 GLOBAL_PRINT_COUNT
++;
779 if((*pattern
==0) || (pattern
[strlen(pattern
)-1]!='\n')) {
787 log_err(const char* pattern
, ...)
791 if(strchr(pattern
, '\n') != NULL
) {
793 * Count errors only if there is a line feed in the pattern
794 * so that we do not exaggerate our error count.
798 /* Count at least one error. */
801 va_start(ap
, pattern
);
802 vlog_err(NULL
, pattern
, ap
);
805 UBool T_CTEST_EXPORT2
806 log_knownIssue(const char *ticket
, const char *pattern
, ...) {
808 va_start(ap
, pattern
);
809 return vlog_knownIssue(ticket
, pattern
, ap
);
813 log_err_status(UErrorCode status
, const char* pattern
, ...)
816 va_start(ap
, pattern
);
818 if ((status
== U_FILE_ACCESS_ERROR
|| status
== U_MISSING_RESOURCE_ERROR
)) {
819 ++DATA_ERROR_COUNT
; /* for informational message at the end */
821 if (WARN_ON_MISSING_DATA
== 0) {
824 if (strchr(pattern
, '\n') != NULL
) {
829 vlog_err(NULL
, pattern
, ap
); /* no need for prefix in default case */
831 vlog_info("[DATA] ", pattern
, ap
);
836 if(strchr(pattern
, '\n') != NULL
) {
841 vlog_err(NULL
, pattern
, ap
); /* no need for prefix in default case */
846 log_info(const char* pattern
, ...)
850 va_start(ap
, pattern
);
851 vlog_info(NULL
, pattern
, ap
);
855 log_verbose(const char* pattern
, ...)
859 va_start(ap
, pattern
);
860 vlog_verbose(NULL
, pattern
, ap
);
865 log_data_err(const char* pattern
, ...)
868 va_start(ap
, pattern
);
871 ++DATA_ERROR_COUNT
; /* for informational message at the end */
873 if(WARN_ON_MISSING_DATA
== 0) {
875 if(strchr(pattern
, '\n') != NULL
) {
878 vlog_err(NULL
, pattern
, ap
); /* no need for prefix in default case */
880 vlog_info("[DATA] ", pattern
, ap
);
888 static int traceFnNestingDepth
= 0;
890 static void U_CALLCONV
TraceEntry(const void *context
, int32_t fnNumber
) {
892 utrace_format(buf
, sizeof(buf
), traceFnNestingDepth
*3, "%s() enter.\n", utrace_functionName(fnNumber
)); buf
[sizeof(buf
)-1]=0;
894 traceFnNestingDepth
++;
897 static void U_CALLCONV
TraceExit(const void *context
, int32_t fnNumber
, const char *fmt
, va_list args
) { char buf
[500];
899 if (traceFnNestingDepth
>0) {
900 traceFnNestingDepth
--;
902 utrace_format(buf
, sizeof(buf
), traceFnNestingDepth
*3, "%s() ", utrace_functionName(fnNumber
)); buf
[sizeof(buf
)-1]=0;
904 utrace_vformat(buf
, sizeof(buf
), traceFnNestingDepth
*3, fmt
, args
);
905 buf
[sizeof(buf
)-1]=0;
910 static void U_CALLCONV
TraceData(const void *context
, int32_t fnNumber
,
911 int32_t level
, const char *fmt
, va_list args
) {
913 utrace_vformat(buf
, sizeof(buf
), traceFnNestingDepth
*3, fmt
, args
);
914 buf
[sizeof(buf
)-1]=0;
919 static void *U_CALLCONV
ctest_libMalloc(const void *context
, size_t size
) {
921 printf("Allocated %ld\n", (long)size);
923 if (MINIMUM_MEMORY_SIZE_FAILURE
<= size
&& size
<= MAXIMUM_MEMORY_SIZE_FAILURE
) {
928 static void *U_CALLCONV
ctest_libRealloc(const void *context
, void *mem
, size_t size
) {
930 printf("Reallocated %ld\n", (long)size);
932 if (MINIMUM_MEMORY_SIZE_FAILURE
<= size
&& size
<= MAXIMUM_MEMORY_SIZE_FAILURE
) {
933 /*free(mem);*/ /* Realloc doesn't free on failure. */
936 return realloc(mem
, size
);
938 static void U_CALLCONV
ctest_libFree(const void *context
, void *mem
) {
943 initArgs( int argc
, const char* const argv
[], ArgHandlerPtr argHandler
, void *context
)
953 for( i
=1; i
<argc
; i
++)
955 if ( argv
[i
][0] == '/' )
957 /* We don't run the tests here. */
960 else if ((strcmp( argv
[i
], "-a") == 0) || (strcmp(argv
[i
],"-all") == 0))
962 /* We don't run the tests here. */
965 else if (strcmp( argv
[i
], "-v" )==0 || strcmp( argv
[i
], "-verbose")==0)
969 else if (strcmp( argv
[i
], "-l" )==0 )
973 else if (strcmp( argv
[i
], "-e1") == 0)
977 else if (strcmp( argv
[i
], "-e") ==0)
981 else if (strcmp( argv
[i
], "-K") ==0)
985 else if (strncmp( argv
[i
], "-E",2) ==0)
987 SUMMARY_FILE
=argv
[i
]+2;
989 else if (strcmp( argv
[i
], "-w") ==0)
991 WARN_ON_MISSING_DATA
= TRUE
;
993 else if (strcmp( argv
[i
], "-m") ==0)
995 UErrorCode errorCode
= U_ZERO_ERROR
;
999 MINIMUM_MEMORY_SIZE_FAILURE
= (size_t)strtol(argv
[i
], &endPtr
, 10);
1000 if (endPtr
== argv
[i
]) {
1001 printf("Can't parse %s\n", argv
[i
]);
1005 if (*endPtr
== '-') {
1006 char *maxPtr
= endPtr
+1;
1008 MAXIMUM_MEMORY_SIZE_FAILURE
= (size_t)strtol(maxPtr
, &endPtr
, 10);
1009 if (endPtr
== argv
[i
]) {
1010 printf("Can't parse %s\n", argv
[i
]);
1016 /* Use the default value */
1017 u_setMemoryFunctions(NULL
, ctest_libMalloc
, ctest_libRealloc
, ctest_libFree
, &errorCode
);
1018 if (U_FAILURE(errorCode
)) {
1019 printf("u_setMemoryFunctions returned %s\n", u_errorName(errorCode
));
1023 else if(strcmp( argv
[i
], "-n") == 0 || strcmp( argv
[i
], "-no_err_msg") == 0)
1027 else if (strcmp( argv
[i
], "-r") == 0)
1029 if (!REPEAT_TESTS_INIT
) {
1033 else if (strcmp( argv
[i
], "-x") == 0)
1036 printf("* Error: '-x' option requires an argument. usage: '-x outfile.xml'.\n");
1039 if(ctest_xml_setFileName(argv
[i
])) { /* set the name */
1043 else if (strcmp( argv
[i
], "-t_info") == 0) {
1044 ICU_TRACE
= UTRACE_INFO
;
1046 else if (strcmp( argv
[i
], "-t_error") == 0) {
1047 ICU_TRACE
= UTRACE_ERROR
;
1049 else if (strcmp( argv
[i
], "-t_warn") == 0) {
1050 ICU_TRACE
= UTRACE_WARNING
;
1052 else if (strcmp( argv
[i
], "-t_verbose") == 0) {
1053 ICU_TRACE
= UTRACE_VERBOSE
;
1055 else if (strcmp( argv
[i
], "-t_oc") == 0) {
1056 ICU_TRACE
= UTRACE_OPEN_CLOSE
;
1058 else if (strcmp( argv
[i
], "-h" )==0 || strcmp( argv
[i
], "--help" )==0)
1063 else if (argHandler
!= NULL
&& (argSkip
= argHandler(i
, argc
, argv
, context
)) > 0)
1069 printf("* unknown option: %s\n", argv
[i
]);
1074 if (ICU_TRACE
!= UTRACE_OFF
) {
1075 utrace_setFunctions(NULL
, TraceEntry
, TraceExit
, TraceData
);
1076 utrace_setLevel(ICU_TRACE
);
1079 return 1; /* total error count */
1083 runTestRequest(const TestNode
* root
,
1085 const char* const argv
[])
1088 * This main will parse the l, v, h, n, and path arguments
1090 const TestNode
* toRun
;
1093 int subtreeOptionSeen
= FALSE
;
1099 if(ctest_xml_init(ARGV_0
)) {
1100 return 1; /* couldn't fire up XML thing */
1103 for( i
=1; i
<argc
; i
++)
1105 if ( argv
[i
][0] == '/' )
1107 printf("Selecting subtree '%s'\n", argv
[i
]);
1109 if ( argv
[i
][1] == 0 )
1112 toRun
= getTest(root
, argv
[i
]);
1114 if ( toRun
== NULL
)
1116 printf("* Could not find any matching subtree\n");
1120 ON_LINE
=FALSE
; /* just in case */
1127 ON_LINE
=FALSE
; /* just in case */
1129 errorCount
+= ERROR_COUNT
;
1131 subtreeOptionSeen
= TRUE
;
1132 } else if ((strcmp( argv
[i
], "-a") == 0) || (strcmp(argv
[i
],"-all") == 0)) {
1133 subtreeOptionSeen
=FALSE
;
1134 } else if (strcmp( argv
[i
], "-l") == 0) {
1137 /* else option already handled by initArgs */
1140 if( subtreeOptionSeen
== FALSE
) /* no other subtree given, run the default */
1142 ON_LINE
=FALSE
; /* just in case */
1147 ON_LINE
=FALSE
; /* just in case */
1149 errorCount
+= ERROR_COUNT
;
1153 if( ( doList
== FALSE
) && ( errorCount
> 0 ) )
1154 printf(" Total errors: %d\n", errorCount
);
1157 REPEAT_TESTS_INIT
= 1;
1159 if(ctest_xml_fini()) {
1163 return errorCount
; /* total error count */
1167 * Display program invocation arguments
1170 static void help ( const char *argv0
)
1172 printf("Usage: %s [ -l ] [ -v ] [ -verbose] [-a] [ -all] [-n] [ -no_err_msg]\n"
1173 " [ -h ] [-t_info | -t_error | -t_warn | -t_oc | -t_verbose] [-m n[-q] ]\n"
1174 " [ /path/to/test ]\n",
1176 printf(" -l To get a list of test names\n");
1177 printf(" -e to do exhaustive testing\n");
1178 printf(" -verbose To turn ON verbosity\n");
1179 printf(" -v To turn ON verbosity(same as -verbose)\n");
1180 printf(" -x file.xml Write junit format output to file.xml\n");
1181 printf(" -h To print this message\n");
1182 printf(" -K to turn OFF suppressing known issues\n");
1183 printf(" -n To turn OFF printing error messages\n");
1184 printf(" -w Don't fail on data-loading errs, just warn. Useful if\n"
1185 " user has reduced/changed the common set of ICU data \n");
1186 printf(" -t_info | -t_error | -t_warn | -t_oc | -t_verbose Enable ICU tracing\n");
1187 printf(" -no_err_msg (same as -n) \n");
1188 printf(" -m n[-q] Min-Max memory size that will cause an allocation failure.\n");
1189 printf(" The default is the maximum value of size_t. Max is optional.\n");
1190 printf(" -r Repeat tests after calling u_cleanup \n");
1191 printf(" [/subtest] To run a subtest \n");
1192 printf(" eg: to run just the utility tests type: cintltest /tsutil) \n");
1195 int32_t T_CTEST_EXPORT2
1196 getTestOption ( int32_t testOption
) {
1197 switch (testOption
) {
1198 case VERBOSITY_OPTION
:
1200 case WARN_ON_MISSING_DATA_OPTION
:
1201 return WARN_ON_MISSING_DATA
;
1204 case REPEAT_TESTS_OPTION
:
1205 return REPEAT_TESTS
;
1206 case ERR_MSG_OPTION
:
1208 case ICU_TRACE_OPTION
:
1215 void T_CTEST_EXPORT2
1216 setTestOption ( int32_t testOption
, int32_t value
) {
1217 if (value
== DECREMENT_OPTION_VALUE
) {
1218 value
= getTestOption(testOption
);
1221 switch (testOption
) {
1222 case VERBOSITY_OPTION
:
1225 case WARN_ON_MISSING_DATA_OPTION
:
1226 WARN_ON_MISSING_DATA
= value
;
1231 case REPEAT_TESTS_OPTION
:
1232 REPEAT_TESTS
= value
;
1234 case ICU_TRACE_OPTION
:
1235 ICU_TRACE
= (UTraceLevel
)value
;
1244 * ================== JUnit support ================================
1249 ctest_xml_setFileName(const char *name
) {
1257 ctest_xml_init(const char *rootName
) {
1258 if(!XML_FILE_NAME
) return 0;
1259 XML_FILE
= fopen(XML_FILE_NAME
,"w");
1262 fprintf(stderr
," Error: couldn't open XML output file %s\n", XML_FILE_NAME
);
1265 while(*rootName
&&!isalnum((int)*rootName
)) {
1268 strcpy(XML_PREFIX
,rootName
);
1270 char *p
= XML_PREFIX
+strlen(XML_PREFIX
);
1271 for(p
--;*p
&&p
>XML_PREFIX
&&!isalnum((int)*p
);p
--) {
1276 fprintf(XML_FILE
, "<testsuite name=\"%s\">\n", XML_PREFIX
);
1283 ctest_xml_fini(void) {
1284 if(!XML_FILE
) return 0;
1286 fprintf(XML_FILE
, "</testsuite>\n");
1288 printf(" ( test results written to %s )\n", XML_FILE_NAME
);
1296 ctest_xml_testcase(const char *classname
, const char *name
, const char *timeSeconds
, const char *failMsg
) {
1297 if(!XML_FILE
) return 0;
1299 fprintf(XML_FILE
, "\t<testcase classname=\"%s:%s\" name=\"%s:%s\" time=\"%s\"", XML_PREFIX
, classname
, XML_PREFIX
, name
, timeSeconds
);
1301 fprintf(XML_FILE
, ">\n\t\t<failure type=\"err\" message=\"%s\"/>\n\t</testcase>\n", failMsg
);
1303 fprintf(XML_FILE
, "/>\n");