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) { |
Brian Silverman | c41fb86 | 2019-03-02 21:14:46 -0800 | [diff] [blame] | 13 | CameraFrame input_message; |
Brian Silverman | 2eb8976 | 2019-02-17 15:16:37 -0800 | [diff] [blame] | 14 | for (int i = 0; i < 3; ++i) { |
Brian Silverman | a1e4d33 | 2019-02-17 22:53:13 -0800 | [diff] [blame] | 15 | input_message.targets.push_back({}); |
| 16 | Target *const target = &input_message.targets.back(); |
| 17 | target->distance = i * 7 + 1; |
| 18 | target->height = i * 7 + 2; |
| 19 | target->heading = i * 7 + 3; |
| 20 | target->skew = i * 7 + 5; |
| 21 | } |
| 22 | input_message.age = camera_duration(123); |
| 23 | const UartToTeensyBuffer buffer = UartPackToTeensy(input_message); |
| 24 | const auto output_message = UartUnpackToTeensy(buffer); |
| 25 | ASSERT_TRUE(output_message); |
| 26 | EXPECT_EQ(input_message, output_message.value()); |
| 27 | } |
| 28 | |
| 29 | // Tests packing and then unpacking a message with arbitrary values and no |
| 30 | // frames. |
| 31 | TEST(UartToTeensyTest, NoFrames) { |
Brian Silverman | c41fb86 | 2019-03-02 21:14:46 -0800 | [diff] [blame] | 32 | CameraFrame input_message; |
Brian Silverman | a1e4d33 | 2019-02-17 22:53:13 -0800 | [diff] [blame] | 33 | input_message.age = camera_duration(123); |
| 34 | const UartToTeensyBuffer buffer = UartPackToTeensy(input_message); |
| 35 | const auto output_message = UartUnpackToTeensy(buffer); |
| 36 | ASSERT_TRUE(output_message); |
| 37 | EXPECT_EQ(input_message, output_message.value()); |
| 38 | } |
| 39 | |
| 40 | // Tests packing and then unpacking a message with just one frame. |
| 41 | TEST(UartToTeensyTest, OneFrame) { |
Brian Silverman | c41fb86 | 2019-03-02 21:14:46 -0800 | [diff] [blame] | 42 | CameraFrame input_message; |
Brian Silverman | a1e4d33 | 2019-02-17 22:53:13 -0800 | [diff] [blame] | 43 | { |
| 44 | input_message.targets.push_back({}); |
| 45 | Target *const target = &input_message.targets.back(); |
| 46 | target->distance = 1; |
| 47 | target->height = 2; |
| 48 | target->heading = 3; |
| 49 | target->skew = 5; |
Brian Silverman | 2eb8976 | 2019-02-17 15:16:37 -0800 | [diff] [blame] | 50 | } |
| 51 | input_message.age = camera_duration(123); |
| 52 | const UartToTeensyBuffer buffer = UartPackToTeensy(input_message); |
| 53 | const auto output_message = UartUnpackToTeensy(buffer); |
| 54 | ASSERT_TRUE(output_message); |
| 55 | EXPECT_EQ(input_message, output_message.value()); |
| 56 | } |
| 57 | |
| 58 | // Tests packing and then unpacking a message with arbitrary values. |
| 59 | TEST(UartToCameraTest, Basic) { |
| 60 | CameraCalibration input_message; |
| 61 | for (int i = 0; i < 3; ++i) { |
| 62 | for (int j = 0; j < 3; ++j) { |
| 63 | input_message.calibration(i, j) = i * 5 + j * 971; |
| 64 | } |
| 65 | } |
| 66 | input_message.teensy_now = |
| 67 | aos::monotonic_clock::time_point(std::chrono::seconds(1678)); |
| 68 | input_message.realtime_now = aos::realtime_clock::min_time; |
Brian Silverman | e9924fd | 2019-03-02 15:20:42 -0800 | [diff] [blame] | 69 | input_message.camera_command = CameraCommand::kCameraPassthrough; |
Brian Silverman | 2eb8976 | 2019-02-17 15:16:37 -0800 | [diff] [blame] | 70 | const UartToCameraBuffer buffer = UartPackToCamera(input_message); |
| 71 | const auto output_message = UartUnpackToCamera(buffer); |
| 72 | ASSERT_TRUE(output_message); |
| 73 | EXPECT_EQ(input_message, output_message.value()); |
| 74 | } |
| 75 | |
| 76 | // Tests that corrupting the data in various ways is handled properly. |
| 77 | TEST(UartToTeensyTest, CorruptData) { |
Brian Silverman | c41fb86 | 2019-03-02 21:14:46 -0800 | [diff] [blame] | 78 | CameraFrame input_message{}; |
Brian Silverman | 2eb8976 | 2019-02-17 15:16:37 -0800 | [diff] [blame] | 79 | { |
| 80 | UartToTeensyBuffer buffer = UartPackToTeensy(input_message); |
| 81 | buffer[0]++; |
| 82 | EXPECT_FALSE(UartUnpackToTeensy(buffer)); |
| 83 | } |
| 84 | { |
| 85 | UartToTeensyBuffer buffer = UartPackToTeensy(input_message); |
| 86 | buffer[buffer.size() - 1]++; |
| 87 | EXPECT_FALSE(UartUnpackToTeensy(buffer)); |
| 88 | } |
| 89 | { |
| 90 | UartToTeensyBuffer buffer = UartPackToTeensy(input_message); |
| 91 | buffer.set_size(buffer.size() - 1); |
| 92 | EXPECT_FALSE(UartUnpackToTeensy(buffer)); |
| 93 | } |
| 94 | { |
| 95 | UartToTeensyBuffer buffer = UartPackToTeensy(input_message); |
| 96 | buffer[0] = 255; |
| 97 | EXPECT_FALSE(UartUnpackToTeensy(buffer)); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // Tests that corrupting the data in various ways is handled properly. |
| 102 | TEST(UartToCameraTest, CorruptData) { |
| 103 | CameraCalibration input_message{}; |
| 104 | { |
| 105 | UartToCameraBuffer buffer = UartPackToCamera(input_message); |
| 106 | buffer[0]++; |
| 107 | EXPECT_FALSE(UartUnpackToCamera(buffer)); |
| 108 | } |
| 109 | { |
| 110 | UartToCameraBuffer buffer = UartPackToCamera(input_message); |
| 111 | buffer[buffer.size() - 1]++; |
| 112 | EXPECT_FALSE(UartUnpackToCamera(buffer)); |
| 113 | } |
| 114 | { |
| 115 | UartToCameraBuffer buffer = UartPackToCamera(input_message); |
| 116 | buffer.set_size(buffer.size() - 1); |
| 117 | EXPECT_FALSE(UartUnpackToCamera(buffer)); |
| 118 | } |
| 119 | { |
| 120 | UartToCameraBuffer buffer = UartPackToCamera(input_message); |
| 121 | buffer[0] = 255; |
| 122 | EXPECT_FALSE(UartUnpackToCamera(buffer)); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | } // namespace testing |
| 127 | } // namespace jevois |
| 128 | } // namespace frc971 |