Skip to main content

test-suite.cpp File

C++ source file with implementations for the µTest++ test suite methods. More...

Included Headers

#include <micro-os-plus/micro-test-plus.h> #include <stdio.h>

Namespaces Index

namespacemicro_os_plus

The primary namespace for the µOS++ framework. More...

namespacemicro_test_plus

Primary namespace for the µTest++ testing framework. More...

Description

C++ source file with implementations for the µTest++ test suite methods.

This source file contains the core implementations for the test suite facilities of the µTest++ framework. It provides the logic for constructing, registering, and managing test suites and their associated test cases. The implementation covers initialisation and clean-up routines, execution of test suites and test cases, tracking of successful and failed checks, and integration with the test reporter for structured output.

The design ensures that test suites are non-copyable and non-movable, maintaining unique ownership and consistent state. Flexible support for callable objects enables a wide range of test suite definitions, facilitating expressive and maintainable test organisation across embedded and general C++ projects.

All definitions reside within the micro_os_plus::micro_test_plus namespace, ensuring clear separation from user code and minimising the risk of naming conflicts.

This file must be included when building the µTest++ library.

File Listing

The file content with the documentation metadata removed is:

1/*
2 * This file is part of the µOS++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2021-2026 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can be
9 * obtained from https://opensource.org/licenses/mit.
10 *
11 * Major parts of the code are inspired from v1.1.8 of the Boost UT project,
12 * released under the terms of the Boost Version 1 Software License,
13 * which can be obtained from https://www.boost.org/LICENSE_1_0.txt.
14 */
15
16// ----------------------------------------------------------------------------
17
43
44// ----------------------------------------------------------------------------
45
46#if defined(MICRO_OS_PLUS_INCLUDE_CONFIG_H)
47#include <micro-os-plus/config.h>
48#endif // MICRO_OS_PLUS_INCLUDE_CONFIG_H
49
51
52#include <stdio.h>
53
54// ----------------------------------------------------------------------------
55
56#if defined(__clang__)
57#pragma clang diagnostic ignored "-Wc++98-compat"
58#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
59#endif
60
62{
63 // --------------------------------------------------------------------------
64
76 {
77#if defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS)
78 printf ("%s\n", __PRETTY_FUNCTION__);
79#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS
80
81 name_ = name;
82 // The default test suite needs no registration.
83 }
84
93 {
94 }
95
105 void
107 {
108#if defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS)
109 printf ("%s\n", __PRETTY_FUNCTION__);
110#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS
111 }
112
121 void
123 {
124#if defined(_WIN32) || defined(CLOCK_MONOTONIC)
125#if defined(_WIN32)
126 timespec_get (&begin_time, TIME_UTC);
127#else
128 clock_gettime (CLOCK_MONOTONIC, &begin_time);
129#endif
130#endif
131
133
134 reporter->begin_test_suite (name_);
135 }
136
146 void
148 {
150 {
151 reporter->begin_test (runner.test_suites_count ());
152
154 }
155
156#if defined(_WIN32) || defined(CLOCK_MONOTONIC)
157#if defined(_WIN32)
158 timespec_get (&end_time, TIME_UTC);
159#else
160 clock_gettime (CLOCK_MONOTONIC, &end_time);
161#endif
162#endif
163 reporter->end_test_suite (*this);
164 }
165
176 void
178 {
180 {
181 reporter->begin_test (runner.test_suites_count ());
182
184 }
185
188
190
191 reporter->begin_test_case (test_case_name_);
192 }
193
202 void
204 {
205 reporter->end_test_case (test_case_name_);
206 }
207
215 void
217 {
219 ++current_test_case.successful_checks;
220 }
221
229 void
231 {
233 ++current_test_case.failed_checks;
234 }
235
236#if defined(_WIN32) || defined(CLOCK_MONOTONIC)
244#pragma GCC diagnostic push
245#pragma GCC diagnostic ignored "-Wshadow"
246 void
247 test_suite_base::compute_elapsed_time (timespec& begin_time,
248 timespec& end_time,
249 long& milliseconds,
250 long& microseconds)
251 {
252 long long delta_ns = end_time.tv_nsec - begin_time.tv_nsec;
253 long long delta_s = end_time.tv_sec - begin_time.tv_sec;
254 if (delta_ns < 0)
255 {
256 delta_ns += 1000000000LL;
257 --delta_s;
258 }
259
260 // Split into milliseconds and microseconds.
261 const long long total_us = delta_s * 1000000LL + delta_ns / 1000LL;
262 milliseconds = static_cast<long> (total_us / 1000LL);
263 microseconds = static_cast<long> (total_us % 1000LL);
264 }
265#pragma GCC diagnostic pop
266
267#endif
268
269 // ==========================================================================
270
279 void
281 {
282 // Run the test suite function prepared with std::bin();
283 callable_ ();
284 }
285
295 {
296#if defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS)
297 printf ("%s\n", __PRETTY_FUNCTION__);
298#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS
299 }
300
301 // --------------------------------------------------------------------------
302} // namespace micro_os_plus::micro_test_plus
303
304// ----------------------------------------------------------------------------

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.14.0.