|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2026 Tendry Lab |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +#include "unity.h" |
| 7 | + |
| 8 | +#include "ocs_core/func_value_monitor.h" |
| 9 | +#include "ocs_core/noncopyable.h" |
| 10 | + |
| 11 | +namespace ocs { |
| 12 | +namespace core { |
| 13 | + |
| 14 | +namespace { |
| 15 | + |
| 16 | +class TestValueMonitor : public IValueMonitor<int>, private NonCopyable<> { |
| 17 | +public: |
| 18 | + int get() const override { |
| 19 | + return value; |
| 20 | + } |
| 21 | + |
| 22 | + status::StatusCode update() override { |
| 23 | + ++update_call_count; |
| 24 | + |
| 25 | + return status::StatusCode::OK; |
| 26 | + } |
| 27 | + |
| 28 | + int value { 0 }; |
| 29 | + size_t update_call_count { 0 }; |
| 30 | +}; |
| 31 | + |
| 32 | +} // namespace |
| 33 | + |
| 34 | +TEST_CASE("Func value monitor: get value from the underlying monitor", |
| 35 | + "[func_value_monitor], [ocs_core]") { |
| 36 | + TestValueMonitor monitor; |
| 37 | + size_t func_call_count = 0; |
| 38 | + |
| 39 | + FuncValueMonitor<int> func_monitor(monitor, [&func_call_count]() { |
| 40 | + ++func_call_count; |
| 41 | + |
| 42 | + return status::StatusCode::OK; |
| 43 | + }); |
| 44 | + |
| 45 | + monitor.value = 42; |
| 46 | + TEST_ASSERT_EQUAL(42, func_monitor.get()); |
| 47 | + |
| 48 | + monitor.value = -1; |
| 49 | + TEST_ASSERT_EQUAL(-1, func_monitor.get()); |
| 50 | + |
| 51 | + TEST_ASSERT_EQUAL(0, func_call_count); |
| 52 | + TEST_ASSERT_EQUAL(0, monitor.update_call_count); |
| 53 | +} |
| 54 | + |
| 55 | +TEST_CASE("Func value monitor: update calls the function", |
| 56 | + "[func_value_monitor], [ocs_core]") { |
| 57 | + TestValueMonitor monitor; |
| 58 | + size_t func_call_count = 0; |
| 59 | + |
| 60 | + FuncValueMonitor<int> func_monitor(monitor, [&func_call_count]() { |
| 61 | + ++func_call_count; |
| 62 | + |
| 63 | + return status::StatusCode::OK; |
| 64 | + }); |
| 65 | + |
| 66 | + TEST_ASSERT_EQUAL(status::StatusCode::OK, func_monitor.update()); |
| 67 | + TEST_ASSERT_EQUAL(1, func_call_count); |
| 68 | + |
| 69 | + TEST_ASSERT_EQUAL(status::StatusCode::OK, func_monitor.update()); |
| 70 | + TEST_ASSERT_EQUAL(2, func_call_count); |
| 71 | + |
| 72 | + // The underlying monitor is only updated by the function. |
| 73 | + TEST_ASSERT_EQUAL(0, monitor.update_call_count); |
| 74 | +} |
| 75 | + |
| 76 | +TEST_CASE("Func value monitor: update propagates the function failure", |
| 77 | + "[func_value_monitor], [ocs_core]") { |
| 78 | + TestValueMonitor monitor; |
| 79 | + |
| 80 | + FuncValueMonitor<int> func_monitor(monitor, []() { |
| 81 | + return status::StatusCode::Error; |
| 82 | + }); |
| 83 | + |
| 84 | + TEST_ASSERT_EQUAL(status::StatusCode::Error, func_monitor.update()); |
| 85 | +} |
| 86 | + |
| 87 | +} // namespace core |
| 88 | +} // namespace ocs |
0 commit comments