5 #include <SimpleIteratorCommand.h> 6 #include <Communicator.h> 7 #include "../include/CommandManager.h" 8 #include "../catch/catch.hpp" 9 #include "sensors/IRangeFinderSensor.h" 10 #include "sensors/SensorManager.h" 11 #include "hardwareinterface/MotorAdapter.h" 12 #include "sensors/EncoderSensor.h" 13 #include "exceptions/DataSizeException.h" 15 #include "../include/hardwareinterface/EncoderAdapter.h" 18 #include "mocks/include/MockIRRangeFinderSensor.h" 19 #include "mocks/include/MockSensorManager.h" 20 #include "mocks/include/MockHardwareInterface.h" 21 #include "mocks/include/MockIRRangeFinderSensorMessage.h" 22 #include "mocks/include/SimpleIteratorCommand.h" 25 TEST_CASE(
"Command subsystem tests",
"[CommandManager]") {
27 SECTION(
"Command manager can be killed.") {
29 REQUIRE(commandManager->
kill() == 0);
30 delete commandManager;
33 SECTION(
"Simple commands run to completion.") {
39 REQUIRE(commandManager1->
inFlight() == 2);
41 REQUIRE(sc1->getCount() == 10);
42 REQUIRE(sc2->getCount() == 20);
43 REQUIRE(commandManager1->
inFlight() == 0);
44 commandManager1->
kill();
45 delete commandManager1;
50 SECTION(
"Commands can be canceled.") {
54 commandManager2->
cancel(sc3);
56 REQUIRE(sc3->getCount() != 1000);
65 REQUIRE(sc4->getCount() != 1000);
66 REQUIRE(sc5->getCount() != 2000);
67 REQUIRE(sc6->getCount() != 3000);
68 commandManager2->
kill();
69 delete commandManager2;
77 TEST_CASE(
"ISensorManager tests",
"[ISensorManager]") {
81 SECTION(
"Adding hardware twice results in exception.") {
82 Hardware mockHardware = H_LEFT_MOTOR;
85 sensorManager->
addSensor(mockHardware, mockSensor);
87 sensorManager->
addSensor(mockHardware, mockSensor),
92 SECTION(
"Updating non-existent hardware results in exception") {
93 int data = FULL_FORWARD;
95 std::list<IMessage*>* messages =
new std::list<IMessage*>;
96 messages->push_back(msg);
104 SECTION(
"Updates sensors correctly") {
107 sensorManager->
addSensor(mockHardware, mockSensor);
109 std::string data =
"255";
111 std::list<IMessage*>* messages =
new std::list<IMessage*>;
112 messages->push_back(msg);
116 REQUIRE(mockSensor->getData() == data);
119 SECTION(
"Destructor") {
120 delete sensorManager;
124 TEST_CASE(
"Communicator Tests",
"[Communicator]") {
130 SECTION(
"Test queuing message for single hardware interface") {
132 Hardware mockHardware = {255,
sizeof(int)};
134 std::list<IMessage*>* readMessages =
new std::list<IMessage*>;
135 mockHardwareInterface->setReadMessages(readMessages);
141 int mockData = FULL_FORWARD;
150 REQUIRE(mockHardwareInterface->getWriteMessages()->size() == 1);
151 REQUIRE(mockHardwareInterface->getWriteMessages()->front() == mockMessage);
154 SECTION(
"Test queuing messages for multiple hardware interfaces") {
155 Hardware mockHardware1 = {255,
sizeof(int)};
156 Hardware mockHardware2 = {254,
sizeof(int)};
166 int data1 = FULL_FORWARD, data2 = FULL_BACKWARD;
178 REQUIRE(mockHardwareInterface1->getWriteMessages()->size() == 1);
179 REQUIRE(mockHardwareInterface2->getWriteMessages()->size() == 1);
181 REQUIRE(mockHardwareInterface1->getWriteMessages()->front() == mockMessage1);
182 REQUIRE(mockHardwareInterface2->getWriteMessages()->front() == mockMessage2);
185 SECTION(
"Test receiving message from single hardware interface") {
186 Hardware mockHardware = {255,
sizeof(int)};
193 int data = FULL_FORWARD;
195 std::list<IMessage*>* messages =
new std::list<IMessage*>;
196 messages->push_back(mockMessage);
197 mockHardwareInterface->setReadMessages(messages);
205 REQUIRE(mockSensorManager->updateMessages->size() == 1);
206 REQUIRE(mockSensorManager->updateMessages->front() == mockMessage);
209 SECTION(
"Test receiving messages from multiple hardware interfaces") {
210 Hardware mockHardware1 = {255,
sizeof(int)};
211 Hardware mockHardware2 = {254,
sizeof(int)};
220 int data1 = FULL_FORWARD, data2 = FULL_BACKWARD;
223 std::list<IMessage*>* messages1 =
new std::list<IMessage*>;
224 messages1->push_back(mockMessage1);
225 std::list<IMessage*>* messages2 =
new std::list<IMessage*>;
226 messages2->push_back(mockMessage2);
228 mockHardwareInterface1->setReadMessages(messages1);
229 mockHardwareInterface2->setReadMessages(messages2);
237 REQUIRE(mockSensorManager->updateMessages->size() == 2);
239 REQUIRE(mockSensorManager->updateMessages->front() == mockMessage1);
240 mockSensorManager->updateMessages->pop_front();
241 REQUIRE(mockSensorManager->updateMessages->front() == mockMessage2);
244 SECTION(
"Test throws duplicate hardware exception when hardware with the same address is registered more than once.") {
245 Hardware mockHardware1 = {255,
sizeof(int)};
246 Hardware mockHardware2 = {255,
sizeof(int)};
258 SECTION(
"Test queue list of messages sent to single interface") {
259 Hardware mockHardware = {255,
sizeof(int)};
266 int data1 = FULL_FORWARD, data2 = FULL_BACKWARD;
269 std::list<IMessage*>* messages =
new std::list<IMessage*>;
270 messages->push_back(mockMessage1);
271 messages->push_back(mockMessage2);
287 SECTION(
"Test queue list of messages sent to multiple interfaces") {
288 Hardware mockHardware1 = {255,
sizeof(int)};
289 Hardware mockHardware2 = {254,
sizeof(int)};
298 int data1 = FULL_FORWARD, data2 = FULL_BACKWARD;
301 std::list<IMessage*>* messages =
new std::list<IMessage*>;
302 messages->push_back(mockMessage1);
303 messages->push_back(mockMessage2);
312 REQUIRE(mockHardwareInterface1->getWriteMessages()->size() == 1);
313 REQUIRE(mockHardwareInterface2->getWriteMessages()->size() == 1);
315 REQUIRE(mockHardwareInterface1->getWriteMessages()->front() == mockMessage1);
316 REQUIRE(mockHardwareInterface2->getWriteMessages()->front() == mockMessage2);
319 SECTION(
"Test queuing multiple messages for different hardware sent to the same hardware interface") {
320 Hardware mockHardware1 = {255,
sizeof(int)};
321 Hardware mockHardware2 = {254,
sizeof(int)};
329 int data1 = FULL_FORWARD, data2 = FULL_BACKWARD;
332 std::list<IMessage*>* messages =
new std::list<IMessage*>;
333 messages->push_back(mockMessage1);
334 messages->push_back(mockMessage2);
350 SECTION(
"Destructor") {
356 TEST_CASE(
"MotorMessage Tests",
"[MotorMessage]") {
358 unsigned char address = 255;
359 Hardware mockHardware = {address,
sizeof(int)};
360 int mockData = FULL_FORWARD;
363 SECTION(
"GetHardware returns proper hardware") {
367 SECTION(
"GetData returns proper data") {
368 REQUIRE(msg->
getData() == mockData);
371 SECTION(
"Not enough data throws MessageLengthException") {
373 Hardware mockHardware = {address, 2};
381 SECTION(
"Too much data throws MessageLengthException") {
383 Hardware mockHardware = {address, 0};
391 SECTION(
"Serialize to proper string") {
393 serial.append(1, address);
394 serial.append((
char*)(&mockData),
sizeof(
int));
397 int unserializeData = *((
int*)(serial.c_str() +
sizeof(char)));
398 REQUIRE(unserializeData == mockData);
401 SECTION(
"Data too large throws DataSizeException") {
408 SECTION(
"Data too small throws DataSizeException") {
415 SECTION(
"Destructor") {
421 TEST_CASE(
"EncoderMessage Tests",
"[EncoderMessage]") {
422 unsigned char address = 255;
423 Hardware mockHardware = {address,
sizeof(double)};
424 double mockData = 128.0;
427 SECTION(
"GetHardware returns proper hardware") {
431 SECTION(
"GetData returns proper data") {
432 REQUIRE(msg->
getData() == mockData);
435 SECTION(
"Not enough data throws MessageLengthException") {
437 Hardware mockHardware = {address, 10};
445 SECTION(
"Too much data throws MessageLengthException") {
447 Hardware mockHardware = {address, 0};
455 SECTION(
"Serialize to proper string") {
457 serial.append(1, address);
458 serial.append((
char*)(&mockData),
sizeof(
double));
465 double data = *((
double*)(serial.c_str() +
sizeof(char)));
466 REQUIRE(data == mockData);
470 SECTION(
"Destructor") {
475 TEST_CASE(
"MotorAdapter Tests",
"[MotorAdapter]") {
477 int forwardPin = 1, backwardPin = 2;
482 SECTION(
"Read returns empty list. Cannot read motors.") {
483 REQUIRE(adapter->
read()->size() == 0);
486 SECTION(
"Drive the motor full backwards") {
487 int speed = FULL_BACKWARD;
494 SECTION(
"Drive the motor full forwards") {
495 int speed = FULL_FORWARD;
502 SECTION(
"MismatchedMessageException thrown when message of wrong type is passed in") {
511 SECTION(
"Destructor") {
518 TEST_CASE(
"EncoderAdapter Tests",
"[EncoderAdapter]") {
520 Hardware mockHardware = {255,
sizeof(double)};
521 int ticksPerRev = 100;
522 int channelA = 0, channelB = 1;
525 SECTION(
"Destructor") {
530 TEST_CASE(
"EncoderSensor Tests",
"[EncoderSensor]") {
534 SECTION(
"Sensor value updates correctly") {
539 REQUIRE(sensor->
getRPM() == data);
543 SECTION(
"MismatchMessageException thrown if wrong message type sent to encoder") {
552 SECTION(
"Destructor") {
void write(IMessage *msg) override
virtual void updateSensors(std::list< IMessage * > *messages)=0
bool cancel(Command *command)
void updateSensor(IMessage *message)
std::string serialize() override
virtual void addSensor(Hardware hardware, ISensor *sensor)=0
void registerHardware(Hardware hardware, IHardwareInterface *interface)
void queueMessage(IMessage *message)
std::list< IMessage * > * read() override
bool runCommand(Command *command)
std::string serialize() override