blob: 1e90d84eb50cf4c5903535604a4edc8595502da7 [file] [log] [blame]
Brian Silverman2eb89762019-02-17 15:16:37 -08001#include "y2019/jevois/uart.h"
2
3#include <stdint.h>
4
5#include "gtest/gtest.h"
6
7namespace frc971 {
8namespace jevois {
9namespace testing {
10
11// Tests packing and then unpacking a message with arbitrary values.
12TEST(UartToTeensyTest, Basic) {
13 Frame input_message;
14 for (int i = 0; i < 3; ++i) {
Brian Silvermana1e4d332019-02-17 22:53:13 -080015 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.
31TEST(UartToTeensyTest, NoFrames) {
32 Frame input_message;
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.
41TEST(UartToTeensyTest, OneFrame) {
42 Frame input_message;
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 Silverman2eb89762019-02-17 15:16:37 -080050 }
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.
59TEST(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;
69 input_message.camera_command =
70 CameraCalibration::CameraCommand::kCameraPassthrough;
71 const UartToCameraBuffer buffer = UartPackToCamera(input_message);
72 const auto output_message = UartUnpackToCamera(buffer);
73 ASSERT_TRUE(output_message);
74 EXPECT_EQ(input_message, output_message.value());
75}
76
77// Tests that corrupting the data in various ways is handled properly.
78TEST(UartToTeensyTest, CorruptData) {
79 Frame input_message{};
80 {
81 UartToTeensyBuffer buffer = UartPackToTeensy(input_message);
82 buffer[0]++;
83 EXPECT_FALSE(UartUnpackToTeensy(buffer));
84 }
85 {
86 UartToTeensyBuffer buffer = UartPackToTeensy(input_message);
87 buffer[buffer.size() - 1]++;
88 EXPECT_FALSE(UartUnpackToTeensy(buffer));
89 }
90 {
91 UartToTeensyBuffer buffer = UartPackToTeensy(input_message);
92 buffer.set_size(buffer.size() - 1);
93 EXPECT_FALSE(UartUnpackToTeensy(buffer));
94 }
95 {
96 UartToTeensyBuffer buffer = UartPackToTeensy(input_message);
97 buffer[0] = 255;
98 EXPECT_FALSE(UartUnpackToTeensy(buffer));
99 }
100}
101
102// Tests that corrupting the data in various ways is handled properly.
103TEST(UartToCameraTest, CorruptData) {
104 CameraCalibration input_message{};
105 {
106 UartToCameraBuffer buffer = UartPackToCamera(input_message);
107 buffer[0]++;
108 EXPECT_FALSE(UartUnpackToCamera(buffer));
109 }
110 {
111 UartToCameraBuffer buffer = UartPackToCamera(input_message);
112 buffer[buffer.size() - 1]++;
113 EXPECT_FALSE(UartUnpackToCamera(buffer));
114 }
115 {
116 UartToCameraBuffer buffer = UartPackToCamera(input_message);
117 buffer.set_size(buffer.size() - 1);
118 EXPECT_FALSE(UartUnpackToCamera(buffer));
119 }
120 {
121 UartToCameraBuffer buffer = UartPackToCamera(input_message);
122 buffer[0] = 255;
123 EXPECT_FALSE(UartUnpackToCamera(buffer));
124 }
125}
126
127} // namespace testing
128} // namespace jevois
129} // namespace frc971