123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716 |
- /***********************************************************************
- **
- ** Filename : test.c
- ** Project : Unit Test
- ** Subsystem :
- ** Module : Basic routines for performing a Unit Test
- ** Document :
- **
- *************************************************************************
- **
- ** Brief Description
- ** =================
- **
- ** Functions to provide a nice, consistent Unit Test infrastructure.
- **
- **
- *************************************************************************
- **
- ** Change History
- ** --------------
- **
- ** 20-Jun-2005 Graeme McKerrell Populated lub_test_stop_here() t ensure it is reached when a
- ** optimising compiler is used.
- ** 7-Dec-2004 Graeme McKerrell Updated to use the "lub_test_" prefix
- ** rather than "unittest_"
- ** 4 Mar 2004 Graeme McKerrell Ported for use in Garibaldi
- ** 4 Oct 2002 Graeme McKerrell updated to use central interface
- ** definition
- ** 18-Mar-2002 Graeme McKerrell Added unitest_stop_here() support
- ** 16-Mar-2002 Graeme McKerrell LINTed...
- ** 14 Nov 2000 Brett B. Bonner created
- **
- *************************************************************************
- **
- ** Copyright (C) 3Com Corporation. All Rights Reserved.
- **
- \************************************************************************/
- #include <stdio.h>
- #include <string.h>
- #include <stdarg.h>
- #include <stdlib.h>
- /*lint -esym(534,vsprintf) */
- /*lint -esym(632,va_list,__va_list) */
- /*lint -esym(633,va_list,__gnuc_va_list) */
- #include "lub/test.h"
- /* Where to direct output (bitmasks) */
- #define LUB_TEST_LOGTOFILE 0x1
- #define LUB_TEST_LOGTOSTDOUT 0x2
- /* Termination Mode */
- typedef enum {
- ContinueOnFail,
- StopOnFail
- } TerminationMode;
- /* local variables */
- static char unitTestName[80];
- static char seqDescr[80];
- static FILE *logp=NULL;
- static lub_test_verbosity_t verbosity = LUB_TEST_NORMAL;
- static int outputTo;
- static lub_test_status_t unitTestStatus=LUB_TEST_PASS;
- static int seqNum=0;
- static int testNum=0;
- static int failureCount=0;
- static int testCount=0;
- static TerminationMode termMode=ContinueOnFail;
- /* local functions */
- static void testLogNoIndent(lub_test_verbosity_t level, const char *format, ...);
- static void printUsage(void);
- /* definitions */
- #define LOGGING_TO_FILE ((outputTo & LUB_TEST_LOGTOFILE) != 0)
- #define LOGGING_TO_STDOUT ((outputTo & LUB_TEST_LOGTOSTDOUT) != 0)
- /*
- * This is provided as a debug aid.
- */
- void
- lub_test_stop_here(void)
- {
- /* If any test fails, the unit test fails */
- unitTestStatus = LUB_TEST_FAIL;
- failureCount++;
- if (termMode == StopOnFail) {
- lub_test_end();
- exit(1);
- }
- }
- static lub_test_status_t
- checkStatus(lub_test_status_t value)
- {
- /* Test number gets incremented automatically... */
- testNum++;
- testCount++; /* update total number of tests performed */
- if(LUB_TEST_FAIL == value)
- {
- lub_test_stop_here();
- }
- return value;
- }
- /*******************************************************
- * unit-test-level functions
- ********************************************************/
- /* NAME: unitTestLog
- PURPOSE: Log output to file and/or stdout, verbosity-filtered
- ARGS: level - priority of this message.
- Will be logged only if priority equals or exceeds
- the current lub_test_verbosity_t level.
- format, args - printf-style format and parameters
- RETURN: none
- */
- static void
- unitTestLog(lub_test_verbosity_t level, const char *format, ...)
- {
- va_list args;
- char string[320]; /* ought to be big enough; that's 4 lines */
- /* Turn format,args into a string */
- va_start(args, format);
- vsprintf(string, format, args);
- va_end(args);
- /* output to selected destination if lub_test_verbosity_t equals or exceeds
- current setting. */
- if (level <= verbosity)
- {
- if (LOGGING_TO_FILE)
- {
- if (NULL != logp)
- {
- fprintf(logp, "%s\n", string);
- }
- else
- {
- fprintf(stderr, "ERROR: Trying to log to file, but no logfile is open!\n");
- }
- }
- if (LOGGING_TO_STDOUT)
- {
- fprintf(stdout, "%s\n", string);
- }
- }
- }
- /* NAME: TestStartLog
- PURPOSE: Sets up logging destination(s) and opens logfile.
- ARGS: whereToLog - where output gets directed to
- Bitmask of lub_test_LOGTOFILE, lub_test_LOGTOSTDOUT
- logfile - file name to open.
- Use NULL if not logging to file.
- RETURN: BOOL_FALSE == failure
- BOOL_TRUE == success
- */
- static int TestStartLog(int whereToLog, const char *logFile)
- {
- bool_t status = BOOL_TRUE;
- /* Where are we logging? */
- outputTo = whereToLog;
-
- /* open log file */
- if (LOGGING_TO_FILE && (strlen(logFile) < 1))
- {
- status = BOOL_FALSE;
- fprintf(stderr, "ERROR: No logfile name specified.\n");
- }
- if (LOGGING_TO_FILE && status)
- {
- if ( (logp = fopen(logFile, "w")) == NULL )
- {
- status = BOOL_FALSE;
- fprintf(stderr,
- "ERROR: could not open log file '%s'.\n",
- logFile);
- }
- }
- return status;
- }
- /* NAME: lub_test_begin
- PURPOSE: Starts unit test.
- ARGS: format,args - Specification of unit test name
- RETURN: none
- */
- void
- lub_test_begin(const char *name, ...)
- {
- va_list args;
- /* Process varargs list into unit test name string */
- va_start(args, name);
- vsprintf(unitTestName, name, args);
- va_end(args);
- unitTestLog(LUB_TEST_NORMAL, "BEGIN: Testing '%s'.", unitTestName);
- /* reset counters */
- seqNum=testNum=testCount=failureCount=0;
- }
- /* NAME: lub_test_get_status
- PURPOSE: Reports current unit test status
- ARGS: none
- RETURN: Status code - TESTPASS/TESTFAIL
- */
- lub_test_status_t
- lub_test_get_status(void)
- {
- return unitTestStatus;
- }
- /* NAME: lub_test_failure_count
- PURPOSE: Reports current number of test failures
- ARGS: none
- RETURN: Count of failures
- */
- int
- lub_test_failure_count(void)
- {
- return failureCount;
- }
- /* NAME: lub_test_end
- PURPOSE: Ends test. Closes log file.
- ARGS: none
- RETURN: none
- */
- void
- lub_test_end(void)
- {
- char result[40];
- if (unitTestStatus == LUB_TEST_PASS)
- {
- sprintf(result, "PASSED (%d tests)",testCount);
- }
- else
- {
- if (failureCount == 1)
- {
- sprintf(result, "FAILED (%d failure, %d tests)",
- failureCount, testCount);
- }
- else
- {
- sprintf(result, "FAILED (%d failures, %d tests)",
- failureCount, testCount);
- }
- }
- if ((termMode == ContinueOnFail) ||
- (unitTestStatus == LUB_TEST_PASS))
- {
- /* ran to end - either due to continue-on-fail, or because
- everything passed */
- unitTestLog(LUB_TEST_TERSE, "END: Test '%s' %s.\n",unitTestName, result);
- }
- else
- {
- /* stopped on first failure */
- unitTestLog(LUB_TEST_TERSE, "END: Test '%s': STOPPED AT FIRST FAILURE.\n",unitTestName);
- }
- if (LOGGING_TO_FILE)
- {
- fclose(logp);
- }
- }
- /* NAME: printUsage
- PURPOSE: Prints usage message
- ARGS: none
- RETURN: none
- */
- static void
- printUsage(void)
- {
- printf("Usage:\n");
- printf("-terse, -normal, -verbose : set lub_test_verbosity_t level (default: normal)\n");
- printf("-stoponfail, -continueonfail : set behavior upon failure (default: continue)\n");
- printf("-logfile [FILENAME] : log to FILENAME (default: 'test.log')\n");
- printf("-nologfile : disable logging to file \n");
- printf("-stdout, -nostdout : enable/disable logging to STDOUT\n");
- printf("-usage, -help : print these options and exit\n");
- printf("\nAll arguments are optional. Defaults are equivalent to: \n");
- printf(" -normal -continueonfail -logfile test.log -stdout\n");
- }
- /* NAME: lub_test_parse_command_line
- PURPOSE: Parses command-line arguments & sets unit test options.
- ARGS: argc, argv - as passed to main()
- RETURN: none (will exit if there is an error)
- */
- void
- lub_test_parse_command_line(int argc, const char * const *argv)
- {
- bool_t status=BOOL_TRUE;
- int logDest=0;
- static char defaultFile[] = "test.log";
- char *logFile;
- bool_t logFileAllocatedFromHeap=BOOL_FALSE;
- /* variables used to detect conflicting options */
- int verbset=0;
- int failset=0;
- int logfset=0;
- int stdset=0;
- /* Set logFile to defaultFile so there is always a valid filename.
- Also set logging destination to both STDOUT and FILE.
- These will be changed later if user specifies something else. */
- logFile = defaultFile;
- logDest = (LUB_TEST_LOGTOFILE | LUB_TEST_LOGTOSTDOUT);
- /* Loop through the command line arguments. */
- while (--argc > 0)
- {
- /* Usage/Help */
- if ((strstr(argv[argc], "-usage") != NULL) ||
- (strstr(argv[argc], "-help") != NULL))
- {
- printUsage();
- exit (0);
- }
- /* lub_test_verbosity_t Levels */
- else if (strstr(argv[argc], "-terse") != NULL)
- {
- verbosity = LUB_TEST_TERSE; verbset++;
- }
- else if (strstr(argv[argc], "-normal") != NULL)
- {
- verbosity = LUB_TEST_NORMAL; verbset++;
- }
- else if (strstr(argv[argc], "-verbose") != NULL)
- {
- verbosity = LUB_TEST_VERBOSE; verbset++;
- }
- /* Failure behavior */
- else if (strstr(argv[argc], "-stoponfail") != NULL)
- {
- termMode = StopOnFail; failset++;
- }
- else if (strstr(argv[argc], "-continueonfail") != NULL)
- {
- termMode = ContinueOnFail; failset++;
- }
- /* Log file options */
- else if (strstr(argv[argc], "-logfile") != NULL)
- {
- logDest |= LUB_TEST_LOGTOFILE;
- /* Was a log file name specified?
- We assume it's a file name if it doesn't start with '-' */
- if (strstr(argv[argc+1], "-") != argv[argc+1])
- {
- /* Yes, got a filename */
- logFile = (char *)malloc(strlen(argv[argc+1])+1);
- if (NULL == logFile)
- {
- status = BOOL_FALSE;
- fprintf(stderr, "unitTestParseCL: ERROR: Memory allocation problem.\n");
- }
- else
- {
- /* all is well, go ahead and copy the string */
- strcpy(logFile, argv[argc+1]);
- }
- logFileAllocatedFromHeap=BOOL_TRUE;
- }
- logfset++;
- }
- else if (strstr(argv[argc], "-nologfile") != NULL)
- {
- logDest &= ~LUB_TEST_LOGTOFILE;
- logfset++;
- }
- /* Stdout options */
- else if (strstr(argv[argc], "-stdout") != NULL)
- {
- logDest |= LUB_TEST_LOGTOSTDOUT;
- stdset++;
- }
- else if (strstr(argv[argc], "-nostdout") != NULL)
- {
- logDest &= ~LUB_TEST_LOGTOSTDOUT;
- stdset++;
- }
- /* Unhandled arguments */
- else
- {
- /* If the next argument down in the list is '-logfile', then
- this is the logfile name; don't complain. */
- if (strstr(argv[argc-1], "-logfile") == NULL)
- {
- /* This is an unrecognized option.
- Don't bother setting status and forcing an exit; just ignore it. */
- fprintf(stderr,"Unrecognized argument: '%s', ignoring it...\n",argv[argc]);
- }
- }
- }
- /* See if there is a logging destination */
- if (logDest == 0)
- {
- fprintf(stderr, "WARNING: No logging is enabled to either stdout or a logfile; expect no output.\n");
- }
- /* Make sure there were no conflicting options */
- if (verbset > 1)
- {
- fprintf(stderr,"ERROR: conflicting lub_test_verbosity_t options specified.\n");
- fprintf(stderr," Specify only ONE of -terse, -normal, -verbose\n");
- status = BOOL_FALSE;
- }
- if (failset > 1)
- {
- fprintf(stderr,"ERROR: conflicting Failure Mode options specified.\n");
- fprintf(stderr," Specify only ONE of -stoponfail, -continueonfail\n");
- status = BOOL_FALSE;
- }
- if (logfset > 1)
- {
- fprintf(stderr,"ERROR: conflicting Logfile options specified.\n");
- fprintf(stderr," Specify only ONE of -logfile, -nologfile\n");
- status = BOOL_FALSE;
- }
- if (stdset > 1)
- {
- fprintf(stderr,"ERROR: conflicting Stdout options specified.\n");
- fprintf(stderr," Specify only ONE of -stdout, -nostdout\n");
- status = BOOL_FALSE;
- }
- /* Set up log file, if things are OK so far */
- if (status && !TestStartLog(logDest, logFile))
- {
- status = BOOL_FALSE;
- }
- if (logFileAllocatedFromHeap)
- {
- /*lint -e673 Possibly inappropriate deallocation (free) for 'static' data */
- free(logFile);
- /*lint +e673 */
- }
- if (BOOL_FALSE == status)
- {
- fprintf(stderr, "Something bad has occurred. Aborting...\n");
- exit(1);
- }
- }
- /*******************************************************
- * sequence level functions
- ********************************************************/
- /* NAME: lub_test_seq_begin
- PURPOSE: Starts a new test sequence.
- ARGS: num - sequence number, must be different than last one
- format, args - printf-style name for the sequence
- RETURN: none
- */
- void
- lub_test_seq_begin(int num, const char *seqName, ...)
- {
- va_list args;
- /* Get sequence name */
- va_start(args, seqName);
- vsprintf(seqDescr, seqName, args);
- va_end(args);
- /* Start new sequence number */
- if (num == seqNum)
- {
- seqNum++;
- unitTestLog(LUB_TEST_TERSE, "seq_start: duplicate sequence number. Using next available sequence number (%d).\n", seqNum);
- }
- else
- {
- seqNum = num;
- }
- /* Reset test number */
- testNum = 0;
- /* Log beginning of sequence */
- lub_test_seq_log(LUB_TEST_NORMAL, "*** Sequence '%s' begins ***", seqDescr);
- }
- /* NAME: lub_test_seq_end
- PURPOSE: Ends current test sequence
- ARGS: none
- RETURN: none
- */
- void
- lub_test_seq_end(void)
- {
- lub_test_seq_log(LUB_TEST_NORMAL,
- "----------------------------------------"
- "--------------------");
- }
- /* NAME: lub_test_seq_log
- PURPOSE: Log output to file and/or stdout, verbosity-filtered and
- formatted as a "sequence-level" message
- ARGS: level - priority of this message.
- Will be logged only if priority equals or exceeds
- the current lub_test_verbosity_t level.
- format, args - printf-style format and parameters
- RETURN: none
- */
- void
- lub_test_seq_log(lub_test_verbosity_t level, const char *format, ...)
- {
- va_list args;
- char string[160];
- /* Turn format,args into a string */
- va_start(args, format);
- vsprintf(string, format, args);
- va_end(args);
- unitTestLog(level, "%03d : %s", seqNum, string);
- }
- /*******************************************************
- * test level functions
- ********************************************************/
- /* NAME: lub_test_check
- PURPOSE: True/False test of an expression. If True, test passes.
- ARGS: expr - expression to evaluate.
- format, args - Printf-style format/parameters describing test.
- RETURN: Status code - PASS or FAIL
- */
- lub_test_status_t
- lub_test_check(bool_t expr, const char *testName, ...)
- {
- va_list args;
- char testDescr[80];
- lub_test_status_t testStatus;
- char result[5];
- lub_test_verbosity_t verb;
- /* evaluate expression */
- testStatus = expr ? LUB_TEST_PASS : LUB_TEST_FAIL;
- /* extract description as a string */
- va_start(args, testName);
- vsprintf(testDescr, testName, args);
- va_end(args);
- /* log results */
- if (testStatus == LUB_TEST_PASS)
- {
- verb=LUB_TEST_NORMAL;
- sprintf(result, "pass");
- }
- else
- {
- verb=LUB_TEST_TERSE;
- sprintf(result, "FAIL");
- }
- testLogNoIndent(verb, "[%s] %s", result, testDescr);
- return checkStatus(testStatus);
- }
- /* NAME: lub_test_check_int
- PURPOSE: Checks whether an integer equals its expected value.
- ARGS: expect - expected value.
- actual - value being checked.
- format, args - Printf-style format/parameters describing test.
- RETURN: Status code - PASS or FAIL
- */
- lub_test_status_t
- lub_test_check_int(int expect, int actual, const char *testName, ... )
- {
- va_list args;
- char testDescr[80];
- lub_test_status_t testStatus;
- char result[5];
- char eqne[3];
- lub_test_verbosity_t verb;
- /* evaluate expression */
- testStatus = (expect == actual) ? LUB_TEST_PASS : LUB_TEST_FAIL;
- /* extract description as a string */
- va_start(args, testName);
- vsprintf(testDescr, testName, args);
- va_end(args);
- /* log results */
- if (testStatus == LUB_TEST_PASS)
- {
- sprintf(result, "pass");
- sprintf(eqne, "==");
- verb = LUB_TEST_NORMAL;
- }
- else
- {
- sprintf(result, "FAIL");
- sprintf(eqne, "!=");
- verb = LUB_TEST_TERSE;
- }
- testLogNoIndent(verb, "[%s] (%d%s%d) %s",
- result, actual, eqne, expect, testDescr);
- return checkStatus(testStatus);
- }
- /* NAME: lub_test_check_float
- PURPOSE: Checks whether a float is within min/max limits.
- ARGS: min - minimum acceptable value.
- max - maximum acceptable value.
- actual - value being checked.
- format, args - Printf-style format/parameters describing test.
- RETURN: Status code - PASS or FAIL
- */
- lub_test_status_t
- lub_test_check_float(double min, double max, double actual,
- const char *testName, ...)
- {
- va_list args;
- char testDescr[80];
- lub_test_status_t testStatus;
- char result[5];
- char gteq[4], lteq[4];
- /* evaluate expression */
- testStatus = ((actual >= min) && (actual <= max)) ? LUB_TEST_PASS : LUB_TEST_FAIL;
- /* extract description as a string */
- va_start(args, testName);
- vsprintf(testDescr, testName, args);
- va_end(args);
- /* log results */
- if (testStatus == LUB_TEST_PASS)
- {
- sprintf(result, "pass");
- sprintf(gteq, " <=");
- sprintf(lteq, " <=");
- }
- else
- {
- sprintf(result, "FAIL");
- if (actual < min)
- {
- sprintf(gteq, "!<=");
- sprintf(lteq, " <=");
- }
- else
- {
- sprintf(gteq, " <=");
- sprintf(lteq, "!<=");
- }
- }
- testLogNoIndent(LUB_TEST_NORMAL, "[%s] (%8f%s%8f%s%8f) %s",
- result, min, gteq, actual, lteq, max, testDescr);
- return checkStatus(testStatus);
- }
- /* NAME: lub_test_log
- PURPOSE: Log output to file and/or stdout, verbosity-filtered and
- formatted as a "test-level" message
- ARGS: level - priority of this message.
- Will be logged only if priority equals or exceeds
- the current lub_test_verbosity_t level.
- format, args - printf-style format and parameters
- RETURN: none
- */
- void
- lub_test_log(lub_test_verbosity_t level, const char *format, ...)
- {
- va_list args;
- char string[160];
- /* Turn format,args into a string */
- va_start(args, format);
- vsprintf(string, format, args);
- va_end(args);
- unitTestLog(level, "%03d-%04d: %s", seqNum, testNum, string);
- }
- /* NAME: testLogNoIndent
- PURPOSE: Log output to file and/or stdout, verbosity-filtered and
- formatted as a "test-level" message.
- This "internal" version does not indent, so that the
- pass/fail column can be printed in the proper place.
- ARGS: level - priority of this message.
- Will be logged only if priority equals or exceeds
- the current lub_test_verbosity_t level.
- format, args - printf-style format and parameters
- RETURN: none
- */
- static void testLogNoIndent(lub_test_verbosity_t level, const char *format, ...)
- {
- va_list args;
- char string[160];
- /* Turn format,args into a string */
- va_start(args, format);
- vsprintf(string, format, args);
- va_end(args);
- unitTestLog(level, "%03d-%04d: %s", seqNum, testNum, string);
- }
|