Add table2d tests (#748)

* Improve native debug experience

* Add unit tests for table2d
This commit is contained in:
tx_haggis 2021-12-22 22:07:38 -06:00 committed by GitHub
parent 2cd7ed327b
commit be0f2c69c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 185 additions and 13 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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);

View File

@ -0,0 +1,155 @@
#include <string.h> // memcpy
#include <unity.h>
#include <stdio.h>
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 <typename dataT, typename axisT>
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);
}

View File

@ -0,0 +1,3 @@
#pragma once
extern void testTable2d();

View File

@ -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
}

View File

@ -1,18 +1,18 @@
#include <unity.h>
#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;