micro-test-plus 6.0.0
µTest++ Testing Framework
Loading...
Searching...
No Matches
stopwatch.h
Go to the documentation of this file.
1// ----------------------------------------------------------------------------
2/*
3 * This file is part of the µOS++ project (https://micro-os-plus.github.io/).
4 * Copyright (c) 2021-2026 Liviu Ionescu. All rights reserved.
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose is hereby granted, under the terms of the MIT license.
8 *
9 * If a copy of the license was not distributed with this file, it can be
10 * obtained from https://opensource.org/licenses/mit.
11 */
12
13// ----------------------------------------------------------------------------
14
38
39#ifndef MICRO_OS_PLUS_MICRO_TEST_PLUS_STOPWATCH_H_
40#define MICRO_OS_PLUS_MICRO_TEST_PLUS_STOPWATCH_H_
41
42// ----------------------------------------------------------------------------
43
44#if defined(__cplusplus)
45
46// ----------------------------------------------------------------------------
47
48#if __has_include("micro-os-plus/project-config.h")
49#include "micro-os-plus/project-config.h"
50#endif // __has_include("micro-os-plus/project-config.h")
51
52#if __has_include("micro-os-plus/micro-test-plus-defines.h")
53#include "micro-os-plus/micro-test-plus-defines.h"
54#endif // __has_include("micro-os-plus/micro-test-plus-defines.h")
55
56#include <chrono>
57#include <cstdint>
58#include <ctime>
59
60// ----------------------------------------------------------------------------
61
62#if defined(__GNUC__)
63#pragma GCC diagnostic push
64
65#if defined(__clang__)
66#pragma clang diagnostic ignored "-Wc++98-compat"
67#pragma clang diagnostic ignored "-Wpre-c++17-compat"
68#endif // defined(__clang__)
69#endif // defined(__GNUC__)
70
71// ============================================================================
72
74{
75 // --------------------------------------------------------------------------
76
77 namespace detail
78 {
79 // ========================================================================
80
95 void
96 read_clock (timespec& ts) noexcept;
97
98 // ========================================================================
99
137 class stopwatch final
138 {
139 public:
147 stopwatch (void) = default;
148
155 stopwatch (const stopwatch&) = delete;
156
163 stopwatch (stopwatch&&) = delete;
164
171 stopwatch&
173 = delete;
174
181 stopwatch&
183 = delete;
184
191 ~stopwatch () = default;
192
205 void
206 start (void) noexcept;
207
220 void
221 stop (void) noexcept;
222
237 [[nodiscard]] bool
238 has_elapsed (void) const noexcept;
239
251 [[nodiscard]] std::chrono::microseconds
252 elapsed (void) const noexcept;
253
254#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_DEPRECATED_ENABLED)
259
273 [[deprecated ("use has_elapsed() (since 6.0.0)"), nodiscard]] bool
274 has_begin (void) const noexcept;
275
290 [[deprecated ("use has_elapsed() (since 6.0.0)"), nodiscard]] bool
291 has_end (void) const noexcept;
292
304 [[deprecated ("use has_elapsed() (since 6.0.0)"), nodiscard]] bool
305 has_timestamps (void) const noexcept;
306
323 [[deprecated ("use elapsed() (since 6.0.0)")]] void
324 compute_elapsed_time (uint32_t& milliseconds,
325 uint32_t& microseconds) const noexcept;
326
343 [[deprecated ("use start() (since 6.0.0)")]] void
344 timestamp_begin (void) noexcept;
345
359 [[deprecated ("use start() (since 6.0.0)")]] void
360 timestamp_begin (const timespec& ts) noexcept;
361
376 [[deprecated ("use stop() (since 6.0.0)")]] void
377 timestamp_end (void) noexcept;
378
392 [[deprecated ("use stop() (since 6.0.0)")]] void
393 timestamp_end (const timespec& ts) noexcept;
394
398#endif // defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_DEPRECATED_ENABLED)
399
400 private:
409
421 [[nodiscard]] static bool
422 is_recorded (const timespec& ts) noexcept;
423
430 timespec start_time_{};
431
439 timespec stop_time_{};
440 };
441
442 // ------------------------------------------------------------------------
443 } // namespace detail
444 // ==========================================================================
445} // namespace micro_os_plus::micro_test_plus
446
447#if defined(__GNUC__)
448#pragma GCC diagnostic pop
449#endif // defined(__GNUC__)
450
451// ----------------------------------------------------------------------------
452
453#endif // defined(__cplusplus)
454
455// ============================================================================
456// Templates, inlines & constexpr implementations.
457
459
460// ----------------------------------------------------------------------------
461
462#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_STOPWATCH_H_
463
464// ----------------------------------------------------------------------------
static bool is_recorded(const timespec &ts) noexcept
Checks whether a clock reading was recorded.
stopwatch(stopwatch &&)=delete
Deleted move constructor to prevent moving.
bool has_begin(void) const noexcept
Deprecated check of the start reading.
std::chrono::microseconds elapsed(void) const noexcept
Returns the time elapsed between start() and stop().
bool has_elapsed(void) const noexcept
Checks whether the elapsed time is available.
stopwatch(const stopwatch &)=delete
Deleted copy constructor to prevent copying.
void timestamp_end(void) noexcept
Deprecated recording of the stop time, from the system clock.
stopwatch(void)=default
Default constructor.
timespec start_time_
The clock reading at the start of the measured interval.
Definition stopwatch.h:430
void timestamp_begin(void) noexcept
Deprecated recording of the start time, from the system clock.
void stop(void) noexcept
Records the stop time, from the system clock.
bool has_timestamps(void) const noexcept
Deprecated name of has_elapsed().
stopwatch & operator=(const stopwatch &)=delete
Deleted copy assignment operator to prevent copying.
friend struct stopwatch_test_access
The unit tests helper, which sets known readings.
Definition stopwatch.h:408
bool has_end(void) const noexcept
Deprecated check of the stop reading.
~stopwatch()=default
Defaulted destructor.
void start(void) noexcept
Records the start time, from the system clock.
Definition stopwatch.cpp:87
timespec stop_time_
The clock reading at the end of the measured interval.
Definition stopwatch.h:439
void compute_elapsed_time(uint32_t &milliseconds, uint32_t &microseconds) const noexcept
Deprecated split of the elapsed time.
Internal implementation details for the µTest++ framework.
void read_clock(timespec &ts) noexcept
Reads the system clock.
Definition stopwatch.cpp:66
Primary namespace for the µTest++ testing framework.
C++ header file with inline implementations for the µTest++ stopwatch.