From 4f1ede944908d3449b9050394e87cba48cd68909 Mon Sep 17 00:00:00 2001 From: AlexandreHiroyuki Date: Sat, 23 Dec 2023 20:10:05 -0300 Subject: [PATCH] test: :white_check_mark: add mvavg partial unit tests --- .github/workflows/tests.yml | 2 +- README.md | 2 +- src/DataTomeMvAvg.h | 2 +- test/DataTomeMvAvg.test.cpp | 83 +++++++++++++++++++++++++++---------- 4 files changed, 64 insertions(+), 25 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ca69cc3..041ed11 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -1,4 +1,4 @@ -name: PlatformIO CI +name: PlatformIO Unit Tests on: [push, pull_request, workflow_call] diff --git a/README.md b/README.md index 6f72601..a2e9495 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/DataTomeMvAvg.h b/src/DataTomeMvAvg.h index 8758e3a..5b543be 100644 --- a/src/DataTomeMvAvg.h +++ b/src/DataTomeMvAvg.h @@ -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; diff --git a/test/DataTomeMvAvg.test.cpp b/test/DataTomeMvAvg.test.cpp index 40855b1..5b12497 100644 --- a/test/DataTomeMvAvg.test.cpp +++ b/test/DataTomeMvAvg.test.cpp @@ -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 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 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 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 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 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 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(); }