2 ********************************************************************************
4 * Copyright (C) 1996-2010, 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
);
94 * Log test structure, with indent
95 * @param pattern printf pattern
97 static void log_testinfo_i(const char *pattern
, ...);
100 * Log test structure, NO indent
101 * @param pattern printf pattern
103 static void log_testinfo(const char *pattern
, ...);
105 /* If we need to make the framework multi-thread safe
106 we need to pass around the following vars
108 static int ERRONEOUS_FUNCTION_COUNT
= 0;
109 static int ERROR_COUNT
= 0; /* Count of errors from all tests. */
110 static int ONE_ERROR
= 0; /* were there any other errors? */
111 static int DATA_ERROR_COUNT
= 0; /* count of data related errors or warnings */
112 static int INDENT_LEVEL
= 0;
113 static UBool ON_LINE
= FALSE
; /* are we on the top line with our test name? */
114 static UBool HANGING_OUTPUT
= FALSE
; /* did the user leave us without a trailing \n ? */
115 static int GLOBAL_PRINT_COUNT
= 0; /* global count of printouts */
116 int REPEAT_TESTS_INIT
= 0; /* Was REPEAT_TESTS initialized? */
117 int REPEAT_TESTS
= 1; /* Number of times to run the test */
118 int VERBOSITY
= 0; /* be No-verbose by default */
119 int ERR_MSG
=1; /* error messages will be displayed by default*/
120 int QUICK
= 1; /* Skip some of the slower tests? */
121 int WARN_ON_MISSING_DATA
= 0; /* Reduce data errs to warnings? */
122 UTraceLevel ICU_TRACE
= UTRACE_OFF
; /* ICU tracing level */
123 size_t MINIMUM_MEMORY_SIZE_FAILURE
= (size_t)-1; /* Minimum library memory allocation window that will fail. */
124 size_t MAXIMUM_MEMORY_SIZE_FAILURE
= (size_t)-1; /* Maximum library memory allocation window that will fail. */
125 int32_t ALLOCATION_COUNT
= 0;
126 static const char *ARGV_0
= "[ALL]";
127 static const char *XML_FILE_NAME
=NULL
;
128 static char XML_PREFIX
[256];
130 FILE *XML_FILE
= NULL
;
131 /*-------------------------------------------*/
133 /* strncmp that also makes sure there's a \0 at s2[0] */
134 static int strncmp_nullcheck( const char* s1
,
138 if (((int)strlen(s2
) >= n
) && s2
[n
] != 0) {
139 return 3; /* null check fails */
142 return strncmp ( s1
, s2
, n
);
146 static void getNextLevel( const char* name
,
148 const char** nextName
)
150 /* Get the next component of the name */
151 *nextName
= strchr(name
, TEST_SEPARATOR
);
156 *nameLen
= (int)((*nextName
) - name
);
157 (*nextName
)++; /* skip '/' */
158 strncpy(n
, name
, *nameLen
);
160 /*printf("->%s-< [%d] -> [%s]\n", name, *nameLen, *nextName);*/
163 *nameLen
= (int)strlen(name
);
167 static TestNode
*createTestNode(const char* name
, int32_t nameLen
)
171 newNode
= (TestNode
*)malloc(sizeof(TestNode
) + (nameLen
+ 1));
173 newNode
->test
= NULL
;
174 newNode
->sibling
= NULL
;
175 newNode
->child
= NULL
;
177 strncpy( newNode
->name
, name
, nameLen
);
178 newNode
->name
[nameLen
] = 0;
184 cleanUpTestTree(TestNode
*tn
)
186 if(tn
->child
!= NULL
) {
187 cleanUpTestTree(tn
->child
);
189 if(tn
->sibling
!= NULL
) {
190 cleanUpTestTree(tn
->sibling
);
199 addTest(TestNode
** root
,
200 TestFunctionPtr test
,
205 /*if this is the first Test created*/
207 *root
= createTestNode("", 0);
209 newNode
= addTestNode( *root
, name
);
210 assert(newNode
!= 0 );
211 /* printf("addTest: nreName = %s\n", newNode->name );*/
213 newNode
->test
= test
;
216 /* non recursive insert function */
217 static TestNode
*addTestNode ( TestNode
*root
, const char *name
)
219 const char* nextName
;
220 TestNode
*nextNode
, *curNode
;
221 int nameLen
; /* length of current 'name' */
223 /* remove leading slash */
224 if ( *name
== TEST_SEPARATOR
)
231 /* Start with the next child */
232 nextNode
= curNode
->child
;
234 getNextLevel ( name
, &nameLen
, &nextName
);
236 /* printf("* %s\n", name );*/
238 /* if nextNode is already null, then curNode has no children
240 if( nextNode
== NULL
)
242 /* Add all children of the node */
245 /* Get the next component of the name */
246 getNextLevel(name
, &nameLen
, &nextName
);
248 /* update curName to have the next name segment */
249 curNode
->child
= createTestNode(name
, nameLen
);
250 /* printf("*** added %s\n", curNode->child->name );*/
251 curNode
= curNode
->child
;
254 while( name
!= NULL
);
259 /* Search across for the name */
260 while (strncmp_nullcheck ( name
, nextNode
->name
, nameLen
) != 0 )
263 nextNode
= nextNode
-> sibling
;
265 if ( nextNode
== NULL
)
267 /* Did not find 'name' on this level. */
268 nextNode
= createTestNode(name
, nameLen
);
269 curNode
->sibling
= nextNode
;
274 /* nextNode matches 'name' */
276 if (nextName
== NULL
) /* end of the line */
281 /* Loop again with the next item */
288 * Log the time taken. May not output anything.
289 * @param deltaTime change in time
291 void T_CTEST_EXPORT2
str_timeDelta(char *str
, UDate deltaTime
) {
292 if (deltaTime
> 110000.0 ) {
293 double mins
= uprv_floor(deltaTime
/60000.0);
294 sprintf(str
, "[(%.0fm %.1fs)]", mins
, (deltaTime
-(mins
*60000.0))/1000.0);
295 } else if (deltaTime
> 1500.0) {
296 sprintf(str
, "((%.1fs))", deltaTime
/1000.0);
297 } else if(deltaTime
>900.0) {
298 sprintf(str
, "( %.2fs )", deltaTime
/1000.0);
299 } else if(deltaTime
> 5.0) {
300 sprintf(str
, " (%.0fms) ", deltaTime
);
302 str
[0]=0; /* at least terminate it. */
306 static void print_timeDelta(UDate deltaTime
) {
308 str_timeDelta(str
, deltaTime
);
315 * Run or list tests (according to mode) in a subtree.
317 * @param root root of the subtree to operate on
318 * @param depth The depth of this tree (0=root)
319 * @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.
320 * @param mode what mode we are operating in.
322 static void iterateTestsWithLevel ( const TestNode
* root
,
324 const TestNode
** nodeList
,
329 char pathToFunction
[MAXTESTNAME
] = "";
330 char separatorString
[2] = { TEST_SEPARATOR
, '\0'};
332 UDate allStartTime
= -1, allStopTime
= -1;
336 allStartTime
= uprv_getRawUTCtime();
342 /* record the current root node, and increment depth. */
343 nodeList
[depth
++] = root
;
344 /* depth is now the depth of root's children. */
346 /* Collect the 'path' to the current subtree. */
347 for ( i
=0;i
<(depth
-1);i
++ )
349 strcat(pathToFunction
, nodeList
[i
]->name
);
350 strcat(pathToFunction
, separatorString
);
352 strcat(pathToFunction
, nodeList
[i
]->name
); /* including 'root' */
354 /* print test name and space. */
355 INDENT_LEVEL
= depth
-1;
357 log_testinfo_i("%s ", root
->name
);
359 log_testinfo_i("(%s) ", ARGV_0
);
361 ON_LINE
= TRUE
; /* we are still on the line with the test name */
364 if ( (mode
== RUNTESTS
) &&
365 (root
->test
!= NULL
)) /* if root is a leaf node, run it */
367 int myERROR_COUNT
= ERROR_COUNT
;
368 int myGLOBAL_PRINT_COUNT
= GLOBAL_PRINT_COUNT
;
370 UDate startTime
, stopTime
;
372 char timeSeconds
[256];
374 const char timeDelta
[] = "(unknown)";
375 const char timeSeconds
[] = "0.000";
378 INDENT_LEVEL
= depth
; /* depth of subitems */
380 HANGING_OUTPUT
=FALSE
;
382 startTime
= uprv_getRawUTCtime();
384 root
->test(); /* PERFORM THE TEST ************************/
386 stopTime
= uprv_getRawUTCtime();
390 HANGING_OUTPUT
=FALSE
;
392 INDENT_LEVEL
= depth
-1; /* depth of root */
394 if((ONE_ERROR
>0)&&(ERROR_COUNT
==0)) {
395 ERROR_COUNT
++; /* There was an error without a newline */
400 str_timeDelta(timeDelta
, stopTime
-startTime
);
401 sprintf(timeSeconds
, "%f", (stopTime
-startTime
)/1000.0);
403 ctest_xml_testcase(pathToFunction
, pathToFunction
, timeSeconds
, (myERROR_COUNT
!=ERROR_COUNT
)?"error":NULL
);
405 if (myERROR_COUNT
!= ERROR_COUNT
) {
406 log_testinfo_i("} ---[%d ERRORS in %s] ", ERROR_COUNT
- myERROR_COUNT
, pathToFunction
);
407 strcpy(ERROR_LOG
[ERRONEOUS_FUNCTION_COUNT
++], pathToFunction
);
409 if(!ON_LINE
) { /* had some output */
410 int spaces
= FLAG_INDENT
-(depth
-1);
411 log_testinfo_i("} %*s[OK] ", spaces
, "---");
412 if((GLOBAL_PRINT_COUNT
-myGLOBAL_PRINT_COUNT
)>PAGE_SIZE_LIMIT
) {
413 log_testinfo(" %s ", pathToFunction
); /* in case they forgot. */
416 /* put -- out at 30 sp. */
417 int spaces
= FLAG_INDENT
-(strlen(root
->name
)+depth
);
418 if(spaces
<0) spaces
=0;
419 log_testinfo(" %*s[OK] ", spaces
,"---");
424 if(timeDelta
[0]) printf("%s", timeDelta
);
427 ON_LINE
= TRUE
; /* we are back on-line */
430 INDENT_LEVEL
= depth
-1; /* root */
432 /* we want these messages to be at 0 indent. so just push the indent level breifly. */
433 if(mode
==SHOWTESTS
) {
434 log_testinfo("---%s%c\n",pathToFunction
, nodeList
[i
]->test
?' ':TEST_SEPARATOR
);
437 INDENT_LEVEL
= depth
;
440 int myERROR_COUNT
= ERROR_COUNT
;
441 int myGLOBAL_PRINT_COUNT
= GLOBAL_PRINT_COUNT
;
442 if(mode
!=SHOWTESTS
) {
443 INDENT_LEVEL
=depth
-1;
448 iterateTestsWithLevel ( root
->child
, depth
, nodeList
, mode
);
450 if(mode
!=SHOWTESTS
) {
451 INDENT_LEVEL
=depth
-1;
452 log_testinfo_i("} "); /* TODO: summarize subtests */
453 if((depth
>1) && (ERROR_COUNT
> myERROR_COUNT
)) {
454 log_testinfo("[%d %s in %s] ", ERROR_COUNT
-myERROR_COUNT
, (ERROR_COUNT
-myERROR_COUNT
)==1?"error":"errors", pathToFunction
);
455 } else if((GLOBAL_PRINT_COUNT
-myGLOBAL_PRINT_COUNT
)>PAGE_SIZE_LIMIT
|| (depth
<1)) {
456 if(pathToFunction
[0]) {
457 log_testinfo(" %s ", pathToFunction
); /* in case they forgot. */
459 log_testinfo(" / (%s) ", ARGV_0
);
470 allStopTime
= uprv_getRawUTCtime();
471 print_timeDelta(allStopTime
-allStartTime
);
475 if(mode
!=SHOWTESTS
&& ON_LINE
) {
479 if ( depth
!= 0 ) { /* DO NOT iterate over siblings of the root. TODO: why not? */
480 iterateTestsWithLevel ( root
->sibling
, depth
, nodeList
, mode
);
487 showTests ( const TestNode
*root
)
489 /* make up one for them */
490 const TestNode
*nodeList
[MAXTESTS
];
493 log_err("TEST CAN'T BE FOUND!");
495 iterateTestsWithLevel ( root
, 0, nodeList
, SHOWTESTS
);
500 runTests ( const TestNode
*root
)
503 const TestNode
*nodeList
[MAXTESTS
];
504 /* make up one for them */
508 log_err("TEST CAN'T BE FOUND!\n");
510 ERRONEOUS_FUNCTION_COUNT
= ERROR_COUNT
= 0;
511 iterateTestsWithLevel ( root
, 0, nodeList
, RUNTESTS
);
513 /*print out result summary*/
515 ON_LINE
=FALSE
; /* just in case */
519 fprintf(stdout
,"\nSUMMARY:\n");
521 fprintf(stdout
,"******* [Total error count:\t%d]\n", ERROR_COUNT
);
523 fprintf(stdout
, " Errors in\n");
524 for (i
=0;i
< ERRONEOUS_FUNCTION_COUNT
; i
++)
525 fprintf(stdout
, "[%s]\n",ERROR_LOG
[i
]);
529 log_testinfo("\n[All tests passed successfully...]\n");
532 if(DATA_ERROR_COUNT
) {
533 if(WARN_ON_MISSING_DATA
==0) {
534 log_testinfo("\t*Note* some errors are data-loading related. If the data used is not the \n"
535 "\tstock ICU data (i.e some have been added or removed), consider using\n"
536 "\tthe '-w' option to turn these errors into warnings.\n");
538 log_testinfo("\t*WARNING* some data-loading errors were ignored by the -w option.\n");
543 const char* T_CTEST_EXPORT2
546 if(currentTest
!= NULL
) {
547 return currentTest
->name
;
553 const TestNode
* T_CTEST_EXPORT2
554 getTest(const TestNode
* root
, const char* name
)
556 const char* nextName
;
558 const TestNode
* curNode
;
559 int nameLen
; /* length of current 'name' */
562 log_err("TEST CAN'T BE FOUND!\n");
565 /* remove leading slash */
566 if ( *name
== TEST_SEPARATOR
)
573 /* Start with the next child */
574 nextNode
= curNode
->child
;
576 getNextLevel ( name
, &nameLen
, &nextName
);
578 /* printf("* %s\n", name );*/
580 /* if nextNode is already null, then curNode has no children
582 if( nextNode
== NULL
)
587 /* Search across for the name */
588 while (strncmp_nullcheck ( name
, nextNode
->name
, nameLen
) != 0 )
591 nextNode
= nextNode
-> sibling
;
593 if ( nextNode
== NULL
)
595 /* Did not find 'name' on this level. */
600 /* nextNode matches 'name' */
602 if (nextName
== NULL
) /* end of the line */
607 /* Loop again with the next item */
613 /* =========== io functions ======== */
615 static void go_offline_with_marker(const char *mrk
) {
616 UBool wasON_LINE
= ON_LINE
;
619 log_testinfo(" {\n");
623 if(!HANGING_OUTPUT
|| wasON_LINE
) {
630 static void go_offline() {
631 go_offline_with_marker(NULL
);
634 static void go_offline_err() {
638 static void first_line_verbose() {
639 go_offline_with_marker("v");
642 static void first_line_err() {
643 go_offline_with_marker("!");
646 static void first_line_info() {
647 go_offline_with_marker("\"");
650 static void first_line_test() {
655 static void vlog_err(const char *prefix
, const char *pattern
, va_list ap
)
657 if( ERR_MSG
== FALSE
){
660 fputs("!", stdout
); /* col 1 - bang */
661 fprintf(stdout
, "%-*s", INDENT_LEVEL
,"" );
663 fputs(prefix
, stdout
);
665 vfprintf(stdout
, pattern
, ap
);
668 if((*pattern
==0) || (pattern
[strlen(pattern
)-1]!='\n')) {
673 GLOBAL_PRINT_COUNT
++;
677 vlog_info(const char *prefix
, const char *pattern
, va_list ap
)
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 * Log test structure, with indent
697 static void log_testinfo_i(const char *pattern
, ...)
701 fprintf(stdout
, "%-*s", INDENT_LEVEL
,"" );
702 va_start(ap
, pattern
);
703 vfprintf(stdout
, pattern
, ap
);
706 GLOBAL_PRINT_COUNT
++;
709 * Log test structure (no ident)
711 static void log_testinfo(const char *pattern
, ...)
714 va_start(ap
, pattern
);
716 vfprintf(stdout
, pattern
, ap
);
719 GLOBAL_PRINT_COUNT
++;
723 static void vlog_verbose(const char *prefix
, const char *pattern
, va_list ap
)
725 if ( VERBOSITY
== FALSE
)
728 first_line_verbose();
729 fprintf(stdout
, "%-*s", INDENT_LEVEL
,"" );
731 fputs(prefix
, stdout
);
733 vfprintf(stdout
, pattern
, ap
);
736 GLOBAL_PRINT_COUNT
++;
737 if((*pattern
==0) || (pattern
[strlen(pattern
)-1]!='\n')) {
745 log_err(const char* pattern
, ...)
749 if(strchr(pattern
, '\n') != NULL
) {
751 * Count errors only if there is a line feed in the pattern
752 * so that we do not exaggerate our error count.
756 /* Count at least one error. */
759 va_start(ap
, pattern
);
760 vlog_err(NULL
, pattern
, ap
);
764 log_err_status(UErrorCode status
, const char* pattern
, ...)
767 va_start(ap
, pattern
);
770 if ((status
== U_FILE_ACCESS_ERROR
|| status
== U_MISSING_RESOURCE_ERROR
)) {
771 ++DATA_ERROR_COUNT
; /* for informational message at the end */
773 if (WARN_ON_MISSING_DATA
== 0) {
775 if (strchr(pattern
, '\n') != NULL
) {
780 vlog_err(NULL
, pattern
, ap
); /* no need for prefix in default case */
782 vlog_info("[DATA] ", pattern
, ap
);
786 if(strchr(pattern
, '\n') != NULL
) {
791 vlog_err(NULL
, pattern
, ap
); /* no need for prefix in default case */
796 log_info(const char* pattern
, ...)
800 va_start(ap
, pattern
);
801 vlog_info(NULL
, pattern
, ap
);
805 log_verbose(const char* pattern
, ...)
809 va_start(ap
, pattern
);
810 vlog_verbose(NULL
, pattern
, ap
);
815 log_data_err(const char* pattern
, ...)
818 va_start(ap
, pattern
);
821 ++DATA_ERROR_COUNT
; /* for informational message at the end */
823 if(WARN_ON_MISSING_DATA
== 0) {
825 if(strchr(pattern
, '\n') != NULL
) {
828 vlog_err(NULL
, pattern
, ap
); /* no need for prefix in default case */
830 vlog_info("[DATA] ", pattern
, ap
);
838 static int traceFnNestingDepth
= 0;
840 static void U_CALLCONV
TraceEntry(const void *context
, int32_t fnNumber
) {
842 utrace_format(buf
, sizeof(buf
), traceFnNestingDepth
*3, "%s() enter.\n", utrace_functionName(fnNumber
)); buf
[sizeof(buf
)-1]=0;
844 traceFnNestingDepth
++;
847 static void U_CALLCONV
TraceExit(const void *context
, int32_t fnNumber
, const char *fmt
, va_list args
) { char buf
[500];
849 if (traceFnNestingDepth
>0) {
850 traceFnNestingDepth
--;
852 utrace_format(buf
, sizeof(buf
), traceFnNestingDepth
*3, "%s() ", utrace_functionName(fnNumber
)); buf
[sizeof(buf
)-1]=0;
854 utrace_vformat(buf
, sizeof(buf
), traceFnNestingDepth
*3, fmt
, args
);
855 buf
[sizeof(buf
)-1]=0;
860 static void U_CALLCONV
TraceData(const void *context
, int32_t fnNumber
,
861 int32_t level
, const char *fmt
, va_list args
) {
863 utrace_vformat(buf
, sizeof(buf
), traceFnNestingDepth
*3, fmt
, args
);
864 buf
[sizeof(buf
)-1]=0;
869 static void *U_CALLCONV
ctest_libMalloc(const void *context
, size_t size
) {
871 printf("Allocated %ld\n", (long)size);
873 if (MINIMUM_MEMORY_SIZE_FAILURE
<= size
&& size
<= MAXIMUM_MEMORY_SIZE_FAILURE
) {
876 umtx_atomic_inc(&ALLOCATION_COUNT
);
879 static void *U_CALLCONV
ctest_libRealloc(const void *context
, void *mem
, size_t size
) {
881 printf("Reallocated %ld\n", (long)size);
883 if (MINIMUM_MEMORY_SIZE_FAILURE
<= size
&& size
<= MAXIMUM_MEMORY_SIZE_FAILURE
) {
884 /*free(mem);*/ /* Realloc doesn't free on failure. */
888 /* New allocation. */
889 umtx_atomic_inc(&ALLOCATION_COUNT
);
891 return realloc(mem
, size
);
893 static void U_CALLCONV
ctest_libFree(const void *context
, void *mem
) {
895 umtx_atomic_dec(&ALLOCATION_COUNT
);
901 initArgs( int argc
, const char* const argv
[], ArgHandlerPtr argHandler
, void *context
)
912 for( i
=1; i
<argc
; i
++)
914 if ( argv
[i
][0] == '/' )
916 /* We don't run the tests here. */
919 else if ((strcmp( argv
[i
], "-a") == 0) || (strcmp(argv
[i
],"-all") == 0))
921 /* We don't run the tests here. */
924 else if (strcmp( argv
[i
], "-v" )==0 || strcmp( argv
[i
], "-verbose")==0)
928 else if (strcmp( argv
[i
], "-l" )==0 )
932 else if (strcmp( argv
[i
], "-e1") == 0)
936 else if (strcmp( argv
[i
], "-e") ==0)
940 else if (strcmp( argv
[i
], "-w") ==0)
942 WARN_ON_MISSING_DATA
= TRUE
;
944 else if (strcmp( argv
[i
], "-m") ==0)
946 UErrorCode errorCode
= U_ZERO_ERROR
;
950 MINIMUM_MEMORY_SIZE_FAILURE
= (size_t)strtol(argv
[i
], &endPtr
, 10);
951 if (endPtr
== argv
[i
]) {
952 printf("Can't parse %s\n", argv
[i
]);
956 if (*endPtr
== '-') {
957 char *maxPtr
= endPtr
+1;
959 MAXIMUM_MEMORY_SIZE_FAILURE
= (size_t)strtol(maxPtr
, &endPtr
, 10);
960 if (endPtr
== argv
[i
]) {
961 printf("Can't parse %s\n", argv
[i
]);
967 /* Use the default value */
968 u_setMemoryFunctions(NULL
, ctest_libMalloc
, ctest_libRealloc
, ctest_libFree
, &errorCode
);
969 if (U_FAILURE(errorCode
)) {
970 printf("u_setMemoryFunctions returned %s\n", u_errorName(errorCode
));
974 else if(strcmp( argv
[i
], "-n") == 0 || strcmp( argv
[i
], "-no_err_msg") == 0)
978 else if (strcmp( argv
[i
], "-r") == 0)
980 if (!REPEAT_TESTS_INIT
) {
984 else if (strcmp( argv
[i
], "-x") == 0)
987 printf("* Error: '-x' option requires an argument. usage: '-x outfile.xml'.\n");
990 if(ctest_xml_setFileName(argv
[i
])) { /* set the name */
994 else if (strcmp( argv
[i
], "-t_info") == 0) {
995 ICU_TRACE
= UTRACE_INFO
;
997 else if (strcmp( argv
[i
], "-t_error") == 0) {
998 ICU_TRACE
= UTRACE_ERROR
;
1000 else if (strcmp( argv
[i
], "-t_warn") == 0) {
1001 ICU_TRACE
= UTRACE_WARNING
;
1003 else if (strcmp( argv
[i
], "-t_verbose") == 0) {
1004 ICU_TRACE
= UTRACE_VERBOSE
;
1006 else if (strcmp( argv
[i
], "-t_oc") == 0) {
1007 ICU_TRACE
= UTRACE_OPEN_CLOSE
;
1009 else if (strcmp( argv
[i
], "-h" )==0 || strcmp( argv
[i
], "--help" )==0)
1014 else if (argHandler
!= NULL
&& (argSkip
= argHandler(i
, argc
, argv
, context
)) > 0)
1020 printf("* unknown option: %s\n", argv
[i
]);
1025 if (ICU_TRACE
!= UTRACE_OFF
) {
1026 utrace_setFunctions(NULL
, TraceEntry
, TraceExit
, TraceData
);
1027 utrace_setLevel(ICU_TRACE
);
1030 return 1; /* total error count */
1034 runTestRequest(const TestNode
* root
,
1036 const char* const argv
[])
1039 * This main will parse the l, v, h, n, and path arguments
1041 const TestNode
* toRun
;
1044 int subtreeOptionSeen
= FALSE
;
1050 if(ctest_xml_init(ARGV_0
)) {
1051 return 1; /* couldn't fire up XML thing */
1054 for( i
=1; i
<argc
; i
++)
1056 if ( argv
[i
][0] == '/' )
1058 printf("Selecting subtree '%s'\n", argv
[i
]);
1060 if ( argv
[i
][1] == 0 )
1063 toRun
= getTest(root
, argv
[i
]);
1065 if ( toRun
== NULL
)
1067 printf("* Could not find any matching subtree\n");
1071 ON_LINE
=FALSE
; /* just in case */
1078 ON_LINE
=FALSE
; /* just in case */
1080 errorCount
+= ERROR_COUNT
;
1082 subtreeOptionSeen
= TRUE
;
1083 } else if ((strcmp( argv
[i
], "-a") == 0) || (strcmp(argv
[i
],"-all") == 0)) {
1084 subtreeOptionSeen
=FALSE
;
1085 } else if (strcmp( argv
[i
], "-l") == 0) {
1088 /* else option already handled by initArgs */
1091 if( subtreeOptionSeen
== FALSE
) /* no other subtree given, run the default */
1093 ON_LINE
=FALSE
; /* just in case */
1098 ON_LINE
=FALSE
; /* just in case */
1100 errorCount
+= ERROR_COUNT
;
1104 if( ( doList
== FALSE
) && ( errorCount
> 0 ) )
1105 printf(" Total errors: %d\n", errorCount
);
1108 REPEAT_TESTS_INIT
= 1;
1110 if(ctest_xml_fini()) {
1114 return errorCount
; /* total error count */
1118 * Display program invocation arguments
1121 static void help ( const char *argv0
)
1123 printf("Usage: %s [ -l ] [ -v ] [ -verbose] [-a] [ -all] [-n] [ -no_err_msg]\n"
1124 " [ -h ] [-t_info | -t_error | -t_warn | -t_oc | -t_verbose] [-m n[-q] ]\n"
1125 " [ /path/to/test ]\n",
1127 printf(" -l To get a list of test names\n");
1128 printf(" -e to do exhaustive testing\n");
1129 printf(" -verbose To turn ON verbosity\n");
1130 printf(" -v To turn ON verbosity(same as -verbose)\n");
1131 printf(" -x file.xml Write junit format output to file.xml\n");
1132 printf(" -h To print this message\n");
1133 printf(" -n To turn OFF printing error messages\n");
1134 printf(" -w Don't fail on data-loading errs, just warn. Useful if\n"
1135 " user has reduced/changed the common set of ICU data \n");
1136 printf(" -t_info | -t_error | -t_warn | -t_oc | -t_verbose Enable ICU tracing\n");
1137 printf(" -no_err_msg (same as -n) \n");
1138 printf(" -m n[-q] Min-Max memory size that will cause an allocation failure.\n");
1139 printf(" The default is the maximum value of size_t. Max is optional.\n");
1140 printf(" -r Repeat tests after calling u_cleanup \n");
1141 printf(" [/subtest] To run a subtest \n");
1142 printf(" eg: to run just the utility tests type: cintltest /tsutil) \n");
1145 int32_t T_CTEST_EXPORT2
1146 getTestOption ( int32_t testOption
) {
1147 switch (testOption
) {
1148 case VERBOSITY_OPTION
:
1150 case WARN_ON_MISSING_DATA_OPTION
:
1151 return WARN_ON_MISSING_DATA
;
1154 case REPEAT_TESTS_OPTION
:
1155 return REPEAT_TESTS
;
1156 case ERR_MSG_OPTION
:
1158 case ICU_TRACE_OPTION
:
1165 void T_CTEST_EXPORT2
1166 setTestOption ( int32_t testOption
, int32_t value
) {
1167 if (value
== DECREMENT_OPTION_VALUE
) {
1168 value
= getTestOption(testOption
);
1171 switch (testOption
) {
1172 case VERBOSITY_OPTION
:
1175 case WARN_ON_MISSING_DATA_OPTION
:
1176 WARN_ON_MISSING_DATA
= value
;
1181 case REPEAT_TESTS_OPTION
:
1182 REPEAT_TESTS
= value
;
1184 case ICU_TRACE_OPTION
:
1194 * ================== JUnit support ================================
1199 ctest_xml_setFileName(const char *name
) {
1207 ctest_xml_init(const char *rootName
) {
1208 if(!XML_FILE_NAME
) return 0;
1209 XML_FILE
= fopen(XML_FILE_NAME
,"w");
1212 fprintf(stderr
," Error: couldn't open XML output file %s\n", XML_FILE_NAME
);
1215 while(*rootName
&&!isalnum(*rootName
)) {
1218 strcpy(XML_PREFIX
,rootName
);
1220 char *p
= XML_PREFIX
+strlen(XML_PREFIX
);
1221 for(p
--;*p
&&p
>XML_PREFIX
&&!isalnum(*p
);p
--) {
1226 fprintf(XML_FILE
, "<testsuite name=\"%s\">\n", XML_PREFIX
);
1233 ctest_xml_fini(void) {
1234 if(!XML_FILE
) return 0;
1236 fprintf(XML_FILE
, "</testsuite>\n");
1238 printf(" ( test results written to %s )\n", XML_FILE_NAME
);
1246 ctest_xml_testcase(const char *classname
, const char *name
, const char *time
, const char *failMsg
) {
1247 if(!XML_FILE
) return 0;
1249 fprintf(XML_FILE
, "\t<testcase classname=\"%s:%s\" name=\"%s:%s\" time=\"%s\"", XML_PREFIX
, classname
, XML_PREFIX
, name
, time
);
1251 fprintf(XML_FILE
, ">\n\t\t<failure type=\"err\" message=\"%s\"/>\n\t</testcase>\n", failMsg
);
1253 fprintf(XML_FILE
, "/>\n");