micro-test-plus 4.1.0
µ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_TEST_PLUS_TIMINGS_H_
46#define MICRO_TEST_PLUS_TIMINGS_H_
47
48// ----------------------------------------------------------------------------
49
50#ifdef __cplusplus
51
52// ----------------------------------------------------------------------------
53
54#include <cstdint>
55#include <optional>
56#include <ctime>
57
58// ----------------------------------------------------------------------------
59
60#if defined(__GNUC__)
61#pragma GCC diagnostic push
62#if defined(__clang__)
63#pragma clang diagnostic ignored "-Wc++98-compat"
64#pragma clang diagnostic ignored "-Wpre-c++17-compat"
65#endif
66#endif
67
68// ============================================================================
69
71{
72 // --------------------------------------------------------------------------
73
74 namespace detail
75 {
76 // ========================================================================
77
94 {
95 public:
99 timestamp () noexcept;
100
106 timestamp (const timespec& ts) noexcept;
107
108 // `timespec` is trivially copyable, so copy and move are safe to
109 // default. Defaulting these operations allows `timestamp` to be used in
110 // contexts that require copyability or movability (e.g. containers,
111 // algorithms).
115 timestamp (const timestamp&) = default;
116
120 timestamp (timestamp&&) = default;
121
125 timestamp&
127 = default;
128
132 timestamp&
134 = default;
135
139 ~timestamp () = default;
140
149 bool
150 has_clock (void) const noexcept;
151
159 [[nodiscard]] timespec&
160 value () noexcept;
161
169 [[nodiscard]] const timespec&
170 value () const noexcept;
171
172 protected:
176 timespec value_{};
177 };
178
179 // ========================================================================
180
205 {
206 public:
210 timestamps () = default;
211
215 timestamps (const timestamps&) = delete;
216
220 timestamps (timestamps&&) = delete;
221
227 = delete;
228
234 = delete;
235
239 ~timestamps () = default;
240
249 void
250 timestamp_begin (void) noexcept;
251
259 void
260 timestamp_begin (const timespec& ts) noexcept;
261
270 void
271 timestamp_end (void) noexcept;
272
280 void
281 timestamp_end (const timespec& ts) noexcept;
282
291 bool
292 has_begin () const noexcept;
293
302 bool
303 has_end () const noexcept;
304
315 bool
316 has_timestamps (void) const noexcept;
317
325 void
326 compute_elapsed_time (uint32_t& milliseconds,
327 uint32_t& microseconds) const;
328
329 protected:
333 std::optional<timestamp> begin_time_;
334
338 std::optional<timestamp> end_time_;
339 };
340
341 // ------------------------------------------------------------------------
342 } // namespace detail
343 // ==========================================================================
344} // namespace micro_os_plus::micro_test_plus
345
346#if defined(__GNUC__)
347#pragma GCC diagnostic pop
348#endif
349
350// ----------------------------------------------------------------------------
351
352#endif // __cplusplus
353
354// ============================================================================
355// Templates & constexpr implementations.
356
358
359// ----------------------------------------------------------------------------
360
361#endif // MICRO_TEST_PLUS_TIMINGS_H_
362
363// ----------------------------------------------------------------------------
A single point-in-time measurement, wrapping a timespec value.
Definition timings.h:94
~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:176
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:338
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:333
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.