From 5231d55d8e6d50e3f34bd841c856cba3eeb2c866 Mon Sep 17 00:00:00 2001 From: jakeg00dwin Date: Mon, 2 Sep 2024 13:52:47 -0700 Subject: [PATCH] Added tests for the newly added modules. --- tests/ADC/CMakeLists.txt | 17 +++ tests/ADC/test_ADC.cpp | 192 +++++++++++++++++++++++++++++++++ tests/CMakeLists.txt | 5 +- tests/MockADC/CMakeLists.txt | 10 ++ tests/MockADC/test_MockADC.cpp | 94 ++++++++++++++++ tests/load/CMakeLists.txt | 12 +++ tests/load/test_load.cpp | 56 ++++++++++ 7 files changed, 384 insertions(+), 2 deletions(-) create mode 100644 tests/ADC/CMakeLists.txt create mode 100644 tests/ADC/test_ADC.cpp create mode 100644 tests/MockADC/CMakeLists.txt create mode 100644 tests/MockADC/test_MockADC.cpp create mode 100644 tests/load/CMakeLists.txt create mode 100644 tests/load/test_load.cpp diff --git a/tests/ADC/CMakeLists.txt b/tests/ADC/CMakeLists.txt new file mode 100644 index 0000000..6318a53 --- /dev/null +++ b/tests/ADC/CMakeLists.txt @@ -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 +) diff --git a/tests/ADC/test_ADC.cpp b/tests/ADC/test_ADC.cpp new file mode 100644 index 0000000..b9abb6a --- /dev/null +++ b/tests/ADC/test_ADC.cpp @@ -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 + +//This define allows us to dircetly include the device header without error. +#define _AVR_IO_H_ + + +extern "C" +{ +#include //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); +} + diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 9a138fb..38b845d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -4,11 +4,12 @@ project(Tests) add_subdirectory(Relays) add_subdirectory(SuperLoop) #add_subdirectory(timer) -#add_subdirectory(usart) add_subdirectory(MockRegEdit) add_subdirectory(RegEdit) add_subdirectory(simple_test) - +add_subdirectory(ADC) +add_subdirectory(MockADC) +add_subdirectory(load) diff --git a/tests/MockADC/CMakeLists.txt b/tests/MockADC/CMakeLists.txt new file mode 100644 index 0000000..cd51613 --- /dev/null +++ b/tests/MockADC/CMakeLists.txt @@ -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 +) diff --git a/tests/MockADC/test_MockADC.cpp b/tests/MockADC/test_MockADC.cpp new file mode 100644 index 0000000..8b0b5ee --- /dev/null +++ b/tests/MockADC/test_MockADC.cpp @@ -0,0 +1,94 @@ +/* + * Author: Jake G + * Date: 2024 + * filename: test_MockADC.c + * description: + */ + +#include "CppUTest/CommandLineTestRunner.h" +#include "CppUTestExt/MockSupport.h" +#include + +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()); +} diff --git a/tests/load/CMakeLists.txt b/tests/load/CMakeLists.txt new file mode 100644 index 0000000..f33cce9 --- /dev/null +++ b/tests/load/CMakeLists.txt @@ -0,0 +1,12 @@ +# TEST_RUNNER +add_library(test_load + test_load.cpp +) + +target_link_libraries(test_load + ${CPPUTEST_LIBRARIES}/libCppUTest.a + ${CPPUTEST_LIBRARIES}/libCppUTestExt.a + load + MockADC + MockRegEdit +) diff --git a/tests/load/test_load.cpp b/tests/load/test_load.cpp new file mode 100644 index 0000000..72fd3a9 --- /dev/null +++ b/tests/load/test_load.cpp @@ -0,0 +1,56 @@ +/* + * Author: Jake G + * Date: 2024 + * filename: test_load.cpp + * description: + */ + +#include "CppUTest/CommandLineTestRunner.h" +#include "CppUTestExt/MockSupport.h" +#include + + +//This define allows us to dircetly include the device header without error. +#define _AVR_IO_H_ + +extern "C" +{ +#include //ATtiny404 header fille. +#include "load.h" +#include "MockADC.h" +#include "MockADC.h" +} + +TEST_GROUP(test_load) +{ + void setup() + { + + } + void teardown() + { + mock().checkExpectations(); + mock().clear(); + } +}; + +TEST(test_load, LoadPass) +{ + CHECK_TRUE(true); +} + +TEST(test_load, PortAHandlerSuccess) +{ + mock().expectOneCall("ADC_Init") + .withUnsignedIntParameter("pin_num", 4); + + mock().expectOneCall("ADC_Enable"); + mock().expectOneCall("ADC_ReadValue_Impl") + .withUnsignedIntParameter("pin_num", 4); + mock().expectOneCall("ADC_Disable"); + mock().expectOneCall("RegEdit_ClearBit") + .withPointerParameter("reg", (void *) &PORTA.OUT) + .withUnsignedIntParameter("bit_num", 7); + + Load_HandleLoadPortA(4, 7); +}