foxBMS - Unit Tests  1.4.1
The foxBMS Unit Tests API Documentation
test_os.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_os.c
44  * @author foxBMS Team
45  * @date 2020-03-13 (date of creation)
46  * @updated 2022-10-27 (date of last update)
47  * @version v1.4.1
48  * @ingroup UNIT_TEST_IMPLEMENTATION
49  * @prefix OS
50  *
51  * @brief Test of the os.c module
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "unity.h"
57 #include "Mockftask.h"
58 #include "Mockftask_cfg.h"
59 #include "Mockportmacro.h"
60 #include "Mockqueue.h"
61 #include "Mocktask.h"
62 
63 #include "os.h"
64 #include "test_assert_helper.h"
65 
66 TEST_FILE("os.c")
67 TEST_FILE("os_freertos.c")
68 
69 /*========== Definitions and Implementations for Unit Test ==================*/
71 
72 /*========== Setup and Teardown =============================================*/
73 void setUp(void) {
75 }
76 
77 void tearDown(void) {
78 }
79 
80 /*========== Test Cases =====================================================*/
81 
83  FTSK_CreateQueues_Expect();
84  FTSK_CreateTasks_Expect();
85 
87 
88  TEST_ASSERT_EQUAL_INT8(OS_INIT_PRE_OS, os_boot);
89 }
90 
92  OS_TIMER_s testTimerExpected = {0u, 0u, 0u, 0u, 0u, 0u, 0u};
93 
95  /* Set expected value */
96  testTimerExpected.timer_1ms = 1u;
97  TEST_ASSERT_EQUAL_MEMORY(&testTimerExpected, test_timer, sizeof(OS_TIMER_s));
98 
99  /* Call trigger function 9 more times -> 9ms + 1ms have passed: 10ms in total */
100  for (uint8_t i = 0; i < 9; i++) {
102  }
103  /* Set expected value */
104  testTimerExpected.timer_1ms = 0u;
105  testTimerExpected.timer_10ms = 1u;
106  TEST_ASSERT_EQUAL_MEMORY(&testTimerExpected, test_timer, sizeof(OS_TIMER_s));
107 
108  /* Set timer to 99ms and call trigger function one more time -> 100ms passed */
109  test_timer->timer_1ms = 9u;
110  test_timer->timer_10ms = 9u;
112  /* Set extpected value */
113  testTimerExpected.timer_1ms = 0u;
114  testTimerExpected.timer_10ms = 0u;
115  testTimerExpected.timer_100ms = 1u;
116  TEST_ASSERT_EQUAL_MEMORY(&testTimerExpected, test_timer, sizeof(OS_TIMER_s));
117 
118  /* Set timer to 999ms and call trigger function one more time -> 1s passed */
119  test_timer->timer_1ms = 9u;
120  test_timer->timer_10ms = 9u;
121  test_timer->timer_100ms = 9u;
122  test_timer->timer_sec = 0u;
123  test_timer->timer_min = 0u;
124  test_timer->timer_h = 0u;
125  test_timer->timer_d = 0u;
127  /* Set extpected value */
128  testTimerExpected.timer_1ms = 0u;
129  testTimerExpected.timer_10ms = 0u;
130  testTimerExpected.timer_100ms = 0u;
131  testTimerExpected.timer_sec = 1u;
132  testTimerExpected.timer_min = 0u;
133  testTimerExpected.timer_h = 0u;
134  testTimerExpected.timer_d = 0u;
135  TEST_ASSERT_EQUAL_MEMORY(&testTimerExpected, test_timer, sizeof(OS_TIMER_s));
136 
137  /* Set timer to 59.999s and call trigger function one more time -> 1min passed */
138  test_timer->timer_1ms = 9u;
139  test_timer->timer_10ms = 9u;
140  test_timer->timer_100ms = 9u;
141  test_timer->timer_sec = 59u;
142  test_timer->timer_min = 0u;
143  test_timer->timer_h = 0u;
144  test_timer->timer_d = 0u;
146  /* Set extpected value */
147  testTimerExpected.timer_1ms = 0u;
148  testTimerExpected.timer_10ms = 0u;
149  testTimerExpected.timer_100ms = 0u;
150  testTimerExpected.timer_sec = 0u;
151  testTimerExpected.timer_min = 1u;
152  testTimerExpected.timer_h = 0u;
153  testTimerExpected.timer_d = 0u;
154  TEST_ASSERT_EQUAL_MEMORY(&testTimerExpected, test_timer, sizeof(OS_TIMER_s));
155 
156  /* Set timer to 59min 59.999s and call trigger function one more time -> 1h passed */
157  test_timer->timer_1ms = 9u;
158  test_timer->timer_10ms = 9u;
159  test_timer->timer_100ms = 9u;
160  test_timer->timer_sec = 59u;
161  test_timer->timer_min = 59u;
162  test_timer->timer_h = 0u;
163  test_timer->timer_d = 0u;
165  /* Set extpected value */
166  testTimerExpected.timer_1ms = 0u;
167  testTimerExpected.timer_10ms = 0u;
168  testTimerExpected.timer_100ms = 0u;
169  testTimerExpected.timer_sec = 0u;
170  testTimerExpected.timer_min = 0u;
171  testTimerExpected.timer_h = 1u;
172  testTimerExpected.timer_d = 0u;
173  TEST_ASSERT_EQUAL_MEMORY(&testTimerExpected, test_timer, sizeof(OS_TIMER_s));
174 
175  /* Set timer to 59h 59min 59.999s and call trigger function one more time -> 1d passed */
176  test_timer->timer_1ms = 9u;
177  test_timer->timer_10ms = 9u;
178  test_timer->timer_100ms = 9u;
179  test_timer->timer_sec = 59u;
180  test_timer->timer_min = 59u;
181  test_timer->timer_h = 23u;
182  test_timer->timer_d = 0u;
184  /* Set extpected value */
185  testTimerExpected.timer_1ms = 0u;
186  testTimerExpected.timer_10ms = 0u;
187  testTimerExpected.timer_100ms = 0u;
188  testTimerExpected.timer_sec = 0u;
189  testTimerExpected.timer_min = 0u;
190  testTimerExpected.timer_h = 0u;
191  testTimerExpected.timer_d = 1u;
192  TEST_ASSERT_EQUAL_MEMORY(&testTimerExpected, test_timer, sizeof(OS_TIMER_s));
193 }
194 
196  /* checks whether the overflow of the timer is sanely handled. */
197  OS_TIMER_s testTimerExpected = {0u, 0u, 0u, 0u, 0u, 0u, 0u};
198 
199  /* Set timer to last tick before overflow and call trigger function one more time */
200  test_timer->timer_1ms = 9u;
201  test_timer->timer_10ms = 9u;
202  test_timer->timer_100ms = 9u;
203  test_timer->timer_sec = 59u;
204  test_timer->timer_min = 59u;
205  test_timer->timer_h = 23u;
206  test_timer->timer_d = UINT16_MAX;
208  /* Set extpected value */
209  testTimerExpected.timer_1ms = 0u;
210  testTimerExpected.timer_10ms = 0u;
211  testTimerExpected.timer_100ms = 0u;
212  testTimerExpected.timer_sec = 0u;
213  testTimerExpected.timer_min = 0u;
214  testTimerExpected.timer_h = 0u;
215  testTimerExpected.timer_d = 0u;
216  TEST_ASSERT_EQUAL_MEMORY(&testTimerExpected, test_timer, sizeof(OS_TIMER_s));
217 }
218 
219 /** when no time shall pass, the result of #OS_CheckTimeHasPassed() will always be true, independent of the time */
221  xTaskGetTickCount_ExpectAndReturn(0u);
222  TEST_ASSERT_EQUAL(true, OS_CheckTimeHasPassed(0u, 0u));
223 
224  xTaskGetTickCount_ExpectAndReturn(100u);
225  TEST_ASSERT_EQUAL(true, OS_CheckTimeHasPassed(0u, 0u));
226 
227  xTaskGetTickCount_ExpectAndReturn(UINT32_MAX);
228  TEST_ASSERT_EQUAL(true, OS_CheckTimeHasPassed(0u, 0u));
229 
230  xTaskGetTickCount_ExpectAndReturn(0u);
231  TEST_ASSERT_EQUAL(true, OS_CheckTimeHasPassed(0u, 0u));
232 
233  xTaskGetTickCount_ExpectAndReturn(0u);
234  TEST_ASSERT_EQUAL(true, OS_CheckTimeHasPassed(100u, 0u));
235 
236  xTaskGetTickCount_ExpectAndReturn(0u);
237  TEST_ASSERT_EQUAL(true, OS_CheckTimeHasPassed(UINT32_MAX, 0u));
238 }
239 
240 /** check for when 1ms shall pass with #OS_CheckTimeHasPassed() */
242  xTaskGetTickCount_ExpectAndReturn(0u);
243  TEST_ASSERT_EQUAL(false, OS_CheckTimeHasPassed(0u, 1u));
244 
245  xTaskGetTickCount_ExpectAndReturn(0u);
246  TEST_ASSERT_EQUAL(true, OS_CheckTimeHasPassed(1u, 1u));
247 }
248 
249 /** check behavior before the wraparound of the timestamp for when 1ms shall pass with #OS_CheckTimeHasPassed() */
251  xTaskGetTickCount_ExpectAndReturn(UINT32_MAX);
252  TEST_ASSERT_EQUAL(false, OS_CheckTimeHasPassed(UINT32_MAX, 1u));
253 
254  xTaskGetTickCount_ExpectAndReturn(UINT32_MAX);
255  TEST_ASSERT_EQUAL(true, OS_CheckTimeHasPassed((UINT32_MAX - 1u), 1u));
256 }
257 
258 /** check behavior around the wraparound of the timestamp for when 1ms shall pass with #OS_CheckTimeHasPassed() */
260  xTaskGetTickCount_ExpectAndReturn(0u);
261  TEST_ASSERT_EQUAL(true, OS_CheckTimeHasPassed(UINT32_MAX, 1u));
262 }
263 
264 /** check behavior for the largest time step possible in #OS_CheckTimeHasPassed() */
266  /* edge case: it is to be assumed that here rather no time has passed */
267  xTaskGetTickCount_ExpectAndReturn(0u);
268  TEST_ASSERT_EQUAL(false, OS_CheckTimeHasPassed(0u, UINT32_MAX));
269 
270  xTaskGetTickCount_ExpectAndReturn(1u);
271  TEST_ASSERT_EQUAL(false, OS_CheckTimeHasPassed(0u, UINT32_MAX));
272 
273  xTaskGetTickCount_ExpectAndReturn(UINT32_MAX);
274  TEST_ASSERT_EQUAL(true, OS_CheckTimeHasPassed(0u, UINT32_MAX));
275 }
276 
277 /** check that the selftest passes */
279  TEST_ASSERT_EQUAL(STD_OK, OS_CheckTimeHasPassedSelfTest());
280 }
@ STD_OK
Definition: fstd_types.h:82
bool OS_CheckTimeHasPassed(uint32_t oldTimeStamp_ms, uint32_t timeToPass_ms)
This function checks if timeToPass has passed since the last timestamp to now.
Definition: os.c:144
STD_RETURN_TYPE_e OS_CheckTimeHasPassedSelfTest(void)
Does a self check if the OS_CheckTimeHasPassedWithTimestamp works as expected.
Definition: os.c:148
void OS_InitializeOperatingSystem(void)
Initialization the RTOS interface.
Definition: os.c:79
OS_TIMER_s * TEST_OS_GetOsTimer()
Definition: os.c:185
void OS_IncrementTimer(void)
Increments the system timer os_timer.
Definition: os.c:94
volatile OS_BOOT_STATE_e os_boot
Definition: os.c:69
Declaration of the OS wrapper interface.
@ OS_INIT_PRE_OS
Definition: os.h:105
OS timer.
Definition: os.h:116
uint8_t timer_min
Definition: os.h:121
uint8_t timer_1ms
Definition: os.h:117
uint8_t timer_sec
Definition: os.h:120
uint8_t timer_100ms
Definition: os.h:119
uint8_t timer_10ms
Definition: os.h:118
uint8_t timer_h
Definition: os.h:122
uint16_t timer_d
Definition: os.h:123
Helper for unit tests.
void testOS_CheckTimeHasPassedTimestampAroundMax1ms(void)
Definition: test_os.c:259
void testOS_CheckTimeHasPassedTimestampAtMax1ms(void)
Definition: test_os.c:250
void testOS_CheckTimeHasPassedSelfTestSuccessful(void)
Definition: test_os.c:278
static OS_TIMER_s * test_timer
Definition: test_os.c:70
void testOSTaskInitCallsFTSKFunctions(void)
Definition: test_os.c:82
void testOS_TriggerTimerOverflow(void)
Definition: test_os.c:195
void testOS_CheckTimeHasPassed1ms(void)
Definition: test_os.c:241
void testOS_CheckTimeHasPassedUINT32_MAXms(void)
Definition: test_os.c:265
void setUp(void)
Definition: test_os.c:73
void tearDown(void)
Definition: test_os.c:77
void testOS_IncrementTimer(void)
Definition: test_os.c:91
void testOS_CheckTimeHasPassedNoTime(void)
Definition: test_os.c:220