foxBMS - Unit Tests  1.1.2
The foxBMS Unit Tests API Documentation
test_foxmath.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2021, 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 2021-08-06 (date of last update)
47  * @ingroup UNIT_TEST_IMPLEMENTATION
48  * @prefix TEST
49  *
50  * @brief Tests for the foxmath module
51  *
52  */
53 
54 /*========== Includes =======================================================*/
55 #include "unity.h"
56 
57 #include "foxmath.h"
58 
59 /*========== Definitions and Implementations for Unit Test ==================*/
60 uint16_t val16;
61 uint32_t val32;
62 uint64_t val64;
63 float coord_x1;
64 float coord_x2;
65 float coord_y1;
66 float coord_y2;
68 
69 /*========== Setup and Teardown =============================================*/
70 void setUp(void) {
71  val16 = 0u;
72  val32 = 0u;
73  val64 = 0u;
74  coord_x1 = 10.f;
75  coord_y1 = 50.f;
76  coord_x2 = 20.f;
77  coord_y2 = 100.f;
78  coord_x_interpolate = 15.f;
79 }
80 
81 void tearDown(void) {
82 }
83 
84 /*========== Test Cases =====================================================*/
88 }
89 
91  TEST_ASSERT_EQUAL(75.0f, MATH_LinearInterpolation(10.f, 50.f, 20.f, 100.f, 15.f));
92  TEST_ASSERT_EQUAL(87.0f, MATH_LinearInterpolation(10.f, 50.f, 20.f, 100.f, 17.5f));
93  TEST_ASSERT_EQUAL(50.0f, MATH_LinearInterpolation(10.f, 50.f, 20.f, 100.f, 10.0001f));
94 }
95 
97  TEST_ASSERT_EQUAL(100.0f, MATH_LinearInterpolation(10.f, 50.f, 20.f, 100.f, 20.1f));
98  TEST_ASSERT_EQUAL(-100.0f, MATH_LinearInterpolation(10.f, 50.f, 20.f, 100.f, -20.f));
99  TEST_ASSERT_EQUAL(16739465.0f, MATH_LinearInterpolation(10.f, 50.f, 20.f, 100.f, 3347893.0f));
100 }
101 
103  TEST_ASSERT_EQUAL(0u, MATH_SwapBytesUint16_t(val16));
104 }
105 
107  TEST_ASSERT_EQUAL(0u, MATH_SwapBytesUint32_t(val32));
108 }
109 
111  TEST_ASSERT_EQUAL(0u, MATH_SwapBytesUint64_t(val64));
112 }
113 
114 void test_swap16(void) {
115  val16 = 786u; /* any random value */
116  TEST_ASSERT_EQUAL(4611u, MATH_SwapBytesUint16_t(val16));
117 }
118 
119 void test_swap32(void) {
120  val32 = 0xFFFF0000u; /* any random value */
121  TEST_ASSERT_EQUAL(0x0000FFFFu, MATH_SwapBytesUint32_t(val32));
122 }
123 
124 void test_swap64(void) {
125  val64 = 123u; /* 0x007B */
126  TEST_ASSERT_EQUAL(0x7B00000000000000u, MATH_SwapBytesUint64_t(val64));
127 }
128 
129 void test_swap16_MAX(void) {
130  val16 = UINT16_MAX;
131  TEST_ASSERT_EQUAL(UINT16_MAX, MATH_SwapBytesUint16_t(val16));
132 }
133 
134 void test_swap32_MAX(void) {
135  val32 = UINT32_MAX;
136  TEST_ASSERT_EQUAL(UINT32_MAX, MATH_SwapBytesUint32_t(val32));
137 }
138 
139 void test_swap64_MAX(void) {
140  val64 = UINT64_MAX;
141  TEST_ASSERT_EQUAL(UINT64_MAX, MATH_SwapBytesUint64_t(val64));
142 }
143 
145  float minimum = 0.0f;
146  /* Test 1: all values are equal */
147  minimum = MATH_MinimumOfTwoFloats(1.0f, 1.0f);
148  TEST_ASSERT_EQUAL(1.0f, minimum);
149  /* Test 2: the first value is the smallest */
150  minimum = MATH_MinimumOfTwoFloats(1.0f, 2.0f);
151  TEST_ASSERT_EQUAL(1.0f, minimum);
152  /* Test 3: the last value is the smallest */
153  minimum = MATH_MinimumOfTwoFloats(3.0f, 2.0f);
154  TEST_ASSERT_EQUAL(2.0f, minimum);
155  /* Test 4: the first value is the smallest */
156  minimum = MATH_MinimumOfTwoFloats(-3.0f, 1.0f);
157  TEST_ASSERT_EQUAL(-3.0f, minimum);
158  /* Test 5: the first value is the smallest */
159  minimum = MATH_MinimumOfTwoFloats(-3.0f, -1.0f);
160  TEST_ASSERT_EQUAL(-3.0f, minimum);
161 }
162 
164  uint8_t value1 = 42u;
165  uint8_t value2 = 67u;
166 
167  TEST_ASSERT_EQUAL_UINT8(value1, MATH_MinimumOfTwoUint8_t(value1, value2));
168  TEST_ASSERT_EQUAL_UINT8(value1, MATH_MinimumOfTwoUint8_t(value2, value1));
169  TEST_ASSERT_EQUAL_UINT8(value1, MATH_MinimumOfTwoUint8_t(value1, UINT8_MAX));
170  TEST_ASSERT_EQUAL_UINT8(0u, MATH_MinimumOfTwoUint8_t(value1, 0u));
171  TEST_ASSERT_EQUAL_UINT8(0u, MATH_MinimumOfTwoUint8_t(UINT8_MAX, 0u));
172  TEST_ASSERT_EQUAL_UINT8(0u, MATH_MinimumOfTwoUint8_t(0u, UINT8_MAX));
173 }
174 
176  uint16_t value1 = 0xFF42u;
177  uint16_t value2 = 0xFF67u;
178 
179  TEST_ASSERT_EQUAL_UINT16(value1, MATH_MinimumOfTwoUint16_t(value1, value2));
180  TEST_ASSERT_EQUAL_UINT16(value1, MATH_MinimumOfTwoUint16_t(value2, value1));
181  TEST_ASSERT_EQUAL_UINT16(value1, MATH_MinimumOfTwoUint16_t(value1, UINT16_MAX));
182  TEST_ASSERT_EQUAL_UINT16(0u, MATH_MinimumOfTwoUint16_t(value1, 0u));
183  TEST_ASSERT_EQUAL_UINT16(0u, MATH_MinimumOfTwoUint16_t(UINT16_MAX, 0u));
184  TEST_ASSERT_EQUAL_UINT16(0u, MATH_MinimumOfTwoUint16_t(0u, UINT16_MAX));
185 }
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:84
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
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
float MATH_MinimumOfTwoFloats(const float value1, const float value2)
Returns the minimum of the passed float values.
Definition: foxmath.c:132
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:106
void test_swap16(void)
Definition: test_foxmath.c:114
void test_allZeros_swap64(void)
Definition: test_foxmath.c:110
void test_linearInterpolation_extrapolateFromX1AndX2()
Definition: test_foxmath.c:96
uint32_t val32
Definition: test_foxmath.c:61
float coord_x_interpolate
Definition: test_foxmath.c:67
float coord_x2
Definition: test_foxmath.c:64
void test_minimumOfTwoFloats(void)
Definition: test_foxmath.c:144
void test_linearInterpolation_X1EqualsX2()
Definition: test_foxmath.c:85
void test_swap64_MAX(void)
Definition: test_foxmath.c:139
float coord_x1
Definition: test_foxmath.c:63
void test_linearInterpolation_interpolateBetweenX1AndX2()
Definition: test_foxmath.c:90
void test_MATH_MinimumOfTwoUint16_t(void)
Definition: test_foxmath.c:175
void setUp(void)
Definition: test_foxmath.c:70
float coord_y2
Definition: test_foxmath.c:66
void tearDown(void)
Definition: test_foxmath.c:81
void test_allZeros_swap16(void)
Definition: test_foxmath.c:102
void test_swap32_MAX(void)
Definition: test_foxmath.c:134
void test_swap16_MAX(void)
Definition: test_foxmath.c:129
float coord_y1
Definition: test_foxmath.c:65
uint16_t val16
Definition: test_foxmath.c:60
uint64_t val64
Definition: test_foxmath.c:62
void test_swap64(void)
Definition: test_foxmath.c:124
void test_MATH_MinimumOfTwoUint8_t(void)
Definition: test_foxmath.c:163
void test_swap32(void)
Definition: test_foxmath.c:119