Skip to content

Commit

Permalink
First Unit tests
Browse files Browse the repository at this point in the history
Update links in assert statements and fix relay diversion configuration
  • Loading branch information
FredM67 committed Mar 20, 2024
1 parent 98bbe4a commit bd7cc84
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,6 @@
*/

static_assert(__cplusplus >= 201703L, "**** Please define 'gnu++17' in 'platform.txt' ! ****");
static_assert(__cplusplus >= 201703L, "See also : https://github.com/FredM67/PVRouter-3-phase/blob/main/Mk2_3phase_RFdatalog_temp/Readme.md");
static_assert(__cplusplus >= 201703L, "See also : https://github.com/FredM67/PVRouter-3-phase/blob/main/runtime/Mk2_3phase_RFdatalog_temp/Readme.md");

// The active code can be found in the other cpp/h files
4 changes: 2 additions & 2 deletions runtime/Mk2_3phase_RFdatalog_temp/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ inline constexpr bool OVERRIDE_PIN_PRESENT{ false }; /**< set
#endif

inline constexpr bool WATCHDOG_PIN_PRESENT{ false }; /**< set it to 'true' if there's a watch led */
inline constexpr bool RELAY_DIVERSION{ false }; /**< set it to 'true' if a relay is used for diversion */
inline constexpr bool RELAY_DIVERSION{ true }; /**< set it to 'true' if a relay is used for diversion */
inline constexpr bool DUAL_TARIFF{ false }; /**< set it to 'true' if there's a dual tariff each day AND the router is connected to the billing meter */

// ----------- Pinout assignments -----------
Expand Down Expand Up @@ -83,7 +83,7 @@ inline constexpr uint8_t watchDogPin{ 0xff }; /**< watch dog LED */

inline constexpr uint8_t tempSensorPin{ 0xff }; /**< for 3-phase PCB, sensor pin */

inline constexpr RelayEngine relays{ { { 0xff, 1000, 200, 1, 1 } } }; /**< config for relay diversion, see class definition for defaults and advanced options */
inline constexpr RelayEngine relays{ 15_i, { { 6, 1000, 200, 1, 1 } } }; /**< config for relay diversion, see class definition for defaults and advanced options */

inline constexpr uint8_t ul_OFF_PEAK_DURATION{ 8 }; /**< Duration of the off-peak period in hours */
inline constexpr pairForceLoad rg_ForceLoad[NO_OF_DUMPLOADS]{ { -3, 2 } }; /**< force config for load #1 ONLY for dual tariff */
Expand Down
1 change: 1 addition & 0 deletions runtime/Mk2_3phase_RFdatalog_temp/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <Arduino.h>

#include "constants.h"
#include "type_traits.hpp"

// -------------------------------
// definitions of enumerated types
Expand Down
18 changes: 10 additions & 8 deletions runtime/Mk2_3phase_RFdatalog_temp/utils_relay.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,24 @@
#include "ewma_avg.hpp"
#include "utils_pins.h"

#include "debug.h"

/**
* @brief Relay diversion config and engine
*
* @ingroup RelayDiversion
*/
class relayOutput
class RelayOutput
{
public:
constexpr relayOutput() = delete;
constexpr RelayOutput() = delete;

/**
* @brief Construct a new relay Config object with default parameters
*
* @param _relay_pin Control pin for the relay
*/
explicit constexpr relayOutput(const uint8_t _relay_pin)
explicit constexpr RelayOutput(const uint8_t _relay_pin)
: relay_pin{_relay_pin}
{
}
Expand All @@ -47,7 +49,7 @@ class relayOutput
* @param _surplusThreshold Surplus threshold to turn relay ON
* @param _importThreshold Import threshold to turn relay OFF
*/
constexpr relayOutput(uint8_t _relay_pin, int16_t _surplusThreshold, int16_t _importThreshold)
constexpr RelayOutput(uint8_t _relay_pin, int16_t _surplusThreshold, int16_t _importThreshold)
: relay_pin{_relay_pin}, surplusThreshold{-abs(_surplusThreshold)}, importThreshold{abs(_importThreshold)}
{
}
Expand All @@ -61,7 +63,7 @@ class relayOutput
* @param _minON Minimum duration in minutes to leave relay ON
* @param _minOFF Minimum duration in minutes to leave relay OFF
*/
constexpr relayOutput(uint8_t _relay_pin, int16_t _surplusThreshold, int16_t _importThreshold, uint16_t _minON, uint16_t _minOFF)
constexpr RelayOutput(uint8_t _relay_pin, int16_t _surplusThreshold, int16_t _importThreshold, uint16_t _minON, uint16_t _minOFF)
: relay_pin{_relay_pin}, surplusThreshold{-abs(_surplusThreshold)}, importThreshold{abs(_importThreshold)}, minON{_minON * 60}, minOFF{_minOFF * 60}
{
}
Expand Down Expand Up @@ -255,7 +257,7 @@ class RelayEngine
* @brief Construct a list of relays
*
*/
explicit constexpr RelayEngine(const relayOutput (&ref)[N])
explicit constexpr RelayEngine(const RelayOutput (&ref)[N])
: relay(ref)
{
}
Expand All @@ -264,7 +266,7 @@ class RelayEngine
* @brief Construct a list of relays with a custom sliding average
*
*/
constexpr RelayEngine(integral_constant< uint8_t, D > ic, const relayOutput (&ref)[N])
constexpr RelayEngine(integral_constant< uint8_t, D > ic, const RelayOutput (&ref)[N])
: relay(ref)
{
}
Expand Down Expand Up @@ -391,7 +393,7 @@ class RelayEngine
}

private:
const relayOutput relay[N]; /**< Array of relays */
const RelayOutput relay[N]; /**< Array of relays */

mutable uint8_t settle_change{ 60 }; /**< Delay in seconds until next change occurs */

Expand Down
63 changes: 63 additions & 0 deletions runtime/test/test_RelayEngine/test_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <Arduino.h>
#include <unity.h>

#include "utils_relay.h"

// Assuming you have a RelayOutput array defined somewhere
const RelayOutput relayArray[2]{ { 5, 1000, 200, 5, 5 }, { 6, 1000, 200, 5, 5 } };

RelayEngine< 2 >* relayEngineTest;

void setUp(void)
{
relayEngineTest = new RelayEngine< 2 >(relayArray);
}

void tearDown(void)
{
delete relayEngineTest;
}

void test_get_size(void)
{
TEST_ASSERT_EQUAL(2, relayEngineTest->get_size());
}

void test_get_relay(void)
{
for(uint8_t i = 0; i < relayEngineTest->get_size(); ++i) {
TEST_ASSERT_EQUAL(true, relayArray[i].get_pin() == relayEngineTest->get_relay(i).get_pin());
TEST_ASSERT_EQUAL(true, relayArray[i].get_minOFF() == relayEngineTest->get_relay(i).get_minOFF());
TEST_ASSERT_EQUAL(true, relayArray[i].get_minON() == relayEngineTest->get_relay(i).get_minON());
TEST_ASSERT_EQUAL(true, relayArray[i].get_surplusThreshold() == relayEngineTest->get_relay(i).get_surplusThreshold());
TEST_ASSERT_EQUAL(true, relayArray[i].get_importThreshold() == relayEngineTest->get_relay(i).get_importThreshold());
}
}

void test_get_average(void)
{
// Assuming initial average is 0
TEST_ASSERT_EQUAL(0, RelayEngine< 2 >::get_average());
}

void test_update_average(void)
{
RelayEngine< 2 >::update_average(100);
// Assuming the average changes after update
TEST_ASSERT_NOT_EQUAL(0, RelayEngine< 2 >::get_average());
}

void setup()
{
UNITY_BEGIN();
RUN_TEST(test_get_size);
RUN_TEST(test_get_relay);
RUN_TEST(test_get_average);
RUN_TEST(test_update_average);
UNITY_END();
}

void loop()
{
// Do nothing here
}
75 changes: 75 additions & 0 deletions runtime/test/test_RelayOutput/test_,main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <Arduino.h>
#include <unity.h>

#include "utils_relay.h"

static_assert(__cplusplus >= 201703L, "See also : https://github.com/FredM67/PVRouter-3-phase/blob/main/runtime/Mk2_3phase_RFdatalog_temp/Readme.md");

RelayOutput* RelayOutputTest;

void setUp(void) {
RelayOutputTest = new RelayOutput(5, 1000, 200, 5, 5);
}

void tearDown(void) {
delete RelayOutputTest;
}

void test_get_pin(void) {
TEST_ASSERT_EQUAL(5, RelayOutputTest->get_pin());
}

void test_get_surplusThreshold(void) {
TEST_ASSERT_EQUAL(1000, RelayOutputTest->get_surplusThreshold());
}

void test_get_importThreshold(void) {
TEST_ASSERT_EQUAL(200, RelayOutputTest->get_importThreshold());
}

void test_get_minON(void) {
TEST_ASSERT_EQUAL(5 * 60, RelayOutputTest->get_minON());
}

void test_get_minOFF(void) {
TEST_ASSERT_EQUAL(5 * 60, RelayOutputTest->get_minOFF());
}

void test_isRelayON(void) {
TEST_ASSERT_EQUAL(false, RelayOutputTest->isRelayON());
}

void test_proceed_relay(void) {
TEST_ASSERT_EQUAL(false, RelayOutputTest->proceed_relay(150));
TEST_ASSERT_EQUAL(false, RelayOutputTest->isRelayON());

for(uint16_t _time = 0; _time < RelayOutputTest->get_minOFF(); ++_time) {
RelayOutputTest->inc_duration();
}

TEST_ASSERT_EQUAL(true, RelayOutputTest->proceed_relay(-1500));
TEST_ASSERT_EQUAL(true, RelayOutputTest->isRelayON());

for(uint16_t _time = 0; _time < RelayOutputTest->get_minON(); ++_time) {
RelayOutputTest->inc_duration();
}

TEST_ASSERT_EQUAL(true, RelayOutputTest->proceed_relay(250));
TEST_ASSERT_EQUAL(false, RelayOutputTest->isRelayON());
}

void setup() {
UNITY_BEGIN();
RUN_TEST(test_get_pin);
RUN_TEST(test_get_surplusThreshold);
RUN_TEST(test_get_importThreshold);
RUN_TEST(test_get_minON);
RUN_TEST(test_get_minOFF);
RUN_TEST(test_isRelayON);
RUN_TEST(test_proceed_relay);
UNITY_END();
}

void loop() {
// Do nothing here
}

0 comments on commit bd7cc84

Please sign in to comment.