Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame^] | 1 | #include "interfaces.hpp" |
Patrick Venture | e31ee6d | 2018-06-08 18:49:27 -0700 | [diff] [blame] | 2 | #include "sensors/pluggable.hpp" |
Patrick Venture | e31ee6d | 2018-06-08 18:49:27 -0700 | [diff] [blame] | 3 | #include "test/readinterface_mock.hpp" |
| 4 | #include "test/writeinterface_mock.hpp" |
| 5 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 6 | #include <chrono> |
Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame^] | 7 | #include <cstdint> |
| 8 | #include <memory> |
| 9 | #include <string> |
| 10 | #include <utility> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 11 | |
| 12 | #include <gmock/gmock.h> |
| 13 | #include <gtest/gtest.h> |
| 14 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 15 | namespace pid_control |
| 16 | { |
| 17 | namespace |
| 18 | { |
| 19 | |
Patrick Venture | e31ee6d | 2018-06-08 18:49:27 -0700 | [diff] [blame] | 20 | using ::testing::Invoke; |
| 21 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 22 | TEST(PluggableSensorTest, BoringConstructorTest) |
| 23 | { |
Patrick Venture | e31ee6d | 2018-06-08 18:49:27 -0700 | [diff] [blame] | 24 | // 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 Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 40 | TEST(PluggableSensorTest, TryReadingTest) |
| 41 | { |
Patrick Venture | e31ee6d | 2018-06-08 18:49:27 -0700 | [diff] [blame] | 42 | // 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 Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 54 | ReadInterfaceMock* rip = reinterpret_cast<ReadInterfaceMock*>(ri.get()); |
Patrick Venture | e31ee6d | 2018-06-08 18:49:27 -0700 | [diff] [blame] | 55 | |
| 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 Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 62 | EXPECT_CALL(*rip, read()).WillOnce(Invoke([&](void) { return r; })); |
Patrick Venture | e31ee6d | 2018-06-08 18:49:27 -0700 | [diff] [blame] | 63 | |
| 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 Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 70 | TEST(PluggableSensorTest, TryWritingTest) |
| 71 | { |
Patrick Venture | e31ee6d | 2018-06-08 18:49:27 -0700 | [diff] [blame] | 72 | // 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 Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 84 | WriteInterfaceMock* wip = reinterpret_cast<WriteInterfaceMock*>(wi.get()); |
Patrick Venture | e31ee6d | 2018-06-08 18:49:27 -0700 | [diff] [blame] | 85 | |
| 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 Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 93 | |
| 94 | } // namespace |
| 95 | } // namespace pid_control |