Patrick Venture | e31ee6d | 2018-06-08 18:49:27 -0700 | [diff] [blame] | 1 | #include "sensors/pluggable.hpp" |
Patrick Venture | e31ee6d | 2018-06-08 18:49:27 -0700 | [diff] [blame] | 2 | #include "test/readinterface_mock.hpp" |
| 3 | #include "test/writeinterface_mock.hpp" |
| 4 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 5 | #include <chrono> |
| 6 | |
| 7 | #include <gmock/gmock.h> |
| 8 | #include <gtest/gtest.h> |
| 9 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 10 | namespace pid_control |
| 11 | { |
| 12 | namespace |
| 13 | { |
| 14 | |
Patrick Venture | e31ee6d | 2018-06-08 18:49:27 -0700 | [diff] [blame] | 15 | using ::testing::Invoke; |
| 16 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 17 | TEST(PluggableSensorTest, BoringConstructorTest) |
| 18 | { |
Patrick Venture | e31ee6d | 2018-06-08 18:49:27 -0700 | [diff] [blame] | 19 | // Build a boring Pluggable Sensor. |
| 20 | |
| 21 | int64_t min = 0; |
| 22 | int64_t max = 255; |
| 23 | |
| 24 | std::unique_ptr<ReadInterface> ri = std::make_unique<ReadInterfaceMock>(); |
| 25 | std::unique_ptr<WriteInterface> wi = |
| 26 | std::make_unique<WriteInterfaceMock>(min, max); |
| 27 | |
| 28 | std::string name = "name"; |
| 29 | int64_t timeout = 1; |
| 30 | |
| 31 | PluggableSensor p(name, timeout, std::move(ri), std::move(wi)); |
| 32 | // Successfully created it. |
| 33 | } |
| 34 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 35 | TEST(PluggableSensorTest, TryReadingTest) |
| 36 | { |
Patrick Venture | e31ee6d | 2018-06-08 18:49:27 -0700 | [diff] [blame] | 37 | // Verify calling read, calls the ReadInterface. |
| 38 | |
| 39 | int64_t min = 0; |
| 40 | int64_t max = 255; |
| 41 | |
| 42 | std::unique_ptr<ReadInterface> ri = std::make_unique<ReadInterfaceMock>(); |
| 43 | std::unique_ptr<WriteInterface> wi = |
| 44 | std::make_unique<WriteInterfaceMock>(min, max); |
| 45 | |
| 46 | std::string name = "name"; |
| 47 | int64_t timeout = 1; |
| 48 | |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 49 | ReadInterfaceMock* rip = reinterpret_cast<ReadInterfaceMock*>(ri.get()); |
Patrick Venture | e31ee6d | 2018-06-08 18:49:27 -0700 | [diff] [blame] | 50 | |
| 51 | PluggableSensor p(name, timeout, std::move(ri), std::move(wi)); |
| 52 | |
| 53 | ReadReturn r; |
| 54 | r.value = 0.1; |
| 55 | r.updated = std::chrono::high_resolution_clock::now(); |
| 56 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 57 | EXPECT_CALL(*rip, read()).WillOnce(Invoke([&](void) { return r; })); |
Patrick Venture | e31ee6d | 2018-06-08 18:49:27 -0700 | [diff] [blame] | 58 | |
| 59 | // TODO(venture): Implement comparison operator for ReadReturn. |
| 60 | ReadReturn v = p.read(); |
| 61 | EXPECT_EQ(r.value, v.value); |
| 62 | EXPECT_EQ(r.updated, v.updated); |
| 63 | } |
| 64 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 65 | TEST(PluggableSensorTest, TryWritingTest) |
| 66 | { |
Patrick Venture | e31ee6d | 2018-06-08 18:49:27 -0700 | [diff] [blame] | 67 | // Verify calling write, calls the WriteInterface. |
| 68 | |
| 69 | int64_t min = 0; |
| 70 | int64_t max = 255; |
| 71 | |
| 72 | std::unique_ptr<ReadInterface> ri = std::make_unique<ReadInterfaceMock>(); |
| 73 | std::unique_ptr<WriteInterface> wi = |
| 74 | std::make_unique<WriteInterfaceMock>(min, max); |
| 75 | |
| 76 | std::string name = "name"; |
| 77 | int64_t timeout = 1; |
| 78 | |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 79 | WriteInterfaceMock* wip = reinterpret_cast<WriteInterfaceMock*>(wi.get()); |
Patrick Venture | e31ee6d | 2018-06-08 18:49:27 -0700 | [diff] [blame] | 80 | |
| 81 | PluggableSensor p(name, timeout, std::move(ri), std::move(wi)); |
| 82 | |
| 83 | double value = 0.303; |
| 84 | |
| 85 | EXPECT_CALL(*wip, write(value)); |
| 86 | p.write(value); |
| 87 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 88 | |
| 89 | } // namespace |
| 90 | } // namespace pid_control |