-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(DataTomeExpAvg): ✨ add DataTomeExpAvg with unit tests
- Loading branch information
1 parent
7fa4acd
commit f44a902
Showing
10 changed files
with
181 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,8 @@ | |
"test", | ||
"readme", | ||
"library_metadata", | ||
"DataTome" | ||
"DataTome", | ||
"DataTomeCumulative", | ||
"DataTomeExpAvg" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |