123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716 |
- #include <stdio.h>
- #include <string.h>
- #include <stdarg.h>
- #include <stdlib.h>
- #include "lub/test.h"
- #define LUB_TEST_LOGTOFILE 0x1
- #define LUB_TEST_LOGTOSTDOUT 0x2
- typedef enum {
- ContinueOnFail,
- StopOnFail
- } TerminationMode;
- 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;
- static void testLogNoIndent(lub_test_verbosity_t level, const char *format, ...);
- static void printUsage(void);
- #define LOGGING_TO_FILE ((outputTo & LUB_TEST_LOGTOFILE) != 0)
- #define LOGGING_TO_STDOUT ((outputTo & LUB_TEST_LOGTOSTDOUT) != 0)
- void
- lub_test_stop_here(void)
- {
-
- unitTestStatus = LUB_TEST_FAIL;
- failureCount++;
- if (termMode == StopOnFail) {
- lub_test_end();
- exit(1);
- }
- }
- static lub_test_status_t
- checkStatus(lub_test_status_t value)
- {
-
- testNum++;
- testCount++;
- if(LUB_TEST_FAIL == value)
- {
- lub_test_stop_here();
- }
- return value;
- }
- static void
- unitTestLog(lub_test_verbosity_t level, const char *format, ...)
- {
- va_list args;
- char string[320];
-
- va_start(args, format);
- vsprintf(string, format, args);
- va_end(args);
-
-
- 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);
- }
- }
- }
- static int TestStartLog(int whereToLog, const char *logFile)
- {
- bool_t status = BOOL_TRUE;
-
- outputTo = whereToLog;
-
-
- 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;
- }
- void
- lub_test_begin(const char *name, ...)
- {
- va_list args;
-
- va_start(args, name);
- vsprintf(unitTestName, name, args);
- va_end(args);
- unitTestLog(LUB_TEST_NORMAL, "BEGIN: Testing '%s'.", unitTestName);
-
- seqNum=testNum=testCount=failureCount=0;
- }
- lub_test_status_t
- lub_test_get_status(void)
- {
- return unitTestStatus;
- }
- int
- lub_test_failure_count(void)
- {
- return failureCount;
- }
- 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))
- {
-
-
- unitTestLog(LUB_TEST_TERSE, "END: Test '%s' %s.\n",unitTestName, result);
- }
- else
- {
-
- unitTestLog(LUB_TEST_TERSE, "END: Test '%s': STOPPED AT FIRST FAILURE.\n",unitTestName);
- }
- if (LOGGING_TO_FILE)
- {
- fclose(logp);
- }
- }
- 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");
- }
- 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;
-
- int verbset=0;
- int failset=0;
- int logfset=0;
- int stdset=0;
-
- logFile = defaultFile;
- logDest = (LUB_TEST_LOGTOFILE | LUB_TEST_LOGTOSTDOUT);
-
- while (--argc > 0)
- {
-
- if ((strstr(argv[argc], "-usage") != NULL) ||
- (strstr(argv[argc], "-help") != NULL))
- {
- printUsage();
- exit (0);
- }
-
- 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++;
- }
-
- else if (strstr(argv[argc], "-stoponfail") != NULL)
- {
- termMode = StopOnFail; failset++;
- }
- else if (strstr(argv[argc], "-continueonfail") != NULL)
- {
- termMode = ContinueOnFail; failset++;
- }
-
- else if (strstr(argv[argc], "-logfile") != NULL)
- {
- logDest |= LUB_TEST_LOGTOFILE;
-
- if (strstr(argv[argc+1], "-") != argv[argc+1])
- {
-
- logFile = (char *)malloc(strlen(argv[argc+1])+1);
- if (NULL == logFile)
- {
- status = BOOL_FALSE;
- fprintf(stderr, "unitTestParseCL: ERROR: Memory allocation problem.\n");
- }
- else
- {
-
- strcpy(logFile, argv[argc+1]);
- }
- logFileAllocatedFromHeap=BOOL_TRUE;
- }
- logfset++;
- }
- else if (strstr(argv[argc], "-nologfile") != NULL)
- {
- logDest &= ~LUB_TEST_LOGTOFILE;
- logfset++;
- }
-
- else if (strstr(argv[argc], "-stdout") != NULL)
- {
- logDest |= LUB_TEST_LOGTOSTDOUT;
- stdset++;
- }
- else if (strstr(argv[argc], "-nostdout") != NULL)
- {
- logDest &= ~LUB_TEST_LOGTOSTDOUT;
- stdset++;
- }
-
- else
- {
-
- if (strstr(argv[argc-1], "-logfile") == NULL)
- {
-
- fprintf(stderr,"Unrecognized argument: '%s', ignoring it...\n",argv[argc]);
- }
- }
- }
-
- if (logDest == 0)
- {
- fprintf(stderr, "WARNING: No logging is enabled to either stdout or a logfile; expect no output.\n");
- }
-
- 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;
- }
-
- if (status && !TestStartLog(logDest, logFile))
- {
- status = BOOL_FALSE;
- }
- if (logFileAllocatedFromHeap)
- {
-
- free(logFile);
-
- }
- if (BOOL_FALSE == status)
- {
- fprintf(stderr, "Something bad has occurred. Aborting...\n");
- exit(1);
- }
- }
- void
- lub_test_seq_begin(int num, const char *seqName, ...)
- {
- va_list args;
-
- va_start(args, seqName);
- vsprintf(seqDescr, seqName, args);
- va_end(args);
-
- if (num == seqNum)
- {
- seqNum++;
- unitTestLog(LUB_TEST_TERSE, "seq_start: duplicate sequence number. Using next available sequence number (%d).\n", seqNum);
- }
- else
- {
- seqNum = num;
- }
-
- testNum = 0;
-
- lub_test_seq_log(LUB_TEST_NORMAL, "*** Sequence '%s' begins ***", seqDescr);
- }
- void
- lub_test_seq_end(void)
- {
- lub_test_seq_log(LUB_TEST_NORMAL,
- "----------------------------------------"
- "--------------------");
- }
- void
- lub_test_seq_log(lub_test_verbosity_t level, const char *format, ...)
- {
- va_list args;
- char string[160];
-
- va_start(args, format);
- vsprintf(string, format, args);
- va_end(args);
- unitTestLog(level, "%03d : %s", seqNum, string);
- }
- 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;
-
- testStatus = expr ? LUB_TEST_PASS : LUB_TEST_FAIL;
-
- va_start(args, testName);
- vsprintf(testDescr, testName, args);
- va_end(args);
-
- 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);
- }
- 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;
-
- testStatus = (expect == actual) ? LUB_TEST_PASS : LUB_TEST_FAIL;
-
- va_start(args, testName);
- vsprintf(testDescr, testName, args);
- va_end(args);
-
- 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);
- }
- 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];
-
- testStatus = ((actual >= min) && (actual <= max)) ? LUB_TEST_PASS : LUB_TEST_FAIL;
-
- va_start(args, testName);
- vsprintf(testDescr, testName, args);
- va_end(args);
-
- 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);
- }
- void
- lub_test_log(lub_test_verbosity_t level, const char *format, ...)
- {
- va_list args;
- char string[160];
-
- va_start(args, format);
- vsprintf(string, format, args);
- va_end(args);
- unitTestLog(level, "%03d-%04d: %s", seqNum, testNum, string);
- }
- static void testLogNoIndent(lub_test_verbosity_t level, const char *format, ...)
- {
- va_list args;
- char string[160];
-
- va_start(args, format);
- vsprintf(string, format, args);
- va_end(args);
- unitTestLog(level, "%03d-%04d: %s", seqNum, testNum, string);
- }
|