Unit Testing

Libstephen: Unit Testing.

Author
Stephen Brennan
Date
Created Sunday, 3 August 2014
Copyright
Copyright (c) 2013-2016, Stephen Brennan. Released under the Revised BSD License. See the LICENSE.txt file for details.

Defines

SMB_UNIT_DESCRIPTION_SIZE

The size of a description text field (in characters) for an smbunit test.

SMB_UNIT_TESTS_PER_GROUP

The max number of unit tests in a single test group.

TEST_ASSERT(expr)

Asserts that an expression is true. If false, returns line number.

Note that this is a macro, so using some things (++ and – operators especially) can have unintended results. As a general rule, do not put any complicated code inside the TEST_ASSERT() statement at all.

# PARAMETERS #

  • expr: The expression to check. If the expression evaluates to true (that is, not 0), the assertion passes. If the expression evaluates to false (that is, zero), the assertion fails.

_ASSERTION_BASE_(X, Y, SPEC, TYPE, INV)
TA_PTR_EQ(X, Y)
TA_PTR_NE(X, Y)
TA_PTR_LT(X, Y)
TA_PTR_GT(X, Y)
TA_PTR_LE(X, Y)
TA_PTR_GE(X, Y)
TA_SIZE_EQ(X, Y)
TA_SIZE_NE(X, Y)
TA_SIZE_LT(X, Y)
TA_SIZE_GT(X, Y)
TA_SIZE_LE(X, Y)
TA_SIZE_GE(X, Y)
TA_INT_EQ(X, Y)
TA_INT_NE(X, Y)
TA_INT_LT(X, Y)
TA_INT_GT(X, Y)
TA_INT_LE(X, Y)
TA_INT_GE(X, Y)
TA_LLINT_EQ(X, Y)
TA_LLINT_NE(X, Y)
TA_LLINT_LT(X, Y)
TA_LLINT_GT(X, Y)
TA_LLINT_LE(X, Y)
TA_LLINT_GE(X, Y)
TA_UINT_EQ(X, Y)
TA_UINT_NE(X, Y)
TA_UINT_LT(X, Y)
TA_UINT_GT(X, Y)
TA_UINT_LE(X, Y)
TA_UINT_GE(X, Y)
TA_LLUINT_EQ(X, Y)
TA_LLUINT_NE(X, Y)
TA_LLUINT_LT(X, Y)
TA_LLUINT_GT(X, Y)
TA_LLUINT_LE(X, Y)
TA_LLUINT_GE(X, Y)
TA_FLT_EQ(X, Y)
TA_FLT_NE(X, Y)
TA_FLT_LT(X, Y)
TA_FLT_GT(X, Y)
TA_FLT_LE(X, Y)
TA_FLT_GE(X, Y)
TA_CHAR_EQ(X, Y)
TA_CHAR_NE(X, Y)
TA_CHAR_LT(X, Y)
TA_CHAR_GT(X, Y)
TA_CHAR_LE(X, Y)
TA_CHAR_GE(X, Y)
TA_WCHAR_EQ(X, Y)
TA_WCHAR_NE(X, Y)
TA_WCHAR_LT(X, Y)
TA_WCHAR_GT(X, Y)
TA_WCHAR_LE(X, Y)
TA_WCHAR_GE(X, Y)
TA_STR_EQ(x, y)
TA_STR_NE(x, y)
TA_STRN_EQ(x, y, n)
TA_STRN_NE(x, y, n)
TA_WSTR_EQ(x, y)
TA_WSTR_NE(x, y)
TA_WSTRN_EQ(x, y, n)
TA_WSTRN_NE(x, y, n)

Typedefs

typedef smb_ut_test

Defines a single unit test.

Members should be modified with care, preferably not at all, except by using the smbunit functions.

typedef smb_ut_group

A structure holding a group of unit tests that are all related.

Members shouldn’t me modified by client code. All should be managed by the functions in smbunit.

Functions

smb_ut_test* su_create_test(char * description, int(*run)())

Create and return a new unit test.

Return
A pointer to the new test.
Parameters
  • description: A description of the test.
  • run: A function pointer to the test function.

smb_ut_group* su_create_test_group(char * description)

Create and return a new test group.

Return
A pointer to the test group.
Parameters
  • description: A short description for the group.

void su_add_test(smb_ut_group * group, smb_ut_test * test)

Add a test to the given test group.

A maximum of SMB_UNIT_TESTS_PER_GROUP may be added to the group. After the limit is reached, this function fails *silently*, so as to prevent interference with the actual tests.

Parameters
  • group: A pointer to the group to add the test to.
  • test: A pointer to the test.

int su_run_test(smb_ut_test * test, char * file)

Run the given test.

This is pretty simple, it runs the test and returns the error code (which should be a line number). If there was an error, it prints the code in the standard “file:lineno” way so that Emacs will jump right to the failed assertion.

Return
The line number of the failed assertion, or 0 if none occurred.
Parameters
  • test: The test to run
  • file: The filename of the test (used for the error message).

int su_run_group(smb_ut_group * group)

Run a group of tests.

The tests are run sequentially (in the order they were added to the group). If a test fails, the remaining tests are not executed.

Return
An integer. Since the tests are run sequentially via the su_run_test() function, it returns 0 if all tests succeeded, or else the return code of the failed test from su_run_test().
Parameters

void su_delete_test(smb_ut_test * test)

Frees the memory associated with the test, and performs cleanup.

Note that no actual cleanup is required by the test, so the only benefit to using this function is that it is future-safe (updates to smbunit may require cleanup to be performed in this function).

Parameters
  • test: The test to free

void su_delete_group(smb_ut_group * group)

Free the memory associated with the group AND ALL TESTS WITHIN IT. You MUST use this to delete test groups.

Note that if a pointer to a smb_ut_test within the smb_ut_group is already invalid (freed), then su_delete_group() assumes that it has been freed and moves on. So you may include a single test in more than one group and safely delete them both (but after deleting the first group, the test will no longer be valid and a segmentation fault will occur if you try to run the second group).

Parameters
  • group: A pointer to the group to free

struct smb_ut_test
#include <ut.h>

Defines a single unit test.

Members should be modified with care, preferably not at all, except by using the smbunit functions.

Public Members

char description[SMB_UNIT_DESCRIPTION_SIZE]

A 20 character null-terminated string that identifies this particular test.

int(* run)

Function pointer to the test to run. The function should return 0 if the test is successful.

int expected_errors

The error value of any expected errors from this test.

struct smb_ut_group
#include <ut.h>

A structure holding a group of unit tests that are all related.

Members shouldn’t me modified by client code. All should be managed by the functions in smbunit.

Public Members

char description[SMB_UNIT_DESCRIPTION_SIZE]

A short description (length defined by SMB_UNIT_DESCRIPTION_SIZE) for the test.

int num_tests

The number of tests in the group.

smb_ut_test* tests[SMB_UNIT_TESTS_PER_GROUP]

Pointers to the tests contained. Max amount of tests is SMB_UNIT_TESTS_PER_GROUP.