Skip to content

Commit

Permalink
test: ✅ add mvavg partial unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreHiroyuki committed Dec 23, 2023
1 parent 6503007 commit 4f1ede9
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: PlatformIO CI
name: PlatformIO Unit Tests

on: [push, pull_request, workflow_call]

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Data Tome is a C++ library for data analysis and data filtering on IoT devices.

_**—Recommended Platform—**_ Use the [PlatformIO Registry](https://registry.platformio.org/libraries/alexandrehiroyuki/DataTome) to install the library!

To avoid duplicating data on smaller samples, _[check the Partials feature](https://alexandrehiroyuki.github.io/DataTomeDocs/docs/category/partials)_!
To avoid duplicating data for smaller samples of the same data, _[check the Partials feature](https://alexandrehiroyuki.github.io/DataTomeDocs/docs/category/partials)_!

## Documentation

Expand Down
2 changes: 1 addition & 1 deletion src/DataTomeMvAvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class DataTomeMvAvg {

for (size_t i = _array_size; i < new_size; i++) _array[i] = 0;

if (_array_size < new_size) {
if (new_size > _array_size) {
_current_index = new_size - 1;
} else {
if (_current_index >= new_size) _current_index = new_size - 1;
Expand Down
83 changes: 61 additions & 22 deletions test/DataTomeMvAvg.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,65 +9,104 @@ void tearDown(void) {
// clean stuff up here
}

long int data[10] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};

void test_get_average(void) {
DataTomeMvAvg<long int> TestMV(5);

TestMV.push(2).push(4).push(6);
TestMV.push(data[0]).push(data[1]).push(data[2]);

TEST_ASSERT_EQUAL(4, TestMV.get());
TEST_ASSERT_EQUAL(5, TestMV.get_by_brute(2));
long int mock_average = (data[0] + data[1] + data[2]) / 3;
long int mock_brute_average = (data[1] + data[2]) / 2;
TEST_ASSERT_EQUAL(mock_average, TestMV.get());
TEST_ASSERT_EQUAL(mock_brute_average, TestMV.get_by_brute(2));
}

void test_get_front_n_back_elements(void) {
DataTomeMvAvg<long int> TestMV(5);

TestMV.push(2).push(4).push(6);
TestMV.push(data[0]).push(data[1]).push(data[2]).push(data[3]).push(data[4]);

TEST_ASSERT_EQUAL(6, TestMV.front());
TEST_ASSERT_EQUAL(2, TestMV.back());
TEST_ASSERT_EQUAL(data[4], TestMV.front());
TEST_ASSERT_EQUAL(data[0], TestMV.back());
}

void test_get_elements_by_index(void) {
DataTomeMvAvg<long int> TestMV(5);

TestMV.push(2).push(4).push(6);
TestMV.push(data[0]).push(data[1]).push(data[2]);

TEST_ASSERT_EQUAL(6, TestMV[0]);
TEST_ASSERT_EQUAL(4, TestMV[1]);
TEST_ASSERT_EQUAL(2, TestMV[2]);
TEST_ASSERT_EQUAL(2, TestMV.at_index(0));
TEST_ASSERT_EQUAL(4, TestMV.at_index(1));
TEST_ASSERT_EQUAL(6, TestMV.at_index(2));
TEST_ASSERT_EQUAL(data[2], TestMV[0]);
TEST_ASSERT_EQUAL(data[1], TestMV[1]);
TEST_ASSERT_EQUAL(data[0], TestMV[2]);
TEST_ASSERT_EQUAL(data[0], TestMV.at_index(0));
TEST_ASSERT_EQUAL(data[1], TestMV.at_index(1));
TEST_ASSERT_EQUAL(data[2], TestMV.at_index(2));
}

void test_size_n_resize(void) {
DataTomeMvAvg<long int> TestMV(5);

TestMV.push(2).push(4).push(6).push(8).push(10).push(12);

TEST_ASSERT_EQUAL(5, TestMV.size());
TEST_ASSERT_EQUAL(4, TestMV.back());
TestMV.resize(10);
TEST_ASSERT_EQUAL(10, TestMV.size());
size_t expected_size = 5;
size_t new_expected_size = 10;

long int mock_sum = 0;
for (size_t i = 0; i < expected_size + 1; i++) {
TestMV.push(data[i]);
mock_sum += data[i];
}
mock_sum -= data[0];

TEST_ASSERT_EQUAL(expected_size, TestMV.size());

long int old_average = TestMV.get();
TestMV.resize(new_expected_size);
TEST_ASSERT_EQUAL(new_expected_size, TestMV.size());
TEST_ASSERT_EQUAL(old_average, TestMV.get());

TestMV.push(data[6]);
mock_sum += data[6];
TEST_ASSERT_EQUAL(mock_sum / 6, TestMV.get());
}

void test_clear(void) {
DataTomeMvAvg<long int> TestMV(5);

TestMV.push(2);
TestMV.push(data[0]);

TEST_ASSERT_EQUAL(data[0], TestMV.get());

TEST_ASSERT_EQUAL(2, TestMV.get());
TestMV.clear();
TEST_ASSERT_EQUAL(0, TestMV.get());
}

void test_partials(void) {
DataTomeMvAvg<long int> TestMV(10);
size_t partial_size = 3;
size_t data_count = 5;

size_t partial_id = TestMV.create_partial(partial_size);

for (size_t i = 0; i < data_count; i++) {
TestMV.push(data[i]);
}

long int mock_sum = 0;
for (size_t i = data_count - 1; i > data_count - partial_size - 1; i--) {
mock_sum += data[i];
}

TEST_ASSERT_EQUAL((mock_sum / partial_size), TestMV.get_partial(partial_id));
}

void process() {
UNITY_BEGIN();

RUN_TEST(test_get_average);
RUN_TEST(test_get_front_n_back_elements);
RUN_TEST(test_get_elements_by_index);
RUN_TEST(test_size_n_resize);
RUN_TEST(test_clear);
RUN_TEST(test_partials);

UNITY_END();
}
Expand Down

0 comments on commit 4f1ede9

Please sign in to comment.