test_data.cpp
Go to the documentation of this file.
1 
31 #include "create/data.h"
32 #include "create/packet.h"
33 #include "create/types.h"
34 
35 #include "gtest/gtest.h"
36 
37 #include <memory>
38 
39 TEST(DataTest, Constructor)
40 {
41  create::Data data_default;
42 
43  create::Data data_v_1(create::V_1);
44 
45  create::Data data_v_2(create::V_2);
46 
47  create::Data data_v_3(create::V_3);
48 
49  create::Data data_v_all(create::V_ALL);
50 }
51 
52 // Number of packets for a given protocol are determined in the Data() constructor
53 TEST(DataTest, GetNumPackets)
54 {
55  // Number of packets shared by all protocols is 17
56  create::Data data_v_1(create::V_1);
57  // Number exclusive to V_1 = 3
58  // 17 + 3 = 20
59  EXPECT_EQ(static_cast<int>(data_v_1.getNumPackets()), 20);
60 
61  create::Data data_v_2(create::V_2);
62  // Number exclusive to V_2 = 3
63  // 17 + 3 = 20
64  EXPECT_EQ(static_cast<int>(data_v_2.getNumPackets()), 20);
65 
66  create::Data data_v_3(create::V_3);
67  // Number exclusive to V_3 = 13
68  // 17 + 13 = 30
69  EXPECT_EQ(static_cast<int>(data_v_3.getNumPackets()), 30);
70 
71  create::Data data_v_all(create::V_ALL);
72  EXPECT_EQ(static_cast<int>(data_v_all.getNumPackets()), 33);
73 }
74 
75 TEST(DataTest, GetPacket)
76 {
77  // Get a packet exclusive to V_1
78  create::Data data_v_1(create::V_1);
79  std::shared_ptr<create::Packet> v_1_packet_ptr = data_v_1.getPacket(create::ID_OVERCURRENTS);
80  EXPECT_NE(v_1_packet_ptr, std::shared_ptr<create::Packet>())
81  << "ID_OVERCURRENTS packet not found for protocol V_1";
82  EXPECT_EQ(static_cast<int>(v_1_packet_ptr->nbytes), 1);
83  EXPECT_EQ(v_1_packet_ptr->info, std::string("overcurrents"));
84 
85  // Get a packet for V_2
86  create::Data data_v_2(create::V_2);
87  std::shared_ptr<create::Packet> v_2_packet_ptr = data_v_2.getPacket(create::ID_DISTANCE);
88  EXPECT_NE(v_2_packet_ptr, std::shared_ptr<create::Packet>())
89  << "ID_DISTANCE packet not found for protocol V_2";
90  EXPECT_EQ(static_cast<int>(v_2_packet_ptr->nbytes), 2);
91  EXPECT_EQ(v_2_packet_ptr->info, std::string("distance"));
92 
93  // Get a packet exclusive to V_3
94  create::Data data_v_3(create::V_3);
95  std::shared_ptr<create::Packet> v_3_packet_ptr = data_v_3.getPacket(create::ID_LIGHT_FRONT_RIGHT);
96  EXPECT_NE(v_3_packet_ptr, std::shared_ptr<create::Packet>())
97  << "ID_LIGHT_FRONT_RIGHT packet not found for protocol V_3";
98  EXPECT_EQ(static_cast<int>(v_3_packet_ptr->nbytes), 2);
99  EXPECT_EQ(v_3_packet_ptr->info, std::string("light_bumper_front_right"));
100 
101  // Get a non-existent packet
102  std::shared_ptr<create::Packet> not_a_packet_ptr = data_v_3.getPacket(60);
103  EXPECT_EQ(not_a_packet_ptr, std::shared_ptr<create::Packet>());
104 }
105 
106 TEST(DataTest, GetPacketIDs)
107 {
108  create::Data data_v_3(create::V_3);
109  const std::vector<uint8_t> packet_ids = data_v_3.getPacketIDs();
110  // Vector should have same length as reported by getNumPackets()
111  ASSERT_EQ(static_cast<int>(packet_ids.size()), 30);
112 
113  // Vector should contain ID_LEFT_ENC
114  bool found = false;
115  for (std::size_t i = 0; (i < packet_ids.size()) && !found; i++)
116  {
117  if (packet_ids[i] == create::ID_LEFT_ENC)
118  {
119  found = true;
120  }
121  }
122  EXPECT_TRUE(found) << "ID_LEFT_ENC packet ID not returned for protocol V_3";
123 }
124 
125 TEST(DataTest, GetTotalDataBytes)
126 {
127  // All protocols have 21 mutual data bytes
128  // V_1 has an additional 5 bytes
129  create::Data data_v_1(create::V_1);
130  EXPECT_EQ(static_cast<int>(data_v_1.getTotalDataBytes()), 26);
131 
132  // V_2 has an additional 5 bytes
133  create::Data data_v_2(create::V_2);
134  EXPECT_EQ(static_cast<int>(data_v_2.getTotalDataBytes()), 26);
135 
136  // V_3 has an additional 21 bytes
137  create::Data data_v_3(create::V_3);
138  EXPECT_EQ(static_cast<int>(data_v_3.getTotalDataBytes()), 42);
139 }
140 
141 TEST(DataTest, IsValidPacketID)
142 {
143  create::Data data_v_1(create::V_1);
144  EXPECT_TRUE(data_v_1.isValidPacketID(create::ID_DIRT_DETECT_RIGHT))
145  << "ID_DIRT_DETECT_RIGHT packet not found for protocol V_1";
146  EXPECT_FALSE(data_v_1.isValidPacketID(create::ID_OI_MODE))
147  << "ID_OI_MODE packet should not exist for protocol V_1";
148 
149  // V_2 has an additional 5 bytes
150  create::Data data_v_2(create::V_2);
151  EXPECT_TRUE(data_v_2.isValidPacketID(create::ID_ANGLE))
152  << "ID_ANGLE packet not found for protocol V_2";
153  EXPECT_FALSE(data_v_2.isValidPacketID(create::ID_LIGHT))
154  << "ID_LIGHT packet should not exist for protocol V_2";
155 
156  // V_3 has an additional 21 bytes
157  create::Data data_v_3(create::V_3);
158  EXPECT_TRUE(data_v_3.isValidPacketID(create::ID_STASIS))
159  << "ID_STATIS packet not found for protocol V_3";
160  EXPECT_FALSE(data_v_3.isValidPacketID(create::ID_DISTANCE))
161  << "ID_DISTANCE packet should not exist for protocol V_3";
162 }
163 
164 TEST(DataTest, ValidateAll)
165 {
166  create::Data data_v_3(create::V_3);
167  // Don't crash
168  data_v_3.validateAll();
169 }
uint8_t getNumPackets() const
Definition: data.cpp:84
bool isValidPacketID(const uint8_t id) const
Definition: data.cpp:58
std::vector< uint8_t > getPacketIDs()
Definition: data.cpp:88
uint32_t getTotalDataBytes() const
Definition: data.cpp:80
std::shared_ptr< Packet > getPacket(const uint8_t id)
Definition: data.cpp:65
TEST(DataTest, Constructor)
Definition: test_data.cpp:39
void validateAll()
Definition: data.cpp:72


libcreate
Author(s): Jacob Perron
autogenerated on Sat May 8 2021 03:02:37