micro-test-plus 3.3.1
µTest++ Testing Framework
Loading...
Searching...
No Matches
test-suite.cpp
Go to the documentation of this file.
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.0 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
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// ----------------------------------------------------------------------------
virtual void run(void)
Runs the sequence of test cases in the suite.
test_suite_base(const char *name)
Constructs a test suite.
void increment_failed(void)
Increments the count of failed test conditions.
constexpr const char * name(void)
Gets the suite name.
Definition test-suite.h:180
size_t test_cases_count_
Count of test cases in the test suite.
Definition test-suite.h:335
void begin_test_case(const char *name)
Marks the beginning of a named test case.
const char * name_
The test suite name.
Definition test-suite.h:315
void increment_successful(void)
Increments the count of passed test conditions.
virtual ~test_suite_base()
Virtual destructor for the test_suite_base class.
void end_test_suite(void)
Marks the end of the test suite.
void end_test_case(void)
Marks the end of a test case.
size_t successful_checks_
Count of test conditions that passed.
Definition test-suite.h:325
bool process_deferred_begin
Indicates whether to process deferred begin for test cases.
Definition test-suite.h:358
struct micro_os_plus::micro_test_plus::test_suite_base::@025050103341037176303320301005170305141235061044 current_test_case
Structure holding the current test case's check counters.
void begin_test_suite(void)
Begins the execution of the test suite.
const char * test_case_name_
The current test case name.
Definition test-suite.h:320
size_t failed_checks_
Count of test conditions that failed.
Definition test-suite.h:330
std::function< void(void)> callable_
Callable object representing the test suite's execution logic.
Definition test-suite.h:474
virtual void run(void) override
Runs the sequence of test cases in the suite by invoking the stored callable.
virtual ~test_suite() override
Virtual destructor for the test_suite class.
Main C++ header with the declarations for the µTest++ Testing Framework.
Primary namespace for the µTest++ testing framework.
test_reporter * reporter
Global pointer to test_reporter.
test_runner runner
Global instance of test_runner.