]>
git.saurik.com Git - apple/icu.git/blob - icuSources/tools/ctestfw/ctest.c
2 *****************************************************************************************
4 * Copyright (C) 1996-2004, 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");
377 /* remove leading slash */
378 if ( *name
== TEST_SEPARATOR
)
385 /* Start with the next child */
386 nextNode
= curNode
->child
;
388 getNextLevel ( name
, &nameLen
, &nextName
);
390 /* printf("* %s\n", name );*/
392 /* if nextNode is already null, then curNode has no children
394 if( nextNode
== NULL
)
399 /* Search across for the name */
400 while (strncmp_nullcheck ( name
, nextNode
->name
, nameLen
) != 0 )
403 nextNode
= nextNode
-> sibling
;
405 if ( nextNode
== NULL
)
407 /* Did not find 'name' on this level. */
412 /* nextNode matches 'name' */
414 if (nextName
== NULL
) /* end of the line */
419 /* Loop again with the next item */
425 static void vlog_err(const char *prefix
, const char *pattern
, va_list ap
)
427 if( ERR_MSG
== FALSE
){
430 fprintf(stderr
, "%-*s", INDENT_LEVEL
," " );
432 fputs(prefix
, stderr
);
434 vfprintf(stderr
, pattern
, ap
);
440 vlog_info(const char *prefix
, const char *pattern
, va_list ap
)
442 fprintf(stdout
, "%-*s", INDENT_LEVEL
," " );
444 fputs(prefix
, stderr
);
446 vfprintf(stdout
, pattern
, ap
);
451 static void vlog_verbose(const char *prefix
, const char *pattern
, va_list ap
)
453 if ( VERBOSITY
== FALSE
)
456 fprintf(stdout
, "%-*s", INDENT_LEVEL
," " );
458 fputs(prefix
, stderr
);
460 vfprintf(stdout
, pattern
, ap
);
466 log_err(const char* pattern
, ...)
469 if(strchr(pattern
, '\n') != NULL
) {
471 * Count errors only if there is a line feed in the pattern
472 * so that we do not exaggerate our error count.
476 va_start(ap
, pattern
);
477 vlog_err(NULL
, pattern
, ap
);
481 log_info(const char* pattern
, ...)
485 va_start(ap
, pattern
);
486 vlog_info(NULL
, pattern
, ap
);
490 log_verbose(const char* pattern
, ...)
494 va_start(ap
, pattern
);
495 vlog_verbose(NULL
, pattern
, ap
);
500 log_data_err(const char* pattern
, ...)
503 va_start(ap
, pattern
);
505 ++DATA_ERROR_COUNT
; /* for informational message at the end */
507 if(WARN_ON_MISSING_DATA
== 0) {
509 if(strchr(pattern
, '\n') != NULL
) {
512 vlog_err(NULL
, pattern
, ap
); /* no need for prefix in default case */
514 vlog_info("[Data] ", pattern
, ap
);
520 processArgs(const TestNode
* root
,
522 const char* const argv
[])
525 * This main will parse the l, v, h, n, and path arguments
527 const TestNode
* toRun
;
530 int subtreeOptionSeen
= FALSE
;
538 for( i
=1; i
<argc
; i
++)
540 if ( argv
[i
][0] == '/' )
542 printf("Selecting subtree '%s'\n", argv
[i
]);
544 if ( argv
[i
][1] == 0 )
547 toRun
= getTest(root
, argv
[i
]);
551 printf("* Could not find any matching subtree\n");
560 errorCount
+= ERROR_COUNT
;
562 subtreeOptionSeen
= TRUE
;
564 else if (strcmp( argv
[i
], "-v" )==0 || strcmp( argv
[i
], "-verbose")==0)
568 else if (strcmp( argv
[i
], "-l" )==0 )
572 else if (strcmp( argv
[i
], "-e1") == 0)
576 else if (strcmp( argv
[i
], "-e") ==0)
580 else if (strcmp( argv
[i
], "-w") ==0)
582 WARN_ON_MISSING_DATA
= TRUE
;
584 else if(strcmp( argv
[i
], "-n") == 0 || strcmp( argv
[i
], "-no_err_msg") == 0)
588 else if (strcmp( argv
[i
], "-r") == 0)
590 if (!REPEAT_TESTS_INIT
) {
594 else if ((strcmp( argv
[i
], "-a") == 0) || (strcmp(argv
[i
],"-all") == 0))
596 subtreeOptionSeen
=FALSE
;
598 else if (strcmp( argv
[i
], "-t_info") == 0) {
599 ICU_TRACE
= UTRACE_INFO
;
601 else if (strcmp( argv
[i
], "-t_error") == 0) {
602 ICU_TRACE
= UTRACE_ERROR
;
604 else if (strcmp( argv
[i
], "-t_warn") == 0) {
605 ICU_TRACE
= UTRACE_WARNING
;
607 else if (strcmp( argv
[i
], "-t_verbose") == 0) {
608 ICU_TRACE
= UTRACE_VERBOSE
;
610 else if (strcmp( argv
[i
], "-t_oc") == 0) {
611 ICU_TRACE
= UTRACE_OPEN_CLOSE
;
613 else if (strcmp( argv
[i
], "-h" )==0 || strcmp( argv
[i
], "--help" )==0)
620 printf("* unknown option: %s\n", argv
[i
]);
626 if( subtreeOptionSeen
== FALSE
) /* no other subtree given, run the default */
633 errorCount
+= ERROR_COUNT
;
637 if( ( doList
== FALSE
) && ( errorCount
> 0 ) )
638 printf(" Total errors: %d\n", errorCount
);
641 REPEAT_TESTS_INIT
= 1;
643 return errorCount
; /* total error count */
647 * Display program invocation arguments
650 static void help ( const char *argv0
)
652 printf("Usage: %s [ -l ] [ -v ] [ -verbose] [-a] [ -all] [-n] [ -no_err_msg]\n"
653 " [ -h ] [-t_info | -t_error | -t_warn | -t_oc | -t_verbose]"
654 " [ /path/to/test ]\n",
656 printf(" -l To get a list of test names\n");
657 printf(" -e to do exhaustive testing\n");
658 printf(" -verbose To turn ON verbosity\n");
659 printf(" -v To turn ON verbosity(same as -verbose)\n");
660 printf(" -h To print this message\n");
661 printf(" -n To turn OFF printing error messages\n");
662 printf(" -w Don't fail on data-loading errs, just warn. Useful if\n"
663 " user has reduced/changed the common set of ICU data \n");
664 printf(" -t_info | -t_error | -t_warn | -t_oc | -t_verbose Enable ICU tracing\n");
665 printf(" -no_err_msg (same as -n) \n");
666 printf(" -r repeat tests after calling u_cleanup \n");
667 printf(" -[/subtest] To run a subtest \n");
668 printf(" eg: to run just the utility tests type: cintltest /tsutil) \n");