micro-test-plus 5.0.0
µTest++ Testing Framework
Loading...
Searching...
No Matches
timings.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
35
36// ----------------------------------------------------------------------------
37
38#if __has_include(<micro-os-plus/project-config.h>)
39#include <micro-os-plus/project-config.h>
40#elif __has_include(<micro-os-plus/config.h>)
41#pragma message \
42 "micro-os-plus/config.h is deprecated, rename to micro-os-plus/project-config.h and include it instead of micro-os-plus/config.h"
43#include <micro-os-plus/config.h>
44#endif // __has_include(<micro-os-plus/project-config.h>)
45
46#if __has_include(<micro-os-plus/micro-test-plus-defines.h>)
47#include <micro-os-plus/micro-test-plus-defines.h>
48#endif // __has_include(<micro-os-plus/micro-test-plus-defines.h>)
49
51
52#include <cassert>
53
54// ----------------------------------------------------------------------------
55
56#if defined(__GNUC__)
57#pragma GCC diagnostic ignored "-Waggregate-return"
58#if defined(__clang__)
59#pragma clang diagnostic ignored "-Wpre-c++17-compat"
60#pragma clang diagnostic ignored "-Wc++98-compat"
61#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
62#endif
63#endif
64
65// ============================================================================
66
68{
69 // --------------------------------------------------------------------------
70
80 {
81#if defined(_WIN32)
82 timespec_get (&value_, TIME_UTC);
83#elif defined(CLOCK_MONOTONIC)
84 clock_gettime (CLOCK_MONOTONIC, &value_);
85#endif
86 }
87
94 bool
95 timestamp::has_clock (void) const noexcept
96 {
97 return value_.tv_sec != 0 || value_.tv_nsec != 0;
98 }
99
100 // --------------------------------------------------------------------------
101
109 void
111 {
112 // Ensure it is timestamped only once.
113 if (!begin_time_.has_value ())
114 {
115 begin_time_.emplace ();
116 }
117 }
118
125 void
126 timestamps::timestamp_begin (const timespec& ts) noexcept
127 {
128 // Ensure it is timestamped only once.
129 if (!begin_time_.has_value ())
130 {
131 begin_time_.emplace (ts);
132 }
133 }
134
142 void
144 {
145 // Ensure it is timestamped only once.
146 if (!end_time_.has_value ())
147 {
148 end_time_.emplace ();
149 }
150 }
151
158 void
159 timestamps::timestamp_end (const timespec& ts) noexcept
160 {
161 // Ensure it is timestamped only once.
162 if (!end_time_.has_value ())
163 {
164 end_time_.emplace (ts);
165 }
166 }
167
174 bool
175 timestamps::has_timestamps (void) const noexcept
176 {
177 return begin_time_.has_value () && begin_time_->has_clock ()
178 && end_time_.has_value () && end_time_->has_clock ();
179 }
180
191 void
192 timestamps::compute_elapsed_time (uint32_t& milliseconds,
193 uint32_t& microseconds) const
194 {
195 assert (has_timestamps ());
196
197 // Precondition: has_timestamps() must be true before calling this method.
198 // Invoking it with disengaged optionals is undefined behaviour.
199 long long delta_ns
200 = end_time_->value ().tv_nsec - begin_time_->value ().tv_nsec;
201 long long delta_s
202 = end_time_->value ().tv_sec - begin_time_->value ().tv_sec;
203 if (delta_ns < 0)
204 {
205 delta_ns += 1000000000LL;
206 --delta_s;
207 }
208
209 // Split into milliseconds and microseconds.
210 const long long total_us = delta_s * 1000000LL + delta_ns / 1000LL;
211 milliseconds = static_cast<uint32_t> (total_us / 1000LL);
212 microseconds = static_cast<uint32_t> (total_us % 1000LL);
213 }
214
215 // --------------------------------------------------------------------------
216} // namespace micro_os_plus::micro_test_plus::detail
217
218// ----------------------------------------------------------------------------
timestamp() noexcept
Default constructor. Zero-initialises the internal timespec.
Definition timings.cpp:79
timespec value_
The underlying timespec value.
Definition timings.h:176
bool has_clock(void) const noexcept
Returns true if a monotonic clock is available on this target.
Definition timings.cpp:95
bool has_timestamps(void) const noexcept
Returns true if both begin and end timestamps are available.
Definition timings.cpp:175
void timestamp_begin(void) noexcept
Records the begin timestamp using the current system clock.
Definition timings.cpp:110
void compute_elapsed_time(uint32_t &milliseconds, uint32_t &microseconds) const
Computes the elapsed time between begin and end timestamps.
Definition timings.cpp:192
std::optional< timestamp > end_time_
The timestamp recorded at the end of the test suite.
Definition timings.h:338
std::optional< timestamp > begin_time_
The timestamp recorded at the beginning of the test suite.
Definition timings.h:333
void timestamp_end(void) noexcept
Records the end timestamp using the current system clock.
Definition timings.cpp:143
Internal implementation details for the µTest++ framework.
C++ header file with declarations for the µTest++ timing utilities.