ctest
Macros | Typedefs | Functions
ctest.h File Reference
#include <stdlib.h>

Macros

#define CTEST_FIXTURE(name, type, setup_func, teardown_func)
 
#define CTEST_ADD_TEST_F(fixture_name, test)
 
#define CTEST_SUITE(name, setup_func, teardown_func)
 
#define CTEST_ADD_TEST_S(suite_name, test)
 
#define CTEST_PASS_TEST()
 
#define CTEST_FAIL_TEST()
 
#define CTEST_ADD_TEST(test_func)
 
#define CTEST_EXPECT_TRUE(EXP)
 
#define CTEST_EXPECT_FALSE(EXP)
 
#define CTEST_EXPECT_INT_EQ(EXP, VAL)
 
#define CTEST_EXPECT_INT_LT(EXP, VAL)
 
#define CTEST_EXPECT_INT_GT(EXP, VAL)
 
#define CTEST_EXPECT_FLOAT_EQ(EXP, VAL)
 
#define CTEST_EXPECT_PTR_EQ(EXPECTED, ACTUAL)
 
#define CTEST_EXPECT_PTR_NEQ(PTR1, PTR2)
 
#define CTEST_EXPECT_PTR_NULL(PTR)
 
#define CTEST_EXPECT_PTR_NOT_NULL(EXP)
 
#define CTEST_ASSERT_TRUE(EXP)
 
#define CTEST_ASSERT_FALSE(EXP)
 
#define CTEST_ASSERT_INT_EQ(EXP, VAL)
 
#define CTEST_ASSERT_INT_LT(EXP, VAL)
 
#define CTEST_ASSERT_INT_GT(EXP, VAL)
 
#define CTEST_ASSERT_FLOAT_EQ(EXP, VAL)
 
#define CTEST_ASSERT_PTR_EQ(EXPECTED, ACTUAL)
 
#define CTEST_ASSERT_PTR_NEQ(PTR1, PTR2)
 
#define CTEST_ASSERT_PTR_NULL(PTR)
 
#define CTEST_ASSERT_PTR_NOT_NULL(PTR)
 

Typedefs

typedef void(* ctest_test_func) ()
 
typedef void(* ctest_suite_func) ()
 

Functions

void ctest_init ()
 
void ctest_destroy ()
 
void ctest_config_set_filter (const char *filter_str)
 
int ctest_run ()
 

Macro Definition Documentation

◆ CTEST_ADD_TEST

#define CTEST_ADD_TEST (   test_func)

Adds a test function to the system. The test function being added should have a signature matching ctest_test_func

Parameters
suite_nameThe function that will be added. Note that for filtering purposes, the test name in the system will be the same as the added function's name.

◆ CTEST_ADD_TEST_F

#define CTEST_ADD_TEST_F (   fixture_name,
  test 
)

Adds a fixture test to the system. A Fixture tests runs after the appropriate setup method has been called and receives a freshly allocated and initialized test data structure. If a test is being added to a fixture whose type is T, the test should take a single parameter of T*. After the test returns, the fixture's teardown method will be called on the structure that was passed to the test.

Parameters
fixture_nameThe name of the fixture that this test will be added to.
testname of the function serving as the test method to be added to the system. Note that for filtering purposes, the test name in the system will be <fixture_name>.<test>

◆ CTEST_ADD_TEST_S

#define CTEST_ADD_TEST_S (   suite_name,
  test 
)

Adds a suite test to the system A suite test runs in between the setup and teardown functions defined by the suite it is a part of. The test function being added should have a signature matching ctest_test_func

Parameters
suite_nameThe name of the suite this test will be added to.
testname of the function serving as the test method to be added to the system. Note that for filtering purposes, the test name in the system will be <suite_name>.<test>

◆ CTEST_ASSERT_FALSE

#define CTEST_ASSERT_FALSE (   EXP)

Asserts that an expression evaluates to false. If it does not, the currently running test is marked as failed and the current function will return. The expression is false if it evaluates to zero.

Parameters
EXPThe expression to test.

◆ CTEST_ASSERT_FLOAT_EQ

#define CTEST_ASSERT_FLOAT_EQ (   EXP,
  VAL 
)

Asserts that an expression evaluates to an expected floating point value. If it does not, the currently running test is marked as failed and the current function will return. Note this test performs a strict equality check.

Parameters
EXPThe expression to test.
VALthe expected float value.

◆ CTEST_ASSERT_INT_EQ

#define CTEST_ASSERT_INT_EQ (   EXP,
  VAL 
)

Asserts that an expression evaluates to an expected integer value. If it does not, the currently running test is marked as failed and the current function will return.

Parameters
EXPThe expression to test.
VALthe expected integer value.

◆ CTEST_ASSERT_INT_GT

#define CTEST_ASSERT_INT_GT (   EXP,
  VAL 
)

Asserts that an expression evaluates to greater than an expected integer value. If it does not, the currently running test is marked as failed and the current function will return.

Parameters
EXPThe expression to test.
VALthe expected integer value that EXP should be greater than.

◆ CTEST_ASSERT_INT_LT

#define CTEST_ASSERT_INT_LT (   EXP,
  VAL 
)

Asserts that an expression evaluates to less than an expected integer value. If it does not, the currently running test is marked as failed and the current function will return.

Parameters
EXPThe expression to test.
VALthe expected integer value that EXP should be less than.

◆ CTEST_ASSERT_PTR_EQ

#define CTEST_ASSERT_PTR_EQ (   EXPECTED,
  ACTUAL 
)

Asserts two pointers are equal. If they are not, the currently running test is marked as failed and the current function will return.

Parameters
EXPECTEDThe expected value of the pointer.
ACTUALThe actual value of the pointer.

◆ CTEST_ASSERT_PTR_NEQ

#define CTEST_ASSERT_PTR_NEQ (   PTR1,
  PTR2 
)

Asserts two pointers are not equal. If they are equal, the currently running test is marked as failed and the current function will return.

Parameters
PTR1The first pointer.
PTR2The second pointer.

◆ CTEST_ASSERT_PTR_NOT_NULL

#define CTEST_ASSERT_PTR_NOT_NULL (   PTR)

Asserts that a pointer is not NULL. If it is, the currently running test is marked as failed and the current function will return.

Parameters
PTRThe pointer to test.

◆ CTEST_ASSERT_PTR_NULL

#define CTEST_ASSERT_PTR_NULL (   PTR)

Asserts that a pointer is NULL. If it is not, the currently running test is marked as failed and the current function will return.

Parameters
PTRThe pointer to test.

◆ CTEST_ASSERT_TRUE

#define CTEST_ASSERT_TRUE (   EXP)

Asserts that an expression evaluates to true. If it does not, the currently running test is marked as failed and the current function will return. The expression is true if it evaluates to a non-zero value.

Parameters
EXPThe expression to test.

◆ CTEST_EXPECT_FALSE

#define CTEST_EXPECT_FALSE (   EXP)

Tests that an expression evaluates to false. If it does not, the currently running test is marked as failed. The expression is false if it evaluates to zero.

Parameters
EXPThe expression to test.

◆ CTEST_EXPECT_FLOAT_EQ

#define CTEST_EXPECT_FLOAT_EQ (   EXP,
  VAL 
)

Tests that an expression evaluates to an expected floating point value. If it does not, the currently running test is marked as failed. Note this test performs a strict equality check.

Parameters
EXPThe expression to test.
VALthe expected float value.

◆ CTEST_EXPECT_INT_EQ

#define CTEST_EXPECT_INT_EQ (   EXP,
  VAL 
)

Tests that an expression evaluates to an expected integer value. If it does not, the currently running test is marked as failed.

Parameters
EXPThe expression to test.
VALthe expected integer value.

◆ CTEST_EXPECT_INT_GT

#define CTEST_EXPECT_INT_GT (   EXP,
  VAL 
)

Tests that an expression evaluates to greater than an expected integer value. If it does not, the currently running test is marked as failed.

Parameters
EXPThe expression to test.
VALthe expected integer value that EXP should be greater than.

◆ CTEST_EXPECT_INT_LT

#define CTEST_EXPECT_INT_LT (   EXP,
  VAL 
)

Tests that an expression evaluates to less than an expected integer value. If it does not, the currently running test is marked as failed.

Parameters
EXPThe expression to test.
VALthe expected integer value that EXP should be less than.

◆ CTEST_EXPECT_PTR_EQ

#define CTEST_EXPECT_PTR_EQ (   EXPECTED,
  ACTUAL 
)

Tests two pointers are equal. If they are not, the currently running test is marked as failed.

Parameters
EXPECTEDThe expected value of the pointer.
ACTUALThe actual value of the pointer.

◆ CTEST_EXPECT_PTR_NEQ

#define CTEST_EXPECT_PTR_NEQ (   PTR1,
  PTR2 
)

Tests two pointers are not equal. If they are equal, the currently running test is marked as failed.

Parameters
PTR1The first pointer.
PTR2The second pointer.

◆ CTEST_EXPECT_PTR_NOT_NULL

#define CTEST_EXPECT_PTR_NOT_NULL (   EXP)

Tests that a pointer is not NULL. If it is, the currently running test is marked as failed.

Parameters
PTRThe pointer to test.

◆ CTEST_EXPECT_PTR_NULL

#define CTEST_EXPECT_PTR_NULL (   PTR)

Tests that a pointer is NULL. If it is not, the currently running test is marked as failed.

Parameters
PTRThe pointer to test.

◆ CTEST_EXPECT_TRUE

#define CTEST_EXPECT_TRUE (   EXP)

Tests that an expression evaluates to true. If it does not, the currently running test is marked as failed. The expression is true if it evaluates to a non-zero value.

Parameters
EXPThe expression to test.

◆ CTEST_FAIL_TEST

#define CTEST_FAIL_TEST ( )

Unconditionally fails the current test.

◆ CTEST_FIXTURE

#define CTEST_FIXTURE (   name,
  type,
  setup_func,
  teardown_func 
)

Defines a new text fixture. A test fixture is useful for tests which require non trivial setup and tear down as well as operating on non global data. The system will allocate a test data struct, as well as call the appropriate setup and teardown methods for each test that uses the fixture. For a test to use a fixture with type T. It's function signature should have one parameter of type T*. A test fixture is also a good way to avoid using global data in all of your tests.

Parameters
nameThe name of the test fixture. Each fixture name must be unique.
typeThe type name of the struct which contains the fixture data. A new struct will be allocated and used for each test in the fixture
setup_funcThe name of a function which will be called before each fixture test. This function will receive a pointer to a freshly allocated struct of the type named by the name parameter. This function should initialize the members of the fixture struct to default values.
teardown_funcThe name of a function which will be called after each fixture test. This function will recieve a pointer to a struct of the type named by the name parameter. This function should free all resources used by the object, but not the object itself.

◆ CTEST_PASS_TEST

#define CTEST_PASS_TEST ( )

Unconditionally passes the current test. Assert and expect macros that fail after this method is called will not affect the status of the test.

◆ CTEST_SUITE

#define CTEST_SUITE (   name,
  setup_func,
  teardown_func 
)

Defines a new test suite. A test suite is useful for tests which require non trivial setup and tear down but do not need any test specific data, or use global data. The system will call the appropriate setup and teardown methods for each test that is in the suite.

Parameters
namethe name of this suite. Each Suite name must be unique.
setup_funcThe name of a function which will be called before each test in this suite. This function should have a signature matching ctest_suite_func
teardown_funcThe name of a function which will be called after each test in this suite. This function should have a signature matching ctest_suite_func

Typedef Documentation

◆ ctest_suite_func

typedef void(* ctest_suite_func) ()

Function definition for a suite setup or teardown function. A suite setup function is called before each test in a suite. A suite teardown function is called after each test in a suite.

◆ ctest_test_func

typedef void(* ctest_test_func) ()

Callback for a function test. A function test does not require any parameters and does not need assoicated setup or teardown functionality.

Function Documentation

◆ ctest_config_set_filter()

void ctest_config_set_filter ( const char *  filter_str)

Sets the filter string which is used to determine which tests should be run. When a filter is active, a test will only be ran if its name begins with at least one active filter. Multiple filters may be specified as a ';' separated list. Calling this method multiple times will clear out any previously set filters.

Parameters
filter_strlist of filters to apply to all tests. Multiple filters are spearated by ';'.

◆ ctest_destroy()

void ctest_destroy ( )

Releases all resources used by the testing library. After calling this methid, ctest_init should be called again before any subsequent called to library methods.

◆ ctest_init()

void ctest_init ( )

Initializes the testting framework. This method should be called once before any other library function.

◆ ctest_run()

int ctest_run ( )

Runs all tests.

Returns
zero if all tests passed, otherwise a non zero value.