foxBMS - Unit Tests  1.4.1
The foxBMS Unit Tests API Documentation
test_foxmath.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2022, Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice, this
12  * list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * We kindly request you to use one or more of the following phrases to refer to
34  * foxBMS in your hardware, software, documentation or advertising materials:
35  *
36  * - ″This product uses parts of foxBMS®″
37  * - ″This product includes parts of foxBMS®″
38  * - ″This product is derived from foxBMS®″
39  *
40  */
41 
42 /**
43  * @file test_foxmath.c
44  * @author foxBMS Team
45  * @date 2020-04-01 (date of creation)
46  * @updated 2022-10-27 (date of last update)
47  * @version v1.4.1
48  * @ingroup UNIT_TEST_IMPLEMENTATION
49  * @prefix TEST
50  *
51  * @brief Tests for the foxmath module
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "unity.h"
57 
58 #include "foxmath.h"
59 
60 /*========== Definitions and Implementations for Unit Test ==================*/
61 uint16_t val16;
62 uint32_t val32;
63 uint64_t val64;
64 float coord_x1;
65 float coord_x2;
66 float coord_y1;
67 float coord_y2;
69 
70 /*========== Setup and Teardown =============================================*/
71 void setUp(void) {
72  val16 = 0u;
73  val32 = 0u;
74  val64 = 0u;
75  coord_x1 = 10.f;
76  coord_y1 = 50.f;
77  coord_x2 = 20.f;
78  coord_y2 = 100.f;
79  coord_x_interpolate = 15.f;
80 }
81 
82 void tearDown(void) {
83 }
84 
85 /*========== Test Cases =====================================================*/
89 }
90 
92  TEST_ASSERT_EQUAL(75.0f, MATH_LinearInterpolation(10.f, 50.f, 20.f, 100.f, 15.f));
93  TEST_ASSERT_EQUAL(87.0f, MATH_LinearInterpolation(10.f, 50.f, 20.f, 100.f, 17.5f));
94  TEST_ASSERT_EQUAL(50.0f, MATH_LinearInterpolation(10.f, 50.f, 20.f, 100.f, 10.0001f));
95 }
96 
98  TEST_ASSERT_EQUAL(100.0f, MATH_LinearInterpolation(10.f, 50.f, 20.f, 100.f, 20.1f));
99  TEST_ASSERT_EQUAL(-100.0f, MATH_LinearInterpolation(10.f, 50.f, 20.f, 100.f, -20.f));
100  TEST_ASSERT_EQUAL(16739465.0f, MATH_LinearInterpolation(10.f, 50.f, 20.f, 100.f, 3347893.0f));
101 }
102 
104  TEST_ASSERT_EQUAL(0u, MATH_SwapBytesUint16_t(val16));
105 }
106 
108  TEST_ASSERT_EQUAL(0u, MATH_SwapBytesUint32_t(val32));
109 }
110 
112  TEST_ASSERT_EQUAL(0u, MATH_SwapBytesUint64_t(val64));
113 }
114 
115 void test_swap16(void) {
116  val16 = 786u; /* any random value */
117  TEST_ASSERT_EQUAL(4611u, MATH_SwapBytesUint16_t(val16));
118 }
119 
120 void test_swap32(void) {
121  val32 = 0xFFFF0000u; /* any random value */
122  TEST_ASSERT_EQUAL(0x0000FFFFu, MATH_SwapBytesUint32_t(val32));
123 }
124 
125 void test_swap64(void) {
126  val64 = 123u; /* 0x007B */
127  TEST_ASSERT_EQUAL(0x7B00000000000000u, MATH_SwapBytesUint64_t(val64));
128 }
129 
130 void test_swap16_MAX(void) {
131  val16 = UINT16_MAX;
132  TEST_ASSERT_EQUAL(UINT16_MAX, MATH_SwapBytesUint16_t(val16));
133 }
134 
135 void test_swap32_MAX(void) {
136  val32 = UINT32_MAX;
137  TEST_ASSERT_EQUAL(UINT32_MAX, MATH_SwapBytesUint32_t(val32));
138 }
139 
140 void test_swap64_MAX(void) {
141  val64 = UINT64_MAX;
142  TEST_ASSERT_EQUAL(UINT64_MAX, MATH_SwapBytesUint64_t(val64));
143 }
144 
146  float minimum = 0.0f;
147  /* Test 1: all values are equal */
148  minimum = MATH_MinimumOfTwoFloats(1.0f, 1.0f);
149  TEST_ASSERT_EQUAL(1.0f, minimum);
150  /* Test 2: the first value is the smallest */
151  minimum = MATH_MinimumOfTwoFloats(1.0f, 2.0f);
152  TEST_ASSERT_EQUAL(1.0f, minimum);
153  /* Test 3: the last value is the smallest */
154  minimum = MATH_MinimumOfTwoFloats(3.0f, 2.0f);
155  TEST_ASSERT_EQUAL(2.0f, minimum);
156  /* Test 4: the first value is the smallest */
157  minimum = MATH_MinimumOfTwoFloats(-3.0f, 1.0f);
158  TEST_ASSERT_EQUAL(-3.0f, minimum);
159  /* Test 5: the first value is the smallest */
160  minimum = MATH_MinimumOfTwoFloats(-3.0f, -1.0f);
161  TEST_ASSERT_EQUAL(-3.0f, minimum);
162 }
163 
165  uint8_t value1 = 42u;
166  uint8_t value2 = 67u;
167 
168  TEST_ASSERT_EQUAL_UINT8(value1, MATH_MinimumOfTwoUint8_t(value1, value2));
169  TEST_ASSERT_EQUAL_UINT8(value1, MATH_MinimumOfTwoUint8_t(value2, value1));
170  TEST_ASSERT_EQUAL_UINT8(value1, MATH_MinimumOfTwoUint8_t(value1, UINT8_MAX));
171  TEST_ASSERT_EQUAL_UINT8(0u, MATH_MinimumOfTwoUint8_t(value1, 0u));
172  TEST_ASSERT_EQUAL_UINT8(0u, MATH_MinimumOfTwoUint8_t(UINT8_MAX, 0u));
173  TEST_ASSERT_EQUAL_UINT8(0u, MATH_MinimumOfTwoUint8_t(0u, UINT8_MAX));
174 }
175 
177  uint16_t value1 = 0xFF42u;
178  uint16_t value2 = 0xFF67u;
179 
180  TEST_ASSERT_EQUAL_UINT16(value1, MATH_MinimumOfTwoUint16_t(value1, value2));
181  TEST_ASSERT_EQUAL_UINT16(value1, MATH_MinimumOfTwoUint16_t(value2, value1));
182  TEST_ASSERT_EQUAL_UINT16(value1, MATH_MinimumOfTwoUint16_t(value1, UINT16_MAX));
183  TEST_ASSERT_EQUAL_UINT16(0u, MATH_MinimumOfTwoUint16_t(value1, 0u));
184  TEST_ASSERT_EQUAL_UINT16(0u, MATH_MinimumOfTwoUint16_t(UINT16_MAX, 0u));
185  TEST_ASSERT_EQUAL_UINT16(0u, MATH_MinimumOfTwoUint16_t(0u, UINT16_MAX));
186 }
float MATH_LinearInterpolation(const float x1, const float y1, const float x2, const float y2, const float x_interpolate)
Linear inter-/extrapolates a third point according to two given points.
Definition: foxmath.c:86
uint16_t MATH_SwapBytesUint16_t(const uint16_t val)
Swap bytes of uint16_t value.
Definition: foxmath.c:109
uint16_t MATH_MinimumOfTwoUint16_t(const uint16_t value1, const uint16_t value2)
Returns the minimum of the passed uint16_t values.
Definition: foxmath.c:146
uint32_t MATH_SwapBytesUint32_t(const uint32_t val)
Swap bytes of uint32_t value.
Definition: foxmath.c:113
uint8_t MATH_MinimumOfTwoUint8_t(const uint8_t value1, const uint8_t value2)
Returns the minimum of the passed uint8_t values.
Definition: foxmath.c:138
float MATH_MinimumOfTwoFloats(const float value1, const float value2)
Returns the minimum of the passed float values.
Definition: foxmath.c:134
uint64_t MATH_SwapBytesUint64_t(const uint64_t val)
Swap bytes of uint64_t value.
Definition: foxmath.c:121
math library for often used math functions
void test_allZeros_swap32(void)
Definition: test_foxmath.c:107
void test_swap16(void)
Definition: test_foxmath.c:115
void test_allZeros_swap64(void)
Definition: test_foxmath.c:111
void test_linearInterpolation_extrapolateFromX1AndX2()
Definition: test_foxmath.c:97
uint32_t val32
Definition: test_foxmath.c:62
float coord_x_interpolate
Definition: test_foxmath.c:68
float coord_x2
Definition: test_foxmath.c:65
void test_minimumOfTwoFloats(void)
Definition: test_foxmath.c:145
void test_linearInterpolation_X1EqualsX2()
Definition: test_foxmath.c:86
void test_swap64_MAX(void)
Definition: test_foxmath.c:140
float coord_x1
Definition: test_foxmath.c:64
void test_linearInterpolation_interpolateBetweenX1AndX2()
Definition: test_foxmath.c:91
void test_MATH_MinimumOfTwoUint16_t(void)
Definition: test_foxmath.c:176
void setUp(void)
Definition: test_foxmath.c:71
float coord_y2
Definition: test_foxmath.c:67
void tearDown(void)
Definition: test_foxmath.c:82
void test_allZeros_swap16(void)
Definition: test_foxmath.c:103
void test_swap32_MAX(void)
Definition: test_foxmath.c:135
void test_swap16_MAX(void)
Definition: test_foxmath.c:130
float coord_y1
Definition: test_foxmath.c:66
uint16_t val16
Definition: test_foxmath.c:61
uint64_t val64
Definition: test_foxmath.c:63
void test_swap64(void)
Definition: test_foxmath.c:125
void test_MATH_MinimumOfTwoUint8_t(void)
Definition: test_foxmath.c:164
void test_swap32(void)
Definition: test_foxmath.c:120