UnitTest.h File Reference


Detailed Description

Goes along with UnitTest.tcl in the test/ directory.

See sample-test.cc for a good example of the UnitTesting macros. They should cover most common case usages of the unit testing framework.

Each small test is a thunk run by deriving from the UnitTest class and adding the test to the list maintained in UnitTester. The UnitTester then goes through each test and spits out to stderr a Tcl struct which represents the success/failure of a particular run. There is also a third type of test success condition, input, which means that the test results cannot be determined by the program and needs to be done externally.

Each *-test.cc file is both a Tcl program and C++ program. For each unit test class Foo that is defined to return UNIT_TEST_INPUT, there should be Tcl proc checkFoo that checks the output of the program for the correct property. The reason for putting the Tcl/C++ together in the same file is to make it easy to maintain the tests.

Note that all Tcl code should be bracketed by conditional compilation, using ifdef DECLARE_TEST_TCL ... endif.

Each *-test in the directory is run and output is placed in output/ *-test/std[out|err].

TODO: save and rerun only those that failed

Definition in file UnitTest.h.

#include <string>
#include <vector>
#include <stdio.h>
#include "../debug/FatalSignals.h"
#include "../debug/Log.h"
#include "../io/PrettyPrintBuffer.h"

Go to the source code of this file.

Namespaces

namespace  oasys

Classes

struct  oasys::UnitTest
 Override the method run to create an individual unit test. More...
class  oasys::UnitTester
 UnitTester runs all unit test and produces a nice format which hooks into the parsing script. More...

Defines

#define ADD_TEST(_name)   add(new _name ## UnitTest())
 Helper macros for implementing unit tests.
#define DECLARE_TEST(_name)
 Helper macros for implementing unit tests.
#define RUN_TESTER(_UnitTesterClass, testname, argc, argv)
 Helper macros for implementing unit tests.
#define DECLARE_TEST_FILE(_UnitTesterClass, testname)
 Helper macros for implementing unit tests.
#define DECLARE_TESTER(_name)
 Helper macros for implementing unit tests.
#define DO(x)
 Helper macros for implementing unit tests.
#define CHECK(x)
 Helper macros for implementing unit tests.
#define CHECK_EQUAL(_a, _b)
 Helper macros for implementing unit tests.
#define CHECK_LT(_a, _b)
 Helper macros for implementing unit tests.
#define CHECK_GT(_a, _b)
 Helper macros for implementing unit tests.
#define CHECK_LTU(_a, _b)
 Helper macros for implementing unit tests.
#define CHECK_GTU(_a, _b)
 Helper macros for implementing unit tests.
#define CHECK_EQUAL_U64(a, b)
 Helper macros for implementing unit tests.
#define CHECK_EQUALSTR(a, b)
 Helper macros for implementing unit tests.
#define CHECK_EQUALSTRN(a, b, len)
 Helper macros for implementing unit tests.

Enumerations

enum  { oasys::UNIT_TEST_PASSED = 0, oasys::UNIT_TEST_FAILED, oasys::UNIT_TEST_INPUT }


Define Documentation

#define ADD_TEST ( _name   )     add(new _name ## UnitTest())

Helper macros for implementing unit tests.

Definition at line 253 of file UnitTest.h.

#define CHECK (  ) 

Value:

do { if (! (x)) {                                                   \
        ::oasys::Breaker::break_here();                                 \
        ::oasys::__logf(oasys::LOG_ERR, "/test",                        \
                    "CHECK FAILED (%s) at %s:%d",                       \
                    #x, __FILE__, __LINE__);                            \
        return oasys::UNIT_TEST_FAILED;                                 \
    } else {                                                            \
        ::oasys::__logf(oasys::LOG_NOTICE, "/test",                     \
                    "CHECK (%s) ok at %s:%d", #x, __FILE__, __LINE__);  \
    } } while(0)
Helper macros for implementing unit tests.

Definition at line 288 of file UnitTest.h.

#define CHECK_EQUAL ( _a,
_b   ) 

Value:

do { int a = _a; int b = _b; if ((a) != (b)) {                              \
        ::oasys::Breaker::break_here();                                         \
        oasys::logf("/test", oasys::LOG_ERR,                                    \
                    "CHECK FAILED: '" #_a "' (%d) != '" #_b "' (%d) at %s:%d",  \
                    (a), (b), __FILE__, __LINE__);                              \
        return oasys::UNIT_TEST_FAILED;                                         \
    } else {                                                                    \
        oasys::logf("/test", oasys::LOG_NOTICE,                                 \
                    "CHECK '" #_a "' (%d) == '" #_b "' (%d) "                   \
                    "at %s:%d", (a), (b), __FILE__, __LINE__);                  \
    } } while(0)
Helper macros for implementing unit tests.

Definition at line 300 of file UnitTest.h.

#define CHECK_EQUAL_U64 ( a,
 ) 

Value:

do { if ((a) != (b)) {                                                              \
        ::oasys::Breaker::break_here();                                                 \
        oasys::logf("/test", oasys::LOG_ERR,                                            \
                    "CHECK FAILED: '" #a "' (%llu) != '" #b "' (%llu) at %s:%d",        \
                    (u_int64_t)(a), (u_int64_t)(b), __FILE__, __LINE__);                \
        return oasys::UNIT_TEST_FAILED;                                                 \
    } else {                                                                            \
        oasys::logf("/test", oasys::LOG_NOTICE,                                         \
                    "CHECK '" #a "' (%llu) == '" #b "' (%llu) "                         \
                    "at %s:%d",                                                         \
                    (u_int64_t)(a), (u_int64_t)(b), __FILE__, __LINE__);                \
    } } while(0)
Helper macros for implementing unit tests.

Definition at line 365 of file UnitTest.h.

#define CHECK_EQUALSTR ( a,
 ) 

Value:

do { if (strcmp((a), (b)) != 0) {                                   \
        ::oasys::Breaker::break_here();                                 \
        oasys::logf("/test", oasys::LOG_ERR,                            \
                    "CHECK FAILED: '" #a "' != '" #b "' at %s:%d.",     \
                    __FILE__, __LINE__);                                \
        oasys::logf("/test", oasys::LOG_ERR, "Contents of " #a          \
                    " (length %zu): ", strlen(a));                      \
        oasys::PrettyPrintBuf buf_a(a, strlen(a));                      \
        std::string s;                                                  \
        bool done;                                                      \
        do {                                                            \
            done = buf_a.next_str(&s);                                  \
            oasys::logf("/test", oasys::LOG_ERR, s.c_str());            \
        } while (!done);                                                \
                                                                        \
        oasys::logf("/test", oasys::LOG_ERR, "Contents of " #b          \
                    " (length %zu): ", strlen(b));                      \
        oasys::PrettyPrintBuf buf_b(b, strlen(b));                      \
                                                                        \
        do {                                                            \
            done = buf_b.next_str(&s);                                  \
            oasys::logf("/test", oasys::LOG_ERR, s.c_str());            \
        } while (!done);                                                \
                                                                        \
        return oasys::UNIT_TEST_FAILED;                                 \
    } else {                                                            \
        oasys::logf("/test", oasys::LOG_NOTICE,                         \
                    "CHECK '" #a "' (%s) == '" #b "' (%s) "             \
                    "at %s:%d", (a), (b), __FILE__, __LINE__);          \
    } } while(0);
Helper macros for implementing unit tests.

Definition at line 379 of file UnitTest.h.

#define CHECK_EQUALSTRN ( a,
b,
len   ) 

Value:

do { u_int print_len = (len > 32) ? 32 : len;                               \
         if (strncmp((const char*)(a), (const char*)(b), (len)) != 0) {         \
             ::oasys::Breaker::break_here();                                    \
             oasys::logf("/test", oasys::LOG_ERR,  "CHECK FAILED: "             \
                         "'" #a "' (%.*s...) != '" #b "' (%.*s...) at %s:%d",   \
                         print_len, (a), print_len, (b),                        \
                         __FILE__, __LINE__);                                   \
             return oasys::UNIT_TEST_FAILED;                                    \
         } else {                                                               \
             oasys::logf("/test", oasys::LOG_NOTICE,                            \
                         "CHECK '" #a "' (%.*s...) == '" #b "' (%.*s...) "      \
                         "at %s:%d",                                            \
                         print_len, (a), print_len, (b), __FILE__, __LINE__);   \
        }                                                                       \
    } while(0)
Helper macros for implementing unit tests.

Definition at line 411 of file UnitTest.h.

#define CHECK_GT ( _a,
_b   ) 

Value:

do { int a = _a; int b = _b; if (! ((a) > (b))) {                           \
        ::oasys::Breaker::break_here();                                         \
        oasys::logf("/test", oasys::LOG_ERR,                                    \
                    "CHECK FAILED: '" #_a "' (%d) > '" #_b "' (%d) at %s:%d",  \
                    (a), (b), __FILE__, __LINE__);                              \
        return oasys::UNIT_TEST_FAILED;                                         \
    } else {                                                                    \
        oasys::logf("/test", oasys::LOG_NOTICE,                                 \
                    "CHECK '" #_a "' (%d) > '" #_b "' (%d) "                    \
                    "at %s:%d", (a), (b), __FILE__, __LINE__);                  \
    } } while(0)
Helper macros for implementing unit tests.

Definition at line 326 of file UnitTest.h.

#define CHECK_GTU ( _a,
_b   ) 

Value:

do { u_int a = _a; u_int b = _b; if (! ((a) >= (b))) {                      \
        ::oasys::Breaker::break_here();                                         \
        oasys::logf("/test", oasys::LOG_ERR,                                    \
                    "CHECK FAILED: '" #_a "' (%u) >= '" #_b "' (%u) at %s:%u",  \
                    (a), (b), __FILE__, __LINE__);                              \
        return oasys::UNIT_TEST_FAILED;                                         \
    } else {                                                                    \
        oasys::logf("/test", oasys::LOG_NOTICE,                                 \
                    "CHECK '" #_a "' (%u) >= '" #_b "' (%u) "                   \
                    "at %s:%d", (a), (b), __FILE__, __LINE__);                  \
    } } while(0)
Helper macros for implementing unit tests.

Definition at line 352 of file UnitTest.h.

#define CHECK_LT ( _a,
_b   ) 

Value:

do { int a = _a; int b = _b; if (! (((a) < (b))) {                          \
        ::oasys::Breaker::break_here();                                         \
        oasys::logf("/test", oasys::LOG_ERR,                                    \
                    "CHECK FAILED: '" #_a "' (%d) < '" #_b "' (%d) at %s:%d",  \
                    (a), (b), __FILE__, __LINE__);                              \
        return oasys::UNIT_TEST_FAILED;                                         \
    } else {                                                                    \
        oasys::logf("/test", oasys::LOG_NOTICE,                                 \
                    "CHECK '" #_a "' (%d) < '" #_b "' (%d) "                    \
                    "at %s:%d", (a), (b), __FILE__, __LINE__);                  \
    } } while(0)
Helper macros for implementing unit tests.

Definition at line 313 of file UnitTest.h.

#define CHECK_LTU ( _a,
_b   ) 

Value:

do { u_int a = _a; u_int b = _b; if (! ((a) <= (b))) {                      \
        ::oasys::Breaker::break_here();                                         \
        oasys::logf("/test", oasys::LOG_ERR,                                    \
                    "CHECK FAILED: '" #_a "' (%u) <= '" #_b "' (%u) at %s:%u",  \
                    (a), (b), __FILE__, __LINE__);                              \
        return oasys::UNIT_TEST_FAILED;                                         \
    } else {                                                                    \
        oasys::logf("/test", oasys::LOG_NOTICE,                                 \
                    "CHECK '" #_a "' (%u) <= '" #_b "' (%u) "                   \
                    "at %s:%d", (a), (b), __FILE__, __LINE__);                  \
    } } while(0)
Helper macros for implementing unit tests.

Definition at line 339 of file UnitTest.h.

#define DECLARE_TEST ( _name   ) 

Value:

struct _name ## UnitTest : public oasys::UnitTest { \
        _name ## UnitTest() : UnitTest(#_name) {}       \
        int run();                                      \
    };                                                  \
    int _name ## UnitTest::run()
Helper macros for implementing unit tests.

Definition at line 256 of file UnitTest.h.

#define DECLARE_TEST_FILE ( _UnitTesterClass,
testname   ) 

Value:

int main(int argc, const char* argv[]) {                        \
    RUN_TESTER(_UnitTesterClass, testname, argc, argv);         \
}
Helper macros for implementing unit tests.

Definition at line 267 of file UnitTest.h.

#define DECLARE_TESTER ( _name   ) 

Value:

class _name : public oasys::UnitTester {                        \
public:                                                         \
    _name::_name(std::string name) : UnitTester(name) {}        \
protected:                                                      \
    void add_tests();                                           \
};                                                              \
void _name::add_tests()                                         \
Helper macros for implementing unit tests.

Definition at line 272 of file UnitTest.h.

#define DO (  ) 

Value:

do {                                                                \
        ::oasys::__logf(oasys::LOG_NOTICE, "/test",                     \
                    "DO (%s) at %s:%d", #x, __FILE__, __LINE__);        \
        x;                                                              \
    } while (0)
Helper macros for implementing unit tests.

Definition at line 281 of file UnitTest.h.

#define RUN_TESTER ( _UnitTesterClass,
testname,
argc,
argv   ) 

Value:

_UnitTesterClass test(testname);                            \
    return test.run_tests(argc, argv);
Helper macros for implementing unit tests.

Definition at line 263 of file UnitTest.h.


Generated on Fri Dec 22 14:48:02 2006 for DTN Reference Implementation by  doxygen 1.5.1