From 8bec415466c1dba10f1a5fe5e561b949ad1ccb2f Mon Sep 17 00:00:00 2001 From: jakeg00dwin Date: Mon, 2 Sep 2024 14:07:20 -0700 Subject: [PATCH] Added zcd module's tests --- tests/zero_cross_detection/CMakeLists.txt | 11 ++ .../test_zero_cross_detection.cpp | 111 ++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 tests/zero_cross_detection/CMakeLists.txt create mode 100644 tests/zero_cross_detection/test_zero_cross_detection.cpp diff --git a/tests/zero_cross_detection/CMakeLists.txt b/tests/zero_cross_detection/CMakeLists.txt new file mode 100644 index 0000000..c9ab4a3 --- /dev/null +++ b/tests/zero_cross_detection/CMakeLists.txt @@ -0,0 +1,11 @@ +# TEST_RUNNER +add_library(test_zero_cross_detection + test_zero_cross_detection.cpp +) + +target_link_libraries(test_zero_cross_detection + ${CPPUTEST_LIBRARIES}/libCppUTest.a + ${CPPUTEST_LIBRARIES}/libCppUTestExt.a + zero_cross_detection + MockADC +) diff --git a/tests/zero_cross_detection/test_zero_cross_detection.cpp b/tests/zero_cross_detection/test_zero_cross_detection.cpp new file mode 100644 index 0000000..2b7d6e5 --- /dev/null +++ b/tests/zero_cross_detection/test_zero_cross_detection.cpp @@ -0,0 +1,111 @@ +/* + * Author: username + * Date: todays_date + * filename: test_zero_cross_detection.c + * description: module_purpose + */ + +#include "CppUTest/CommandLineTestRunner.h" +#include "CppUTest/TestFailure.h" +#include "CppUTest/TestPlugin.h" +#include "CppUTest/UtestMacros.h" +#include "CppUTestExt/MockSupport.h" +#include + +extern "C" +{ +#include "MockADC.h" +#include "zero_cross_detection.h" +} + +TEST_GROUP(test_zero_cross_detection) +{ + + void setup() + { + + } + void teardown() + { + mock().checkExpectations(); + mock().clear(); + } +}; + + +static void ZCDSetupExpectations(void) +{ + mock().expectOneCall("ADC_Init") + .withUnsignedIntParameter("pin_num", 7); + + mock().expectOneCall("ADC_Enable"); +} + + +static void PollIterationExpectations(void) +{ + ZCDSetupExpectations(); + + mock().expectOneCall("ADC_ReadValue_Impl") + .withUnsignedIntParameter("pin_num", 7) + .ignoreOtherParameters(); + + mock().expectOneCall("ADC_Disable"); + + ZCDSetupExpectations(); + + mock().expectOneCall("ADC_ReadValue_Impl") + .withUnsignedIntParameter("pin_num", 7) + .ignoreOtherParameters(); + + mock().expectOneCall("ADC_Disable"); +} + + +TEST(test_zero_cross_detection, ZCD_SetupCallsCorrectFuncs) +{ + ZCDSetupExpectations(); + + ZCD_Setup(); +} + + +TEST(test_zero_cross_detection, ZCD_IsTriggeredCallsCorrectFunctions) +{ + MockADC_ZeroIndex(); + + PollIterationExpectations(); + + CHECK_FALSE(ZCD_IsTriggered()); +} + + +TEST(test_zero_cross_detection, ZCD_IsTriggerdTrueWhenRising) +{ + MockADC_ZeroIndex(); + + MockADC_PushValue(512); + MockADC_PushValue(450); + + PollIterationExpectations(); + + CHECK_TRUE(ZCD_IsTriggered()); +} + + +TEST(test_zero_cross_detection, ZCD_PollWorksAfterCalls) +{ + MockADC_ZeroIndex(); + + MockADC_PushValue(512); + MockADC_PushValue(450); + MockADC_PushValue(50); + MockADC_PushValue(50); + + PollIterationExpectations(); + PollIterationExpectations(); + + ZCD_Poll(); +} + +