thermostat/tests/load/test_load.cpp

172 lines
3.6 KiB
C++
Raw Normal View History

2024-09-18 19:48:01 +00:00
/*
* Author: Jake G
* Date: 2024
* filename: test_load.cpp
* description:
*/
#include "CppUTest/CommandLineTestRunner.h"
#include "CppUTestExt/MockSupport.h"
#include <cstdint>
//This define allows us to dircetly include the device header without error.
#define _AVR_IO_H_
extern "C"
{
#include <iotn404.h> //ATtiny404 header fille.
#include "load.h"
#include "MockADC.h"
#include "MockADC.h"
}
TEST_GROUP(test_load)
{
uint8_t adc_pin;
uint8_t load_pin;
2024-09-18 19:48:01 +00:00
void setup()
{
adc_pin = 4;
load_pin = 7;
2024-09-18 19:48:01 +00:00
}
void teardown()
{
mock().checkExpectations();
mock().clear();
}
};
TEST(test_load, LoadPass)
{
CHECK_TRUE(true);
}
void setup_adc_expectations(uint8_t adc_pin)
2024-09-18 19:48:01 +00:00
{
mock().expectOneCall("ADC_Init")
.withUnsignedIntParameter("pin_num", adc_pin);
2024-09-18 19:48:01 +00:00
mock().expectOneCall("ADC_Enable");
mock().expectOneCall("ADC_ReadValue_Impl")
.withUnsignedIntParameter("pin_num", adc_pin);
2024-09-18 19:48:01 +00:00
mock().expectOneCall("ADC_Disable");
}
void expect_porta_disabled(uint8_t load_pin)
{
2024-09-18 19:48:01 +00:00
mock().expectOneCall("RegEdit_ClearBit")
.withPointerParameter("reg", (void *) &PORTA.OUT)
.withUnsignedIntParameter("bit_num", load_pin);
2024-09-18 19:48:01 +00:00
}
void expect_porta_enabled(uint8_t load_pin)
{
mock().expectOneCall("RegEdit_SetBit")
.withPointerParameter("reg", (void *) &PORTA.DIR)
.withUnsignedIntParameter("bit_num", load_pin);
mock().expectOneCall("RegEdit_SetBit")
.withPointerParameter("reg", (void *) &PORTA.OUT)
.withUnsignedIntParameter("bit_num", load_pin);
}
void expect_portb_disabled(uint8_t load_pin)
{
mock().expectOneCall("RegEdit_ClearBit")
.withPointerParameter("reg", (void *) &PORTB.OUT)
.withUnsignedIntParameter("bit_num", load_pin);
}
TEST(test_load, PortAHandlerDisabledHigh)
{
MockADC_ZeroIndex();
MockADC_PushValue(HIGHTHRESH);
setup_adc_expectations(adc_pin);
expect_porta_disabled(load_pin);
Load_HandleLoadPortA(adc_pin, load_pin);
}
TEST(test_load, PortAHandlerDisabledLow)
{
MockADC_ZeroIndex();
MockADC_PushValue(LOWTHRESH);
setup_adc_expectations(adc_pin);
expect_porta_disabled(load_pin);
Load_HandleLoadPortA(adc_pin, load_pin);
}
TEST(test_load, PortAHandlerEnabledBelowTarget)
{
MockADC_ZeroIndex();
MockADC_PushValue(HYSTERESIS - 1);
setup_adc_expectations(adc_pin);
expect_porta_enabled(load_pin);
Load_HandleLoadPortA(adc_pin, load_pin);
}
TEST(test_load, PortAHandlerDisabledAboveTarget)
{
MockADC_ZeroIndex();
MockADC_PushValue(HYSTERESIS);
setup_adc_expectations(adc_pin);
expect_porta_disabled(load_pin);
Load_HandleLoadPortA(adc_pin, load_pin);
}
TEST(test_load, PortBHandlerSuccess)
{
/*
MockADC_ZeroIndex();
MockADC_PushValue(LOWTHRESH + 1);
MockADC_PushValue(HIGHTHRESH - 1);
MockADC_PushValue(HYSTERESIS - 1);
*/
setup_adc_expectations(adc_pin);
expect_portb_disabled(load_pin);
Load_HandleLoadPortB(adc_pin, load_pin);
}
TEST(test_load, HighThreshValid)
{
bool result = is_valid_load(HIGHTHRESH);
CHECK_TRUE(!result);
result = is_valid_load(HIGHTHRESH - 1);
CHECK_TRUE(result);
}
TEST(test_load, LowThreshValid)
{
bool result = is_valid_load(LOWTHRESH);
CHECK_TRUE(!result);
result = is_valid_load(LOWTHRESH + 1);
CHECK_TRUE(result);
}
TEST(test_load, BelowTargetValid)
{
bool result = is_below_target(HYSTERESIS - 1);
CHECK_TRUE(result);
result = is_below_target(HYSTERESIS);
CHECK_FALSE(result);
result = is_below_target(HYSTERESIS + 1);
CHECK_FALSE(result);
}