micro-test-plus 3.3.1
µTest++ Testing Framework
Loading...
Searching...
No Matches
test-suite.h
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
47
48#ifndef MICRO_TEST_PLUS_TEST_SUITE_H_
49#define MICRO_TEST_PLUS_TEST_SUITE_H_
50
51// ----------------------------------------------------------------------------
52
53#include <time.h>
54
55#ifdef __cplusplus
56
57// ----------------------------------------------------------------------------
58
59#include <functional>
60
61// ----------------------------------------------------------------------------
62
63#if defined(__GNUC__)
64#pragma GCC diagnostic push
65#pragma GCC diagnostic ignored "-Wpadded"
66#if !defined(__clang__) // GCC only
67#pragma GCC diagnostic ignored "-Wsuggest-final-types"
68#pragma GCC diagnostic ignored "-Wsuggest-final-methods"
69#endif
70#if defined(__clang__)
71#pragma clang diagnostic ignored "-Wc++98-compat"
72#endif
73#endif
74
76{
77 // --------------------------------------------------------------------------
78
100 {
101 public:
110 test_suite_base (const char* name);
111
116
121
127 = delete;
128
134 = delete;
135
139 virtual ~test_suite_base ();
140
149 virtual void
150 run (void);
151
159 void
160 begin_test_case (const char* name);
161
169 void
170 end_test_case (void);
171
179 [[nodiscard]] constexpr const char*
180 name (void)
181 {
182 return name_;
183 }
184
193 void
195
204 void
205 increment_failed (void);
206
214 [[nodiscard]] constexpr size_t
216 {
217 return successful_checks_;
218 }
219
227 [[nodiscard]] constexpr size_t
229 {
230 return failed_checks_;
231 }
232
240 [[nodiscard]] constexpr size_t
242 {
243 return test_cases_count_;
244 }
245
254 void
255 begin_test_suite (void);
256
265 void
266 end_test_suite (void);
267
268#if defined(_WIN32) || defined(CLOCK_MONOTONIC)
278 void
279 compute_elapsed_time (timespec& begin_time, timespec& end_time,
280 long& milliseconds, long& microseconds);
281#endif
282
290 [[nodiscard]] constexpr bool
292 {
293 // Also fail if none passed.
294 return (failed_checks_ == 0 && successful_checks_ != 0);
295 }
296
304 [[nodiscard]] constexpr bool
305 unused (void)
306 {
307 return (failed_checks_ == 0 && successful_checks_ == 0
308 && test_cases_count_ == 0);
309 }
310
311 protected:
315 const char* name_;
316
320 const char* test_case_name_;
321
326
330 size_t failed_checks_ = 0;
331
336
337 public:
341 size_t index = 1;
342
343#if defined(_WIN32) || defined(CLOCK_MONOTONIC)
347 timespec begin_time{};
348
352 timespec end_time{};
353#endif
354
359
367 struct
368 {
373
377 size_t failed_checks = 0;
378
379 size_t index = 0;
381 };
382
408 {
409 public:
425 template <typename Callable_T, typename... Args_T>
426 test_suite (const char* name, Callable_T&& callable,
427 Args_T&&... arguments);
428
432 test_suite (const test_suite&) = delete;
433
437 test_suite (test_suite&&) = delete;
438
444 = delete;
445
451 = delete;
452
456 virtual ~test_suite () override;
457
467 virtual void
468 run (void) override;
469
470 protected:
474 std::function<void (void)> callable_;
475 };
476
477 // --------------------------------------------------------------------------
478} // namespace micro_os_plus::micro_test_plus
479
480#if defined(__GNUC__)
481#pragma GCC diagnostic pop
482#endif
483
484// ----------------------------------------------------------------------------
485
486#endif // __cplusplus
487
488// ----------------------------------------------------------------------------
489
490#endif // MICRO_TEST_PLUS_TEST_SUITE_H_
491
492// ----------------------------------------------------------------------------
size_t index
The test suite index, counting from 1.
Definition test-suite.h:341
virtual void run(void)
Runs the sequence of test cases in the suite.
constexpr size_t test_cases_count(void)
Gets the number of test cases.
Definition test-suite.h:241
test_suite_base(const char *name)
Constructs a test suite.
void increment_failed(void)
Increments the count of failed test conditions.
constexpr bool unused(void)
Checks if the test suite was not used.
Definition test-suite.h:305
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.
constexpr size_t successful_checks(void)
Gets the number of conditions that passed.
Definition test-suite.h:215
size_t successful_checks
Number of successful checks in the current test case.
Definition test-suite.h:372
void end_test_case(void)
Marks the end of a test case.
constexpr size_t failed_checks(void)
Gets the number of test conditions that failed.
Definition test-suite.h:228
test_suite_base & operator=(const test_suite_base &)=delete
Deleted copy assignment operator to prevent copying.
size_t successful_checks_
Count of test conditions that passed.
Definition test-suite.h:325
test_suite_base(const test_suite_base &)=delete
Deleted copy constructor to prevent copying.
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.
test_suite_base(test_suite_base &&)=delete
Deleted move constructor to prevent moving.
size_t failed_checks
Number of failed checks in the current test case.
Definition test-suite.h:377
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
constexpr bool was_successful(void)
Gets the test suite result.
Definition test-suite.h:291
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
test_suite(const test_suite &)=delete
Deleted copy constructor to prevent copying.
test_suite(const char *name, Callable_T &&callable, Args_T &&... arguments)
Class template constructor for test_suite.
test_suite & operator=(const test_suite &)=delete
Deleted copy assignment operator to prevent copying.
virtual void run(void) override
Runs the sequence of test cases in the suite by invoking the stored callable.
test_suite(test_suite &&)=delete
Deleted move constructor to prevent moving.
virtual ~test_suite() override
Virtual destructor for the test_suite class.
Primary namespace for the µTest++ testing framework.