micro-test-plus 5.0.1
µTest++ Testing Framework
Loading...
Searching...
No Matches
timings.h
Go to the documentation of this file.
1
2// ----------------------------------------------------------------------------
3/*
4 * This file is part of the µOS++ project (https://micro-os-plus.github.io/).
5 * Copyright (c) 2021-2026 Liviu Ionescu. All rights reserved.
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose is hereby granted, under the terms of the MIT license.
9 *
10 * If a copy of the license was not distributed with this file, it can be
11 * obtained from https://opensource.org/licenses/mit.
12 */
13
14// ----------------------------------------------------------------------------
15
44
45#ifndef MICRO_OS_PLUS_MICRO_TEST_PLUS_TIMINGS_H_
46#define MICRO_OS_PLUS_MICRO_TEST_PLUS_TIMINGS_H_
47
48// ----------------------------------------------------------------------------
49
50#if defined(__cplusplus)
51
52// ----------------------------------------------------------------------------
53
54#if __has_include("micro-os-plus/project-config.h")
55#include "micro-os-plus/project-config.h"
56#endif // __has_include("micro-os-plus/project-config.h")
57
58#if __has_include("micro-os-plus/micro-test-plus-defines.h")
59#include "micro-os-plus/micro-test-plus-defines.h"
60#endif // __has_include("micro-os-plus/micro-test-plus-defines.h")
61
62#include <cstdint>
63#include <optional>
64#include <ctime>
65
66// ----------------------------------------------------------------------------
67
68#if defined(__GNUC__)
69#pragma GCC diagnostic push
70
71#if defined(__clang__)
72#pragma clang diagnostic ignored "-Wc++98-compat"
73#pragma clang diagnostic ignored "-Wpre-c++17-compat"
74#endif // defined(__clang__)
75#endif // defined(__GNUC__)
76
77// ============================================================================
78
80{
81 // --------------------------------------------------------------------------
82
83 namespace detail
84 {
85 // ========================================================================
86
103 {
104 public:
108 timestamp () noexcept;
109
115 timestamp (const timespec& ts) noexcept;
116
117 // `timespec` is trivially copyable, so copy and move are safe to
118 // default. Defaulting these operations allows `timestamp` to be used in
119 // contexts that require copyability or movability (e.g. containers,
120 // algorithms).
124 timestamp (const timestamp&) = default;
125
129 timestamp (timestamp&&) = default;
130
134 timestamp&
136 = default;
137
141 timestamp&
143 = default;
144
148 ~timestamp () = default;
149
158 bool
159 has_clock (void) const noexcept;
160
168 [[nodiscard]] timespec&
169 value () noexcept;
170
178 [[nodiscard]] const timespec&
179 value () const noexcept;
180
181 protected:
185 timespec value_{};
186 };
187
188 // ========================================================================
189
214 {
215 public:
219 timestamps () = default;
220
224 timestamps (const timestamps&) = delete;
225
229 timestamps (timestamps&&) = delete;
230
236 = delete;
237
243 = delete;
244
248 ~timestamps () = default;
249
258 void
259 timestamp_begin (void) noexcept;
260
268 void
269 timestamp_begin (const timespec& ts) noexcept;
270
279 void
280 timestamp_end (void) noexcept;
281
289 void
290 timestamp_end (const timespec& ts) noexcept;
291
300 bool
301 has_begin () const noexcept;
302
311 bool
312 has_end () const noexcept;
313
324 bool
325 has_timestamps (void) const noexcept;
326
334 void
335 compute_elapsed_time (uint32_t& milliseconds,
336 uint32_t& microseconds) const;
337
338 protected:
342 std::optional<timestamp> begin_time_;
343
347 std::optional<timestamp> end_time_;
348 };
349
350 // ------------------------------------------------------------------------
351 } // namespace detail
352 // ==========================================================================
353} // namespace micro_os_plus::micro_test_plus
354
355#if defined(__GNUC__)
356#pragma GCC diagnostic pop
357#endif // defined(__GNUC__)
358
359// ----------------------------------------------------------------------------
360
361#endif // defined(__cplusplus)
362
363// ============================================================================
364// Templates, inlines & constexpr implementations.
365
367
368// ----------------------------------------------------------------------------
369
370#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TIMINGS_H_
371
372// ----------------------------------------------------------------------------
A single point-in-time measurement, wrapping a timespec value.
Definition timings.h:103
~timestamp()=default
Defaulted destructor.
timestamp(const timestamp &)=default
Defaulted copy constructor.
timestamp() noexcept
Default constructor. Zero-initialises the internal timespec.
Definition timings.cpp:71
timespec & value() noexcept
Returns a mutable reference to the underlying timespec value.
timespec value_
The underlying timespec value.
Definition timings.h:185
timestamp(timestamp &&)=default
Defaulted move constructor.
timestamp & operator=(const timestamp &)=default
Defaulted copy assignment operator.
bool has_clock(void) const noexcept
Returns true if a monotonic clock is available on this target.
Definition timings.cpp:87
bool has_timestamps(void) const noexcept
Returns true if both begin and end timestamps are available.
Definition timings.cpp:167
void timestamp_begin(void) noexcept
Records the begin timestamp using the current system clock.
Definition timings.cpp:102
timestamps()=default
Default constructor. Both timestamps are uninitialised.
~timestamps()=default
Defaulted destructor.
bool has_end() const noexcept
Returns true if the end timestamp has been recorded.
void compute_elapsed_time(uint32_t &milliseconds, uint32_t &microseconds) const
Computes the elapsed time between begin and end timestamps.
Definition timings.cpp:184
std::optional< timestamp > end_time_
The timestamp recorded at the end of the test suite.
Definition timings.h:347
timestamps & operator=(const timestamps &)=delete
Deleted copy assignment operator to prevent copying.
bool has_begin() const noexcept
Returns true if the begin timestamp has been recorded.
std::optional< timestamp > begin_time_
The timestamp recorded at the beginning of the test suite.
Definition timings.h:342
timestamps(const timestamps &)=delete
Deleted copy constructor to prevent copying.
void timestamp_end(void) noexcept
Records the end timestamp using the current system clock.
Definition timings.cpp:135
timestamps(timestamps &&)=delete
Deleted move constructor to prevent moving.
Internal implementation details for the µTest++ framework.
Primary namespace for the µTest++ testing framework.
C++ header file with inline implementations for the µTest++ timing utilities.