foxBMS - Unit Tests  1.6.0
The foxBMS Unit Tests API Documentation
test_foxmath.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2023, 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 2023-10-12 (date of last update)
47  * @version v1.6.0
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 /*========== Unit Testing Framework Directives ==============================*/
61 TEST_INCLUDE_PATH("../../src/app/driver/foxmath")
62 
63 /*========== Definitions and Implementations for Unit Test ==================*/
64 uint16_t val16;
65 uint32_t val32;
66 uint64_t val64;
67 float coord_x1;
68 float coord_x2;
69 float coord_y1;
70 float coord_y2;
72 
73 /*========== Setup and Teardown =============================================*/
74 void setUp(void) {
75  val16 = 0u;
76  val32 = 0u;
77  val64 = 0u;
78  coord_x1 = 10.f;
79  coord_y1 = 50.f;
80  coord_x2 = 20.f;
81  coord_y2 = 100.f;
82  coord_x_interpolate = 15.f;
83 }
84 
85 void tearDown(void) {
86 }
87 
88 /*========== Test Cases =====================================================*/
92 }
93 
95  TEST_ASSERT_EQUAL(75.0f, MATH_LinearInterpolation(10.f, 50.f, 20.f, 100.f, 15.f));
96  TEST_ASSERT_EQUAL(87.0f, MATH_LinearInterpolation(10.f, 50.f, 20.f, 100.f, 17.5f));
97  TEST_ASSERT_EQUAL(50.0f, MATH_LinearInterpolation(10.f, 50.f, 20.f, 100.f, 10.0001f));
98 }
99 
101  TEST_ASSERT_EQUAL(100.0f, MATH_LinearInterpolation(10.f, 50.f, 20.f, 100.f, 20.1f));
102  TEST_ASSERT_EQUAL(-100.0f, MATH_LinearInterpolation(10.f, 50.f, 20.f, 100.f, -20.f));
103  TEST_ASSERT_EQUAL(16739465.0f, MATH_LinearInterpolation(10.f, 50.f, 20.f, 100.f, 3347893.0f));
104 }
105 
107  TEST_ASSERT_EQUAL(0u, MATH_SwapBytesUint16_t(val16));
108 }
109 
111  TEST_ASSERT_EQUAL(0u, MATH_SwapBytesUint32_t(val32));
112 }
113 
115  TEST_ASSERT_EQUAL(0u, MATH_SwapBytesUint64_t(val64));
116 }
117 
118 void test_swap16(void) {
119  val16 = 786u; /* any random value */
120  TEST_ASSERT_EQUAL(4611u, MATH_SwapBytesUint16_t(val16));
121 }
122 
123 void test_swap32(void) {
124  val32 = 0xFFFF0000u; /* any random value */
125  TEST_ASSERT_EQUAL(0x0000FFFFu, MATH_SwapBytesUint32_t(val32));
126 }
127 
128 void test_swap64(void) {
129  val64 = 123u; /* 0x007B */
130  TEST_ASSERT_EQUAL(0x7B00000000000000u, MATH_SwapBytesUint64_t(val64));
131 }
132 
133 void test_swap16_MAX(void) {
134  val16 = UINT16_MAX;
135  TEST_ASSERT_EQUAL(UINT16_MAX, MATH_SwapBytesUint16_t(val16));
136 }
137 
138 void test_swap32_MAX(void) {
139  val32 = UINT32_MAX;
140  TEST_ASSERT_EQUAL(UINT32_MAX, MATH_SwapBytesUint32_t(val32));
141 }
142 
143 void test_swap64_MAX(void) {
144  val64 = UINT64_MAX;
145  TEST_ASSERT_EQUAL(UINT64_MAX, MATH_SwapBytesUint64_t(val64));
146 }
147 
149  float minimum = 0.0f;
150  /* Test 1: all values are equal */
151  minimum = MATH_MinimumOfTwoFloats(1.0f, 1.0f);
152  TEST_ASSERT_EQUAL(1.0f, minimum);
153  /* Test 2: the first value is the smallest */
154  minimum = MATH_MinimumOfTwoFloats(1.0f, 2.0f);
155  TEST_ASSERT_EQUAL(1.0f, minimum);
156  /* Test 3: the last value is the smallest */
157  minimum = MATH_MinimumOfTwoFloats(3.0f, 2.0f);
158  TEST_ASSERT_EQUAL(2.0f, minimum);
159  /* Test 4: the first value is the smallest */
160  minimum = MATH_MinimumOfTwoFloats(-3.0f, 1.0f);
161  TEST_ASSERT_EQUAL(-3.0f, minimum);
162  /* Test 5: the first value is the smallest */
163  minimum = MATH_MinimumOfTwoFloats(-3.0f, -1.0f);
164  TEST_ASSERT_EQUAL(-3.0f, minimum);
165 }
166 
168  uint8_t value1 = 42u;
169  uint8_t value2 = 67u;
170 
171  TEST_ASSERT_EQUAL_UINT8(value1, MATH_MinimumOfTwoUint8_t(value1, value2));
172  TEST_ASSERT_EQUAL_UINT8(value1, MATH_MinimumOfTwoUint8_t(value2, value1));
173  TEST_ASSERT_EQUAL_UINT8(value1, MATH_MinimumOfTwoUint8_t(value1, UINT8_MAX));
174  TEST_ASSERT_EQUAL_UINT8(0u, MATH_MinimumOfTwoUint8_t(value1, 0u));
175  TEST_ASSERT_EQUAL_UINT8(0u, MATH_MinimumOfTwoUint8_t(UINT8_MAX, 0u));
176  TEST_ASSERT_EQUAL_UINT8(0u, MATH_MinimumOfTwoUint8_t(0u, UINT8_MAX));
177 }
178 
180  uint16_t value1 = 0xFF42u;
181  uint16_t value2 = 0xFF67u;
182 
183  TEST_ASSERT_EQUAL_UINT16(value1, MATH_MinimumOfTwoUint16_t(value1, value2));
184  TEST_ASSERT_EQUAL_UINT16(value1, MATH_MinimumOfTwoUint16_t(value2, value1));
185  TEST_ASSERT_EQUAL_UINT16(value1, MATH_MinimumOfTwoUint16_t(value1, UINT16_MAX));
186  TEST_ASSERT_EQUAL_UINT16(0u, MATH_MinimumOfTwoUint16_t(value1, 0u));
187  TEST_ASSERT_EQUAL_UINT16(0u, MATH_MinimumOfTwoUint16_t(UINT16_MAX, 0u));
188  TEST_ASSERT_EQUAL_UINT16(0u, MATH_MinimumOfTwoUint16_t(0u, UINT16_MAX));
189 }
float_t MATH_MinimumOfTwoFloats(const float_t value1, const float_t value2)
Returns the minimum of the passed float values.
Definition: foxmath.c:132
uint16_t MATH_SwapBytesUint16_t(const uint16_t val)
Swap bytes of uint16_t value.
Definition: foxmath.c:107
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:144
uint32_t MATH_SwapBytesUint32_t(const uint32_t val)
Swap bytes of uint32_t value.
Definition: foxmath.c:111
float_t MATH_LinearInterpolation(const float_t x1, const float_t y1, const float_t x2, const float_t y2, const float_t x_interpolate)
Linear inter-/extrapolates a third point according to two given points.
Definition: foxmath.c:84
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:136
uint64_t MATH_SwapBytesUint64_t(const uint64_t val)
Swap bytes of uint64_t value.
Definition: foxmath.c:119
math library for often used math functions
void test_allZeros_swap32(void)
Definition: test_foxmath.c:110
void test_swap16(void)
Definition: test_foxmath.c:118
void test_allZeros_swap64(void)
Definition: test_foxmath.c:114
uint32_t val32
Definition: test_foxmath.c:65
float coord_x_interpolate
Definition: test_foxmath.c:71
float coord_x2
Definition: test_foxmath.c:68
void test_minimumOfTwoFloats(void)
Definition: test_foxmath.c:148
void test_swap64_MAX(void)
Definition: test_foxmath.c:143
void test_linearInterpolation_X1EqualsX2(void)
Definition: test_foxmath.c:89
float coord_x1
Definition: test_foxmath.c:67
void test_linearInterpolation_interpolateBetweenX1AndX2(void)
Definition: test_foxmath.c:94
void test_MATH_MinimumOfTwoUint16_t(void)
Definition: test_foxmath.c:179
void setUp(void)
Definition: test_foxmath.c:74
float coord_y2
Definition: test_foxmath.c:70
void tearDown(void)
Definition: test_foxmath.c:85
void test_allZeros_swap16(void)
Definition: test_foxmath.c:106
void test_linearInterpolation_extrapolateFromX1AndX2(void)
Definition: test_foxmath.c:100
void test_swap32_MAX(void)
Definition: test_foxmath.c:138
void test_swap16_MAX(void)
Definition: test_foxmath.c:133
float coord_y1
Definition: test_foxmath.c:69
uint16_t val16
Definition: test_foxmath.c:64
uint64_t val64
Definition: test_foxmath.c:66
void test_swap64(void)
Definition: test_foxmath.c:128
void test_MATH_MinimumOfTwoUint8_t(void)
Definition: test_foxmath.c:167
void test_swap32(void)
Definition: test_foxmath.c:123