Skip to content
synesissoftwarePublic

About

Brain-Dead Unit-Testing: extremely lightweight, single-header unit-testing for C and C++

Topics

Resources

Contributing

Stars

3 stars

Watchers

2 watching

Forks

Latest commit

 

History

39 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BDUT

Brain-Dead Unit-Testing, extremely lightweight, single-header unit-testing for C and C++.

C C++ License GitHub release Last Commit CI

Table of Contents

Introduction

Brain-Dead Unit-Testing, is a very simple - simplistic, in fact - small, header-only, standalone library for C and C++. Its intent is to be bundled into other projects for which it is not desired to couple to a more sophisticated library.

Installation

BDUT is a single-header library with no dependencies. You can use it by copying the header into your project, or by installing it via CMake and using find_package(). See INSTALL.md for full build, test, and install instructions.

Manual installation

Since BDUT is intended to be bundled into other open-source libraries, the simplest way to use it is to add a directory bdut under your project's include directory, into which you can then simply copy bdut.h from include/bdut/bdut.h in this distribution.

Your consumer code then uses:

#include <bdut/bdut.h>

CMake installation

BDUT provides CMake 3.20+ support: an INTERFACE library target, install rules, and an exported bdut-config.cmake package so downstream projects can use find_package(BDUT).

Quick start (from a clone of this repository):

./prepare_cmake.sh -m
sudo cmake --install ${SIS_CMAKE_BUILD_DIR:-./_build} --config Release

After installation, a CMake consumer can link against BDUT as follows:

find_package(BDUT REQUIRED)

add_executable(my_tests main.c)
target_link_libraries(my_tests PRIVATE BDUT::BDUT)

Because BDUT is header-only, no library file is linked; the imported target supplies the include path. See INSTALL.md for find_package, add_subdirectory, FetchContent, vcpkg, configure, build, test, and install options.

API reference

Include the header once:

#include <bdut/bdut.h>

Write tests as sequential assertions in main(). When all pass, return BDUT_TESTS_PASSED(argc, argv).

Macro Description
BDUT_ASSERT_TRUE(expr) Asserts that expr is true
BDUT_ASSERT_TRUE_MSG(expr, message) Same, then appends message
BDUT_ASSERT_FALSE(expr) Asserts that expr is false
BDUT_ASSERT_FALSE_MSG(expr, message) Same, then appends message
BDUT_ASSERT_EQ(expected, actual) Asserts actual == expected
BDUT_ASSERT_EQ_MSG(expected, actual, message) Same, then appends message
BDUT_ASSERT_NE(expected, actual) Asserts actual != expected
BDUT_ASSERT_NE_MSG(expected, actual, message) Same, then appends message
BDUT_ASSERT_GE(expected, actual) Asserts actual >= expected
BDUT_ASSERT_GE_MSG(expected, actual, message) Same, then appends message
BDUT_ASSERT_GT(expected, actual) Asserts actual > expected
BDUT_ASSERT_GT_MSG(expected, actual, message) Same, then appends message
BDUT_ASSERT_LE(expected, actual) Asserts actual <= expected
BDUT_ASSERT_LE_MSG(expected, actual, message) Same, then appends message
BDUT_ASSERT_LT(expected, actual) Asserts actual < expected
BDUT_ASSERT_LT_MSG(expected, actual, message) Same, then appends message
BDUT_ASSERT_PTR_EQ(expected, actual) Asserts actual and expected are the same address
BDUT_ASSERT_PTR_EQ_MSG(expected, actual, message) Same, then appends message
BDUT_ASSERT_PTR_NE(expected, actual) Asserts actual and expected are different addresses
BDUT_ASSERT_PTR_NE_MSG(expected, actual, message) Same, then appends message
BDUT_ASSERT_FP_EQ(expected, actual) Asserts approximate equality (stock margin/multiplier)
BDUT_ASSERT_FP_EQ_MSG(expected, actual, message) Same, then appends message
BDUT_ASSERT_FP_EQ_MARGIN(expected, actual, margin_factor) Asserts absolute-margin approximate equality
BDUT_ASSERT_FP_EQ_MARGIN_MSG(expected, actual, margin_factor, message) Same, then appends message
BDUT_ASSERT_FP_EQ_MULTIPLIER(expected, actual, multiplier_factor) Asserts relative-multiplier approximate equality
BDUT_ASSERT_FP_EQ_MULTIPLIER_MSG(expected, actual, multiplier_factor, message) Same, then appends message
BDUT_ASSERT_FP_EQ_ZERO_MARGIN_OR_MULTIPLIER(expected, actual, multiplier_factor, zero_margin_factor) Multiplier, or margin when either is zero
BDUT_ASSERT_FP_EQ_ZERO_MARGIN_OR_MULTIPLIER_MSG(expected, actual, multiplier_factor, zero_margin_factor, message) Same, then appends message
BDUT_ASSERT_STRING_CONTAINS(needle, haystack) Asserts needle is a substring of haystack
BDUT_ASSERT_STRING_CONTAINS_MSG(needle, haystack, message) Same, then appends message
BDUT_ASSERT_STRING_EQUAL(expected, actual) Asserts strcmp(expected, actual) == 0
BDUT_ASSERT_STRING_EQUAL_MSG(expected, actual, message) Same, then appends message
BDUT_ASSERT_STRING_EQUAL_N(expected, actual, n) Asserts strncmp(expected, actual, n) == 0
BDUT_ASSERT_STRING_EQUAL_N_MSG(expected, actual, n, message) Same, then appends message
BDUT_TESTS_PASSED(argc, argv) Call after all assertions pass; prints success and returns 0

On failure, BDUT prints file, line, optional function name, and a message to stderr, then calls exit(1). Each _MSG macro takes a char const* message and appends it after the standard text. NULL or "" leaves that text unchanged. String comparisons treat a NULL pointer as an empty string. BDUT_ASSERT_STRING_EQUAL_N with a count of 0 passes. BDUT_ASSERT_PTR_EQ and BDUT_ASSERT_PTR_NE compare object addresses, including NULL. Floating-point approximate equality uses an absolute margin or a relative multiplier (from test_help-rs); BDUT_ASSERT_FP_EQ uses a multiplier of BDUT_FP_DEFAULT_MULTIPLIER (0.000001), or a margin of BDUT_FP_DEFAULT_MARGIN (0.0001) when either operand is zero.

Version macros (BDUT_VER_MAJOR, BDUT_VER_MINOR, BDUT_VER_PATCH, BDUT_VER, etc.) are defined in include/bdut/bdut.h.

For a full index of sample programs (passing and intentional failures), see EXAMPLES.md.

Examples

BDUT is included into any C or C++ project by #includeing its header file and invoking assertion macros. Call BDUT_TESTS_PASSED(argc, argv) at the end of main() when all assertions have passed.

Passing example

The following program (adapted from test/scratch/test.scratch.all_pass) exercises several macros and completes successfully:

/* test/scratch/test.scratch.all_pass/main.c */

#include <bdut/bdut.h>

int main(int argc, char* argv[])
{
  BDUT_ASSERT_EQ(123, 123);
  BDUT_ASSERT_EQ_MSG(123, 123, "ok");
  BDUT_ASSERT_NE(123, 321);

  BDUT_ASSERT_LE(123, 123);
  BDUT_ASSERT_GE(123, 123);

  BDUT_ASSERT_STRING_CONTAINS("abc", "abcdef");
  BDUT_ASSERT_STRING_EQUAL("abc", "abc");

  return BDUT_TESTS_PASSED(argc, argv);
}

On success, output is along the lines of:

my_tests: ALL TESTS PASSED

Failure output examples

The examples/ programs under this repository are deliberately written to fail, in order to illustrate assertion output. For example:

/* examples/c/example.c.BDUT_ASSERT_EQ/main.c */

#include <bdut/bdut.h>

int main(int argc, char* argv[])
{
    int const actual1 = 1;
    int const actual2 = 2;

    BDUT_ASSERT_EQ(1, 1);
    BDUT_ASSERT_EQ(1, actual1);
    BDUT_ASSERT_EQ_MSG(1, actual1, "match");

    /* this one will fail */
    BDUT_ASSERT_EQ_MSG(1, actual2, "intentional failure");

    return BDUT_TESTS_PASSED(argc, argv);
}

The output of this will be along the lines of:

~/open-source/BDUT/examples/c/example.c.BDUT_ASSERT_EQ/main.c:14:main: assertion failed: actual value of `actual2` not equal-to expected value `1`: intentional failure (expected 1, actual 2)

Similarly, examples/cpp/example.cpp.BDUT_ASSERT_STRING_CONTAINS demonstrates string containment failure:

/* examples/cpp/example.cpp.BDUT_ASSERT_STRING_CONTAINS/main.cpp */

#include <bdut/bdut.h>

int main(int argc, char* argv[])
{
    char const* const actual_abc = "abc";
    char const* const actual_abd = "abd";

    BDUT_ASSERT_STRING_CONTAINS("", "abcdefghijklmnopqrstuvwxyz");
    BDUT_ASSERT_STRING_CONTAINS(actual_abc, "abcdefghijklmnopqrstuvwxyz");
    BDUT_ASSERT_STRING_CONTAINS_MSG(actual_abc, "abcdefghijklmnopqrstuvwxyz", "match");

    /* this one will fail */
    BDUT_ASSERT_STRING_CONTAINS_MSG(actual_abd, "abcdefghijklmnopqrstuvwxyz", "intentional failure");

    return BDUT_TESTS_PASSED(argc, argv);
}

The output of this will be along the lines of:

~/open-source/BDUT/examples/cpp/example.cpp.BDUT_ASSERT_STRING_CONTAINS/main.cpp:14:main: assertion failed: actual string 'abcdefghijklmnopqrstuvwxyz' does not contain search string 'abd': intentional failure

Project Information

Where to get help

GitHub Page

Contribution guidelines

Defect reports, feature requests, and pull requests are welcome on https://github.com/synesissoftware/BDUT. See CONTRIBUTING.md for development setup, coding standards, and pull request expectations.

Dependencies

BDUT has no dependencies.

Related projects

Projects in which BDUT is used for testing include:

License

BDUT is released under the 3-clause BSD license. See LICENSE for details.

About

Brain-Dead Unit-Testing: extremely lightweight, single-header unit-testing for C and C++

Topics

Resources

Contributing

Stars

3 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages