test_data.cpp
Go to the documentation of this file.
00001 
00031 #include "create/data.h"
00032 #include "create/packet.h"
00033 #include "create/types.h"
00034 
00035 #include "gtest/gtest.h"
00036 
00037 #include <boost/shared_ptr.hpp>
00038 
00039 TEST(DataTest, Constructor)
00040 {
00041   create::Data data_default;
00042 
00043   create::Data data_v_1(create::V_1);
00044 
00045   create::Data data_v_2(create::V_2);
00046 
00047   create::Data data_v_3(create::V_3);
00048 
00049   create::Data data_v_all(create::V_ALL);
00050 }
00051 
00052 // Number of packets for a given protocol are determined in the Data() constructor
00053 TEST(DataTest, GetNumPackets)
00054 {
00055   // Number of packets shared by all protocols is 16
00056   create::Data data_v_1(create::V_1);
00057   // Number exclusive to V_1 = 4
00058   // 16 + 4 = 20
00059   EXPECT_EQ(static_cast<int>(data_v_1.getNumPackets()), 20);
00060 
00061   create::Data data_v_2(create::V_2);
00062   // Number exclusive to V_2 = 3
00063   // 16 + 3 = 19
00064   EXPECT_EQ(static_cast<int>(data_v_2.getNumPackets()), 19);
00065 
00066   create::Data data_v_3(create::V_3);
00067   // Number exclusive to V_3 = 13
00068   // 16 + 13 = 29
00069   EXPECT_EQ(static_cast<int>(data_v_3.getNumPackets()), 29);
00070 
00071   create::Data data_v_all(create::V_ALL);
00072   EXPECT_EQ(static_cast<int>(data_v_all.getNumPackets()), 33);
00073 }
00074 
00075 TEST(DataTest, GetPacket)
00076 {
00077   // Get a packet exclusive to V_1
00078   create::Data data_v_1(create::V_1);
00079   boost::shared_ptr<create::Packet> v_1_packet_ptr = data_v_1.getPacket(create::ID_OVERCURRENTS);
00080   EXPECT_NE(v_1_packet_ptr, boost::shared_ptr<create::Packet>())
00081       << "ID_OVERCURRENTS packet not found for protocol V_1";
00082   EXPECT_EQ(static_cast<int>(v_1_packet_ptr->nbytes), 1);
00083   EXPECT_EQ(v_1_packet_ptr->info, std::string("overcurrents"));
00084 
00085   // Get a packet for V_2
00086   create::Data data_v_2(create::V_2);
00087   boost::shared_ptr<create::Packet> v_2_packet_ptr = data_v_2.getPacket(create::ID_DISTANCE);
00088   EXPECT_NE(v_2_packet_ptr, boost::shared_ptr<create::Packet>())
00089       << "ID_DISTANCE packet not found for protocol V_2";
00090   EXPECT_EQ(static_cast<int>(v_2_packet_ptr->nbytes), 2);
00091   EXPECT_EQ(v_2_packet_ptr->info, std::string("distance"));
00092 
00093   // Get a packet exclusive to V_3
00094   create::Data data_v_3(create::V_3);
00095   boost::shared_ptr<create::Packet> v_3_packet_ptr = data_v_3.getPacket(create::ID_LIGHT_FRONT_RIGHT);
00096   EXPECT_NE(v_3_packet_ptr, boost::shared_ptr<create::Packet>())
00097       << "ID_LIGHT_FRONT_RIGHT packet not found for protocol V_3";
00098   EXPECT_EQ(static_cast<int>(v_3_packet_ptr->nbytes), 2);
00099   EXPECT_EQ(v_3_packet_ptr->info, std::string("light_bumper_front_right"));
00100 
00101   // Get a non-existent packet
00102   boost::shared_ptr<create::Packet> not_a_packet_ptr = data_v_3.getPacket(60);
00103   EXPECT_EQ(not_a_packet_ptr, boost::shared_ptr<create::Packet>());
00104 }
00105 
00106 TEST(DataTest, GetPacketIDs)
00107 {
00108   create::Data data_v_3(create::V_3);
00109   const std::vector<uint8_t> packet_ids = data_v_3.getPacketIDs();
00110   // Vector should have same length as reported by getNumPackets()
00111   ASSERT_EQ(static_cast<int>(packet_ids.size()), 29);
00112 
00113   // Vector should contain ID_LEFT_ENC
00114   bool found = false;
00115   for (std::size_t i = 0; (i < packet_ids.size()) && !found; i++)
00116   {
00117     if (packet_ids[i] == create::ID_LEFT_ENC)
00118     {
00119       found = true;
00120     }
00121   }
00122   EXPECT_TRUE(found) << "ID_LEFT_ENC packet ID not returned for protocol V_3";
00123 }
00124 
00125 TEST(DataTest, GetTotalDataBytes)
00126 {
00127   // All protocols have 20 mutual data bytes
00128   // V_1 has an additional 6 bytes
00129   create::Data data_v_1(create::V_1);
00130   EXPECT_EQ(static_cast<int>(data_v_1.getTotalDataBytes()), 26);
00131 
00132   // V_2 has an additional 5 bytes
00133   create::Data data_v_2(create::V_2);
00134   EXPECT_EQ(static_cast<int>(data_v_2.getTotalDataBytes()), 25);
00135 
00136   // V_3 has an additional 21 bytes
00137   create::Data data_v_3(create::V_3);
00138   EXPECT_EQ(static_cast<int>(data_v_3.getTotalDataBytes()), 41);
00139 }
00140 
00141 TEST(DataTest, IsValidPacketID)
00142 {
00143   create::Data data_v_1(create::V_1);
00144   EXPECT_TRUE(data_v_1.isValidPacketID(create::ID_DIRT_DETECT_RIGHT))
00145       << "ID_DIRT_DETECT_RIGHT packet not found for protocol V_1";
00146   EXPECT_FALSE(data_v_1.isValidPacketID(create::ID_OI_MODE))
00147       << "ID_OI_MODE packet should not exist for protocol V_1";
00148 
00149   // V_2 has an additional 5 bytes
00150   create::Data data_v_2(create::V_2);
00151   EXPECT_TRUE(data_v_2.isValidPacketID(create::ID_ANGLE))
00152       << "ID_ANGLE packet not found for protocol V_2";
00153   EXPECT_FALSE(data_v_2.isValidPacketID(create::ID_LIGHT))
00154       << "ID_LIGHT packet should not exist for protocol V_2";
00155 
00156   // V_3 has an additional 21 bytes
00157   create::Data data_v_3(create::V_3);
00158   EXPECT_TRUE(data_v_3.isValidPacketID(create::ID_STASIS))
00159       << "ID_STATIS packet not found for protocol V_3";
00160   EXPECT_FALSE(data_v_3.isValidPacketID(create::ID_DISTANCE))
00161       << "ID_DISTANCE packet should not exist for protocol V_3";
00162 }
00163 
00164 TEST(DataTest, ValidateAll)
00165 {
00166   create::Data data_v_3(create::V_3);
00167   // Don't crash
00168   data_v_3.validateAll();
00169 }


libcreate
Author(s): Jacob Perron
autogenerated on Thu Jun 6 2019 21:02:06