Added tests and mocks from other template
This commit is contained in:
parent
34afa025aa
commit
455c70c34b
23 changed files with 957 additions and 3 deletions
|
@ -1,4 +1,6 @@
|
|||
|
||||
add_subdirectory(MockRegEdit)
|
||||
add_subdirectory(MockADC)
|
||||
add_subdirectory(TimerMock)
|
||||
|
||||
|
||||
|
||||
|
|
7
mocks/MockADC/CMakeLists.txt
Normal file
7
mocks/MockADC/CMakeLists.txt
Normal file
|
@ -0,0 +1,7 @@
|
|||
add_library(MockADC STATIC
|
||||
MockADC.c
|
||||
)
|
||||
|
||||
target_include_directories(MockADC PUBLIC
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
)
|
84
mocks/MockADC/MockADC.c
Normal file
84
mocks/MockADC/MockADC.c
Normal file
|
@ -0,0 +1,84 @@
|
|||
/*
|
||||
* Author: username
|
||||
* Date: 2024
|
||||
* filename: MockADC.c
|
||||
* description: module_purpose
|
||||
*/
|
||||
|
||||
#include "MockADC.h"
|
||||
#include "CppUTestExt/MockSupport_c.h"
|
||||
|
||||
#define FAKESIZE 256
|
||||
|
||||
uint16_t fake_data[FAKESIZE];
|
||||
int fake_index = 0;
|
||||
|
||||
static bool is_setup = false;
|
||||
|
||||
|
||||
|
||||
void ADC_SetPin(uint8_t pin_num)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
void ADC_Setup(void)
|
||||
{
|
||||
is_setup = true;
|
||||
return;
|
||||
}
|
||||
|
||||
void ADC_Init(uint8_t pin_num)
|
||||
{
|
||||
mock_c()->actualCall("ADC_Init")
|
||||
->withUnsignedIntParameters("pin_num", pin_num);
|
||||
}
|
||||
|
||||
void ADC_Enable(void)
|
||||
{
|
||||
mock_c()->actualCall("ADC_Enable");
|
||||
}
|
||||
|
||||
void ADC_Disable()
|
||||
{
|
||||
mock_c()->actualCall("ADC_Disable");
|
||||
}
|
||||
|
||||
uint16_t ADC_ReadValue_Impl(uint8_t pin_num)
|
||||
{
|
||||
mock_c()->actualCall("ADC_ReadValue_Impl")
|
||||
->withUnsignedIntParameters("pin_num", pin_num);
|
||||
|
||||
if(fake_index == 0){
|
||||
return 0;
|
||||
}
|
||||
return fake_data[--fake_index];
|
||||
}
|
||||
|
||||
uint16_t (*ADC_ReadValue)(uint8_t pin_num) = ADC_ReadValue_Impl;
|
||||
|
||||
|
||||
void MockADC_PushValue(uint16_t value){
|
||||
if(fake_index >= FAKESIZE - 1){
|
||||
return;
|
||||
}
|
||||
fake_data[fake_index++] = value;
|
||||
}
|
||||
|
||||
|
||||
void MockADC_ZeroIndex(void)
|
||||
{
|
||||
fake_index = 0;
|
||||
}
|
||||
|
||||
|
||||
int MockADC_GetIndex(void)
|
||||
{
|
||||
return fake_index;
|
||||
}
|
||||
|
||||
|
||||
bool MockADC_IsSetup(void)
|
||||
{
|
||||
return is_setup;
|
||||
}
|
30
mocks/MockADC/MockADC.h
Normal file
30
mocks/MockADC/MockADC.h
Normal file
|
@ -0,0 +1,30 @@
|
|||
/**
|
||||
* @brief PUT_TEXT_HERE
|
||||
* @details This file is...
|
||||
* @author username
|
||||
* @date todays_date
|
||||
* @copyright None
|
||||
* @file MOCKADC.h
|
||||
*/
|
||||
|
||||
#ifndef MOCKADC_H
|
||||
#define MOCKADC_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
void ADC_Setup(void);
|
||||
void ADC_SetPin(uint8_t pin_num);
|
||||
void ADC_Init(uint8_t pin_num);
|
||||
void ADC_Enable(void);
|
||||
void ADC_Disable(void);
|
||||
|
||||
extern uint16_t (*ADC_ReadValue)(uint8_t pin_num);
|
||||
|
||||
void MockADC_PushValue(uint16_t value);
|
||||
void MockADC_ZeroIndex(void);
|
||||
int MockADC_GetIndex(void);
|
||||
bool MockADC_IsSetup(void);
|
||||
|
||||
#endif //MOCKADC_H
|
12
mocks/MockRegEdit/CMakeLists.txt
Normal file
12
mocks/MockRegEdit/CMakeLists.txt
Normal file
|
@ -0,0 +1,12 @@
|
|||
add_library(MockRegEdit STATIC
|
||||
MockRegEdit.c
|
||||
)
|
||||
|
||||
target_include_directories(MockRegEdit PUBLIC
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
)
|
||||
|
||||
target_link_libraries(MockRegEdit
|
||||
${CPPUTEST_LIBRARIES}/libCppUTest.a
|
||||
${CPPUTEST_LIBRARIES}/libCppUTestExt.a
|
||||
)
|
85
mocks/MockRegEdit/MockRegEdit.c
Normal file
85
mocks/MockRegEdit/MockRegEdit.c
Normal file
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
* Author: username
|
||||
* Date: 2024
|
||||
* filename: MockRegEdit.c
|
||||
* description: module_purpose
|
||||
*/
|
||||
|
||||
#include "MockRegEdit.h"
|
||||
#include "CppUTestExt/MockSupport_c.h"
|
||||
|
||||
|
||||
void RegEdit_SetRegister(void *reg)
|
||||
{
|
||||
mock_c()->actualCall("RegEdit_SetRegister")
|
||||
->withPointerParameters("reg", reg);
|
||||
}
|
||||
|
||||
void RegEdit_ClearRegister(void *reg)
|
||||
{
|
||||
mock_c()->actualCall("RegEdit_ClearRegister")
|
||||
->withPointerParameters("reg", reg);
|
||||
}
|
||||
|
||||
|
||||
void RegEdit_SetBit(void *reg, uint8_t bit_num)
|
||||
{
|
||||
mock_c()->actualCall("RegEdit_SetBit")
|
||||
->withPointerParameters("reg", reg)
|
||||
->withUnsignedIntParameters("bit_num", bit_num);
|
||||
}
|
||||
|
||||
|
||||
void RegEdit_ClearBit(void *reg, uint8_t bit_num)
|
||||
{
|
||||
mock_c()->actualCall("RegEdit_ClearBit")
|
||||
->withPointerParameters("reg", reg)
|
||||
->withUnsignedIntParameters("bit_num", bit_num);
|
||||
}
|
||||
|
||||
|
||||
bool RegEdit_IsBitSet(void *reg, uint8_t bit_num)
|
||||
{
|
||||
|
||||
return mock_c()->actualCall("RegEdit_IsBitSet")
|
||||
->withPointerParameters("reg", reg)
|
||||
->withUnsignedIntParameters("bit_num", bit_num)
|
||||
->returnBoolValueOrDefault(true);
|
||||
//return mock_c()->returnBoolValueOrDefault(true);
|
||||
}
|
||||
|
||||
void RegEdit_OR_Num(void *reg, uint32_t num)
|
||||
{
|
||||
mock_c()->actualCall("RegEdit_OR_Num")
|
||||
->withPointerParameters("reg", reg)
|
||||
->withUnsignedIntParameters("num", num);
|
||||
}
|
||||
|
||||
|
||||
void RegEdit_AND_Num(void *reg, uint32_t num)
|
||||
{
|
||||
mock_c()->actualCall("RegEdit_AND_Num")
|
||||
->withPointerParameters("reg", reg)
|
||||
->withUnsignedIntParameters("num", num);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void RegEdit_SetNum(void *reg, uint32_t num)
|
||||
{
|
||||
mock_c()->actualCall("RegEdit_SetNum")
|
||||
->withPointerParameters("reg", reg)
|
||||
->withUnsignedIntParameters("num", num);
|
||||
}
|
||||
|
||||
|
||||
uint8_t RegEdit_ReadReg(void *reg)
|
||||
{
|
||||
uint8_t value = *(uint8_t *)reg;
|
||||
|
||||
mock_c()->actualCall("RegEdit_ReadReg")
|
||||
->withPointerParameters("reg", reg)
|
||||
->returnUnsignedIntValueOrDefault(value);
|
||||
|
||||
return value;
|
||||
}
|
30
mocks/MockRegEdit/MockRegEdit.h
Normal file
30
mocks/MockRegEdit/MockRegEdit.h
Normal file
|
@ -0,0 +1,30 @@
|
|||
/**
|
||||
* @brief PUT_TEXT_HERE
|
||||
* @details This file is...
|
||||
* @author username
|
||||
* @date todays_date
|
||||
* @copyright None
|
||||
* @file MockRegEdit.h
|
||||
*/
|
||||
|
||||
#ifndef MOCKREGEDIT_H
|
||||
#define MOCKREGEDIT_H
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
void RegEdit_SetRegister(void *reg);
|
||||
void RegEdit_ClearRegister(void *reg);
|
||||
void RegEdit_SetBit(void *reg, uint8_t bit_num);
|
||||
void RegEdit_ClearBit(void *reg, uint8_t bit_num);
|
||||
bool RegEdit_IsBitSet(void *reg, uint8_t bit_num);
|
||||
|
||||
void RegEdit_OR_Num(void *reg, uint32_t num);
|
||||
void RegEdit_AND_Num(void *reg, uint32_t num);
|
||||
|
||||
void RegEdit_SetNum(void *reg, uint32_t num);
|
||||
|
||||
uint8_t RegEdit_ReadReg(void *reg);
|
||||
|
||||
#endif //MOCKREGEDIT_H
|
51
mocks/MockRegEdit/u8_comparator.cpp
Normal file
51
mocks/MockRegEdit/u8_comparator.cpp
Normal file
|
@ -0,0 +1,51 @@
|
|||
#include "u8_comparator.hpp"
|
||||
#include "CppUTest/SimpleString.h"
|
||||
|
||||
/*
|
||||
class MyTypeComparator : public MockNamedValueComparator
|
||||
{
|
||||
public:
|
||||
virtual bool isEqual(const void* object1, const void* object2)
|
||||
{
|
||||
return object1 == object2;
|
||||
}
|
||||
virtual SimpleString valueToString(const void* object)
|
||||
{
|
||||
return StringFrom(object);
|
||||
}
|
||||
};
|
||||
*/
|
||||
|
||||
bool UInt8PointerComparator::isEqual(const void* object1, const void* object2) {
|
||||
const uint8_t* ptr1 = reinterpret_cast<const uint8_t*>(object1);
|
||||
const uint8_t* ptr2 = reinterpret_cast<const uint8_t*>(object2);
|
||||
return std::memcmp(ptr1, ptr2, sizeof(uint8_t)) == 0;
|
||||
}
|
||||
|
||||
SimpleString UInt8PointerComparator::valueToString(const void* object) {
|
||||
const uint8_t* ptr = reinterpret_cast<const uint8_t*>(object);
|
||||
return StringFromFormat("0x%02x", *ptr);
|
||||
}
|
||||
|
||||
/*
|
||||
bool UInt8PointerComparator::isEqual(const void* object1, const void* object2) const {
|
||||
const uint8_t* ptr1 = static_cast<const uint8_t*>(object1);
|
||||
const uint8_t* ptr2 = static_cast<const uint8_t*>(object2);
|
||||
return std::memcmp(ptr1, ptr2, sizeof(uint8_t)) == 0;
|
||||
}
|
||||
|
||||
SimpleString UInt8PointerComparator::valueToString(const void* object) const {
|
||||
const uint8_t* ptr = static_cast<const uint8_t*>(object);
|
||||
return StringFromFormat("0x%02x", *ptr);
|
||||
}
|
||||
*/
|
||||
|
||||
bool UInt8Comparator::isEqual(const void* object1, const void* object2) {
|
||||
return (uint8_t*)object1 == (uint8_t *)object2;
|
||||
}
|
||||
|
||||
SimpleString UInt8Comparator::valueToString(const void* object) {
|
||||
//uint8_t value = reinterpret_cast<uint8_t>(object);
|
||||
const uint8_t *ptr = reinterpret_cast<const uint8_t*>(object);
|
||||
return StringFromFormat("0x%02x", *ptr);
|
||||
}
|
20
mocks/MockRegEdit/u8_comparator.hpp
Normal file
20
mocks/MockRegEdit/u8_comparator.hpp
Normal file
|
@ -0,0 +1,20 @@
|
|||
#ifndef U8_COMPARATOR_H
|
||||
#define U8_COMPARATOR_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <CppUTestExt/MockSupport.h>
|
||||
|
||||
class UInt8PointerComparator : public MockNamedValueComparator {
|
||||
public:
|
||||
virtual bool isEqual(const void* object1, const void* object2) override;
|
||||
SimpleString valueToString(const void* object) override;
|
||||
};
|
||||
|
||||
class UInt8Comparator : public MockNamedValueComparator {
|
||||
public:
|
||||
virtual bool isEqual(const void* object1, const void* object2) override;
|
||||
SimpleString valueToString(const void* object) override;
|
||||
};
|
||||
|
||||
#endif //U8_COMPARATOR_H
|
7
mocks/TimerMock/CMakeLists.txt
Normal file
7
mocks/TimerMock/CMakeLists.txt
Normal file
|
@ -0,0 +1,7 @@
|
|||
add_library(TimerMock STATIC
|
||||
TimerMock.c
|
||||
)
|
||||
|
||||
target_include_directories(TimerMock PUBLIC
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
)
|
31
mocks/TimerMock/TimerMock.c
Normal file
31
mocks/TimerMock/TimerMock.c
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Author: Jake G
|
||||
* Date: 2024-09-02
|
||||
* filename: TimerMock.c
|
||||
* description: mocks timers
|
||||
*/
|
||||
|
||||
#include "TimerMock.h"
|
||||
#include <stdbool.h>
|
||||
#include "CppUTestExt/MockSupport_c.h"
|
||||
|
||||
static bool timer_started = false;
|
||||
|
||||
void Timer_Start(void)
|
||||
{
|
||||
mock_c()->actualCall("Timer_Start");
|
||||
timer_started = true;
|
||||
}
|
||||
|
||||
void Timer_Stop(void)
|
||||
{
|
||||
mock_c()->actualCall("Timer_Stop");
|
||||
timer_started = false;
|
||||
}
|
||||
|
||||
uint16_t Timer_GetOverflowCount(void)
|
||||
{
|
||||
uint16_t time = 0xAAAA;
|
||||
return mock_c()->actualCall("Timer_GetOverflowCount")
|
||||
->returnUnsignedIntValueOrDefault(time);
|
||||
}
|
26
mocks/TimerMock/TimerMock.h
Normal file
26
mocks/TimerMock/TimerMock.h
Normal file
|
@ -0,0 +1,26 @@
|
|||
/**
|
||||
* @brief A Mock of the timer module.
|
||||
* @details This file is only used for testing.
|
||||
* @author Jake G
|
||||
* @date 2024-09-02
|
||||
* @copyright None
|
||||
* @file TimerMock.h
|
||||
*/
|
||||
|
||||
#ifndef TIMER_MOCK_H
|
||||
#define TIMER_MOCK_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/**
|
||||
* A function
|
||||
* @param a The first argument
|
||||
*/
|
||||
void Timer_Start(void);
|
||||
|
||||
void Timer_Stop(void);
|
||||
|
||||
uint16_t Timer_GetOverflowCount(void);
|
||||
|
||||
|
||||
#endif //TIMER_MOCK_H
|
17
tests/ADC/CMakeLists.txt
Normal file
17
tests/ADC/CMakeLists.txt
Normal file
|
@ -0,0 +1,17 @@
|
|||
# TEST_RUNNER
|
||||
add_library(test_ADC
|
||||
test_ADC.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(test_ADC
|
||||
${CPPUTEST_LIBRARIES}/libCppUTest.a
|
||||
${CPPUTEST_LIBRARIES}/libCppUTestExt.a
|
||||
ADC
|
||||
MockRegEdit
|
||||
)
|
||||
|
||||
#Needed for the tests to function
|
||||
include_directories(
|
||||
/usr/local/avr/include/avr
|
||||
#/usr/lib/avr/include/avr
|
||||
)
|
192
tests/ADC/test_ADC.cpp
Normal file
192
tests/ADC/test_ADC.cpp
Normal file
|
@ -0,0 +1,192 @@
|
|||
/*
|
||||
* Author: Jake G
|
||||
* Date: 2024
|
||||
* filename: test_ADC.c
|
||||
* description: module_purpose
|
||||
*/
|
||||
|
||||
#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 "ADC.h"
|
||||
}
|
||||
|
||||
TEST_GROUP(test_ADC)
|
||||
{
|
||||
void setup()
|
||||
{
|
||||
|
||||
}
|
||||
void teardown()
|
||||
{
|
||||
mock().checkExpectations();
|
||||
mock().clear();
|
||||
}
|
||||
};
|
||||
|
||||
TEST(test_ADC, FirstTest)
|
||||
{
|
||||
CHECK(true);
|
||||
}
|
||||
|
||||
|
||||
TEST(test_ADC, ADC_SetupSetsRegisters)
|
||||
{
|
||||
//Clears control register A for ADC0
|
||||
mock().expectOneCall("RegEdit_SetNum")
|
||||
.withPointerParameter("reg", (void *) &ADC0.CTRLA)
|
||||
.withUnsignedIntParameter("num", 0x00);
|
||||
|
||||
//Sets The sample accumulation number to 32.
|
||||
mock().expectOneCall("RegEdit_SetNum")
|
||||
.withPointerParameter("reg", (void *) &ADC0.CTRLB)
|
||||
.withUnsignedIntParameter("num", 0x5);
|
||||
|
||||
//Sets the voltage reference to VDD or VCC.
|
||||
mock().expectOneCall("RegEdit_SetBit")
|
||||
.withPointerParameter("reg", (void *) &ADC0.CTRLC)
|
||||
.withUnsignedIntParameter("bit_num", 4);
|
||||
|
||||
//Sets the pre-scalar for the adc sample rate.
|
||||
mock().expectOneCall("RegEdit_SetBit")
|
||||
.withPointerParameter("reg", (void *) &ADC0.CTRLC)
|
||||
.withUnsignedIntParameter("bit_num", 2);
|
||||
|
||||
//Setup an Initalization delay.
|
||||
mock().expectOneCall("RegEdit_OR_Num")
|
||||
.withPointerParameter("reg", (void *) &ADC0.CTRLD)
|
||||
.withUnsignedIntParameter("num", (2<<5));
|
||||
|
||||
//Set the bit for ADC variation during readings.
|
||||
mock().expectOneCall("RegEdit_SetBit")
|
||||
.withPointerParameter("reg", (void *) &ADC0.CTRLD)
|
||||
.withUnsignedIntParameter("bit_num", 4);
|
||||
|
||||
ADC_Setup();
|
||||
}
|
||||
|
||||
TEST(test_ADC, ADC_InitPortAPin7UsesCorrectRegisters)
|
||||
{
|
||||
//Check for setting the direction to input.
|
||||
mock().expectOneCall("RegEdit_ClearBit")
|
||||
.withPointerParameter("reg", (void *) &PORTA.DIR)
|
||||
.withUnsignedIntParameter("bit_num", 7);
|
||||
|
||||
//Check that the pullup is off
|
||||
mock().expectOneCall("RegEdit_ClearBit")
|
||||
.withPointerParameter("reg", (void *) &PORTA.OUT)
|
||||
.withUnsignedIntParameter("bit_num", 7);
|
||||
|
||||
//Set the ISC(input sense config) to disable digital input
|
||||
//buffering and reduce the noise on ADC usage.
|
||||
mock().expectOneCall("RegEdit_SetBit")
|
||||
.withPointerParameter("reg", (void *) &PORTA.PIN7CTRL)
|
||||
.withUnsignedIntParameter("bit_num", PORT_ISC_INPUT_DISABLE_gc);
|
||||
|
||||
|
||||
ADC_Init(7);
|
||||
}
|
||||
|
||||
TEST(test_ADC, ADC_InitPortAPin0UsesCorrectRegisters)
|
||||
{
|
||||
|
||||
//Check for setting the direction to input.
|
||||
mock().expectOneCall("RegEdit_ClearBit")
|
||||
.withPointerParameter("reg", (void *) &PORTA.DIR)
|
||||
.withUnsignedIntParameter("bit_num", 0);
|
||||
|
||||
//Check that the pullup is off
|
||||
mock().expectOneCall("RegEdit_ClearBit")
|
||||
.withPointerParameter("reg", (void *) &PORTA.OUT)
|
||||
.withUnsignedIntParameter("bit_num", 0);
|
||||
|
||||
//Set the ISC(input sense config) to disable digital input
|
||||
//buffering and reduce the noise on ADC usage.
|
||||
mock().expectOneCall("RegEdit_SetBit")
|
||||
.withPointerParameter("reg", (void *) &PORTA.PIN0CTRL)
|
||||
.withUnsignedIntParameter("bit_num", PORT_ISC_INPUT_DISABLE_gc);
|
||||
|
||||
|
||||
ADC_Init(0);
|
||||
}
|
||||
|
||||
TEST(test_ADC, ADC_InitDoesNothingOnHighPinNumbers)
|
||||
{
|
||||
mock().expectNoCall("RegEdit_SetBit");
|
||||
ADC_Init(8);
|
||||
}
|
||||
|
||||
TEST(test_ADC, ADC_EnablePasses)
|
||||
{
|
||||
|
||||
mock().expectOneCall("RegEdit_SetBit")
|
||||
.withPointerParameter("reg", (void *) &ADC0.CTRLA)
|
||||
.withUnsignedIntParameter("bit_num", 0);
|
||||
|
||||
ADC_Enable();
|
||||
|
||||
}
|
||||
|
||||
TEST(test_ADC, ADC_DisablePasses)
|
||||
{
|
||||
mock().expectOneCall("RegEdit_ClearBit")
|
||||
.withPointerParameter("reg", (void *) &ADC0.CTRLA)
|
||||
.withUnsignedIntParameter("bit_num", 0);
|
||||
|
||||
ADC_Disable();
|
||||
}
|
||||
|
||||
TEST(test_ADC, ADC_SetPinSetsRightRegisters)
|
||||
{
|
||||
//It clears existing MUXPOS register values.
|
||||
mock().expectOneCall("RegEdit_ClearRegister")
|
||||
.withPointerParameter("reg", (void *) &ADC0.MUXPOS);
|
||||
|
||||
//It Correctly sets the pin number.
|
||||
mock().expectOneCall("RegEdit_SetNum")
|
||||
.withPointerParameter("reg", (void *) &ADC0.MUXPOS)
|
||||
.withUnsignedIntParameter("num", 4);
|
||||
|
||||
ADC_SetPin(4);
|
||||
}
|
||||
|
||||
|
||||
TEST(test_ADC, ADC_SetPinFailsOnInvalidPin)
|
||||
{
|
||||
ADC_SetPin(8);
|
||||
}
|
||||
|
||||
|
||||
static uint16_t ADC_ReadValueFake(uint8_t pin_num)
|
||||
{
|
||||
return 512;
|
||||
}
|
||||
|
||||
TEST_GROUP(test_ADCRead)
|
||||
{
|
||||
void setup()
|
||||
{
|
||||
UT_PTR_SET(ADC_ReadValue, ADC_ReadValueFake);
|
||||
}
|
||||
|
||||
void teardown()
|
||||
{
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
TEST(test_ADCRead, FunctionPointerSwapWorks)
|
||||
{
|
||||
uint16_t value = ADC_ReadValue(0);
|
||||
|
||||
LONGS_EQUAL(512, value);
|
||||
}
|
||||
|
|
@ -3,6 +3,8 @@
|
|||
|
||||
//ImportTestGroups
|
||||
IMPORT_TEST_GROUP(simple_test);
|
||||
IMPORT_TEST_GROUP(test_ADC);
|
||||
IMPORT_TEST_GROUP(test_RegEdit);
|
||||
|
||||
//START: main
|
||||
int main(int argc, char** argv)
|
||||
|
|
|
@ -1,7 +1,14 @@
|
|||
project(Tests)
|
||||
|
||||
# TEST_DIRS
|
||||
add_subdirectory(MockRegEdit)
|
||||
add_subdirectory(RegEdit)
|
||||
add_subdirectory(simple_test)
|
||||
add_subdirectory(ADC)
|
||||
add_subdirectory(MockADC)
|
||||
|
||||
|
||||
|
||||
|
||||
# TEST_RUNNER
|
||||
add_executable(AllTests
|
||||
|
@ -12,5 +19,18 @@ target_link_libraries(AllTests
|
|||
${CPPUTEST_LIBRARIES}/libCppUTest.a
|
||||
${CPPUTEST_LIBRARIES}/libCppUTestExt.a
|
||||
# TEST_LINKS
|
||||
test_ADC
|
||||
test_RegEdit
|
||||
simple_test
|
||||
)
|
||||
|
||||
add_executable(Mock_Tests
|
||||
MockTests.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(Mock_Tests
|
||||
${CPPUTEST_LIBRARIES}/libCppUTest.a
|
||||
${CPPUTEST_LIBRARIES}/libCppUTestExt.a
|
||||
test_MockRegEdit
|
||||
test_MockADC
|
||||
)
|
||||
|
|
10
tests/MockADC/CMakeLists.txt
Normal file
10
tests/MockADC/CMakeLists.txt
Normal file
|
@ -0,0 +1,10 @@
|
|||
# TEST_RUNNER
|
||||
add_library(test_MockADC
|
||||
test_MockADC.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(test_MockADC
|
||||
${CPPUTEST_LIBRARIES}/libCppUTest.a
|
||||
${CPPUTEST_LIBRARIES}/libCppUTestExt.a
|
||||
MockADC
|
||||
)
|
94
tests/MockADC/test_MockADC.cpp
Normal file
94
tests/MockADC/test_MockADC.cpp
Normal file
|
@ -0,0 +1,94 @@
|
|||
/*
|
||||
* Author: Jake G
|
||||
* Date: 2024
|
||||
* filename: test_MockADC.c
|
||||
* description:
|
||||
*/
|
||||
|
||||
#include "CppUTest/CommandLineTestRunner.h"
|
||||
#include "CppUTestExt/MockSupport.h"
|
||||
#include <cstdint>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "MockADC.h"
|
||||
}
|
||||
|
||||
TEST_GROUP(test_MockADC)
|
||||
{
|
||||
void setup()
|
||||
{
|
||||
|
||||
}
|
||||
void teardown()
|
||||
{
|
||||
mock().checkExpectations();
|
||||
mock().clear();
|
||||
}
|
||||
};
|
||||
|
||||
TEST(test_MockADC, ADC_InitExpects)
|
||||
{
|
||||
mock().expectOneCall("ADC_Init")
|
||||
.withUnsignedIntParameter("pin_num", 7);
|
||||
|
||||
ADC_Init(7);
|
||||
}
|
||||
|
||||
TEST(test_MockADC, ADC_EnableExpects)
|
||||
{
|
||||
mock().expectOneCall("ADC_Enable");
|
||||
|
||||
ADC_Enable();
|
||||
}
|
||||
|
||||
|
||||
TEST(test_MockADC, ADC_DisableExpect)
|
||||
{
|
||||
mock().expectOneCall("ADC_Disable");
|
||||
|
||||
ADC_Disable();
|
||||
}
|
||||
|
||||
TEST(test_MockADC, ADC_ReadValue)
|
||||
{
|
||||
MockADC_ZeroIndex();
|
||||
MockADC_PushValue(512);
|
||||
|
||||
mock().expectOneCall("ADC_ReadValue_Impl")
|
||||
.withUnsignedIntParameter("pin_num", 0x2);
|
||||
|
||||
uint16_t val = ADC_ReadValue(0x2);
|
||||
LONGS_EQUAL(512, val);
|
||||
|
||||
}
|
||||
|
||||
TEST(test_MockADC, ADC_ReadValueReturnsZeroOnEmptyBuffer)
|
||||
{
|
||||
MockADC_ZeroIndex();
|
||||
|
||||
mock().expectOneCall("ADC_ReadValue_Impl")
|
||||
.withUnsignedIntParameter("pin_num", 0x2)
|
||||
.andReturnValue(0x0000);
|
||||
|
||||
uint16_t val = ADC_ReadValue(0x2);
|
||||
LONGS_EQUAL(0, val);
|
||||
}
|
||||
|
||||
TEST(test_MockADC, MockADC_PushValueDoesntOverflowArray)
|
||||
{
|
||||
MockADC_ZeroIndex();
|
||||
for(int i = 0; i < 257; i++){
|
||||
MockADC_PushValue(512+i);
|
||||
CHECK_TRUE(MockADC_GetIndex() <= 255);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(test_MockADC, MockADC_SetupSetsGlobal)
|
||||
{
|
||||
CHECK_FALSE(MockADC_IsSetup());
|
||||
|
||||
ADC_Setup();
|
||||
|
||||
CHECK_TRUE(MockADC_IsSetup());
|
||||
}
|
11
tests/MockRegEdit/CMakeLists.txt
Normal file
11
tests/MockRegEdit/CMakeLists.txt
Normal file
|
@ -0,0 +1,11 @@
|
|||
# TEST_RUNNER
|
||||
add_library(test_MockRegEdit
|
||||
test_MockRegEdit.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(test_MockRegEdit
|
||||
${CPPUTEST_LIBRARIES}/libCppUTest.a
|
||||
${CPPUTEST_LIBRARIES}/libCppUTestExt.a
|
||||
MockRegEdit
|
||||
)
|
||||
|
168
tests/MockRegEdit/test_MockRegEdit.cpp
Normal file
168
tests/MockRegEdit/test_MockRegEdit.cpp
Normal file
|
@ -0,0 +1,168 @@
|
|||
/*
|
||||
* Author: username
|
||||
* Date: todays_date
|
||||
* filename: test_MockRegEdit.c
|
||||
* description: module_purpose
|
||||
*/
|
||||
|
||||
#include "CppUTest/CommandLineTestRunner.h"
|
||||
#include "CppUTestExt/MockSupport.h"
|
||||
#include <cstdint>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "MockRegEdit.h"
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
TEST_GROUP(test_MockRegEdit)
|
||||
{
|
||||
void setup()
|
||||
{
|
||||
}
|
||||
void teardown()
|
||||
{
|
||||
mock().clear();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
TEST(test_MockRegEdit, RegEdit_ClearRegisterExpectedCallPasses)
|
||||
{
|
||||
uint8_t a;
|
||||
uint8_t *b = &a;
|
||||
|
||||
mock().expectOneCall("RegEdit_ClearRegister")
|
||||
.withPointerParameter("reg", b);
|
||||
|
||||
RegEdit_ClearRegister(b);
|
||||
|
||||
mock().checkExpectations();
|
||||
}
|
||||
|
||||
|
||||
TEST(test_MockRegEdit, RegEdit_SetRegisterExpectedCallPasses)
|
||||
{
|
||||
|
||||
uint8_t a;
|
||||
uint8_t *b = &a;
|
||||
|
||||
mock().expectOneCall("RegEdit_SetRegister")
|
||||
.withPointerParameter("reg", b);
|
||||
|
||||
RegEdit_SetRegister(b);
|
||||
|
||||
mock().checkExpectations();
|
||||
}
|
||||
|
||||
|
||||
TEST(test_MockRegEdit, RegEdit_SetBitExpectedCallPasses)
|
||||
{
|
||||
|
||||
uint8_t a;
|
||||
uint8_t *b = &a;
|
||||
|
||||
mock().expectOneCall("RegEdit_SetBit")
|
||||
.withPointerParameter("reg", b)
|
||||
.withUnsignedIntParameter("bit_num", 5);
|
||||
|
||||
RegEdit_SetBit(b, 5);
|
||||
|
||||
mock().checkExpectations();
|
||||
}
|
||||
|
||||
|
||||
TEST(test_MockRegEdit, RegEdit_ClearBitExpectedCallPasses)
|
||||
{
|
||||
|
||||
uint8_t a;
|
||||
uint8_t *b = &a;
|
||||
|
||||
mock().expectOneCall("RegEdit_ClearBit")
|
||||
.withPointerParameter("reg", b)
|
||||
.withUnsignedIntParameter("bit_num", 5);
|
||||
|
||||
RegEdit_ClearBit(b, 5);
|
||||
|
||||
mock().checkExpectations();
|
||||
}
|
||||
|
||||
TEST(test_MockRegEdit, RegEdit_IsBitSetExpectedCallPasses)
|
||||
{
|
||||
uint8_t a = 0xFF;
|
||||
uint8_t *b = &a;
|
||||
|
||||
mock().expectOneCall("RegEdit_IsBitSet")
|
||||
.withPointerParameter("reg", b)
|
||||
.withUnsignedIntParameter("bit_num", 5)
|
||||
.andReturnValue(true);
|
||||
|
||||
CHECK_TRUE(RegEdit_IsBitSet(b, 5));
|
||||
|
||||
mock().checkExpectations();
|
||||
}
|
||||
|
||||
|
||||
TEST(test_MockRegEdit, RegEdit_IsBitSetExpectedCallPassesWithFalse)
|
||||
{
|
||||
uint8_t a = 0xFF;
|
||||
uint8_t *b = &a;
|
||||
|
||||
mock().expectOneCall("RegEdit_IsBitSet")
|
||||
.withPointerParameter("reg", b)
|
||||
.withUnsignedIntParameter("bit_num", 5)
|
||||
.andReturnValue(false);
|
||||
|
||||
CHECK_FALSE(RegEdit_IsBitSet(b, 5));
|
||||
|
||||
mock().checkExpectations();
|
||||
}
|
||||
|
||||
TEST(test_MockRegEdit, RegEdit_OR_NumExpectedWorks)
|
||||
{
|
||||
uint8_t a = 0xFF;
|
||||
uint8_t *b = &a;
|
||||
|
||||
mock().expectOneCall("RegEdit_OR_Num")
|
||||
.withPointerParameter("reg", b)
|
||||
.withUnsignedIntParameter("num", 0x4)
|
||||
.andReturnValue(false);
|
||||
|
||||
RegEdit_OR_Num(b, 0x4);
|
||||
|
||||
mock().checkExpectations();
|
||||
|
||||
|
||||
}
|
||||
|
||||
TEST(test_MockRegEdit, RegEdit_SetNumPasses)
|
||||
{
|
||||
uint32_t a = 0xFFFFFFFF;
|
||||
uint32_t *b = &a;
|
||||
|
||||
mock().expectOneCall("RegEdit_SetNum")
|
||||
.withPointerParameter("reg", b)
|
||||
.withUnsignedIntParameter("num", 0x4)
|
||||
.andReturnValue(false);
|
||||
|
||||
RegEdit_SetNum(b, 0x4);
|
||||
|
||||
mock().checkExpectations();
|
||||
}
|
||||
|
||||
TEST(test_MockRegEdit, RegEdit_ReadRegPasses)
|
||||
{
|
||||
uint8_t a = 0xFF;
|
||||
uint8_t *b = &a;
|
||||
|
||||
mock().expectOneCall("RegEdit_ReadReg")
|
||||
.withPointerParameter("reg", b)
|
||||
.andReturnValue(0xFF);
|
||||
|
||||
uint8_t reg_val = RegEdit_ReadReg(b);
|
||||
LONGS_EQUAL(0xFF, reg_val);
|
||||
|
||||
mock().checkExpectations();
|
||||
}
|
13
tests/MockTests.cpp
Normal file
13
tests/MockTests.cpp
Normal file
|
@ -0,0 +1,13 @@
|
|||
#include "CppUTest/CommandLineTestRunner.h"
|
||||
|
||||
|
||||
//ImportTestGroups
|
||||
IMPORT_TEST_GROUP(test_MockRegEdit);
|
||||
IMPORT_TEST_GROUP(test_MockADC);
|
||||
|
||||
//START: main
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
return RUN_ALL_TESTS(argc, argv);
|
||||
}
|
||||
//END: main
|
10
tests/RegEdit/CMakeLists.txt
Normal file
10
tests/RegEdit/CMakeLists.txt
Normal file
|
@ -0,0 +1,10 @@
|
|||
# TEST_RUNNER
|
||||
add_library(test_RegEdit
|
||||
test_RegEdit.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(test_RegEdit
|
||||
${CPPUTEST_LIBRARIES}/libCppUTest.a
|
||||
${CPPUTEST_LIBRARIES}/libCppUTestExt.a
|
||||
RegEdit
|
||||
)
|
32
tests/RegEdit/test_RegEdit.cpp
Normal file
32
tests/RegEdit/test_RegEdit.cpp
Normal file
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* Author: username
|
||||
* Date: todays_date
|
||||
* filename: test_RegEdit.c
|
||||
* description: module_purpose
|
||||
*/
|
||||
|
||||
#include "CppUTest/CommandLineTestRunner.h"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "RegEdit.h"
|
||||
}
|
||||
|
||||
TEST_GROUP(test_RegEdit)
|
||||
{
|
||||
void setup()
|
||||
{
|
||||
|
||||
}
|
||||
void teardown()
|
||||
{
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
TEST(test_RegEdit, FirstTest)
|
||||
{
|
||||
//FAIL("Fail me!");
|
||||
CHECK(true);
|
||||
}
|
||||
|
Loading…
Add table
Reference in a new issue