]> git.saurik.com Git - apple/icu.git/blame - icuSources/tools/ctestfw/ctest.c
ICU-6.2.14.tar.gz
[apple/icu.git] / icuSources / tools / ctestfw / ctest.c
CommitLineData
b75a7d8f
A
1/*
2*****************************************************************************************
3*
374ca955 4* Copyright (C) 1996-2004, International Business Machines
b75a7d8f
A
5* Corporation and others. All Rights Reserved.
6*
7*****************************************************************************************
8*/
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <assert.h>
13#include <stdarg.h>
14
374ca955
A
15#include "unicode/utrace.h"
16
b75a7d8f
A
17/* NOTES:
18 3/20/1999 srl - strncpy called w/o setting nulls at the end
19 */
20
21#define MAXTESTNAME 128
22#define MAXTESTS 512
23#define MAX_TEST_LOG 4096
24
25struct TestNode
26{
27 char name[MAXTESTNAME];
28 void (*test)(void);
29 struct TestNode* sibling;
30 struct TestNode* child;
31};
32
33
34static const struct TestNode* currentTest;
35
36typedef enum { RUNTESTS, SHOWTESTS } TestMode;
37#define TEST_SEPARATOR '/'
38
39#ifndef C_TEST_IMPL
40#define C_TEST_IMPL
41#endif
42
43#include "unicode/ctest.h"
44
45static char ERROR_LOG[MAX_TEST_LOG][MAXTESTNAME];
46
47/* Local prototypes */
48static TestNode* addTestNode( TestNode *root, const char *name );
49
50static TestNode* createTestNode();
51
52static int strncmp_nullcheck( const char* s1,
53 const char* s2,
54 int n );
55
56static void getNextLevel( const char* name,
57 int* nameLen,
58 const char** nextName );
59
60static void iterateTestsWithLevel( const TestNode *root, int len,
61 const TestNode** list,
62 TestMode mode);
63
64static void help ( const char *argv0 );
65
66/**
67 * Do the work of logging an error. Doesn't increase the error count.
68 *
69 * @prefix optional prefix prepended to message, or NULL.
70 * @param pattern printf style pattern
71 * @param ap vprintf style arg list
72 */
73static void vlog_err(const char *prefix, const char *pattern, va_list ap);
b75a7d8f
A
74static void vlog_verbose(const char *prefix, const char *pattern, va_list ap);
75
76/* If we need to make the framework multi-thread safe
77 we need to pass around the following vars
78*/
79static int ERRONEOUS_FUNCTION_COUNT = 0;
80static int ERROR_COUNT = 0; /* Count of errors from all tests. */
81static int DATA_ERROR_COUNT = 0; /* count of data related errors or warnings */
82static int INDENT_LEVEL = 0;
83int REPEAT_TESTS_INIT = 0; /* Was REPEAT_TESTS initialized? */
84int REPEAT_TESTS = 1; /* Number of times to run the test */
85int VERBOSITY = 0; /* be No-verbose by default */
86int ERR_MSG =1; /* error messages will be displayed by default*/
87int QUICK = 1; /* Skip some of the slower tests? */
88int WARN_ON_MISSING_DATA = 0; /* Reduce data errs to warnings? */
374ca955 89UTraceLevel ICU_TRACE = UTRACE_OFF; /* ICU tracing level */
b75a7d8f
A
90/*-------------------------------------------*/
91
92/* strncmp that also makes sure there's a \0 at s2[0] */
93static int strncmp_nullcheck( const char* s1,
94 const char* s2,
95 int n )
96{
97 if (((int)strlen(s2) >= n) && s2[n] != 0) {
98 return 3; /* null check fails */
99 }
100 else {
101 return strncmp ( s1, s2, n );
102 }
103}
104
105static void getNextLevel( const char* name,
106 int* nameLen,
107 const char** nextName )
108{
109 /* Get the next component of the name */
110 *nextName = strchr(name, TEST_SEPARATOR);
111
112 if( *nextName != 0 )
113 {
114 char n[255];
115 *nameLen = (int)((*nextName) - name);
116 (*nextName)++; /* skip '/' */
117 strncpy(n, name, *nameLen);
118 n[*nameLen] = 0;
119 /*printf("->%s-< [%d] -> [%s]\n", name, *nameLen, *nextName);*/
120 }
121 else {
122 *nameLen = (int)strlen(name);
123 }
124}
125
126static TestNode *createTestNode( )
127{
128 TestNode *newNode;
129
130 newNode = (TestNode*)malloc ( sizeof ( TestNode ) );
131
132 newNode->name[0] = '\0';
133 newNode->test = NULL;
134 newNode->sibling = NULL;
135 newNode->child = NULL;
136
137 return newNode;
138}
139
374ca955
A
140void T_CTEST_EXPORT2
141cleanUpTestTree(TestNode *tn)
142{
b75a7d8f
A
143 if(tn->child != NULL) {
144 cleanUpTestTree(tn->child);
145 }
146 if(tn->sibling != NULL) {
147 cleanUpTestTree(tn->sibling);
148 }
149
150 free(tn);
151}
152
153
374ca955
A
154void T_CTEST_EXPORT2
155addTest(TestNode** root,
156 TestFunctionPtr test,
157 const char* name )
b75a7d8f
A
158{
159 TestNode *newNode;
160
161 /*if this is the first Test created*/
162 if (*root == NULL)
163 *root = createTestNode();
164
165 newNode = addTestNode( *root, name );
166 assert(newNode != 0 );
167 /* printf("addTest: nreName = %s\n", newNode->name );*/
168
169 newNode->test = test;
170}
171
172/* non recursive insert function */
173static TestNode *addTestNode ( TestNode *root, const char *name )
174{
175 const char* nextName;
176 TestNode *nextNode, *curNode;
177 int nameLen; /* length of current 'name' */
178
179 /* remove leading slash */
180 if ( *name == TEST_SEPARATOR )
181 name++;
182
183 curNode = root;
184
185 for(;;)
186 {
187 /* Start with the next child */
188 nextNode = curNode->child;
189
190 getNextLevel ( name, &nameLen, &nextName );
191
192 /* printf("* %s\n", name );*/
193
194 /* if nextNode is already null, then curNode has no children
195 -- add them */
196 if( nextNode == NULL )
197 {
198 /* Add all children of the node */
199 do
200 {
201 curNode->child = createTestNode ( );
202
203 /* Get the next component of the name */
204 getNextLevel ( name, &nameLen, &nextName );
205
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;
211 name = nextName;
212 }
213 while( name != NULL );
214
215 return curNode;
216 }
217
218 /* Search across for the name */
219 while (strncmp_nullcheck ( name, nextNode->name, nameLen) != 0 )
220 {
221 curNode = nextNode;
222 nextNode = nextNode -> sibling;
223
224 if ( nextNode == NULL )
225 {
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;
231 break;
232 }
233 }
234
235 /* nextNode matches 'name' */
236
237 if (nextName == NULL) /* end of the line */
238 {
239 return nextNode;
240 }
241
242 /* Loop again with the next item */
243 name = nextName;
244 curNode = nextNode;
245 }
246}
247
248static void iterateTestsWithLevel ( const TestNode* root,
249 int len,
250 const TestNode** list,
251 TestMode mode)
252{
253 int i;
254 int saveIndent;
255
256 char pathToFunction[MAXTESTNAME] = "";
257 char separatorString[2] = { TEST_SEPARATOR, '\0'};
258
259 if ( root == NULL )
260 return;
261
262 list[len++] = root;
263
264 for ( i=0;i<(len-1);i++ )
265 {
266 strcat(pathToFunction, list[i]->name);
267 strcat(pathToFunction, separatorString);
268 }
269
270 strcat(pathToFunction, list[i]->name);
271
272 INDENT_LEVEL = len;
273 if ( (mode == RUNTESTS) && (root->test != NULL))
274 {
275 int myERROR_COUNT = ERROR_COUNT;
276 currentTest = root;
277 root->test();
278 currentTest = NULL;
279 if (myERROR_COUNT != ERROR_COUNT)
280 {
281
282 log_info("---[%d ERRORS] ", ERROR_COUNT - myERROR_COUNT);
283 strcpy(ERROR_LOG[ERRONEOUS_FUNCTION_COUNT++], pathToFunction);
284 }
285 else
286 log_info("---[OK] ");
287 }
288
289
290 /* we want these messages to be at 0 indent. so just push the indent level breifly. */
291 saveIndent = INDENT_LEVEL;
292 INDENT_LEVEL = 0;
293 log_info("%s%s%c\n", (list[i]->test||mode==SHOWTESTS)?"---":"",pathToFunction, list[i]->test?' ':TEST_SEPARATOR );
294 INDENT_LEVEL = saveIndent;
295
296 iterateTestsWithLevel ( root->child, len, list, mode );
297
298 len--;
299
300 if ( len != 0 ) /* DO NOT iterate over siblings of the root. */
301 iterateTestsWithLevel ( root->sibling, len, list, mode );
302}
303
304
305
374ca955
A
306void T_CTEST_EXPORT2
307showTests ( const TestNode *root )
b75a7d8f
A
308{
309 /* make up one for them */
310 const TestNode *aList[MAXTESTS];
311
312 if (root == NULL)
313 log_err("TEST CAN'T BE FOUND!");
314
315 iterateTestsWithLevel ( root, 0, aList, SHOWTESTS );
316
317}
318
374ca955
A
319void T_CTEST_EXPORT2
320runTests ( const TestNode *root )
b75a7d8f
A
321{
322 int i;
323 const TestNode *aList[MAXTESTS];
324 /* make up one for them */
325
326
327 if (root == NULL)
328 log_err("TEST CAN'T BE FOUND!\n");
329
330 ERRONEOUS_FUNCTION_COUNT = ERROR_COUNT = 0;
331 iterateTestsWithLevel ( root, 0, aList, RUNTESTS );
332
333 /*print out result summary*/
334
335 if (ERROR_COUNT)
336 {
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]);
340 }
341 else
342 {
343 log_info("\n[All tests passed successfully...]\n");
344 }
345
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");
351 } else {
352 log_info("\t*WARNING* some data-loading errors were ignored by the -w option.\n");
353 }
354 }
355}
356
374ca955
A
357const char* T_CTEST_EXPORT2
358getTestName(void)
b75a7d8f
A
359{
360 if(currentTest != NULL) {
361 return currentTest->name;
362 } else {
363 return NULL;
364 }
365}
366
374ca955
A
367const TestNode* T_CTEST_EXPORT2
368getTest(const TestNode* root, const char* name)
b75a7d8f
A
369{
370 const char* nextName;
371 TestNode *nextNode;
372 const TestNode* curNode;
373 int nameLen; /* length of current 'name' */
374
375 if (root == NULL)
376 log_err("TEST CAN'T BE FOUND!\n");
377 /* remove leading slash */
378 if ( *name == TEST_SEPARATOR )
379 name++;
380
381 curNode = root;
382
383 for(;;)
384 {
385 /* Start with the next child */
386 nextNode = curNode->child;
387
388 getNextLevel ( name, &nameLen, &nextName );
389
390 /* printf("* %s\n", name );*/
391
392 /* if nextNode is already null, then curNode has no children
393 -- add them */
394 if( nextNode == NULL )
395 {
396 return NULL;
397 }
398
399 /* Search across for the name */
400 while (strncmp_nullcheck ( name, nextNode->name, nameLen) != 0 )
401 {
402 curNode = nextNode;
403 nextNode = nextNode -> sibling;
404
405 if ( nextNode == NULL )
406 {
407 /* Did not find 'name' on this level. */
408 return NULL;
409 }
410 }
411
412 /* nextNode matches 'name' */
413
414 if (nextName == NULL) /* end of the line */
415 {
416 return nextNode;
417 }
418
419 /* Loop again with the next item */
420 name = nextName;
421 curNode = nextNode;
422 }
423}
424
425static void vlog_err(const char *prefix, const char *pattern, va_list ap)
426{
427 if( ERR_MSG == FALSE){
428 return;
429 }
430 fprintf(stderr, "%-*s", INDENT_LEVEL," " );
431 if(prefix) {
432 fputs(prefix, stderr);
433 }
434 vfprintf(stderr, pattern, ap);
435 fflush(stderr);
436 va_end(ap);
437}
438
374ca955
A
439void T_CTEST_EXPORT2
440vlog_info(const char *prefix, const char *pattern, va_list ap)
b75a7d8f
A
441{
442 fprintf(stdout, "%-*s", INDENT_LEVEL," " );
443 if(prefix) {
444 fputs(prefix, stderr);
445 }
446 vfprintf(stdout, pattern, ap);
447 fflush(stdout);
448 va_end(ap);
449}
450
451static void vlog_verbose(const char *prefix, const char *pattern, va_list ap)
452{
453 if ( VERBOSITY == FALSE )
454 return;
455
456 fprintf(stdout, "%-*s", INDENT_LEVEL," " );
457 if(prefix) {
458 fputs(prefix, stderr);
459 }
460 vfprintf(stdout, pattern, ap);
461 fflush(stdout);
462 va_end(ap);
463}
464
374ca955
A
465void T_CTEST_EXPORT2
466log_err(const char* pattern, ...)
b75a7d8f
A
467{
468 va_list ap;
469 if(strchr(pattern, '\n') != NULL) {
470 /*
471 * Count errors only if there is a line feed in the pattern
472 * so that we do not exaggerate our error count.
473 */
474 ++ERROR_COUNT;
475 }
476 va_start(ap, pattern);
477 vlog_err(NULL, pattern, ap);
478}
479
374ca955
A
480void T_CTEST_EXPORT2
481log_info(const char* pattern, ...)
b75a7d8f
A
482{
483 va_list ap;
484
485 va_start(ap, pattern);
486 vlog_info(NULL, pattern, ap);
487}
488
374ca955
A
489void T_CTEST_EXPORT2
490log_verbose(const char* pattern, ...)
b75a7d8f
A
491{
492 va_list ap;
493
494 va_start(ap, pattern);
495 vlog_verbose(NULL, pattern, ap);
496}
497
498
374ca955
A
499void T_CTEST_EXPORT2
500log_data_err(const char* pattern, ...)
b75a7d8f
A
501{
502 va_list ap;
503 va_start(ap, pattern);
504
505 ++DATA_ERROR_COUNT; /* for informational message at the end */
506
507 if(WARN_ON_MISSING_DATA == 0) {
508 /* Fatal error. */
509 if(strchr(pattern, '\n') != NULL) {
510 ++ERROR_COUNT;
511 }
512 vlog_err(NULL, pattern, ap); /* no need for prefix in default case */
513 } else {
514 vlog_info("[Data] ", pattern, ap);
515 }
516}
517
518
374ca955
A
519int T_CTEST_EXPORT2
520processArgs(const TestNode* root,
b75a7d8f
A
521 int argc,
522 const char* const argv[])
523{
524 /**
525 * This main will parse the l, v, h, n, and path arguments
526 */
527 const TestNode* toRun;
528 int i;
529 int doList = FALSE;
530 int subtreeOptionSeen = FALSE;
531
532 int errorCount = 0;
533
534 toRun = root;
535 VERBOSITY = FALSE;
536 ERR_MSG = TRUE;
537
538 for( i=1; i<argc; i++)
539 {
540 if ( argv[i][0] == '/' )
541 {
542 printf("Selecting subtree '%s'\n", argv[i]);
543
544 if ( argv[i][1] == 0 )
545 toRun = root;
546 else
547 toRun = getTest(root, argv[i]);
548
549 if ( toRun == NULL )
550 {
551 printf("* Could not find any matching subtree\n");
552 return -1;
553 }
554
555 if( doList == TRUE)
556 showTests(toRun);
557 else
558 runTests(toRun);
559
560 errorCount += ERROR_COUNT;
561
562 subtreeOptionSeen = TRUE;
563 }
564 else if (strcmp( argv[i], "-v" )==0 || strcmp( argv[i], "-verbose")==0)
565 {
566 VERBOSITY = TRUE;
567 }
568 else if (strcmp( argv[i], "-l" )==0 )
569 {
570 doList = TRUE;
571 }
572 else if (strcmp( argv[i], "-e1") == 0)
573 {
574 QUICK = -1;
575 }
576 else if (strcmp( argv[i], "-e") ==0)
577 {
578 QUICK = 0;
579 }
580 else if (strcmp( argv[i], "-w") ==0)
581 {
582 WARN_ON_MISSING_DATA = TRUE;
583 }
584 else if(strcmp( argv[i], "-n") == 0 || strcmp( argv[i], "-no_err_msg") == 0)
585 {
586 ERR_MSG = FALSE;
587 }
588 else if (strcmp( argv[i], "-r") == 0)
589 {
590 if (!REPEAT_TESTS_INIT) {
591 REPEAT_TESTS++;
592 }
593 }
594 else if ((strcmp( argv[i], "-a") == 0) || (strcmp(argv[i],"-all") == 0))
595 {
596 subtreeOptionSeen=FALSE;
597 }
374ca955
A
598 else if (strcmp( argv[i], "-t_info") == 0) {
599 ICU_TRACE = UTRACE_INFO;
600 }
601 else if (strcmp( argv[i], "-t_error") == 0) {
602 ICU_TRACE = UTRACE_ERROR;
603 }
604 else if (strcmp( argv[i], "-t_warn") == 0) {
605 ICU_TRACE = UTRACE_WARNING;
606 }
607 else if (strcmp( argv[i], "-t_verbose") == 0) {
608 ICU_TRACE = UTRACE_VERBOSE;
609 }
610 else if (strcmp( argv[i], "-t_oc") == 0) {
611 ICU_TRACE = UTRACE_OPEN_CLOSE;
612 }
613 else if (strcmp( argv[i], "-h" )==0 || strcmp( argv[i], "--help" )==0)
b75a7d8f
A
614 {
615 help( argv[0] );
616 return 0;
617 }
618 else
619 {
620 printf("* unknown option: %s\n", argv[i]);
621 help( argv[0] );
622 return -1;
623 }
624 }
625
626 if( subtreeOptionSeen == FALSE) /* no other subtree given, run the default */
627 {
628 if( doList == TRUE)
629 showTests(toRun);
630 else
631 runTests(toRun);
632
633 errorCount += ERROR_COUNT;
634 }
635 else
636 {
637 if( ( doList == FALSE ) && ( errorCount > 0 ) )
638 printf(" Total errors: %d\n", errorCount );
639 }
640
641 REPEAT_TESTS_INIT = 1;
642
643 return errorCount; /* total error count */
644}
645
646/**
647 * Display program invocation arguments
648 */
649
650static void help ( const char *argv0 )
651{
374ca955
A
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",
b75a7d8f 655 argv0);
374ca955
A
656 printf(" -l To get a list of test names\n");
657 printf(" -e to do exhaustive testing\n");
b75a7d8f 658 printf(" -verbose To turn ON verbosity\n");
374ca955
A
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"
b75a7d8f 663 " user has reduced/changed the common set of ICU data \n");
374ca955 664 printf(" -t_info | -t_error | -t_warn | -t_oc | -t_verbose Enable ICU tracing\n");
b75a7d8f 665 printf(" -no_err_msg (same as -n) \n");
374ca955
A
666 printf(" -r repeat tests after calling u_cleanup \n");
667 printf(" -[/subtest] To run a subtest \n");
b75a7d8f
A
668 printf(" eg: to run just the utility tests type: cintltest /tsutil) \n");
669}
670