Skip to content

Commit

Permalink
fix everything?
Browse files Browse the repository at this point in the history
  • Loading branch information
GreenWizard2015 committed Dec 29, 2023
1 parent 4d1bab3 commit 3288759
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 33 deletions.
10 changes: 8 additions & 2 deletions controller/tea_poor/lib/WaterPump/WaterPumpScheduler.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#include "WaterPumpScheduler.h"

WaterPumpScheduler::WaterPumpScheduler(IWaterPump* waterPump, unsigned long forceStopIntervalMs) {
WaterPumpScheduler::WaterPumpScheduler(IWaterPump* waterPump, unsigned long forceStopIntervalMs) :
_waterPump(waterPump),
_forceStopIntervalMs(forceStopIntervalMs)
{
}

WaterPumpScheduler::~WaterPumpScheduler() {
delete _waterPump;
// TODO: find better way to manage memory
// for now it's not a big deal, because Arduino will never stop
// and tests are manage memory by themselves
// delete _waterPump;
}

void WaterPumpScheduler::setup() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef WATERPUMPCONTROLLER_H
#define WATERPUMPCONTROLLER_H
#include "IWaterPump.h"
#include <IWaterPump.h>

class WaterPumpController: public IWaterPump {
private:
Expand Down
20 changes: 17 additions & 3 deletions controller/tea_poor/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,23 @@
platform = renesas-ra
board = uno_r4_wifi
framework = arduino
test_framework = unity
lib_deps =
lasselukkari/aWOT@^3.5.0
; throwtheswitch/Unity@^2.5.2
test_ignore = local
test_ignore = test_native

[env:native]
platform = native
test_build_src = no
test_framework = googletest
build_src_filter = +<*> -<test/*>
lib_ldf_mode = deep
check_flags = --verbose --enable=all --std=c++11
build_flags =
-std=c++11
-Wall -Wextra -Wunused
-static -static-libgcc -static-libstdc++
; ignore libraries that are only for the Arduino
lib_ignore =
RemoteControl
WaterPumpController
test_ignore = test_uno_r4_wifi
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// I wasn't able to run tests at all. Run them locally and confirm that they are working.
// Its either a local problem or a problem with the configuration of the project.
// Further goes a sketch of the tests, but I wasn't able to run them.
#include <unity.h>
#include <gtest/gtest.h>
#include <WaterPumpScheduler.h>



// Fake water pump
class FakeWaterPump : public IWaterPump {
private:
Expand All @@ -19,14 +17,8 @@ class FakeWaterPump : public IWaterPump {
};
// End of fake water pump


/* Empty functions required by unity framework*/
void setUp() { /* Setup code here */ }
void tearDown() { /* Teardown code here */ }


// test that pump is stopping after given time
void test_pump_stops_after_given_time() {
TEST(WaterPumpScheduler, test_pump_stops_after_given_time) {
// random time between 1 and 10 seconds
const unsigned long runTimeMs = 1000 + (rand() % 10) * 1000;
FakeWaterPump fakeWaterPump;
Expand All @@ -37,21 +29,21 @@ void test_pump_stops_after_given_time() {
waterPumpScheduler.start(runTimeMs, currentTimeMs);
// check status
auto status = waterPumpScheduler.status();
TEST_ASSERT_TRUE(status.isRunning);
TEST_ASSERT_EQUAL(status.stopTime, runTimeMs);
ASSERT_TRUE(status.isRunning);
ASSERT_EQ(status.stopTime, runTimeMs);

while (currentTimeMs < runTimeMs) {
waterPumpScheduler.tick(currentTimeMs);
TEST_ASSERT_TRUE(fakeWaterPump.isRunning());
ASSERT_TRUE(fakeWaterPump.isRunning());
currentTimeMs += 100;
}
// pump should be stopped after given time
waterPumpScheduler.tick(runTimeMs + 1);
TEST_ASSERT_FALSE(fakeWaterPump.isRunning());
ASSERT_FALSE(fakeWaterPump.isRunning());
}

// test that pump is periodically forced to stop after given time
void test_pump_is_periodically_forced_to_stop_after_given_time () {
TEST(WaterPumpScheduler, test_pump_is_periodically_forced_to_stop_after_given_time) {
FakeWaterPump fakeWaterPump;
WaterPumpScheduler waterPumpScheduler(&fakeWaterPump, 1000); // force stop each 1 second
waterPumpScheduler.setup();
Expand All @@ -60,25 +52,21 @@ void test_pump_is_periodically_forced_to_stop_after_given_time () {
waterPumpScheduler.start(1, currentTimeMs);
currentTimeMs += 1;
waterPumpScheduler.tick(currentTimeMs);
TEST_ASSERT_FALSE(fakeWaterPump.isRunning()); // pump should be stopped after given time
ASSERT_FALSE(fakeWaterPump.isRunning()); // pump should be stopped after given time

for(int i = 0; i < 10; i++) {
// emulate that pump was started again
fakeWaterPump.start();
currentTimeMs += 1000;
waterPumpScheduler.tick(currentTimeMs);
TEST_ASSERT_FALSE(fakeWaterPump.isRunning()); // pump should be stopped
ASSERT_FALSE(fakeWaterPump.isRunning()); // pump should be stopped
}
}

void setup() {
UNITY_BEGIN();
RUN_TEST(test_pump_stops_after_given_time);
RUN_TEST(test_pump_is_periodically_forced_to_stop_after_given_time);
UNITY_END();
}


void loop() {

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
int result = RUN_ALL_TESTS(); // Intentionally ignoring the return value
(void)result; // Silence unused variable warning
// Always return zero-code and allow PlatformIO to parse results
return 0;
}

0 comments on commit 3288759

Please sign in to comment.