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