M5UnitUnified 0.0.2 git rev:a353e8c
Loading...
Searching...
No Matches
test_helper.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
11#ifndef M5_UNIT_COMPONENT_GOOGLETEST_HELPER_HPP
12#define M5_UNIT_COMPONENT_GOOGLETEST_HELPER_HPP
13
14#include <M5Utility.hpp>
15#include <thread>
16#include <cassert>
17
18namespace m5 {
19namespace unit {
20namespace googletest {
21
22template <class U>
23uint32_t test_periodic_measurement(U* unit, const uint32_t times, const uint32_t tolerance,
24 const uint32_t timeout_duration, void (*callback)(U*), const bool skip_after_test)
25{
26 static_assert(std::is_base_of<m5::unit::Component, U>::value, "U must be derived from Component");
27
28 auto interval = unit->interval();
29 decltype(interval) avg{}, avgCnt{};
30 uint32_t cnt{times};
31 auto prev = unit->updatedMillis();
32 auto timeout_at = m5::utility::millis() + timeout_duration;
33 while (cnt && m5::utility::millis() <= timeout_at) {
34 unit->update();
35 if (unit->updated()) {
36 --cnt;
37 auto um = unit->updatedMillis();
38 if (prev) {
39 auto duration = um - prev;
40 ++avgCnt;
41 avg += duration;
42 // M5_LOGI("dur:%ld", duration);
43 // EXPECT_LE(duration, interval + 1);
44 }
45 prev = um;
46 if (callback) {
47 callback(unit);
48 }
49 }
50 std::this_thread::yield();
51 }
52
53 if (!skip_after_test) {
54 EXPECT_EQ(cnt, 0U);
55 EXPECT_EQ(avgCnt, times - 1);
56 if (avgCnt) {
57 avg /= avgCnt;
58 EXPECT_LE(avg, decltype(interval)(interval + tolerance));
59 }
60 return avg;
61 }
62 return 0U;
63}
64
65template <class U>
66uint32_t test_periodic_measurement(U* unit, const uint32_t times = 8, const uint32_t tolerance = 1,
67 void (*callback)(U*) = nullptr, const bool skip_after_test = false)
68{
69 static_assert(std::is_base_of<m5::unit::Component, U>::value, "U must be derived from Component");
70 auto timeout_duration = (unit->interval() * 2) * times;
71 return test_periodic_measurement(unit, times, tolerance, timeout_duration, callback, skip_after_test);
72}
73
74template <class U>
75uint32_t test_periodic_measurement(U* unit, const uint32_t times = 8, void (*callback)(U*) = nullptr,
76 const bool skip_after_test = false)
77{
78 static_assert(std::is_base_of<m5::unit::Component, U>::value, "U must be derived from Component");
79 auto timeout_duration = (unit->interval() * 2) * times;
80 return test_periodic_measurement(unit, times, 1, timeout_duration, callback, skip_after_test);
81}
82
83} // namespace googletest
84} // namespace unit
85} // namespace m5
86#endif
For GoogleTest.
Top level namespace of M5stack.
Definition test_helper.hpp:18
Unit-related namespace.