1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ********************************************************************************
6 * Copyright (C) 1996-2014, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 ********************************************************************************
18 #include "unicode/utrace.h"
19 #include "unicode/uclean.h"
24 3/20/1999 srl - strncpy called w/o setting nulls at the end
27 #define MAXTESTNAME 128
29 #define MAX_TEST_LOG 4096
32 * How may columns to indent the 'OK' markers.
34 #define FLAG_INDENT 45
36 * How many lines of scrollage can go by before we need to remind the user what the test is.
38 #define PAGE_SIZE_LIMIT 25
47 struct TestNode
* sibling
;
48 struct TestNode
* child
;
49 char name
[1]; /* This is dynamically allocated off the end with malloc. */
53 static const struct TestNode
* currentTest
;
55 typedef enum { RUNTESTS
, SHOWTESTS
} TestMode
;
56 #define TEST_SEPARATOR '/'
62 #include "unicode/ctest.h"
64 static char ERROR_LOG
[MAX_TEST_LOG
][MAXTESTNAME
];
66 /* Local prototypes */
67 static TestNode
* addTestNode( TestNode
*root
, const char *name
);
69 static TestNode
*createTestNode(const char* name
, int32_t nameLen
);
71 static int strncmp_nullcheck( const char* s1
,
75 static void getNextLevel( const char* name
,
77 const char** nextName
);
79 static void iterateTestsWithLevel( const TestNode
*root
, int depth
,
80 const TestNode
** nodeList
,
83 static void help ( const char *argv0
);
86 * Do the work of logging an error. Doesn't increase the error count.
88 * @prefix optional prefix prepended to message, or NULL.
89 * @param pattern printf style pattern
90 * @param ap vprintf style arg list
92 static void vlog_err(const char *prefix
, const char *pattern
, va_list ap
);
93 static void vlog_verbose(const char *prefix
, const char *pattern
, va_list ap
);
94 static UBool
vlog_knownIssue(const char *ticket
, const char *pattern
, va_list ap
);
97 * Log test structure, with indent
98 * @param pattern printf pattern
100 static void log_testinfo_i(const char *pattern
, ...);
103 * Log test structure, NO indent
104 * @param pattern printf pattern
106 static void log_testinfo(const char *pattern
, ...);
108 /* If we need to make the framework multi-thread safe
109 we need to pass around the following vars
111 static int ERRONEOUS_FUNCTION_COUNT
= 0;
112 static int ERROR_COUNT
= 0; /* Count of errors from all tests. */
113 static int ONE_ERROR
= 0; /* were there any other errors? */
114 static int DATA_ERROR_COUNT
= 0; /* count of data related errors or warnings */
115 static int INDENT_LEVEL
= 0;
116 static UBool NO_KNOWN
= FALSE
;
117 static void *knownList
= NULL
;
118 static char gTestName
[1024] = "";
119 static UBool ON_LINE
= FALSE
; /* are we on the top line with our test name? */
120 static UBool HANGING_OUTPUT
= FALSE
; /* did the user leave us without a trailing \n ? */
121 static int GLOBAL_PRINT_COUNT
= 0; /* global count of printouts */
122 int REPEAT_TESTS_INIT
= 0; /* Was REPEAT_TESTS initialized? */
123 int REPEAT_TESTS
= 1; /* Number of times to run the test */
124 int VERBOSITY
= 0; /* be No-verbose by default */
125 int ERR_MSG
=1; /* error messages will be displayed by default*/
126 int QUICK
= 1; /* Skip some of the slower tests? */
127 int WARN_ON_MISSING_DATA
= 0; /* Reduce data errs to warnings? */
128 UTraceLevel ICU_TRACE
= UTRACE_OFF
; /* ICU tracing level */
129 size_t MINIMUM_MEMORY_SIZE_FAILURE
= (size_t)-1; /* Minimum library memory allocation window that will fail. */
130 size_t MAXIMUM_MEMORY_SIZE_FAILURE
= (size_t)-1; /* Maximum library memory allocation window that will fail. */
131 static const char *ARGV_0
= "[ALL]";
132 static const char *XML_FILE_NAME
=NULL
;
133 static char XML_PREFIX
[256];
134 static const char *SUMMARY_FILE
= NULL
;
135 FILE *XML_FILE
= NULL
;
136 /*-------------------------------------------*/
138 /* strncmp that also makes sure there's a \0 at s2[0] */
139 static int strncmp_nullcheck( const char* s1
,
143 if (((int)strlen(s2
) >= n
) && s2
[n
] != 0) {
144 return 3; /* null check fails */
147 return strncmp ( s1
, s2
, n
);
151 static void getNextLevel( const char* name
,
153 const char** nextName
)
155 /* Get the next component of the name */
156 *nextName
= strchr(name
, TEST_SEPARATOR
);
161 *nameLen
= (int)((*nextName
) - name
);
162 (*nextName
)++; /* skip '/' */
163 strncpy(n
, name
, *nameLen
);
165 /*printf("->%s-< [%d] -> [%s]\n", name, *nameLen, *nextName);*/
168 *nameLen
= (int)strlen(name
);
172 static TestNode
*createTestNode(const char* name
, int32_t nameLen
)
176 newNode
= (TestNode
*)malloc(sizeof(TestNode
) + (nameLen
+ 1));
178 newNode
->test
= NULL
;
179 newNode
->sibling
= NULL
;
180 newNode
->child
= NULL
;
182 strncpy( newNode
->name
, name
, nameLen
);
183 newNode
->name
[nameLen
] = 0;
189 cleanUpTestTree(TestNode
*tn
)
191 if(tn
->child
!= NULL
) {
192 cleanUpTestTree(tn
->child
);
194 if(tn
->sibling
!= NULL
) {
195 cleanUpTestTree(tn
->sibling
);
204 addTest(TestNode
** root
,
205 TestFunctionPtr test
,
210 /*if this is the first Test created*/
212 *root
= createTestNode("", 0);
214 newNode
= addTestNode( *root
, name
);
215 assert(newNode
!= 0 );
216 /* printf("addTest: nreName = %s\n", newNode->name );*/
218 newNode
->test
= test
;
221 /* non recursive insert function */
222 static TestNode
*addTestNode ( TestNode
*root
, const char *name
)
224 const char* nextName
;
225 TestNode
*nextNode
, *curNode
;
226 int nameLen
; /* length of current 'name' */
228 /* remove leading slash */
229 if ( *name
== TEST_SEPARATOR
)
236 /* Start with the next child */
237 nextNode
= curNode
->child
;
239 getNextLevel ( name
, &nameLen
, &nextName
);
241 /* printf("* %s\n", name );*/
243 /* if nextNode is already null, then curNode has no children
245 if( nextNode
== NULL
)
247 /* Add all children of the node */
250 /* Get the next component of the name */
251 getNextLevel(name
, &nameLen
, &nextName
);
253 /* update curName to have the next name segment */
254 curNode
->child
= createTestNode(name
, nameLen
);
255 /* printf("*** added %s\n", curNode->child->name );*/
256 curNode
= curNode
->child
;
259 while( name
!= NULL
);
264 /* Search across for the name */
265 while (strncmp_nullcheck ( name
, nextNode
->name
, nameLen
) != 0 )
268 nextNode
= nextNode
-> sibling
;
270 if ( nextNode
== NULL
)
272 /* Did not find 'name' on this level. */
273 nextNode
= createTestNode(name
, nameLen
);
274 curNode
->sibling
= nextNode
;
279 /* nextNode matches 'name' */
281 if (nextName
== NULL
) /* end of the line */
286 /* Loop again with the next item */
293 * Log the time taken. May not output anything.
294 * @param deltaTime change in time
296 void 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
);
307 str
[0]=0; /* at least terminate it. */
311 static void print_timeDelta(UDate deltaTime
) {
313 str_timeDelta(str
, deltaTime
);
320 * Run or list tests (according to mode) in a subtree.
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.
327 static void iterateTestsWithLevel ( const TestNode
* root
,
329 const TestNode
** nodeList
,
334 char pathToFunction
[MAXTESTNAME
] = "";
335 char separatorString
[2] = { TEST_SEPARATOR
, '\0'};
337 UDate allStartTime
= -1, allStopTime
= -1;
341 allStartTime
= uprv_getRawUTCtime();
347 /* record the current root node, and increment depth. */
348 nodeList
[depth
++] = root
;
349 /* depth is now the depth of root's children. */
351 /* Collect the 'path' to the current subtree. */
352 for ( i
=0;i
<(depth
-1);i
++ )
354 strcat(pathToFunction
, nodeList
[i
]->name
);
355 strcat(pathToFunction
, separatorString
);
357 strcat(pathToFunction
, nodeList
[i
]->name
); /* including 'root' */
359 /* print test name and space. */
360 INDENT_LEVEL
= depth
-1;
362 log_testinfo_i("%s ", root
->name
);
364 log_testinfo_i("(%s) ", ARGV_0
);
366 ON_LINE
= TRUE
; /* we are still on the line with the test name */
369 if ( (mode
== RUNTESTS
) &&
370 (root
->test
!= NULL
)) /* if root is a leaf node, run it */
372 int myERROR_COUNT
= ERROR_COUNT
;
373 int myGLOBAL_PRINT_COUNT
= GLOBAL_PRINT_COUNT
;
375 UDate startTime
, stopTime
;
377 char timeSeconds
[256];
379 const char timeDelta
[] = "(unknown)";
380 const char timeSeconds
[] = "0.000";
383 INDENT_LEVEL
= depth
; /* depth of subitems */
385 HANGING_OUTPUT
=FALSE
;
387 startTime
= uprv_getRawUTCtime();
389 strcpy(gTestName
, pathToFunction
);
390 root
->test(); /* PERFORM THE TEST ************************/
392 stopTime
= uprv_getRawUTCtime();
396 HANGING_OUTPUT
=FALSE
;
398 INDENT_LEVEL
= depth
-1; /* depth of root */
400 if((ONE_ERROR
>0)&&(ERROR_COUNT
==0)) {
401 ERROR_COUNT
++; /* There was an error without a newline */
406 str_timeDelta(timeDelta
, stopTime
-startTime
);
407 sprintf(timeSeconds
, "%f", (stopTime
-startTime
)/1000.0);
409 ctest_xml_testcase(pathToFunction
, pathToFunction
, timeSeconds
, (myERROR_COUNT
!=ERROR_COUNT
)?"error":NULL
);
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
);
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. */
422 /* put -- out at 30 sp. */
423 int spaces
= FLAG_INDENT
- ((int)strlen(root
->name
) + depth
);
424 if(spaces
<0) spaces
=0;
425 log_testinfo(" %*s[OK] ", spaces
,"---");
430 if(timeDelta
[0]) printf("%s", timeDelta
);
433 ON_LINE
= TRUE
; /* we are back on-line */
436 INDENT_LEVEL
= depth
-1; /* root */
438 /* we want these messages to be at 0 indent. so just push the indent level breifly. */
439 if(mode
==SHOWTESTS
) {
440 log_testinfo("---%s%c\n",pathToFunction
, nodeList
[i
]->test
?' ':TEST_SEPARATOR
);
443 INDENT_LEVEL
= depth
;
446 int myERROR_COUNT
= ERROR_COUNT
;
447 int myGLOBAL_PRINT_COUNT
= GLOBAL_PRINT_COUNT
;
448 if(mode
!=SHOWTESTS
) {
449 INDENT_LEVEL
=depth
-1;
454 iterateTestsWithLevel ( root
->child
, depth
, nodeList
, mode
);
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. */
465 log_testinfo(" / (%s) ", ARGV_0
);
476 allStopTime
= uprv_getRawUTCtime();
477 print_timeDelta(allStopTime
-allStartTime
);
481 if(mode
!=SHOWTESTS
&& ON_LINE
) {
485 if ( depth
!= 0 ) { /* DO NOT iterate over siblings of the root. TODO: why not? */
486 iterateTestsWithLevel ( root
->sibling
, depth
, nodeList
, mode
);
493 showTests ( const TestNode
*root
)
495 /* make up one for them */
496 const TestNode
*nodeList
[MAXTESTS
];
499 log_err("TEST CAN'T BE FOUND!");
501 iterateTestsWithLevel ( root
, 0, nodeList
, SHOWTESTS
);
506 runTests ( const TestNode
*root
)
509 const TestNode
*nodeList
[MAXTESTS
];
510 /* make up one for them */
514 log_err("TEST CAN'T BE FOUND!\n");
516 ERRONEOUS_FUNCTION_COUNT
= ERROR_COUNT
= 0;
517 iterateTestsWithLevel ( root
, 0, nodeList
, RUNTESTS
);
519 /*print out result summary*/
521 ON_LINE
=FALSE
; /* just in case */
523 if(knownList
!= NULL
) {
524 if( udbg_knownIssue_print(knownList
) ) {
525 fprintf(stdout
, "(To run suppressed tests, use the -K option.) \n\n");
527 udbg_knownIssue_close(knownList
);
533 fprintf(stdout
,"\nSUMMARY:\n");
535 fprintf(stdout
,"******* [Total error count:\t%d]\n", ERROR_COUNT
);
537 fprintf(stdout
, " Errors in\n");
538 for (i
=0;i
< ERRONEOUS_FUNCTION_COUNT
; i
++)
539 fprintf(stdout
, "[%s]\n",ERROR_LOG
[i
]);
540 if(SUMMARY_FILE
!= NULL
) {
541 FILE *summf
= fopen(SUMMARY_FILE
, "w");
543 for (i
=0;i
< ERRONEOUS_FUNCTION_COUNT
; i
++)
544 fprintf(summf
, "%s\n",ERROR_LOG
[i
]);
551 log_testinfo("\n[All tests passed successfully...]\n");
554 if(DATA_ERROR_COUNT
) {
555 if(WARN_ON_MISSING_DATA
==0) {
556 log_testinfo("\t*Note* some errors are data-loading related. If the data used is not the \n"
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");
560 log_testinfo("\t*WARNING* some data-loading errors were ignored by the -w option.\n");
565 const char* T_CTEST_EXPORT2
568 if(currentTest
!= NULL
) {
569 return currentTest
->name
;
575 const TestNode
* T_CTEST_EXPORT2
576 getTest(const TestNode
* root
, const char* name
)
578 const char* nextName
;
580 const TestNode
* curNode
;
581 int nameLen
; /* length of current 'name' */
584 log_err("TEST CAN'T BE FOUND!\n");
587 /* remove leading slash */
588 if ( *name
== TEST_SEPARATOR
)
595 /* Start with the next child */
596 nextNode
= curNode
->child
;
598 getNextLevel ( name
, &nameLen
, &nextName
);
600 /* printf("* %s\n", name );*/
602 /* if nextNode is already null, then curNode has no children
604 if( nextNode
== NULL
)
609 /* Search across for the name */
610 while (strncmp_nullcheck ( name
, nextNode
->name
, nameLen
) != 0 )
613 nextNode
= nextNode
-> sibling
;
615 if ( nextNode
== NULL
)
617 /* Did not find 'name' on this level. */
622 /* nextNode matches 'name' */
624 if (nextName
== NULL
) /* end of the line */
629 /* Loop again with the next item */
635 /* =========== io functions ======== */
637 static void go_offline_with_marker(const char *mrk
) {
638 UBool wasON_LINE
= ON_LINE
;
641 log_testinfo(" {\n");
645 if(!HANGING_OUTPUT
|| wasON_LINE
) {
652 static void go_offline() {
653 go_offline_with_marker(NULL
);
656 static void go_offline_err() {
660 static void first_line_verbose() {
661 go_offline_with_marker("v");
664 static void first_line_err() {
665 go_offline_with_marker("!");
668 static void first_line_info() {
669 go_offline_with_marker("\"");
672 static void first_line_test() {
677 static void vlog_err(const char *prefix
, const char *pattern
, va_list ap
)
679 if( ERR_MSG
== FALSE
){
682 fputs("!", stdout
); /* col 1 - bang */
683 fprintf(stdout
, "%-*s", INDENT_LEVEL
,"" );
685 fputs(prefix
, stdout
);
687 vfprintf(stdout
, pattern
, ap
);
690 if((*pattern
==0) || (pattern
[strlen(pattern
)-1]!='\n')) {
695 GLOBAL_PRINT_COUNT
++;
698 static UBool
vlog_knownIssue(const char *ticket
, const char *pattern
, va_list ap
)
701 UBool firstForTicket
;
704 if(NO_KNOWN
) return FALSE
;
705 if(pattern
==NULL
) pattern
="";
707 vsprintf(buf
, pattern
, ap
);
708 knownList
= udbg_knownIssue_open(knownList
, ticket
, gTestName
, buf
,
709 &firstForTicket
, &firstForWhere
);
711 if(firstForTicket
|| firstForWhere
) {
712 log_info("(Known issue #%s) %s\n", ticket
, buf
);
714 log_verbose("(Known issue #%s) %s\n", ticket
, buf
);
722 vlog_info(const char *prefix
, const char *pattern
, va_list ap
)
725 fprintf(stdout
, "%-*s", INDENT_LEVEL
,"" );
727 fputs(prefix
, stdout
);
729 vfprintf(stdout
, pattern
, ap
);
732 if((*pattern
==0) || (pattern
[strlen(pattern
)-1]!='\n')) {
737 GLOBAL_PRINT_COUNT
++;
740 * Log test structure, with indent
742 static void log_testinfo_i(const char *pattern
, ...)
746 fprintf(stdout
, "%-*s", INDENT_LEVEL
,"" );
747 va_start(ap
, pattern
);
748 vfprintf(stdout
, pattern
, ap
);
751 GLOBAL_PRINT_COUNT
++;
754 * Log test structure (no ident)
756 static void log_testinfo(const char *pattern
, ...)
759 va_start(ap
, pattern
);
761 vfprintf(stdout
, pattern
, ap
);
764 GLOBAL_PRINT_COUNT
++;
768 static void vlog_verbose(const char *prefix
, const char *pattern
, va_list ap
)
770 if ( VERBOSITY
== FALSE
)
773 first_line_verbose();
774 fprintf(stdout
, "%-*s", INDENT_LEVEL
,"" );
776 fputs(prefix
, stdout
);
778 vfprintf(stdout
, pattern
, ap
);
781 GLOBAL_PRINT_COUNT
++;
782 if((*pattern
==0) || (pattern
[strlen(pattern
)-1]!='\n')) {
790 log_err(const char* pattern
, ...)
794 if(strchr(pattern
, '\n') != NULL
) {
796 * Count errors only if there is a line feed in the pattern
797 * so that we do not exaggerate our error count.
801 /* Count at least one error. */
804 va_start(ap
, pattern
);
805 vlog_err(NULL
, pattern
, ap
);
808 UBool T_CTEST_EXPORT2
809 log_knownIssue(const char *ticket
, const char *pattern
, ...) {
811 va_start(ap
, pattern
);
812 return vlog_knownIssue(ticket
, pattern
, ap
);
816 log_err_status(UErrorCode status
, const char* pattern
, ...)
819 va_start(ap
, pattern
);
821 if ((status
== U_FILE_ACCESS_ERROR
|| status
== U_MISSING_RESOURCE_ERROR
)) {
822 ++DATA_ERROR_COUNT
; /* for informational message at the end */
824 if (WARN_ON_MISSING_DATA
== 0) {
827 if (strchr(pattern
, '\n') != NULL
) {
832 vlog_err(NULL
, pattern
, ap
); /* no need for prefix in default case */
834 vlog_info("[DATA] ", pattern
, ap
);
839 if(strchr(pattern
, '\n') != NULL
) {
844 vlog_err(NULL
, pattern
, ap
); /* no need for prefix in default case */
849 log_info(const char* pattern
, ...)
853 va_start(ap
, pattern
);
854 vlog_info(NULL
, pattern
, ap
);
858 log_verbose(const char* pattern
, ...)
862 va_start(ap
, pattern
);
863 vlog_verbose(NULL
, pattern
, ap
);
868 log_data_err(const char* pattern
, ...)
871 va_start(ap
, pattern
);
874 ++DATA_ERROR_COUNT
; /* for informational message at the end */
876 if(WARN_ON_MISSING_DATA
== 0) {
878 if(strchr(pattern
, '\n') != NULL
) {
881 vlog_err(NULL
, pattern
, ap
); /* no need for prefix in default case */
883 vlog_info("[DATA] ", pattern
, ap
);
891 static int traceFnNestingDepth
= 0;
893 static void U_CALLCONV
TraceEntry(const void *context
, int32_t fnNumber
) {
895 utrace_format(buf
, sizeof(buf
), traceFnNestingDepth
*3, "%s() enter.\n", utrace_functionName(fnNumber
)); buf
[sizeof(buf
)-1]=0;
897 traceFnNestingDepth
++;
900 static void U_CALLCONV
TraceExit(const void *context
, int32_t fnNumber
, const char *fmt
, va_list args
) { char buf
[500];
902 if (traceFnNestingDepth
>0) {
903 traceFnNestingDepth
--;
905 utrace_format(buf
, sizeof(buf
), traceFnNestingDepth
*3, "%s() ", utrace_functionName(fnNumber
)); buf
[sizeof(buf
)-1]=0;
907 utrace_vformat(buf
, sizeof(buf
), traceFnNestingDepth
*3, fmt
, args
);
908 buf
[sizeof(buf
)-1]=0;
913 static void U_CALLCONV
TraceData(const void *context
, int32_t fnNumber
,
914 int32_t level
, const char *fmt
, va_list args
) {
916 utrace_vformat(buf
, sizeof(buf
), traceFnNestingDepth
*3, fmt
, args
);
917 buf
[sizeof(buf
)-1]=0;
922 static void *U_CALLCONV
ctest_libMalloc(const void *context
, size_t size
) {
924 printf("Allocated %ld\n", (long)size);
926 if (MINIMUM_MEMORY_SIZE_FAILURE
<= size
&& size
<= MAXIMUM_MEMORY_SIZE_FAILURE
) {
931 static void *U_CALLCONV
ctest_libRealloc(const void *context
, void *mem
, size_t size
) {
933 printf("Reallocated %ld\n", (long)size);
935 if (MINIMUM_MEMORY_SIZE_FAILURE
<= size
&& size
<= MAXIMUM_MEMORY_SIZE_FAILURE
) {
936 /*free(mem);*/ /* Realloc doesn't free on failure. */
939 return realloc(mem
, size
);
941 static void U_CALLCONV
ctest_libFree(const void *context
, void *mem
) {
946 initArgs( int argc
, const char* const argv
[], ArgHandlerPtr argHandler
, void *context
)
956 for( i
=1; i
<argc
; i
++)
958 if ( argv
[i
][0] == '/' )
960 /* We don't run the tests here. */
963 else if ((strcmp( argv
[i
], "-a") == 0) || (strcmp(argv
[i
],"-all") == 0))
965 /* We don't run the tests here. */
968 else if (strcmp( argv
[i
], "-v" )==0 || strcmp( argv
[i
], "-verbose")==0)
972 else if (strcmp( argv
[i
], "-l" )==0 )
976 else if (strcmp( argv
[i
], "-e1") == 0)
980 else if (strcmp( argv
[i
], "-e") ==0)
984 else if (strcmp( argv
[i
], "-K") ==0)
988 else if (strncmp( argv
[i
], "-E",2) ==0)
990 SUMMARY_FILE
=argv
[i
]+2;
992 else if (strcmp( argv
[i
], "-w") ==0)
994 WARN_ON_MISSING_DATA
= TRUE
;
996 else if (strcmp( argv
[i
], "-m") ==0)
998 UErrorCode errorCode
= U_ZERO_ERROR
;
1000 char *endPtr
= NULL
;
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
]);
1008 if (*endPtr
== '-') {
1009 char *maxPtr
= endPtr
+1;
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
]);
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
));
1026 else if(strcmp( argv
[i
], "-n") == 0 || strcmp( argv
[i
], "-no_err_msg") == 0)
1030 else if (strcmp( argv
[i
], "-r") == 0)
1032 if (!REPEAT_TESTS_INIT
) {
1036 else if (strcmp( argv
[i
], "-x") == 0)
1039 printf("* Error: '-x' option requires an argument. usage: '-x outfile.xml'.\n");
1042 if(ctest_xml_setFileName(argv
[i
])) { /* set the name */
1046 else if (strcmp( argv
[i
], "-t_info") == 0) {
1047 ICU_TRACE
= UTRACE_INFO
;
1049 else if (strcmp( argv
[i
], "-t_error") == 0) {
1050 ICU_TRACE
= UTRACE_ERROR
;
1052 else if (strcmp( argv
[i
], "-t_warn") == 0) {
1053 ICU_TRACE
= UTRACE_WARNING
;
1055 else if (strcmp( argv
[i
], "-t_verbose") == 0) {
1056 ICU_TRACE
= UTRACE_VERBOSE
;
1058 else if (strcmp( argv
[i
], "-t_oc") == 0) {
1059 ICU_TRACE
= UTRACE_OPEN_CLOSE
;
1061 else if (strcmp( argv
[i
], "-h" )==0 || strcmp( argv
[i
], "--help" )==0)
1066 else if (argHandler
!= NULL
&& (argSkip
= argHandler(i
, argc
, argv
, context
)) > 0)
1072 printf("* unknown option: %s\n", argv
[i
]);
1077 if (ICU_TRACE
!= UTRACE_OFF
) {
1078 utrace_setFunctions(NULL
, TraceEntry
, TraceExit
, TraceData
);
1079 utrace_setLevel(ICU_TRACE
);
1082 return 1; /* total error count */
1086 runTestRequest(const TestNode
* root
,
1088 const char* const argv
[])
1091 * This main will parse the l, v, h, n, and path arguments
1093 const TestNode
* toRun
;
1096 int subtreeOptionSeen
= FALSE
;
1102 if(ctest_xml_init(ARGV_0
)) {
1103 return 1; /* couldn't fire up XML thing */
1106 for( i
=1; i
<argc
; i
++)
1108 if ( argv
[i
][0] == '/' )
1110 printf("Selecting subtree '%s'\n", argv
[i
]);
1112 if ( argv
[i
][1] == 0 )
1115 toRun
= getTest(root
, argv
[i
]);
1117 if ( toRun
== NULL
)
1119 printf("* Could not find any matching subtree\n");
1123 ON_LINE
=FALSE
; /* just in case */
1130 ON_LINE
=FALSE
; /* just in case */
1132 errorCount
+= ERROR_COUNT
;
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) {
1140 /* else option already handled by initArgs */
1143 if( subtreeOptionSeen
== FALSE
) /* no other subtree given, run the default */
1145 ON_LINE
=FALSE
; /* just in case */
1150 ON_LINE
=FALSE
; /* just in case */
1152 errorCount
+= ERROR_COUNT
;
1156 if( ( doList
== FALSE
) && ( errorCount
> 0 ) )
1157 printf(" Total errors: %d\n", errorCount
);
1160 REPEAT_TESTS_INIT
= 1;
1162 if(ctest_xml_fini()) {
1166 return errorCount
; /* total error count */
1170 * Display program invocation arguments
1173 static void help ( const char *argv0
)
1175 printf("Usage: %s [ -l ] [ -v ] [ -verbose] [-a] [ -all] [-n] [ -no_err_msg]\n"
1176 " [ -h ] [-t_info | -t_error | -t_warn | -t_oc | -t_verbose] [-m n[-q] ]\n"
1177 " [ /path/to/test ]\n",
1179 printf(" -l To get a list of test names\n");
1180 printf(" -e to do exhaustive testing\n");
1181 printf(" -verbose To turn ON verbosity\n");
1182 printf(" -v To turn ON verbosity(same as -verbose)\n");
1183 printf(" -x file.xml Write junit format output to file.xml\n");
1184 printf(" -h To print this message\n");
1185 printf(" -K to turn OFF suppressing known issues\n");
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"
1188 " user has reduced/changed the common set of ICU data \n");
1189 printf(" -t_info | -t_error | -t_warn | -t_oc | -t_verbose Enable ICU tracing\n");
1190 printf(" -no_err_msg (same as -n) \n");
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");
1195 printf(" eg: to run just the utility tests type: cintltest /tsutil) \n");
1198 int32_t T_CTEST_EXPORT2
1199 getTestOption ( int32_t testOption
) {
1200 switch (testOption
) {
1201 case VERBOSITY_OPTION
:
1203 case WARN_ON_MISSING_DATA_OPTION
:
1204 return WARN_ON_MISSING_DATA
;
1207 case REPEAT_TESTS_OPTION
:
1208 return REPEAT_TESTS
;
1209 case ERR_MSG_OPTION
:
1211 case ICU_TRACE_OPTION
:
1218 void T_CTEST_EXPORT2
1219 setTestOption ( int32_t testOption
, int32_t value
) {
1220 if (value
== DECREMENT_OPTION_VALUE
) {
1221 value
= getTestOption(testOption
);
1224 switch (testOption
) {
1225 case VERBOSITY_OPTION
:
1228 case WARN_ON_MISSING_DATA_OPTION
:
1229 WARN_ON_MISSING_DATA
= value
;
1234 case REPEAT_TESTS_OPTION
:
1235 REPEAT_TESTS
= value
;
1237 case ICU_TRACE_OPTION
:
1238 ICU_TRACE
= (UTraceLevel
)value
;
1247 * ================== JUnit support ================================
1252 ctest_xml_setFileName(const char *name
) {
1260 ctest_xml_init(const char *rootName
) {
1261 if(!XML_FILE_NAME
) return 0;
1262 XML_FILE
= fopen(XML_FILE_NAME
,"w");
1265 fprintf(stderr
," Error: couldn't open XML output file %s\n", XML_FILE_NAME
);
1268 while(*rootName
&&!isalnum((int)*rootName
)) {
1271 strcpy(XML_PREFIX
,rootName
);
1273 char *p
= XML_PREFIX
+strlen(XML_PREFIX
);
1274 for(p
--;*p
&&p
>XML_PREFIX
&&!isalnum((int)*p
);p
--) {
1279 fprintf(XML_FILE
, "<testsuite name=\"%s\">\n", XML_PREFIX
);
1286 ctest_xml_fini(void) {
1287 if(!XML_FILE
) return 0;
1289 fprintf(XML_FILE
, "</testsuite>\n");
1291 printf(" ( test results written to %s )\n", XML_FILE_NAME
);
1299 ctest_xml_testcase(const char *classname
, const char *name
, const char *timeSeconds
, const char *failMsg
) {
1300 if(!XML_FILE
) return 0;
1302 fprintf(XML_FILE
, "\t<testcase classname=\"%s:%s\" name=\"%s:%s\" time=\"%s\"", XML_PREFIX
, classname
, XML_PREFIX
, name
, timeSeconds
);
1304 fprintf(XML_FILE
, ">\n\t\t<failure type=\"err\" message=\"%s\"/>\n\t</testcase>\n", failMsg
);
1306 fprintf(XML_FILE
, "/>\n");