Brian Silverman | 2eb8976 | 2019-02-17 15:16:37 -0800 | [diff] [blame^] | 1 | #include "y2019/jevois/uart.h" |
| 2 | |
| 3 | #include <stdint.h> |
| 4 | |
| 5 | #include "gtest/gtest.h" |
| 6 | |
| 7 | namespace frc971 { |
| 8 | namespace jevois { |
| 9 | namespace testing { |
| 10 | |
| 11 | // Tests packing and then unpacking a message with arbitrary values. |
| 12 | TEST(UartToTeensyTest, Basic) { |
| 13 | Frame input_message; |
| 14 | for (int i = 0; i < 3; ++i) { |
| 15 | input_message.targets[i].distance = i * 7 + 1; |
| 16 | input_message.targets[i].height = i * 7 + 2; |
| 17 | input_message.targets[i].heading = i * 7 + 3; |
| 18 | input_message.targets[i].skew = i * 7 + 5; |
| 19 | } |
| 20 | input_message.age = camera_duration(123); |
| 21 | const UartToTeensyBuffer buffer = UartPackToTeensy(input_message); |
| 22 | const auto output_message = UartUnpackToTeensy(buffer); |
| 23 | ASSERT_TRUE(output_message); |
| 24 | EXPECT_EQ(input_message, output_message.value()); |
| 25 | } |
| 26 | |
| 27 | // Tests packing and then unpacking a message with arbitrary values. |
| 28 | TEST(UartToCameraTest, Basic) { |
| 29 | CameraCalibration input_message; |
| 30 | for (int i = 0; i < 3; ++i) { |
| 31 | for (int j = 0; j < 3; ++j) { |
| 32 | input_message.calibration(i, j) = i * 5 + j * 971; |
| 33 | } |
| 34 | } |
| 35 | input_message.teensy_now = |
| 36 | aos::monotonic_clock::time_point(std::chrono::seconds(1678)); |
| 37 | input_message.realtime_now = aos::realtime_clock::min_time; |
| 38 | input_message.camera_command = |
| 39 | CameraCalibration::CameraCommand::kCameraPassthrough; |
| 40 | const UartToCameraBuffer buffer = UartPackToCamera(input_message); |
| 41 | const auto output_message = UartUnpackToCamera(buffer); |
| 42 | ASSERT_TRUE(output_message); |
| 43 | EXPECT_EQ(input_message, output_message.value()); |
| 44 | } |
| 45 | |
| 46 | // Tests that corrupting the data in various ways is handled properly. |
| 47 | TEST(UartToTeensyTest, CorruptData) { |
| 48 | Frame input_message{}; |
| 49 | { |
| 50 | UartToTeensyBuffer buffer = UartPackToTeensy(input_message); |
| 51 | buffer[0]++; |
| 52 | EXPECT_FALSE(UartUnpackToTeensy(buffer)); |
| 53 | } |
| 54 | { |
| 55 | UartToTeensyBuffer buffer = UartPackToTeensy(input_message); |
| 56 | buffer[buffer.size() - 1]++; |
| 57 | EXPECT_FALSE(UartUnpackToTeensy(buffer)); |
| 58 | } |
| 59 | { |
| 60 | UartToTeensyBuffer buffer = UartPackToTeensy(input_message); |
| 61 | buffer.set_size(buffer.size() - 1); |
| 62 | EXPECT_FALSE(UartUnpackToTeensy(buffer)); |
| 63 | } |
| 64 | { |
| 65 | UartToTeensyBuffer buffer = UartPackToTeensy(input_message); |
| 66 | buffer[0] = 255; |
| 67 | EXPECT_FALSE(UartUnpackToTeensy(buffer)); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Tests that corrupting the data in various ways is handled properly. |
| 72 | TEST(UartToCameraTest, CorruptData) { |
| 73 | CameraCalibration input_message{}; |
| 74 | { |
| 75 | UartToCameraBuffer buffer = UartPackToCamera(input_message); |
| 76 | buffer[0]++; |
| 77 | EXPECT_FALSE(UartUnpackToCamera(buffer)); |
| 78 | } |
| 79 | { |
| 80 | UartToCameraBuffer buffer = UartPackToCamera(input_message); |
| 81 | buffer[buffer.size() - 1]++; |
| 82 | EXPECT_FALSE(UartUnpackToCamera(buffer)); |
| 83 | } |
| 84 | { |
| 85 | UartToCameraBuffer buffer = UartPackToCamera(input_message); |
| 86 | buffer.set_size(buffer.size() - 1); |
| 87 | EXPECT_FALSE(UartUnpackToCamera(buffer)); |
| 88 | } |
| 89 | { |
| 90 | UartToCameraBuffer buffer = UartPackToCamera(input_message); |
| 91 | buffer[0] = 255; |
| 92 | EXPECT_FALSE(UartUnpackToCamera(buffer)); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | } // namespace testing |
| 97 | } // namespace jevois |
| 98 | } // namespace frc971 |