blob: 98dd3ceb8e4bf4fcc5e84be00e5ec206e8b8864b [file] [log] [blame]
Ed Tanousf8b6e552025-06-27 13:27:50 -07001#include "interfaces.hpp"
Patrick Venturee31ee6d2018-06-08 18:49:27 -07002#include "sensors/pluggable.hpp"
Patrick Venturee31ee6d2018-06-08 18:49:27 -07003#include "test/readinterface_mock.hpp"
4#include "test/writeinterface_mock.hpp"
5
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07006#include <chrono>
Ed Tanousf8b6e552025-06-27 13:27:50 -07007#include <cstdint>
8#include <memory>
9#include <string>
10#include <utility>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070011
12#include <gmock/gmock.h>
13#include <gtest/gtest.h>
14
Patrick Venturea0764872020-08-08 07:48:43 -070015namespace pid_control
16{
17namespace
18{
19
Patrick Venturee31ee6d2018-06-08 18:49:27 -070020using ::testing::Invoke;
21
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070022TEST(PluggableSensorTest, BoringConstructorTest)
23{
Patrick Venturee31ee6d2018-06-08 18:49:27 -070024 // Build a boring Pluggable Sensor.
25
26 int64_t min = 0;
27 int64_t max = 255;
28
29 std::unique_ptr<ReadInterface> ri = std::make_unique<ReadInterfaceMock>();
30 std::unique_ptr<WriteInterface> wi =
31 std::make_unique<WriteInterfaceMock>(min, max);
32
33 std::string name = "name";
34 int64_t timeout = 1;
35
36 PluggableSensor p(name, timeout, std::move(ri), std::move(wi));
37 // Successfully created it.
38}
39
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070040TEST(PluggableSensorTest, TryReadingTest)
41{
Patrick Venturee31ee6d2018-06-08 18:49:27 -070042 // Verify calling read, calls the ReadInterface.
43
44 int64_t min = 0;
45 int64_t max = 255;
46
47 std::unique_ptr<ReadInterface> ri = std::make_unique<ReadInterfaceMock>();
48 std::unique_ptr<WriteInterface> wi =
49 std::make_unique<WriteInterfaceMock>(min, max);
50
51 std::string name = "name";
52 int64_t timeout = 1;
53
Patrick Venturee2ec0f62018-09-04 12:30:27 -070054 ReadInterfaceMock* rip = reinterpret_cast<ReadInterfaceMock*>(ri.get());
Patrick Venturee31ee6d2018-06-08 18:49:27 -070055
56 PluggableSensor p(name, timeout, std::move(ri), std::move(wi));
57
58 ReadReturn r;
59 r.value = 0.1;
60 r.updated = std::chrono::high_resolution_clock::now();
61
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070062 EXPECT_CALL(*rip, read()).WillOnce(Invoke([&](void) { return r; }));
Patrick Venturee31ee6d2018-06-08 18:49:27 -070063
64 // TODO(venture): Implement comparison operator for ReadReturn.
65 ReadReturn v = p.read();
66 EXPECT_EQ(r.value, v.value);
67 EXPECT_EQ(r.updated, v.updated);
68}
69
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070070TEST(PluggableSensorTest, TryWritingTest)
71{
Patrick Venturee31ee6d2018-06-08 18:49:27 -070072 // Verify calling write, calls the WriteInterface.
73
74 int64_t min = 0;
75 int64_t max = 255;
76
77 std::unique_ptr<ReadInterface> ri = std::make_unique<ReadInterfaceMock>();
78 std::unique_ptr<WriteInterface> wi =
79 std::make_unique<WriteInterfaceMock>(min, max);
80
81 std::string name = "name";
82 int64_t timeout = 1;
83
Patrick Venturee2ec0f62018-09-04 12:30:27 -070084 WriteInterfaceMock* wip = reinterpret_cast<WriteInterfaceMock*>(wi.get());
Patrick Venturee31ee6d2018-06-08 18:49:27 -070085
86 PluggableSensor p(name, timeout, std::move(ri), std::move(wi));
87
88 double value = 0.303;
89
90 EXPECT_CALL(*wip, write(value));
91 p.write(value);
92}
Patrick Venturea0764872020-08-08 07:48:43 -070093
94} // namespace
95} // namespace pid_control