Added tests. Start them by sending a message to MQTT_TOPIC/run_tests. First test will test all effects for memory leaks.

This commit is contained in:
Fabian Schlenz 2019-06-19 22:26:38 +02:00
parent 0163bbef6c
commit 26df11fc47
6 changed files with 68 additions and 27 deletions

View File

@ -11,7 +11,7 @@ extern uint8_t cycle_effects_count;
extern Effect* current_effect; extern Effect* current_effect;
extern ClockEffect effect_clock; extern ClockEffect effect_clock;
Effect* string_to_effect(String s); Effect* select_effect(uint32_t c);
bool change_current_effect(String s); bool change_current_effect(String s);
void setup_effects(); void setup_effects();

6
include/tests.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
namespace tests {
void run();
void test_effects_for_memory_leaks();
}

View File

@ -1,5 +1,6 @@
#include "effect_cycle.h" #include "effect_cycle.h"
#include "effects.h" #include "effects.h"
#include <ErriezCRC32.h>
CycleEffect::CycleEffect() { CycleEffect::CycleEffect() {
changeEffect(); changeEffect();
@ -23,7 +24,7 @@ void CycleEffect::changeEffect() {
if (effect) delete effect; if (effect) delete effect;
LOGln("CycleEffect * Searching for new effect '%s'", cycle_effects[new_id]); LOGln("CycleEffect * Searching for new effect '%s'", cycle_effects[new_id]);
delay(25); delay(25);
effect = string_to_effect(cycle_effects[new_id]); effect = select_effect( crc32String(cycle_effects[new_id]) );
effect_id = new_id; effect_id = new_id;
effectSince = millis(); effectSince = millis();
} }

View File

@ -22,32 +22,34 @@ Effect* current_effect;
ClockEffect effect_clock; ClockEffect effect_clock;
Effect* string_to_effect(String name) { Effect* select_effect(uint32_t code) {
uint32_t crc = crc32String(name.c_str()); switch (code) {
switch (crc) {
// use e.g. https://crccalc.com/ for the conversion of name to crc. // use e.g. https://crccalc.com/ for the conversion of name to crc.
case 0xD682E3C8 /* sinematrix3 */ : return new Sinematrix3Effect(); case 0: case 0xD682E3C8 /* sinematrix3 */ : return new Sinematrix3Effect();
case 0x90A887DA /* big_clock */ : return new BigClockEffect(); case 1: case 0x90A887DA /* big_clock */ : return new BigClockEffect();
case 0xBE7BBE92 /* clock */ : return new ClockEffect(); case 2: case 0xBE7BBE92 /* clock */ : return new ClockEffect();
case 0x733BE087 /* bell */ : return new BellEffect(); case 3: case 0x733BE087 /* bell */ : return new BellEffect(); //(new AnimationEffect("/bell.pia", 0x000000, 0, 0))->setFgColor(0xFFFF00);
case 0x2BBC5D43 /* off */ : return new StaticEffect(0x000000); case 4: case 0x2BBC5D43 /* off */ : return new StaticEffect(0x000000);
case 0x1D84F231 /* koopa */ : return new AnimationEffect("/koopa.pia", new CRGB(0x000000), 0, 0); case 5: case 0x1D84F231 /* koopa */ : return new AnimationEffect("/koopa.pia", CRGB(0x000000), 0, 0);
case 0xAC43BCF1 /* couple_rain */ : return new AnimationEffect("/couple_rain.pia", new CRGB(0x000000), -8, -16); case 6: case 0xAC43BCF1 /* couple_rain */ : return new AnimationEffect("/couple_rain.pia", CRGB(0x000000), -8, -16);
case 0xF1B117F7 /* single_dynamic */ : return new SingleDynamicEffect(); case 7: case 0xF1B117F7 /* single_dynamic */ : return new SingleDynamicEffect();
case 0xF52F2804 /* multi_dynamic */ : return new MultiDynamicEffect(); case 8: case 0xF52F2804 /* multi_dynamic */ : return new MultiDynamicEffect();
case 0xF83341CF /* matrix */ : return new MatrixEffect(); case 9: case 0xF83341CF /* matrix */ : return new MatrixEffect();
case 0xD2B79DD0 /* rainbow_matrix */ : return new RainbowMatrixEffect(); case 10: case 0xD2B79DD0 /* rainbow_matrix */ : return new RainbowMatrixEffect();
case 0xE8DD3433 /* random_matrix */ : return new RandomMatrixEffect(); case 11: case 0xE8DD3433 /* random_matrix */ : return new RandomMatrixEffect();
case 0xB086D193 /* cycle */ : return new CycleEffect(); case 12: case 0xB086D193 /* cycle */ : return new CycleEffect();
case 0x2293EF9F /* twirl */ : return new TwirlEffect(); case 13: case 0x2293EF9F /* twirl */ : return new TwirlEffect();
case 0x60ECC3E6 /* heart */ : return new AnimationEffect("/heart.pia", new CRGB(0x000000), 0, 0); case 14: case 0x60ECC3E6 /* heart */ : return new AnimationEffect("/heart.pia", CRGB(0x000000), 0, 0);
case 0x42090A49 /* confetti */ : return new ConfettiEffect(); case 15: case 0x42090A49 /* confetti */ : return new ConfettiEffect();
case 0x516D6B9E /* snake */ : return new SnakeEffect(); case 16: case 0x516D6B9E /* snake */ : return new SnakeEffect();
case 0x58DE09CF /* fire */ : return new FireEffect(); case 17: case 0x58DE09CF /* fire */ : return new FireEffect();
case 0x08BA9C08 /* firework */ : return new FireworkEffect(); case 18: case 0x08BA9C08 /* firework */ : return new FireworkEffect();
case 0x14B85EAC /* gol */ : return new GolEffect(); case 19: case 0x14B85EAC /* gol */ : return new GolEffect();
case 0xFA13015D /* cake */ : return new AnimationEffect("/cake.pia", new CRGB(0x000000), 0, 0); case 20: case 0xFA13015D /* cake */ : return new AnimationEffect("/cake.pia", CRGB(0x000000), 0, 0);
case 0xA2B0D68B /* pixel_clock */ : return new PixelClockEffect(); case 21: case 0xA2B0D68B /* pixel_clock */ : return new PixelClockEffect();
case 22: case 0x2C0E6962 /* big_dynamic */ : return new BigDynamicEffect();
case 23: case 0xDA6F31A5 /* random_confetti */ : return new RandomConfettiEffect();
case 24: case 0x8325C1DF /* dvd */ : return new DvdEffect();
default : return NULL; default : return NULL;
}; };
} }

View File

@ -10,6 +10,7 @@
#include "Effect.h" #include "Effect.h"
#include "effects.h" #include "effects.h"
#include "functions.h" #include "functions.h"
#include "tests.h"
WiFiClient wifi; WiFiClient wifi;
PubSubClient mqtt_client(wifi); PubSubClient mqtt_client(wifi);
@ -63,6 +64,9 @@ void mqtt_callback(char* original_topic, byte* pl, unsigned int length) {
LOGln("MQTT * Rebooting"); LOGln("MQTT * Rebooting");
ESP.restart(); ESP.restart();
return; // Will never be reached, but anyway... return; // Will never be reached, but anyway...
} else if (topic.compareTo("run_tests")==0) {
tests::run();
return;
} }
long value = payload.toInt(); long value = payload.toInt();
LOGln("MQTT * Payload as number: %d", value); LOGln("MQTT * Payload as number: %d", value);

28
src/tests.cpp Normal file
View File

@ -0,0 +1,28 @@
#include "tests.h"
#include "effects.h"
namespace tests {
void run() {
LOGln("Tests * Running test for memory leaks...");
test_effects_for_memory_leaks();
}
void test_effects_for_memory_leaks() {
int i=0;
Effect* effect;
int32_t diffs[3] = {0, 0, 0};
int32_t usage = 0;
while (1) {
for (int j=0; j<3; j++) {
int free_at_start = ESP.getFreeHeap();
effect = select_effect(i);
if (j==0) usage = free_at_start - ESP.getFreeHeap();
if (effect == NULL) return;
delete effect;
diffs[j] = free_at_start - ESP.getFreeHeap();
}
LOGln("Tests * Memory usage of effect #%d: %d, leakage %d, %d, %d", i, usage, diffs[0], diffs[1], diffs[2]);
i++;
}
}
}