Relay_Tester/tests/LedController/test_LedController.cpp

162 lines
3.1 KiB
C++

/*
* Author: username
* Date: todays_date
* filename: test_LedController.c
* description: module_purpose
*/
#include "CppUTest/CommandLineTestRunner.h"
extern "C"
{
#include "LedController.h"
}
TEST_GROUP(test_LedController)
{
void setup()
{
}
void teardown()
{
}
};
TEST(test_LedController, selftest)
{
CHECK_TRUE(true);
}
TEST(test_LedController, LedSetHighWorks)
{
uint8_t fake_port = 0x00;
Led fake_led;
fake_led.port = &fake_port;
fake_led.pin_num = 0;
fake_led.state = false;
LedController_SetHigh(&fake_led);
//Check that it sets the correct bit.
CHECK_TRUE(fake_port & 0x01);
//It should set the state to true.
CHECK_TRUE(fake_led.state);
//Only the first bit should have been set
CHECK_TRUE(fake_port == 0x01);
}
TEST(test_LedController, LedSetHighMaintainsPortState)
{
uint8_t fake_port = 0x00;
Led fake_led;
fake_led.port = &fake_port;
fake_led.pin_num = 0;
fake_led.state = false;
Led led_two;
led_two.pin_num = 1;
led_two.port = &fake_port;
led_two.state = false;
LedController_SetHigh(&fake_led);
LedController_SetHigh(&led_two);
CHECK_TRUE(fake_port & 0x01);
CHECK_TRUE(fake_port & 0x02);
}
TEST(test_LedController, LedSetLow)
{
uint8_t fake_port = 0x01;
Led fake_led;
fake_led.port = &fake_port;
fake_led.pin_num = 0;
fake_led.state = true;
LedController_SetLow(&fake_led);
CHECK_EQUAL(0x00, fake_port);
CHECK_EQUAL(false, fake_led.state);
}
/*
* what's the best way to handle an array of 8 leds. Should they
* reside in a structure? Or should it just be an array that the user defines?
*/
TEST(test_LedController, NewLedByte)
{
uint8_t fake_port = 0x00;
LedByte led_byte = LedController_New(&fake_port);
for(int i = 0; i < 8; i++){
CHECK_TRUE(led_byte.leds[i].state == false);
CHECK_EQUAL(&fake_port, led_byte.leds[i].port);
CHECK_EQUAL(false, led_byte.leds[i].state);
}
}
TEST(test_LedController, LedByteDisplay)
{
uint8_t fake_port = 0x00;
uint8_t byte = 0xFF;
LedByte led_byte = LedController_New(&fake_port);
LedController_ShowByte(&led_byte, byte);
for(int i = 0; i < 8; i++){
CHECK_TRUE(led_byte.leds[i].state);
}
CHECK_EQUAL(0xFF, fake_port);
}
TEST(test_LedController, LedByteDisplayPattern)
{
uint8_t fake_port = 0x00;
uint8_t byte = 0xAA;
LedByte led_byte = LedController_New(&fake_port);
LedController_ShowByte(&led_byte, byte);
for(int i = 0; i < 8; i++){
if(byte & (1<<i)) {
CHECK_TRUE(led_byte.leds[i].state);
}
else {
CHECK_TRUE(!led_byte.leds[i].state);
}
}
CHECK_EQUAL(0xAA, fake_port);
}
TEST(test_LedController, ClearingLedByteWorks)
{
uint8_t fake_port = 0xFF;
LedByte led_byte = LedController_New(&fake_port);
for(int i = 0; i < 8; i++){
led_byte.leds[i].state = true;
}
LedControler_ClearByte(&led_byte);
for(int i = 0; i < 8; i++){
CHECK_TRUE(!led_byte.leds[i].state);
/*loop body*/
}
CHECK_EQUAL(0x00, fake_port);
}