Skip to content

Commit

Permalink
feat(DataTomeExpAvg): ✨ add DataTomeExpAvg with unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreHiroyuki committed Sep 24, 2024
1 parent 7fa4acd commit f44a902
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 97 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
"test",
"readme",
"library_metadata",
"DataTome"
"DataTome",
"DataTomeCumulative",
"DataTomeExpAvg"
]
}
27 changes: 22 additions & 5 deletions build_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

// Create an Arithmetic Moving Average object of unsigned int type,
// 10 in size
DataTomeMvAvg<unsigned, unsigned long> test(10);
DataTomeAnalysis<unsigned, unsigned long> test2(10);
// DataTomeExpAvg<unsigned, unsigned long> test3(10);
DataTomeMvAvg<int, long int> test(10);
DataTomeAnalysis<int, long int> test2(10);
DataTomeCumulative<double> test3;
DataTomeExpAvg<double> test4;

// This variable just generates input for average test
unsigned delta_x = 0;
int delta_x = 0;

void setup() {
// Initialize serial interface
Expand All @@ -19,9 +20,12 @@ void setup() {
void loop() {
// Pushes the input in the moving average object
test.push(delta_x);
test2.push(delta_x);
test3.push(delta_x);
test4.push(delta_x);

// Generates the next input
delta_x += 5;
delta_x += 3;
if (delta_x > 1000) delta_x = 0;

// Prints each value stored in the moving average
Expand All @@ -38,5 +42,18 @@ void loop() {
Serial.print(" b: ");
Serial.println(test.back());

Serial.print("Analysis: ");
Serial.print(test2.mean());
Serial.print(" | Std: ");
Serial.print(test2.std());
Serial.print(" | Median: ");
Serial.println(test2.median());

Serial.print("Cumulative: ");
Serial.println(test3.get());

Serial.print(" | ExpAvg: ");
Serial.println(test4.get());

delay(1000);
}
39 changes: 0 additions & 39 deletions include/README

This file was deleted.

46 changes: 0 additions & 46 deletions lib/README

This file was deleted.

10 changes: 8 additions & 2 deletions library.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
"platforms": "*",
"headers": [
"DataTome.h",
"DataTomeAnalysis.h",
"DataTomeCumulative.h",
"DataTomeExpAvg",
"DataTomeMvAvg.h",
"DataTomeAnalysis.h"
"DataTomeUtils.h"
],
"examples": [
{
Expand All @@ -43,9 +46,12 @@
],
"export": {
"exclude": [
"test/",
".github",
".gitignore",
".vscode"
".vscode",
"build_test.cpp",
"docs/icon.png"
]
},
"srcDir": "src"
Expand Down
1 change: 1 addition & 0 deletions src/DataTome.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <DataTomeAnalysis.h>
#include <DataTomeCumulative.h>
#include <DataTomeExpAvg.h>
#include <DataTomeMvAvg.h>
#include <DataTomeUtils.h>

Expand Down
9 changes: 5 additions & 4 deletions src/DataTomeCumulative.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

#include <limits.h>

// WARNING: using this class with integer types may result in loss of precision
// due to cumulative integer divisions rounding.
// RECOMMENDED: use double type for TypeOfSum
// WARNING: using this class with integer types may result in a loss of
// precision due to cumulative rounding of integer divisions.
template <typename TypeOfSum>
class DataTomeCumulative {
protected:
Expand All @@ -24,10 +25,10 @@ class DataTomeCumulative {
_cumulative_average = 0;
_count = 0;
}
_count++;

_cumulative_average += (input - _cumulative_average) / (_count + 1);
_cumulative_average += (input - _cumulative_average) / (_count);

_count++;
return *this;
}

Expand Down
39 changes: 39 additions & 0 deletions src/DataTomeExpAvg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/***************************************************************
DataTomeExpAvg.h
Created by Alexandre Hiroyuki Yamauchi, August 19, 2024.
***************************************************************/

#ifndef DATA_TOME_EXP_AVG_H
#define DATA_TOME_EXP_AVG_H

#include <limits.h>

// RECOMMENDED: use double type for TypeOfSum
// WARNING: using this class with integer types may result in a loss of
// precision due to cumulative rounding of integer divisions.
template <typename TypeOfSum>
class DataTomeExpAvg {
protected:
TypeOfSum _exp_avg;
unsigned long int _count;

public:
DataTomeExpAvg() : _exp_avg(0), _count(0) {}

DataTomeExpAvg<TypeOfSum> &push(TypeOfSum input) {
if (_count >= ULONG_MAX) {
_exp_avg = 0;
_count = 0;
}
_count++;

TypeOfSum multiplier = 2 / (_count);
_exp_avg = (input * multiplier) + (_exp_avg * (1 - multiplier));

return *this;
}

TypeOfSum get() { return _exp_avg; }
};

#endif // DATA_TOME_EXP_AVG_H
52 changes: 52 additions & 0 deletions test/test_DataTomeCumulative/DataTomeCumulative.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <DataTomeCumulative.h>
#include <stdio.h>
#include <unity.h>

void setUp(void) {} // before test

void tearDown(void) {} // after test

void test_getCumulativeAverage(void) {
DataTomeCumulative<double> testMV;

testMV.push(1);
testMV.push(2);
testMV.push(3);

TEST_ASSERT_EQUAL(2, testMV.get());
}

void process() {
UNITY_BEGIN();

RUN_TEST(test_getCumulativeAverage);

UNITY_END();
}

#ifdef ARDUINO

#include <Arduino.h>
void setup() {
// NOTE!!! Wait for >2 secs
// if board doesn't support software reset via Serial.DTR/RTS
delay(2000);

process();
}

void loop() {
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
}

#else

int main(int argc, char **argv) {
process();
return 0;
}

#endif
51 changes: 51 additions & 0 deletions test/test_DataTomeExpAvg/DataTomeExpAvg.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <DataTomeExpAvg.h>
#include <unity.h>

void setUp(void) {} // before test

void tearDown(void) {} // after test

void test_getExpAverage(void) {
DataTomeExpAvg<double> testMV;

testMV.push(1);
testMV.push(2);
testMV.push(3);

TEST_ASSERT_EQUAL(2, testMV.get());
}

void process() {
UNITY_BEGIN();

RUN_TEST(test_getExpAverage);

UNITY_END();
}

#ifdef ARDUINO

#include <Arduino.h>
void setup() {
// NOTE!!! Wait for >2 secs
// if board doesn't support software reset via Serial.DTR/RTS
delay(2000);

process();
}

void loop() {
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
}

#else

int main(int argc, char **argv) {
process();
return 0;
}

#endif

0 comments on commit f44a902

Please sign in to comment.