Skip to content

Commit 346ea03

Browse files
authored
Teensy 35 test fixes (speeduino#1343)
* Avoid division by zero * Unit tests: compile fixes for teensy 3.5 * Compile warning: use fixed size array parameters
1 parent 4a75bc4 commit 346ea03

File tree

5 files changed

+21
-20
lines changed

5 files changed

+21
-20
lines changed

speeduino/maths.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ static inline int16_t LOW_PASS_FILTER(int16_t input, uint8_t alpha, int16_t prio
372372
*/
373373
static inline uint8_t scale(const uint8_t from, const uint8_t fromRange, const uint8_t toRange) {
374374
// Using uint16_t to avoid overflow when calculating the result
375-
return (((uint16_t)from * (uint16_t)toRange) / (uint16_t)fromRange);
375+
return fromRange==0U ? 0U : (((uint16_t)from * (uint16_t)toRange) / (uint16_t)fromRange);
376376
}
377377

378378
/**

test/test_math/test_fast_map.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static void test_fast_map_edge_cases(void) {
1515
TEST_ASSERT_EQUAL_UINT8(0, fast_map(10, 10, 20, 0, 100));
1616
// Input equals end of range
1717
TEST_ASSERT_EQUAL_UINT8(100, fast_map(20, 10, 20, 0, 100));
18-
// Single point range
18+
// // Single point range
1919
TEST_ASSERT_EQUAL_UINT8(50, fast_map(10, 10, 10, 50, 50));
2020
}
2121

test/test_math/test_low_pass_filter.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,34 @@
44

55
static void test_U16_min(void) {
66
// Passing zero for the filter value should return the input value
7-
TEST_ASSERT_EQUAL_UINT16(0, LOW_PASS_FILTER(0U, 0U, 0U));
8-
TEST_ASSERT_EQUAL_UINT16(1234U, LOW_PASS_FILTER(1234U, 0U, 0U));
9-
TEST_ASSERT_EQUAL_UINT16(1234U, LOW_PASS_FILTER(1234U, 0U, 9999U));
7+
TEST_ASSERT_EQUAL_UINT16(0, LOW_PASS_FILTER(0U, 0U, (uint16_t)0));
8+
TEST_ASSERT_EQUAL_UINT16(1234U, LOW_PASS_FILTER(1234U, 0U, (uint16_t)0));
9+
TEST_ASSERT_EQUAL_UINT16(1234U, LOW_PASS_FILTER(1234U, 0U, (uint16_t)9999));
1010
}
1111

1212
static void test_U16_max(void) {
1313
// Passing UINT8_MAX for the filter value should make the input close to the previous value
14-
TEST_ASSERT_EQUAL_UINT16(0, LOW_PASS_FILTER(0U, UINT8_MAX, 0U));
15-
TEST_ASSERT_EQUAL_UINT16(4U, LOW_PASS_FILTER(1234U, UINT8_MAX, 0U));
16-
TEST_ASSERT_EQUAL_UINT16(9964U, LOW_PASS_FILTER(1234U, UINT8_MAX, 9999U));
14+
TEST_ASSERT_EQUAL_UINT16(0, LOW_PASS_FILTER((uint16_t)0, UINT8_MAX, (uint16_t)0));
15+
TEST_ASSERT_EQUAL_UINT16(4U, LOW_PASS_FILTER((uint16_t)1234, UINT8_MAX, (uint16_t)0));
16+
TEST_ASSERT_EQUAL_UINT16(9964U, LOW_PASS_FILTER((uint16_t)1234, UINT8_MAX, (uint16_t)9999));
1717
}
1818

1919
static void test_S16_min(void) {
2020
// Passing zero for the filter value should return the input value
21-
TEST_ASSERT_EQUAL_INT16(0, LOW_PASS_FILTER(0, 0U, 0));
22-
TEST_ASSERT_EQUAL_INT16(1234, LOW_PASS_FILTER(1234, 0U, 0));
23-
TEST_ASSERT_EQUAL_INT16(-1234, LOW_PASS_FILTER(-1234, 0U, 0));
24-
TEST_ASSERT_EQUAL_INT16(1234, LOW_PASS_FILTER(1234, 0U, 9999));
25-
TEST_ASSERT_EQUAL_INT16(-1234, LOW_PASS_FILTER(-1234, 0U, 9999));
21+
TEST_ASSERT_EQUAL_INT16(0, LOW_PASS_FILTER((int16_t)0, 0U, (int16_t)0));
22+
TEST_ASSERT_EQUAL_INT16(1234, LOW_PASS_FILTER((int16_t)1234, 0U, (int16_t)0));
23+
TEST_ASSERT_EQUAL_INT16(-1234, LOW_PASS_FILTER((int16_t)-1234, 0U, (int16_t)0));
24+
TEST_ASSERT_EQUAL_INT16(1234, LOW_PASS_FILTER((int16_t)1234, 0U, (int16_t)9999));
25+
TEST_ASSERT_EQUAL_INT16(-1234, LOW_PASS_FILTER((int16_t)-1234, 0U, (int16_t)9999));
2626
}
2727

2828
static void test_S16_max(void) {
2929
// Passing UINT8_MAX for the filter value should make the input close to the previous value
30-
TEST_ASSERT_EQUAL_INT16(0, LOW_PASS_FILTER(0, UINT8_MAX, 0));
31-
TEST_ASSERT_EQUAL_INT16(4, LOW_PASS_FILTER(1234, UINT8_MAX, 0));
32-
TEST_ASSERT_EQUAL_INT16(-4, LOW_PASS_FILTER(-1234, UINT8_MAX, 0));
33-
TEST_ASSERT_EQUAL_INT16(9964, LOW_PASS_FILTER(1234, UINT8_MAX, 9999));
34-
TEST_ASSERT_EQUAL_INT16(-9964, LOW_PASS_FILTER(-1234, UINT8_MAX, -9999));
30+
TEST_ASSERT_EQUAL_INT16(0, LOW_PASS_FILTER((int16_t)0, UINT8_MAX, (int16_t)0));
31+
TEST_ASSERT_EQUAL_INT16(4, LOW_PASS_FILTER((int16_t)1234, UINT8_MAX, (int16_t)0));
32+
TEST_ASSERT_EQUAL_INT16(-4, LOW_PASS_FILTER((int16_t)-1234, UINT8_MAX, (int16_t)0));
33+
TEST_ASSERT_EQUAL_INT16(9964, LOW_PASS_FILTER((int16_t)1234, UINT8_MAX, (int16_t)9999));
34+
TEST_ASSERT_EQUAL_INT16(-9964, LOW_PASS_FILTER((int16_t)-1234, UINT8_MAX, (int16_t)-9999));
3535
}
3636

3737
void test_LOW_PASS_FILTER(void) {

test/test_utils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ static inline void populate_2dtable(table2D<axis_t, value_t, sizeT> *pTable, val
143143
}
144144

145145
template <typename axis_t, typename value_t, uint8_t sizeT>
146-
static inline void populate_2dtable(table2D<axis_t, value_t, sizeT> *pTable, const value_t values[], const axis_t bins[]) {
146+
static inline void populate_2dtable(table2D<axis_t, value_t, sizeT> *pTable, const value_t (&values)[sizeT], const axis_t (&bins)[sizeT]) {
147147
memcpy((void*)pTable->axis, bins, sizeT * sizeof(axis_t));
148148
memcpy((void*)pTable->values, values, sizeT * sizeof(value_t));
149149
pTable->cache.cacheTime = UINT8_MAX;
@@ -152,7 +152,7 @@ static inline void populate_2dtable(table2D<axis_t, value_t, sizeT> *pTable, con
152152
// Populate a 2d table (from PROGMEM if available)
153153
// You would typically declare the 2 source arrays using TEST_DATA_P
154154
template <typename axis_t, typename value_t, uint8_t sizeT>
155-
static inline void populate_2dtable_P(table2D<axis_t, value_t, sizeT> *pTable, const value_t values[], const axis_t bins[]) {
155+
static inline void populate_2dtable_P(table2D<axis_t, value_t, sizeT> *pTable, const value_t (&values)[sizeT], const axis_t (&bins)[sizeT]) {
156156
#if defined(PROGMEM)
157157
memcpy_P((void*)pTable->axis, bins, sizeT * sizeof(axis_t));
158158
memcpy_P((void*)pTable->values, values, sizeT * sizeof(value_t));

test/timer.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <inttypes.h>
4+
#include <stdio.h>
45

56
#if defined(__AVR__)
67
#include <Arduino.h>

0 commit comments

Comments
 (0)