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 <cstring>
#include <string>
#include <vector>
#include <stdio.h>
#include <errno.h>
#include "../debug/FatalSignals.h"
#include "../debug/Log.h"
#include "../io/PrettyPrintBuffer.h"
#include "../util/Random.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 RUN_TESTER_NO_LOG(_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_SYS(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 279 of file UnitTest.h.

#define CHECK (  ) 

Value:

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

Definition at line 322 of file UnitTest.h.

#define CHECK_EQUAL ( _a,
_b   ) 

Value:

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

Definition at line 350 of file UnitTest.h.

#define CHECK_EQUAL_U64 ( a,
 ) 

Value:

do { if ((a) != (b)) {                                                              \
        errno_ = errno;                                                                 \
        strerror_ = strerror(errno_);                                                   \
        ::oasys::Breaker::break_here();                                                 \
        log_err_p("/test",                                                              \
                    "CHECK FAILED: '" #a "' (%llu) != '" #b "' (%llu) at %s:%d",        \
                    (long long unsigned int)(a),                                        \
                    (long long unsigned int)(b),                                        \
                    __FILE__, __LINE__);                                                \
        return oasys::UNIT_TEST_FAILED;                                                 \
    } else {                                                                            \
        log_notice_p("/test",                                                           \
                    "CHECK '" #a "' (%llu) == '" #b "' (%llu) "                         \
                    "at %s:%d",                                                         \
                    (long long unsigned int)(a),                                        \
                    (long long unsigned int)(b),                                        \
                    __FILE__, __LINE__);                                                \
    } } while(0)
Helper macros for implementing unit tests.

Definition at line 425 of file UnitTest.h.

#define CHECK_EQUALSTR ( a,
 ) 

Helper macros for implementing unit tests.

Definition at line 445 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) {         \
             errno_ = errno;                                                    \
             strerror_ = strerror(errno_);                                      \
             ::oasys::Breaker::break_here();                                    \
             log_err_p("/test",  "CHECK FAILED: "                               \
                         "'" #a "' (%.*s...) != '" #b "' (%.*s...) at %s:%d",   \
                         print_len, (a), print_len, (b),                        \
                         __FILE__, __LINE__);                                   \
             return oasys::UNIT_TEST_FAILED;                                    \
         } else {                                                               \
             log_notice_p("/test",                                              \
                         "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 479 of file UnitTest.h.

#define CHECK_GT ( _a,
_b   ) 

Value:

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

Definition at line 380 of file UnitTest.h.

#define CHECK_GTU ( _a,
_b   ) 

Value:

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

Definition at line 410 of file UnitTest.h.

#define CHECK_LT ( _a,
_b   ) 

Value:

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

Definition at line 365 of file UnitTest.h.

#define CHECK_LTU ( _a,
_b   ) 

Value:

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

Definition at line 395 of file UnitTest.h.

#define CHECK_SYS (  ) 

Value:

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

Definition at line 336 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 282 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 301 of file UnitTest.h.

#define DECLARE_TESTER ( _name   ) 

Value:

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

Definition at line 306 of file UnitTest.h.

#define DO (  ) 

Value:

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

Definition at line 315 of file UnitTest.h.

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

Value:

_UnitTesterClass test(testname);                            \
    test.init(argc, argv, true);                                \
    int _ret = test.run_tests();                                \
    return _ret;
Helper macros for implementing unit tests.

Definition at line 289 of file UnitTest.h.

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

Value:

_UnitTesterClass test(testname);                              \
    test.init(argc, argv, false);                                 \
    int _ret = test.run_tests();                                  \
    return _ret;
Helper macros for implementing unit tests.

Definition at line 295 of file UnitTest.h.


Generated on Thu Jun 7 12:54:31 2007 for DTN Reference Implementation by  doxygen 1.5.1