From be0f2c69c76a1321970bf06b2c021ac3e45ff94c Mon Sep 17 00:00:00 2001 From: tx_haggis <13982343+adbancroft@users.noreply.github.com> Date: Wed, 22 Dec 2021 22:07:38 -0600 Subject: [PATCH] Add table2d tests (#748) * Improve native debug experience * Add unit tests for table2d --- platformio.ini | 1 + speeduino/globals.h | 3 - speeduino/table2d.h | 3 + speeduino/table2d.ino | 15 ++- test/test_misc/test_table2d.cpp | 155 +++++++++++++++++++++++++ test/test_misc/test_table2d.h | 3 + test/test_misc/tests_misc.cpp | 2 + test/test_table3d_native/test_main.cpp | 16 +-- 8 files changed, 185 insertions(+), 13 deletions(-) create mode 100644 test/test_misc/test_table2d.cpp create mode 100644 test/test_misc/test_table2d.h diff --git a/platformio.ini b/platformio.ini index 98220163..f1b06b1c 100644 --- a/platformio.ini +++ b/platformio.ini @@ -162,6 +162,7 @@ default_envs = megaatmega2560 [env:native] platform = native build_flags = -std=gnu++11 +debug_build_flags = -std=gnu++11 -O0 -g3 test_ignore = test_misc, test_decoders, test_schedules debug_test = test_table3d_native build_type = debug \ No newline at end of file diff --git a/speeduino/globals.h b/speeduino/globals.h index 29f504a1..3157e0a4 100644 --- a/speeduino/globals.h +++ b/speeduino/globals.h @@ -280,9 +280,6 @@ #define HARD_CUT_FULL 0 #define HARD_CUT_ROLLING 1 -#define SIZE_BYTE 8 -#define SIZE_INT 16 - #define EVEN_FIRE 0 #define ODD_FIRE 1 diff --git a/speeduino/table2d.h b/speeduino/table2d.h index 09e75ecc..971212ed 100644 --- a/speeduino/table2d.h +++ b/speeduino/table2d.h @@ -4,6 +4,9 @@ This file is used for everything related to maps/tables including their definiti #ifndef TABLE_H #define TABLE_H +#define SIZE_BYTE 8 +#define SIZE_INT 16 + /* The 2D table can contain either 8-bit (byte) or 16-bit (int) values The valueSize variable should be set to either 8 or 16 to indicate this BEFORE the table is used diff --git a/speeduino/table2d.ino b/speeduino/table2d.ino index cc2175cb..bcd85fc2 100644 --- a/speeduino/table2d.ino +++ b/speeduino/table2d.ino @@ -9,7 +9,18 @@ Because the size of the table is dynamic, this functino is required to reallocat Note that this may clear some of the existing values of the table */ #include "table2d.h" +#if !defined(UNIT_TEST) #include "globals.h" +#endif + + +static inline uint8_t getCacheTime() { +#if !defined(UNIT_TEST) + return currentStatus.secl; +#else + return 0; +#endif +} /* This function pulls a 1D linear interpolated (ie averaged) value from a 2D table @@ -30,7 +41,7 @@ int table2D_getValue(struct table2D *fromTable, int X_in) int xMax = fromTable->xSize-1; //Check whether the X input is the same as last time this ran - if( (X_in == fromTable->lastInput) && (fromTable->cacheTime == currentStatus.secl) ) + if( (X_in == fromTable->lastInput) && (fromTable->cacheTime == getCacheTime()) ) { returnValue = fromTable->lastOutput; valueFound = true; @@ -49,7 +60,7 @@ int table2D_getValue(struct table2D *fromTable, int X_in) //Finally if none of that is found else { - fromTable->cacheTime = currentStatus.secl; //As we're not using the cache value, set the current secl value to track when this new value was calc'd + fromTable->cacheTime = getCacheTime(); //As we're not using the cache value, set the current secl value to track when this new value was calc'd //1st check is whether we're still in the same X bin as last time xMaxValue = table2D_getAxisValue(fromTable, fromTable->lastXMax); diff --git a/test/test_misc/test_table2d.cpp b/test/test_misc/test_table2d.cpp new file mode 100644 index 00000000..5a6ce3c1 --- /dev/null +++ b/test/test_misc/test_table2d.cpp @@ -0,0 +1,155 @@ +#include // memcpy +#include +#include +typedef uint8_t byte; +#include "test_table2d.h" +#include "table2d.h" + + +static constexpr uint8_t TEST_TABLE2D_SIZE = 9; +static uint8_t table2d_data_u8[TEST_TABLE2D_SIZE] = { + 251, 211, 199, 167, 127, 101, 59, 23, 5 +}; +static int16_t table2d_data_s16[TEST_TABLE2D_SIZE] = { + 32029, 26357, 21323, 16363, 11329, 7537, 5531, 2539, 1237 +}; + +static uint8_t table2d_axis_u8[TEST_TABLE2D_SIZE] { + 5, 23, 59, 101, 127, 167, 199, 211, 251, +}; +static int16_t table2d_axis_s16[TEST_TABLE2D_SIZE] = { + 123, 2539, 5531, 7537, 11329, 16363, 21323, 26357, 32029, +}; + +static table2D table2d_u8_u8; +static table2D table2d_u8_s16; +static table2D table2d_s16_u8; +static table2D table2d_s16_s16; + +template +void setup_test_subject(table2D &table, dataT *data, axisT *axis) +{ + table.valueSize = sizeof(dataT)*CHAR_BIT; + table.axisSize = sizeof(axisT)*CHAR_BIT; + table.xSize = TEST_TABLE2D_SIZE; + table.values = data; + table.axisX = axis; +} + +static void setup_test_subjects(void) +{ + setup_test_subject(table2d_u8_u8, table2d_data_u8, table2d_axis_u8); + setup_test_subject(table2d_u8_s16, table2d_data_u8, table2d_axis_s16); + setup_test_subject(table2d_s16_u8, table2d_data_s16, table2d_axis_u8); + setup_test_subject(table2d_s16_s16, table2d_data_s16, table2d_axis_s16); +} + + +void test_table2dLookup_50pct(void) +{ + setup_test_subjects(); + + uint8_t u8_u8_result = table2D_getValue(&table2d_u8_u8, table2d_axis_u8[3]+((table2d_axis_u8[4]-table2d_axis_u8[3])/2)); + TEST_ASSERT_EQUAL(147, u8_u8_result); + TEST_ASSERT_EQUAL(4, table2d_u8_u8.lastXMax); + + uint8_t u8_s16_result = table2D_getValue(&table2d_u8_s16, table2d_axis_s16[6]+((table2d_axis_s16[7]-table2d_axis_s16[6])/2)); + TEST_ASSERT_EQUAL(41, u8_s16_result); + TEST_ASSERT_EQUAL(7, table2d_u8_s16.lastXMax); + + int16_t s16_u8_result = table2D_getValue(&table2d_s16_u8, table2d_axis_u8[3]+((table2d_axis_u8[4]-table2d_axis_u8[3])/2)); + TEST_ASSERT_EQUAL(13846, s16_u8_result); + TEST_ASSERT_EQUAL(4, table2d_s16_u8.lastXMax); + + int16_t s16_s16_result = table2D_getValue(&table2d_s16_s16, table2d_axis_s16[3]+((table2d_axis_s16[4]-table2d_axis_s16[3])/2)); + TEST_ASSERT_EQUAL(13846, s16_s16_result); + TEST_ASSERT_EQUAL(4, table2d_s16_s16.lastXMax); +} + + +void test_table2dLookup_exactAxis(void) +{ + setup_test_subjects(); + + uint8_t u8_u8_result = table2D_getValue(&table2d_u8_u8, table2d_axis_u8[7]); + TEST_ASSERT_EQUAL(23, u8_u8_result); + // TEST_ASSERT_EQUAL(7, table2d_u8_u8.lastXMax); + + uint8_t u8_s16_result = table2D_getValue(&table2d_u8_s16, table2d_axis_s16[1]); + TEST_ASSERT_EQUAL(211, u8_s16_result); + // TEST_ASSERT_EQUAL(7, table2d_u8_s16.lastXMax); + + int16_t s16_u8_result = table2D_getValue(&table2d_s16_u8, table2d_axis_u8[2]); + TEST_ASSERT_EQUAL(21323, s16_u8_result); + // TEST_ASSERT_EQUAL(4, table2d_s16_u8.lastXMax); + + int16_t s16_s16_result = table2D_getValue(&table2d_s16_s16, table2d_axis_s16[5]); + TEST_ASSERT_EQUAL(7537, s16_s16_result); + // TEST_ASSERT_EQUAL(4, table2d_s16_s16.lastXMax); +} + +void test_table2dLookup_overMax(void) +{ + setup_test_subjects(); + + uint8_t u8_u8_result = table2D_getValue(&table2d_u8_u8, table2d_axis_u8[TEST_TABLE2D_SIZE-1]+1); + TEST_ASSERT_EQUAL(5, u8_u8_result); + // TEST_ASSERT_EQUAL(8, table2d_u8_u8.lastXMax); + + uint8_t u8_s16_result = table2D_getValue(&table2d_u8_s16, table2d_axis_s16[TEST_TABLE2D_SIZE-1]+1); + TEST_ASSERT_EQUAL(5, u8_s16_result); + // TEST_ASSERT_EQUAL(8, table2d_u8_s16.lastXMax); + + int16_t s16_u8_result = table2D_getValue(&table2d_s16_u8, table2d_axis_u8[TEST_TABLE2D_SIZE-1]+1); + TEST_ASSERT_EQUAL(1237, s16_u8_result); + // TEST_ASSERT_EQUAL(8, table2d_s16_u8.lastXMax); + + int16_t s16_s16_result = table2D_getValue(&table2d_s16_s16, table2d_axis_s16[TEST_TABLE2D_SIZE-1]+1); + TEST_ASSERT_EQUAL(1237, s16_s16_result); + // TEST_ASSERT_EQUAL(8, table2d_s16_s16.lastXMax); +} + +void test_table2dLookup_underMin(void) +{ + setup_test_subjects(); + + uint8_t u8_u8_result = table2D_getValue(&table2d_u8_u8, table2d_axis_u8[0]-1); + TEST_ASSERT_EQUAL(251, u8_u8_result); + // TEST_ASSERT_EQUAL(0, table2d_u8_u8.lastXMax); + + uint8_t u8_s16_result = table2D_getValue(&table2d_u8_s16, table2d_axis_s16[0]-1); + TEST_ASSERT_EQUAL(251, u8_s16_result); + // TEST_ASSERT_EQUAL(0, table2d_u8_s16.lastXMax); + + int16_t s16_u8_result = table2D_getValue(&table2d_s16_u8, table2d_axis_u8[0]-1); + TEST_ASSERT_EQUAL(32029, s16_u8_result); + // TEST_ASSERT_EQUAL(0, table2d_s16_u8.lastXMax); + + int16_t s16_s16_result = table2D_getValue(&table2d_s16_s16, table2d_axis_s16[0]-1); + TEST_ASSERT_EQUAL(32029, s16_s16_result); + // TEST_ASSERT_EQUAL(0, table2d_s16_s16.lastXMax); +} + + +void test_table2d_all_decrementing(void) +{ + setup_test_subjects(); + + uint8_t u8_u8_result_last = UINT8_MAX; + for (uint8_t loop=table2d_axis_u8[0]; loop<=table2d_axis_u8[TEST_TABLE2D_SIZE-1]; ++loop) + { + uint8_t u8_u8_result = table2D_getValue(&table2d_u8_u8, loop); + TEST_ASSERT_LESS_OR_EQUAL(u8_u8_result_last, u8_u8_result); + u8_u8_result_last = u8_u8_result; + } +} + + +void testTable2d() +{ + RUN_TEST(test_table2dLookup_50pct); + RUN_TEST(test_table2dLookup_exactAxis); + RUN_TEST(test_table2dLookup_overMax); + RUN_TEST(test_table2dLookup_underMin); + RUN_TEST(test_table2d_all_decrementing); +} \ No newline at end of file diff --git a/test/test_misc/test_table2d.h b/test/test_misc/test_table2d.h new file mode 100644 index 00000000..b59be2d7 --- /dev/null +++ b/test/test_misc/test_table2d.h @@ -0,0 +1,3 @@ +#pragma once + +extern void testTable2d(); \ No newline at end of file diff --git a/test/test_misc/tests_misc.cpp b/test/test_misc/tests_misc.cpp index 7be3c232..5245b780 100644 --- a/test/test_misc/tests_misc.cpp +++ b/test/test_misc/tests_misc.cpp @@ -5,6 +5,7 @@ #include "tests_init.h" #include "tests_tables.h" #include "tests_PW.h" +#include "test_table2d.h" #define UNITY_EXCLUDE_DETAILS @@ -22,6 +23,7 @@ void setup() testCorrections(); testPW(); testTables(); + testTable2d(); UNITY_END(); // stop unit testing } diff --git a/test/test_table3d_native/test_main.cpp b/test/test_table3d_native/test_main.cpp index 772c554d..fa1b2d07 100644 --- a/test/test_table3d_native/test_main.cpp +++ b/test/test_table3d_native/test_main.cpp @@ -1,18 +1,18 @@ #include #include "table3d_interpolate.cpp" +typedef uint8_t byte; +#include "table2d.ino" #include "..\test_misc\tests_tables.cpp" +#include "..\test_misc\test_table2d.cpp" int main(int argc, char **argv) { UNITY_BEGIN(); - RUN_TEST(test_tableLookup_50pct); - RUN_TEST(test_tableLookup_exact1Axis); - RUN_TEST(test_tableLookup_exact2Axis); - RUN_TEST(test_tableLookup_overMaxX); - RUN_TEST(test_tableLookup_overMaxY); - RUN_TEST(test_tableLookup_underMinX); - RUN_TEST(test_tableLookup_underMinY); - RUN_TEST(test_tableLookup_roundUp); + + testTables(); RUN_TEST(test_all_incrementing); + + testTable2d(); + UNITY_END(); return 0;