Skip to main content

test.cpp File

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

Included Headers

Namespaces Index

namespacemicro_os_plus

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

namespacemicro_test_plus

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

namespacedetail

Internal implementation details for the µTest++ 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
50#if defined(MICRO_OS_PLUS_TRACE)
51#include <micro-os-plus/diag/trace.h>
52#endif // MICRO_OS_PLUS_TRACE
53
56
57// ----------------------------------------------------------------------------
58
59#if defined(__GNUC__)
60#if defined(__clang__)
61#pragma clang diagnostic ignored "-Wc++98-compat"
62#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
63#else // GCC only
64#pragma GCC diagnostic ignored "-Wredundant-tags"
65#pragma GCC diagnostic ignored "-Wsuggest-final-types"
66#pragma GCC diagnostic ignored "-Wsuggest-final-methods"
67#endif
68#endif
69
70// ============================================================================
71
73{
74 namespace detail
75 {
76 // ========================================================================
77
85 {
86#if defined(MICRO_OS_PLUS_TRACE) \
87 && defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS)
88#if defined(__GNUC__)
89#pragma GCC diagnostic push
90#if defined(__clang__)
91#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
92#endif
93#endif
94 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name);
95#if defined(__GNUC__)
96#pragma GCC diagnostic pop
97#endif
98#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS
99 }
100
108 {
109#if defined(MICRO_OS_PLUS_TRACE) \
110 && defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS)
111#if defined(__GNUC__)
112#pragma GCC diagnostic push
113#if defined(__clang__)
114#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
115#endif
116#endif
117 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name_);
118#if defined(__GNUC__)
119#pragma GCC diagnostic pop
120#endif
121#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS
122 }
123
124 // ========================================================================
125
137 size_t own_index)
139 {
140#if defined(MICRO_OS_PLUS_TRACE) \
141 && defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS)
142#if defined(__GNUC__)
143#pragma GCC diagnostic push
144#if defined(__clang__)
145#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
146#endif
147#endif
148 trace::printf ("%s '%s' %zu\n", __PRETTY_FUNCTION__, name, own_index_);
149#if defined(__GNUC__)
150#pragma GCC diagnostic pop
151#endif
152#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS
153 }
154
163 {
164#if defined(MICRO_OS_PLUS_TRACE) \
165 && defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS)
166#if defined(__GNUC__)
167#pragma GCC diagnostic push
168#if defined(__clang__)
169#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
170#endif
171#endif
172 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name_);
173#if defined(__GNUC__)
174#pragma GCC diagnostic pop
175#endif
176#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS
177
178 // children_subtests_ holds unique_ptrs; destroyed automatically.
179 }
180
186 [[nodiscard]] reporter&
187 runnable_base::reporter (void) const noexcept
188 {
189 return runner_.reporter ();
190 }
191
198 [[noreturn]] void
200 {
201 runner_.abort (sl);
202 }
203
214 void
216 std::unique_ptr<class subtest> child_test, suite& suite)
217 {
218 // Transfer ownership into the vector.
219 children_subtests_.push_back (std::move (child_test));
220
221 // Run the child test case immediately.
222 class subtest& subtest = *children_subtests_.back ();
223 subtest.run ();
224
225 // This test executed one more subtest.
226#if defined(MICRO_OS_PLUS_TRACE) \
227 && defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS)
228#if defined(__GNUC__)
229#pragma GCC diagnostic push
230#if defined(__clang__)
231#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
232#endif
233#endif
234 trace::printf ("%s subtest '%s' executed one more subtest\n",
235 __PRETTY_FUNCTION__, name ());
236#if defined(__GNUC__)
237#pragma GCC diagnostic pop
238#endif
239#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS
241 // Do not accumulate the totals from the child test into the current test
242 // totals, each subtest shows only its counters.
243
244#if defined(MICRO_OS_PLUS_TRACE) \
245 && defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS)
246#if defined(__GNUC__)
247#pragma GCC diagnostic push
248#if defined(__clang__)
249#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
250#endif
251#endif
252 trace::printf ("%s suite '%s' totals\n", __PRETTY_FUNCTION__,
253 suite.name ());
254#if defined(__GNUC__)
255#pragma GCC diagnostic pop
256#endif
257#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS
258
259 // Accumulate the totals from the child test into the suite totals.
260 suite.totals () += subtest.totals ();
261 }
262 } // namespace detail
263
264 // ==========================================================================
265
276 {
277#if defined(MICRO_OS_PLUS_TRACE) \
278 && defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS)
279#if defined(__GNUC__)
280#pragma GCC diagnostic push
281#if defined(__clang__)
282#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
283#endif
284#endif
285 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name_);
286#if defined(__GNUC__)
287#pragma GCC diagnostic pop
288#endif
289#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS
290 }
291
297 void
299 {
300#if defined(MICRO_OS_PLUS_TRACE) \
301 && defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS)
302#if defined(__GNUC__)
303#pragma GCC diagnostic push
304#if defined(__clang__)
305#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
306#endif
307#endif
308 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name_);
309#if defined(__GNUC__)
310#pragma GCC diagnostic pop
311#endif
312#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS
313
314 class reporter& reporter = this->reporter ();
315
316 // For now, subtests do not record the time.
317 // this->timings.timestamp_begin ();
318 reporter.begin_subtest (*this);
319
320 // Invoke the callable, passing the self reference followed by the variadic
321 // arguments.
322 callable_ (*this);
323
324 // this->timings.timestamp_end ();
325 reporter.end_subtest (*this);
326 }
327
328 // ==========================================================================
329
337 {
338#if defined(MICRO_OS_PLUS_TRACE) \
339 && defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS)
340#if defined(__GNUC__)
341#pragma GCC diagnostic push
342#if defined(__clang__)
343#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
344#endif
345#endif
346 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name_);
347#if defined(__GNUC__)
348#pragma GCC diagnostic pop
349#endif
350#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS
351 }
352
358 void
360 {
361#if defined(MICRO_OS_PLUS_TRACE) \
362 && defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS)
363#if defined(__GNUC__)
364#pragma GCC diagnostic push
365#if defined(__clang__)
366#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
367#endif
368#endif
369 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name_);
370#if defined(__GNUC__)
371#pragma GCC diagnostic pop
372#endif
373#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS
374
375 class reporter& reporter = this->reporter ();
376
377 this->timings ().timestamp_begin ();
378 reporter.begin_suite (*this);
379
380 // Invoke the callable, passing the self reference followed by the variadic
381 // arguments.
382 callable_ (*this);
383
384 this->timings ().timestamp_end ();
385 reporter.end_suite (*this);
386 }
387
388 // ==========================================================================
389
397 top_suite::top_suite (const char* name, class runner& runner)
398 : suite{ name, runner, [] (suite&) noexcept {} }
399 {
400#if defined(MICRO_OS_PLUS_TRACE) \
401 && defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS)
402#if defined(__GNUC__)
403#pragma GCC diagnostic push
404#if defined(__clang__)
405#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
406#endif
407#endif
408 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name);
409#if defined(__GNUC__)
410#pragma GCC diagnostic pop
411#endif
412#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS
413
414 own_index (1);
415 }
416
424 {
425#if defined(MICRO_OS_PLUS_TRACE) \
426 && defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS)
427#if defined(__GNUC__)
428#pragma GCC diagnostic push
429#if defined(__clang__)
430#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
431#endif
432#endif
433 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name_);
434#if defined(__GNUC__)
435#pragma GCC diagnostic pop
436#endif
437#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS
438 }
439
440 // ==========================================================================
441
449 {
450#if defined(MICRO_OS_PLUS_TRACE) \
451 && defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS)
452#if defined(__GNUC__)
453#pragma GCC diagnostic push
454#if defined(__clang__)
455#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
456#endif
457#endif
458 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name_);
459#if defined(__GNUC__)
460#pragma GCC diagnostic pop
461#endif
462#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS
463 }
464
472 void
474 {
475#if defined(MICRO_OS_PLUS_TRACE) \
476 && defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS)
477#if defined(__GNUC__)
478#pragma GCC diagnostic push
479#if defined(__clang__)
480#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
481#endif
482#endif
483 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name_);
484#if defined(__GNUC__)
485#pragma GCC diagnostic pop
486#endif
487#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS
488
489 class reporter& reporter = this->reporter ();
490
491 this->timings ().timestamp_begin ();
492 reporter.begin_suite (*this);
493
494 static_callable_ (*this);
495
496 this->timings ().timestamp_end ();
497 reporter.end_suite (*this);
498 }
499
500 // --------------------------------------------------------------------------
501} // namespace micro_os_plus::micro_test_plus
502
503// ----------------------------------------------------------------------------

Generated via doxygen2docusaurus 2.2.0 by Doxygen 1.17.0.