Skip to main content

Embedded targets

This page covers the aspects that matter on embedded targets: the memory footprint, the stack usage, the timings, the console output, and the builds without exceptions.

Memory footprint​

The memory footprint of the tests based on µTest++ is smaller than that of the other C++ testing frameworks, primarily because the iostream library is not used. It is measured with the footprint programs of the project, in tests/sources/benchmarks/footprint, which run the same test case, with two checks, written with each framework.

The µTest++ program is:

static volatile int answer_ = 42;
static volatile bool condition_ = true;

static int
compute_answer (void)
{
return answer_;
}

static bool
compute_condition (void)
{
return condition_;
}

static mt::runner tr;

int
main (int argc, char* argv[])
{
tr.initialise (argc, argv);

auto& ts = tr.add_top_suite ();

ts.test ("Check various conditions", [] (auto& t)
{
t.expect (mt::eq (compute_answer (), 42)) << "answer is 42";
t.expect (compute_condition ()) << "condition is true";
});

return tr.run_suites ();
}

The values are volatile, so that the compiler cannot evaluate the checks at build time.

Built for the Cortex-M4F QEMU test platform, with the xPack GNU Arm Embedded GCC 15.2.1, as a release build with -Os, it requires about 46 KiB more flash than an empty main() (the baseline); of these, about 23 KiB are the functions of µTest++, and the rest are functions of the C and C++ libraries that it uses:

text data bss dec hex filename
80388 2404 9352 92144 167f0 footprint-baseline.elf
127656 2528 9728 139912 22288 footprint-micro-test-plus.elf

The baseline already takes about 81 KiB of flash on this platform, since the µOS++ startup links the formatted output of newlib and the C++ run-time support of the exceptions.

The same test written with Boost UT adds about 350 KiB.

The program with Boost UT 2.3.1, built with the same toolchain and options, is:

int
main ()
{
using namespace boost::ut;

"Check various conditions"_test = []
{
expect (eq (compute_answer (), 42)) << "answer is 42";
expect (compute_condition ()) << "condition is true";
};

// The tests run and are reported when the global runner is destroyed.
return 0;
}
text data bss dec hex filename
440452 4952 16600 462004 70cb4 footprint-boost-ut.elf

The figures of the other frameworks (Unity, CppUTest, doctest, Catch2, Boost.Test, GoogleTest, and snitch), and those of the other platforms, are in the comparison with other frameworks.

However, the use of templates for implementing comparators should be used with care on platforms with severely limited memory resources, as each unique pair of operand types contributes to the overall program size.

If necessary, µTest++ can be used without custom comparators, relying solely on standard boolean expressions, while still providing the essential functionality for testing conditions. In this configuration, the actual values compared are not displayed on failure.

info

The memory footprint in debug builds (compiled with -O0) is significantly larger than in release builds. If required, the optimisation level for the debug build may be increased to -Og to conserve memory.

Stack usage​

The test cases run on the stack of the caller: each call to test() creates the test case as a local object, and runs its body immediately, before returning. A nested test case therefore adds its call chain to the stack of the outer one, and the stack needed by a test grows with its nesting depth.

The stack needed is approximately the sum of:

  • a fixed part, for the runner, the suite, and the start of the first test case;
  • a part for each nesting level, which includes the frame of the test case body;
  • the temporary space used by the reporter at the innermost level, to write the results, plus that of the C library formatting functions.

For example, the values measured with µTest++ 6.0.1 and GCC 15.2.1, on the QEMU test platforms, with the deeply nested test of the suite sample, run with an output file, are (the AArch64 platform is built without exceptions):

Cortex-M33 debug (-O0)Cortex-M33 release (-Os)AArch64 debug (-O0)AArch64 release (-Os)
Fixed partabout 1.1 KiBabout 0.7 KiBabout 1.8 KiBabout 1.3 KiB
Each nesting level416 bytes232 bytes720 bytes432 bytes
Reporter, at the innermost level512 bytes620 bytes1024 bytes1056 bytes
vsnprintf() of newlib, below the reporter504 bytes504 bytes816 bytes768 bytes

Of each nesting level, 112 bytes (debug) and 12 bytes (release), on Cortex-M33, are the frame of the test case body, which depends on the application code: its local variables, and the calls it makes, add to these values. The arguments passed to test() after the body are copied into the frame of the test case.

The values depend on the architecture, the compiler, and the optimisation level, so they are only an indication.

On platforms with small stacks, such as the 8 KiB of the Cortex-M test platforms, deep nesting must be planned: the deeply nested test of the suite sample, with eleven levels of test cases, uses about 5.6 KiB of the stack in a Cortex-M33 debug build before the reporter writes the results of the innermost level, and about 6.6 KiB of the 8 KiB while it writes them. If the stack is not large enough, increase it, reduce the nesting depth, or build the tests with -Og, which, on Cortex-M33, needs 304 bytes for each nesting level.

caution

Most Cortex-M cores do not detect a stack overflow: the stack grows silently into the memory below it, usually the heap, and the test fails later, in an unrelated place, or not at all. The ARMv8-M Mainline cores (for example Cortex-M33) can check the stack limit, and stop with a fault when the startup code sets the limit, as on the Cortex-M33 test platform; a test that passes there fits in its stack.

Timings​

The µTest++ runner measures the time spent in each test suite and reports it in milliseconds alongside the results. The times are measured with clock_gettime (CLOCK_MONOTONIC) on the POSIX platforms, and with timespec_get () on Windows.

For example:

ok 1 - minimal-test # { passed, 1 check in 1 test case, time: 0.009 ms }

1..1
# { total: 1 check passed, 0 failed, in 1 test case, 1 test suite, time: 0.044 ms }

On the platforms that provide neither, usually the embedded ones, the times are not measured, and are omitted from the output.

Console output​

The reporters write all console output through the standard stdout stream, using vfprintf(), fwrite(), and fflush(). Output written by the tests with printf() shares the same stream and buffer, so the two appear in the expected order.

µTest++ has no output hook of its own: the output is redirected by the C library, not by the framework. On the µOS++ platforms, which provide the C library back end, the output is forwarded to the host through semihosting (see Semihosting), and nothing needs to be configured.

In other environments, to send the output to a serial port, RTT, or another debug channel, retarget the C library (for example, with newlib, implement _write()); stdout and stderr are then forwarded together, with the reporters and the whole printf() family.

Redirect stdout, not only printf(). Redirecting only printf() (for example with -Wl,--wrap=printf) is not sufficient, because the reporters write with vfprintf() and fwrite(), so the output of the tests and the output of the reporters would follow different paths and may appear out of order.

The configuration errors (a missing option argument, an unknown reporter, an output file that cannot be opened, a suite added too late) are written to stderr, so the platform should forward stderr as well. The errors of the command-line options are also written through the reporter, to the console and to the output file, which is truncated, so that it never keeps the report of an earlier run; the other errors are not written to the output file.

Semihosting​

On the µOS++ platforms, which provide the C library back end, stdout, stderr, and the file operations (fopen()) are forwarded to the host through semihosting, so the console output, the error messages, and the output file all reach the host without any further configuration.

The recommended semihosting implementation is the µOS++ semihosting xpm package, which the projects that need it add as a dependency.

Semihosting requires a debugger (such as OpenOCD) or an emulator (such as QEMU) connected to the target; without one, the first semihosting call faults, and the program stops before reporting anything. Tests that run on a board without a debugger must therefore retarget the C library to another channel, as described in Console output.

Builds without exceptions​

Embedded projects often build with -fno-exceptions, in which case __cpp_exceptions is not defined, and the framework adapts:

  • throws() and nothrow() are not available, so the checks that use them must be guarded (see Checking exceptions);
  • a failed require() cannot stop only its test case, so it aborts the whole test execution (see Requirements and Aborting the test execution);
  • the results of the aborted test case are displayed also with --quiet, since the totals are never reached (see Verbosity).