CUT_ASSERT


Basic assertion macro.

#define CUT_ASSERT(condition) \ 
    do { \ 
    ++CUT_assertions_count; \ 
    if (!(condition)) { \ 
    fprintf(stderr, \ 
    "%s:%d: error: Test %s%s%s failed : %s\n", \ 
    __FILE__, \ 
    __LINE__, \ 
    CUT_curr_suite, \ 
    (CUT_curr_suite[0]=='\0' ? "":":"), \ 
    __func__, \ 
    #condition); \ 
    return 1; \ 
    } \ 
    } while (0) 
Parameters
condition
An expression that should evaluate to anything non-false on success and false on failure.
Discussion

The basic CUnitTest assertion macro prints the file, line number, and reason for failure if the passed expression is false.


CUT_ASSERT_DOUBLES_EQUAL


Assertion macro.

#define CUT_ASSERT_DOUBLES_EQUAL(expected, actual, delta) \ 
    CUT_ASSERT((CUT_MAX(expected, actual) - CUT_MIN(expected, actual)) <= delta) 
Parameters
expected
An expression that evaluates to the expected value.
actual
An expression that evaluates to the actual value.
delta
The maximum allowable tolerence when deciding whether the actual and expected values are equal.
Discussion

The assertion macro prints the file, line number, reason for failure and custom message if the actual value does not equal the expected value within the given tolerence.


CUT_ASSERT_EQUAL


Assertion macro.

#define CUT_ASSERT_EQUAL(expected, actual) CUT_ASSERT(expected == actual) 
Parameters
expected
An expression that evaluates to the expected value.
actual
An expression that evaluates to the actual value.
Discussion

The macro prints the file, line number, and reason for failure if the expected value does not equal the actual value. This should not normally be used for floats or doubles as two the values will only be considered equal if they have exactly the same binary representation, which might be unlikely due to the impreciseness of floating-point representations. To compare floats or doubles, use CUT_ASSERT_DOUBLES_EQUAL instead.


CUT_ASSERT_EQUAL_MESSAGE


Assertion macro with custom message.

#define CUT_ASSERT_EQUAL_MESSAGE(message, expected, actual) \ 
    CUT_ASSERT(message, expected == actual) 
Parameters
message
A string containing a custom message to print if the assertion fails.
expected
An expression that evaluates to the expected value.
actual
An expression that evaluates to the actual value.
Discussion

The assertion macro prints the file, line number, reason for failure and custom message if the actual value does not equal the expected value.


CUT_ASSERT_FALSE


Assertion macro.

#define CUT_ASSERT_FALSE(condition) CUT_ASSERT(!condition) 
Parameters
condition
An expression that should evaluate to false on success and true on failure.
Discussion

The macro prints the file, line number, and reason for failure if the passed expression is true.


CUT_ASSERT_MESSAGE


Basic assertion macro with custom message.

#define CUT_ASSERT_MESSAGE(message, condition) \ 
    do { \ 
    ++CUT_assertions_count; \ 
    if (!(condition)) { \ 
    fprintf(stderr, \ 
    "%s:%d: error: Test %s%s%s failed : %s. %s\n", \ 
    __FILE__, \ 
    __LINE__, \ 
    CUT_curr_suite, \ 
    (CUT_curr_suite[0]=='\0' ? "":":"), \ 
    __func__, \ 
    #condition, \ 
    message); \ 
    return 1; \ 
    } \ 
    } while (0) 
Parameters
message
A string containing a custom message to print if the assertion fails.
condition
An expression that should evaluate to anything non-false on success and false on failure.
Discussion

The assertion macro prints the file, line number, reason for failure and custom message if the passed expression is false.


CUT_ASSERT_NOT_EQUAL


Assertion macro.

#define CUT_ASSERT_NOT_EQUAL(expected, actual) CUT_ASSERT(expected != actual) 
Parameters
expected
An expression that evaluates to the expected value.
actual
An expression that evaluates to the actual value.
Discussion

The macro prints the file, line number, and reason for failure if the expected value equals the actual value.


CUT_ASSERT_NOT_EQUAL_MESSAGE


Assertion macro with custom message.

#define CUT_ASSERT_NOT_EQUAL_MESSAGE(message, expected, actual) \ 
    CUT_ASSERT(message, expected != actual) 
Parameters
message
A string containing a custom message to print if the assertion fails.
expected
An expression that evaluates to the expected value.
actual
An expression that evaluates to the actual value.
Discussion

The assertion macro prints the file, line number, reason for failure and custom message if the actual value equals the expected value.


CUT_ASSERT_NOT_NULL


Assertion macro.

#define CUT_ASSERT_NOT_NULL(val) CUT_ASSERT(val!=NULL) 
Parameters
val
An expression that should evaluate to something other than NULL on success and NULL on failure.
Discussion

The macro prints the file, line number, and reason for failure if the passed expression is NULL.


CUT_ASSERT_NULL


Assertion macro.

#define CUT_ASSERT_NULL(val) CUT_ASSERT(val==NULL) 
Parameters
val
An expression that should evaluate to NULL on success and anything but NULL on failure.
Discussion

The macro prints the file, line number, and reason for failure if the passed expression is something other than NULL.


CUT_ASSERT_TRUE


Assertion macro.

#define CUT_ASSERT_TRUE(condition) CUT_ASSERT(condition) 
Parameters
condition
An expression that should evaluate to true on success and false on failure.
Discussion

The macro prints the file, line number, and reason for failure if the passed expression is false.


CUT_BEGIN_SUITE


Begin test suite definition.

#define CUT_BEGIN_SUITE(name) CUT_BEGIN_SUITE_F(name, NULL, NULL) 
Parameters
name
Name of the test suite.
Discussion

Beginning of the definition of test suite without fixtures. Creates a function to run the tests in the suite definition.


CUT_BEGIN_SUITE_F


Begin test suite definition with fixtures.

#define CUT_BEGIN_SUITE_F(name, setup, teardown) \ 
    void CUT_SUITE_ ## name(void) { \ 
    CUT_suite_tests_run=0; \ 
    CUT_suite_tests_failed=0; \ 
    CUT_curr_suite=#name; \ 
    CUT_fixture_setup=setup; \ 
    CUT_fixture_teardown=teardown; \ 
    int CUT_assertions_count_init = CUT_assertions_count; 
Parameters
name
Name of the test suite.
setup
Name of the fixture setup function.
teardown
Name of the fixture teardown function.
Discussion

Beginning of the definition of test suite with fixtures. Creates a function to run the tests in the suite definition. Before each test, the setup function is run to initialise any global variables required by the test function. After the test is run, the teardown function is called to reset and free any global variables used by the test. The fixture functions both have void return types and arguments. Either, or both the fixture function names may be NULL if you don't want them to run.


CUT_BEGIN_TEST_HARNESS


Begin test harness definition.

#define CUT_BEGIN_TEST_HARNESS _CUT_INIT; int main(void) { 
Discussion

Beginning of the definition of the test harness. Basically creates a main() function to run the specified tests/suite. Bad things will happen if this is called more than once!


CUT_END_SUITE


End test suite definition.

#define CUT_END_SUITE \ 
    fprintf(stdout, \ 
    "%s: tests-run: %d, assertions: %d, passes: %d, failures: %d\n", \ 
    CUT_curr_suite, \ 
    CUT_suite_tests_run, \ 
    (CUT_assertions_count-CUT_assertions_count_init), \ 
    CUT_suite_tests_run-CUT_suite_tests_failed, \ 
    CUT_suite_tests_failed); \ 
    CUT_curr_suite=CUT_NO_SUITE; \ 
    } 
Discussion

End of a test suite definition. Completes the function started by the corresponding CUT_BEGIN_SUITE definition and prints statistics of tests in the suite.


CUT_END_SUITE_F


End test suite definition with fixtures.

#define CUT_END_SUITE_F \ 
    CUT_fixture_setup=NULL; \ 
    CUT_fixture_teardown=NULL; \ 
    CUT_END_SUITE 
Discussion

End of a test suite definition with fixtures. Completes the function started by the corresponding CUT_BEGIN_SUITE_F definition, cleans up the internal fixture pointers and prints statistics of tests in the suite.


CUT_END_TEST_HARNESS


End test harness definition.

#define CUT_END_TEST_HARNESS \ 
    fprintf(stdout, \ 
    "%s: tests-run: %d, assertions: %d, passes: %d, failures: %d\n", \ 
    "Overall Results", \ 
    CUT_tests_run, \ 
    CUT_assertions_count, \ 
    CUT_tests_run-CUT_tests_failed, \ 
    CUT_tests_failed); \ 
    return CUT_tests_failed; \ 
    } 
Discussion

Ending of the definition of the test harness. Completes the main() function created by CUT_BEGIN_TEST_HARNESS and prints the overall test statistics.


CUT_FAIL


Failure macro with custom message.

#define CUT_FAIL(message) \ 
    do { \ 
    ++CUT_assertions_count; \ 
    fprintf(stderr, \ 
    "%s:%d: error: Test %s%s%s failed : %s. %s\n", \ 
    __FILE__, \ 
    __LINE__, \ 
    CUT_curr_suite, \ 
    (CUT_curr_suite[0]=='\0' ? "":":"), \ 
    __func__, \ 
    "CUT_FAIL()", \ 
    message); \ 
    return 1; \ 
    } while (0) 
Parameters
message
A string containing a custom message to print.
Discussion

The failure macro prints the file, line number, and custom message.


CUT_MAX


Maximum.

#define CUT_MAX(a,b)  
Parameters
a
An argument
b
Another argument
Discussion

Returns the larger of the two values.


CUT_MIN


Mimimum.

#define CUT_MIN(a,b)  
Parameters
a
An argument
b
Another argument
Discussion

Returns the smaller of the two values.


CUT_RUN_SUITE_TESTS


Suite test runner.

#define CUT_RUN_SUITE_TESTS(name) \ 
    do { \ 
    extern void CUT_SUITE_ ## name(void); \ 
    CUT_SUITE_ ## name(); \ 
    } while(0) 
Parameters
name
The name of the test suite to run.
Discussion

Runs the suite specified by name, accumulating statistics based on the outcome of the tests in the suite.


CUT_RUN_TEST


Test runner.

#define CUT_RUN_TEST(test) \ 
    do { \ 
    extern int test(void); \ 
    if (CUT_fixture_setup) CUT_fixture_setup(); \ 
    int f = test(); \ 
    if (CUT_fixture_teardown) CUT_fixture_teardown(); \ 
    CUT_suite_tests_run++; \ 
    CUT_suite_tests_failed += f; \ 
    CUT_tests_run++; \ 
    CUT_tests_failed += f; \ 
    } while (0) 
Parameters
test
The name of the test function to run. The function is assumed to have the following signature: int test_name(void). The function should return 0.
Discussion

Runs a single unit test using the given function and accumulates statistics based on the returned value. By default, the test function must return 0 if no errors occur during its execution. If an error does occur, the assertion macro that detects the error will return a different value.

© 2007 University of Southampton. All rights reserved. Last Updated: March 20, 2007