]>
git.saurik.com Git - apple/icu.git/blob - icuSources/tools/ctestfw/ctest.c
2 ********************************************************************************
4 * Copyright (C) 1996-2006, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ********************************************************************************
15 #include "unicode/utrace.h"
18 3/20/1999 srl - strncpy called w/o setting nulls at the end
21 #define MAXTESTNAME 128
23 #define MAX_TEST_LOG 4096
27 char name
[MAXTESTNAME
];
29 struct TestNode
* sibling
;
30 struct TestNode
* child
;
34 static const struct TestNode
* currentTest
;
36 typedef enum { RUNTESTS
, SHOWTESTS
} TestMode
;
37 #define TEST_SEPARATOR '/'
43 #include "unicode/ctest.h"
45 static char ERROR_LOG
[MAX_TEST_LOG
][MAXTESTNAME
];
47 /* Local prototypes */
48 static TestNode
* addTestNode( TestNode
*root
, const char *name
);
50 static TestNode
* createTestNode();
52 static int strncmp_nullcheck( const char* s1
,
56 static void getNextLevel( const char* name
,
58 const char** nextName
);
60 static void iterateTestsWithLevel( const TestNode
*root
, int len
,
61 const TestNode
** list
,
64 static void help ( const char *argv0
);
67 * Do the work of logging an error. Doesn't increase the error count.
69 * @prefix optional prefix prepended to message, or NULL.
70 * @param pattern printf style pattern
71 * @param ap vprintf style arg list
73 static void vlog_err(const char *prefix
, const char *pattern
, va_list ap
);
74 static void vlog_verbose(const char *prefix
, const char *pattern
, va_list ap
);
76 /* If we need to make the framework multi-thread safe
77 we need to pass around the following vars
79 static int ERRONEOUS_FUNCTION_COUNT
= 0;
80 static int ERROR_COUNT
= 0; /* Count of errors from all tests. */
81 static int DATA_ERROR_COUNT
= 0; /* count of data related errors or warnings */
82 static int INDENT_LEVEL
= 0;
83 int REPEAT_TESTS_INIT
= 0; /* Was REPEAT_TESTS initialized? */
84 int REPEAT_TESTS
= 1; /* Number of times to run the test */
85 int VERBOSITY
= 0; /* be No-verbose by default */
86 int ERR_MSG
=1; /* error messages will be displayed by default*/
87 int QUICK
= 1; /* Skip some of the slower tests? */
88 int WARN_ON_MISSING_DATA
= 0; /* Reduce data errs to warnings? */
89 UTraceLevel ICU_TRACE
= UTRACE_OFF
; /* ICU tracing level */
90 /*-------------------------------------------*/
92 /* strncmp that also makes sure there's a \0 at s2[0] */
93 static int strncmp_nullcheck( const char* s1
,
97 if (((int)strlen(s2
) >= n
) && s2
[n
] != 0) {
98 return 3; /* null check fails */
101 return strncmp ( s1
, s2
, n
);
105 static void getNextLevel( const char* name
,
107 const char** nextName
)
109 /* Get the next component of the name */
110 *nextName
= strchr(name
, TEST_SEPARATOR
);
115 *nameLen
= (int)((*nextName
) - name
);
116 (*nextName
)++; /* skip '/' */
117 strncpy(n
, name
, *nameLen
);
119 /*printf("->%s-< [%d] -> [%s]\n", name, *nameLen, *nextName);*/
122 *nameLen
= (int)strlen(name
);
126 static TestNode
*createTestNode( )
130 newNode
= (TestNode
*)malloc ( sizeof ( TestNode
) );
132 newNode
->name
[0] = '\0';
133 newNode
->test
= NULL
;
134 newNode
->sibling
= NULL
;
135 newNode
->child
= NULL
;
141 cleanUpTestTree(TestNode
*tn
)
143 if(tn
->child
!= NULL
) {
144 cleanUpTestTree(tn
->child
);
146 if(tn
->sibling
!= NULL
) {
147 cleanUpTestTree(tn
->sibling
);
155 addTest(TestNode
** root
,
156 TestFunctionPtr test
,
161 /*if this is the first Test created*/
163 *root
= createTestNode();
165 newNode
= addTestNode( *root
, name
);
166 assert(newNode
!= 0 );
167 /* printf("addTest: nreName = %s\n", newNode->name );*/
169 newNode
->test
= test
;
172 /* non recursive insert function */
173 static TestNode
*addTestNode ( TestNode
*root
, const char *name
)
175 const char* nextName
;
176 TestNode
*nextNode
, *curNode
;
177 int nameLen
; /* length of current 'name' */
179 /* remove leading slash */
180 if ( *name
== TEST_SEPARATOR
)
187 /* Start with the next child */
188 nextNode
= curNode
->child
;
190 getNextLevel ( name
, &nameLen
, &nextName
);
192 /* printf("* %s\n", name );*/
194 /* if nextNode is already null, then curNode has no children
196 if( nextNode
== NULL
)
198 /* Add all children of the node */
201 curNode
->child
= createTestNode ( );
203 /* Get the next component of the name */
204 getNextLevel ( name
, &nameLen
, &nextName
);
206 /* update curName to have the next name segment */
207 strncpy ( curNode
->child
->name
, name
, nameLen
);
208 curNode
->child
->name
[nameLen
] = 0;
209 /* printf("*** added %s\n", curNode->child->name );*/
210 curNode
= curNode
->child
;
213 while( name
!= NULL
);
218 /* Search across for the name */
219 while (strncmp_nullcheck ( name
, nextNode
->name
, nameLen
) != 0 )
222 nextNode
= nextNode
-> sibling
;
224 if ( nextNode
== NULL
)
226 /* Did not find 'name' on this level. */
227 nextNode
= createTestNode ( );
228 strncpy( nextNode
->name
, name
, nameLen
);
229 nextNode
->name
[nameLen
] = 0;
230 curNode
->sibling
= nextNode
;
235 /* nextNode matches 'name' */
237 if (nextName
== NULL
) /* end of the line */
242 /* Loop again with the next item */
248 static void iterateTestsWithLevel ( const TestNode
* root
,
250 const TestNode
** list
,
256 char pathToFunction
[MAXTESTNAME
] = "";
257 char separatorString
[2] = { TEST_SEPARATOR
, '\0'};
264 for ( i
=0;i
<(len
-1);i
++ )
266 strcat(pathToFunction
, list
[i
]->name
);
267 strcat(pathToFunction
, separatorString
);
270 strcat(pathToFunction
, list
[i
]->name
);
273 if ( (mode
== RUNTESTS
) && (root
->test
!= NULL
))
275 int myERROR_COUNT
= ERROR_COUNT
;
279 if (myERROR_COUNT
!= ERROR_COUNT
)
282 log_info("---[%d ERRORS] ", ERROR_COUNT
- myERROR_COUNT
);
283 strcpy(ERROR_LOG
[ERRONEOUS_FUNCTION_COUNT
++], pathToFunction
);
286 log_info("---[OK] ");
290 /* we want these messages to be at 0 indent. so just push the indent level breifly. */
291 saveIndent
= INDENT_LEVEL
;
293 log_info("%s%s%c\n", (list
[i
]->test
||mode
==SHOWTESTS
)?"---":"",pathToFunction
, list
[i
]->test
?' ':TEST_SEPARATOR
);
294 INDENT_LEVEL
= saveIndent
;
296 iterateTestsWithLevel ( root
->child
, len
, list
, mode
);
300 if ( len
!= 0 ) /* DO NOT iterate over siblings of the root. */
301 iterateTestsWithLevel ( root
->sibling
, len
, list
, mode
);
307 showTests ( const TestNode
*root
)
309 /* make up one for them */
310 const TestNode
*aList
[MAXTESTS
];
313 log_err("TEST CAN'T BE FOUND!");
315 iterateTestsWithLevel ( root
, 0, aList
, SHOWTESTS
);
320 runTests ( const TestNode
*root
)
323 const TestNode
*aList
[MAXTESTS
];
324 /* make up one for them */
328 log_err("TEST CAN'T BE FOUND!\n");
330 ERRONEOUS_FUNCTION_COUNT
= ERROR_COUNT
= 0;
331 iterateTestsWithLevel ( root
, 0, aList
, RUNTESTS
);
333 /*print out result summary*/
337 log_info("\nSUMMARY:\n******* [Total error count:\t%d]\n Errors in\n", ERROR_COUNT
);
338 for (i
=0;i
< ERRONEOUS_FUNCTION_COUNT
; i
++)
339 log_info("[%s]\n",ERROR_LOG
[i
]);
343 log_info("\n[All tests passed successfully...]\n");
346 if(DATA_ERROR_COUNT
) {
347 if(WARN_ON_MISSING_DATA
==0) {
348 log_info("\t*Note* some errors are data-loading related. If the data used is not the \n"
349 "\tstock ICU data (i.e some have been added or removed), consider using\n"
350 "\tthe '-w' option to turn these errors into warnings.\n");
352 log_info("\t*WARNING* some data-loading errors were ignored by the -w option.\n");
357 const char* T_CTEST_EXPORT2
360 if(currentTest
!= NULL
) {
361 return currentTest
->name
;
367 const TestNode
* T_CTEST_EXPORT2
368 getTest(const TestNode
* root
, const char* name
)
370 const char* nextName
;
372 const TestNode
* curNode
;
373 int nameLen
; /* length of current 'name' */
376 log_err("TEST CAN'T BE FOUND!\n");
379 /* remove leading slash */
380 if ( *name
== TEST_SEPARATOR
)
387 /* Start with the next child */
388 nextNode
= curNode
->child
;
390 getNextLevel ( name
, &nameLen
, &nextName
);
392 /* printf("* %s\n", name );*/
394 /* if nextNode is already null, then curNode has no children
396 if( nextNode
== NULL
)
401 /* Search across for the name */
402 while (strncmp_nullcheck ( name
, nextNode
->name
, nameLen
) != 0 )
405 nextNode
= nextNode
-> sibling
;
407 if ( nextNode
== NULL
)
409 /* Did not find 'name' on this level. */
414 /* nextNode matches 'name' */
416 if (nextName
== NULL
) /* end of the line */
421 /* Loop again with the next item */
427 static void vlog_err(const char *prefix
, const char *pattern
, va_list ap
)
429 if( ERR_MSG
== FALSE
){
432 fprintf(stderr
, "%-*s", INDENT_LEVEL
," " );
434 fputs(prefix
, stderr
);
436 vfprintf(stderr
, pattern
, ap
);
442 vlog_info(const char *prefix
, const char *pattern
, va_list ap
)
444 fprintf(stdout
, "%-*s", INDENT_LEVEL
," " );
446 fputs(prefix
, stdout
);
448 vfprintf(stdout
, pattern
, ap
);
453 static void vlog_verbose(const char *prefix
, const char *pattern
, va_list ap
)
455 if ( VERBOSITY
== FALSE
)
458 fprintf(stdout
, "%-*s", INDENT_LEVEL
," " );
460 fputs(prefix
, stdout
);
462 vfprintf(stdout
, pattern
, ap
);
468 log_err(const char* pattern
, ...)
471 if(strchr(pattern
, '\n') != NULL
) {
473 * Count errors only if there is a line feed in the pattern
474 * so that we do not exaggerate our error count.
478 va_start(ap
, pattern
);
479 vlog_err(NULL
, pattern
, ap
);
483 log_info(const char* pattern
, ...)
487 va_start(ap
, pattern
);
488 vlog_info(NULL
, pattern
, ap
);
492 log_verbose(const char* pattern
, ...)
496 va_start(ap
, pattern
);
497 vlog_verbose(NULL
, pattern
, ap
);
502 log_data_err(const char* pattern
, ...)
505 va_start(ap
, pattern
);
507 ++DATA_ERROR_COUNT
; /* for informational message at the end */
509 if(WARN_ON_MISSING_DATA
== 0) {
511 if(strchr(pattern
, '\n') != NULL
) {
514 vlog_err(NULL
, pattern
, ap
); /* no need for prefix in default case */
516 vlog_info("[Data] ", pattern
, ap
);
522 processArgs(const TestNode
* root
,
524 const char* const argv
[])
527 * This main will parse the l, v, h, n, and path arguments
529 const TestNode
* toRun
;
532 int subtreeOptionSeen
= FALSE
;
540 for( i
=1; i
<argc
; i
++)
542 if ( argv
[i
][0] == '/' )
544 printf("Selecting subtree '%s'\n", argv
[i
]);
546 if ( argv
[i
][1] == 0 )
549 toRun
= getTest(root
, argv
[i
]);
553 printf("* Could not find any matching subtree\n");
562 errorCount
+= ERROR_COUNT
;
564 subtreeOptionSeen
= TRUE
;
566 else if (strcmp( argv
[i
], "-v" )==0 || strcmp( argv
[i
], "-verbose")==0)
570 else if (strcmp( argv
[i
], "-l" )==0 )
574 else if (strcmp( argv
[i
], "-e1") == 0)
578 else if (strcmp( argv
[i
], "-e") ==0)
582 else if (strcmp( argv
[i
], "-w") ==0)
584 WARN_ON_MISSING_DATA
= TRUE
;
586 else if(strcmp( argv
[i
], "-n") == 0 || strcmp( argv
[i
], "-no_err_msg") == 0)
590 else if (strcmp( argv
[i
], "-r") == 0)
592 if (!REPEAT_TESTS_INIT
) {
596 else if ((strcmp( argv
[i
], "-a") == 0) || (strcmp(argv
[i
],"-all") == 0))
598 subtreeOptionSeen
=FALSE
;
600 else if (strcmp( argv
[i
], "-t_info") == 0) {
601 ICU_TRACE
= UTRACE_INFO
;
603 else if (strcmp( argv
[i
], "-t_error") == 0) {
604 ICU_TRACE
= UTRACE_ERROR
;
606 else if (strcmp( argv
[i
], "-t_warn") == 0) {
607 ICU_TRACE
= UTRACE_WARNING
;
609 else if (strcmp( argv
[i
], "-t_verbose") == 0) {
610 ICU_TRACE
= UTRACE_VERBOSE
;
612 else if (strcmp( argv
[i
], "-t_oc") == 0) {
613 ICU_TRACE
= UTRACE_OPEN_CLOSE
;
615 else if (strcmp( argv
[i
], "-h" )==0 || strcmp( argv
[i
], "--help" )==0)
622 printf("* unknown option: %s\n", argv
[i
]);
628 if( subtreeOptionSeen
== FALSE
) /* no other subtree given, run the default */
635 errorCount
+= ERROR_COUNT
;
639 if( ( doList
== FALSE
) && ( errorCount
> 0 ) )
640 printf(" Total errors: %d\n", errorCount
);
643 REPEAT_TESTS_INIT
= 1;
645 return errorCount
; /* total error count */
649 * Display program invocation arguments
652 static void help ( const char *argv0
)
654 printf("Usage: %s [ -l ] [ -v ] [ -verbose] [-a] [ -all] [-n] [ -no_err_msg]\n"
655 " [ -h ] [-t_info | -t_error | -t_warn | -t_oc | -t_verbose]"
656 " [ /path/to/test ]\n",
658 printf(" -l To get a list of test names\n");
659 printf(" -e to do exhaustive testing\n");
660 printf(" -verbose To turn ON verbosity\n");
661 printf(" -v To turn ON verbosity(same as -verbose)\n");
662 printf(" -h To print this message\n");
663 printf(" -n To turn OFF printing error messages\n");
664 printf(" -w Don't fail on data-loading errs, just warn. Useful if\n"
665 " user has reduced/changed the common set of ICU data \n");
666 printf(" -t_info | -t_error | -t_warn | -t_oc | -t_verbose Enable ICU tracing\n");
667 printf(" -no_err_msg (same as -n) \n");
668 printf(" -r repeat tests after calling u_cleanup \n");
669 printf(" -[/subtest] To run a subtest \n");
670 printf(" eg: to run just the utility tests type: cintltest /tsutil) \n");