VSF Documented
vsf_test.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Copyright(C)2009-2024 by VSF Team *
3 * *
4 * Licensed under the Apache License, Version 2.0 (the "License"); *
5 * you may not use this file except in compliance with the License. *
6 * You may obtain a copy of the License at *
7 * *
8 * http://www.apache.org/licenses/LICENSE-2.0 *
9 * *
10 * Unless required by applicable law or agreed to in writing, software *
11 * distributed under the License is distributed on an "AS IS" BASIS, *
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 * See the License for the specific language governing permissions and *
14 * limitations under the License. *
15 * *
16 ****************************************************************************/
17
18/*============================ INCLUDES ======================================*/
19
23
24/* example:
25
26 // 0. Include vsf header file
27 #include "vsf.h"
28
29 // 1. Defining vsf_test variable
30 static vsf_test_t __test = {
31 .wdt =
32 {
33 .internal =
34 {
35 // 1.1 If you are using a device that already supports
36 // vsf_hal_wdt, then you can just use vsf_test_hal_wdt_*
37 .init = vsf_test_hal_wdt_init,
38 .feed = vsf_test_hal_wdt_feed,
39 },
40 .external =
41 {
42 // 1.2 If you are using an assist device that can
43 // control the power pin or reset pin of the device,
44 // then you can use vsf_test_stdio_wdt_* to control it
45 .init = vsf_test_stdio_wdt_init,
46 .feed = vsf_test_stdio_wdt_feed,
47 },
48 },
49 .reboot =
50 {
51 // 1.3 Reset can be done using the functions provided by
52 // vsf_arch or the chip's APIs
53 .internal = vsf_arch_reset,
54
55 // 1.4 To control the reset or power pins of the device, we can
56 // use the stdio method to communicate.
57 .external = vsf_test_stdio_reboot,
58 },
59 .data = {
60 // We use stdio to communicate with assist devices for data
61 // persistence. This way we only need to implement the stdio stub
62 // function on the device to come.
63 .init = vsf_test_stdio_data_init,
64 .sync = vsf_test_stdio_data_sync,
65 }};
66 static vsf_test_t *test = &__test;
67
68 // 3. Optionally, call vsf_test_reboot in the callback function for all
69 // exceptions to abort the test and provide additional error messages if
70 // possible information.
71 // Here is an example of HardFault_Handler in Cortex-M:
72 void HardFault_Handler(void)
73 {
74 vsf_test_reboot(test, VSF_TEST_RESULT_FAULT_HANDLER_FAIL, __FILE__,
75 __LINE__, __FUNCTION__, "More Info");
76 }
77
78 // 4. Here are some test examples. including:
79 // - test succeeded
80 // - test failed
81 // - test with watchdog reset
82 // - test with an exception
83
84 // test succeeded
85 static void __test_pass(void)
86 {
87 int a = 100;
88 int b = 50 + 50;
89
90 VSF_TEST_ASSERT(test, a == b);
91 }
92
93 // test failed
94 static void __test_fail(void)
95 {
96 int a = 100;
97 int b = 50 + 50;
98
99 VSF_TEST_ASSERT(test, a != b);
100 }
101
102 // test with watchdog reset
103 static void __test_wdt(void)
104 {
105 while (1);
106 }
107
108 // test with an exception, current only support cortex-m
109 void __test_unalign(void)
110 {
111 #ifdef __CORTEX_M
112 SCB->CCR |= (1 << 3);
113 volatile uint32_t *p = (volatile uint32_t *)0x03;
114 uint32_t value = *p;
115 #else
116 # error "TODO"
117 #endif
118 }
119
120 int main(void)
121 {
122 // 5. We support two styles of adding test cases.
123 // - The first is the static way. We can use macros to initialize test
124 // cases. It is more RAM efficient.
125 // - The second is the dynamic way of adding, which is closer to the
126 // traditional testing framework.
127 #if 1
128 static const vsf_test_case_t __test_cases[] = {
129 VSF_TEST_ADD(__test_pass, "test_pass hw_req=none", 0),
130 VSF_TEST_ADD(__test_fail, "test_fail hw_req=none", 0),
131 VSF_TEST_ADD(__test_wdt, "test_wdt hw_req=none", 0),
132 VSF_TEST_ADD(__test_unalign, "test_invalid_address hw_req=none", 0),
133 };
134 vsf_test_init(test, (vsf_test_case_t *)__test_cases,
135 dimof(__test_cases));
136 #else static vsf_test_case_t __test_cases[10];
137 vsf_test_init(test, __test_cases, dimof(__test_cases));
138 vsf_test_add(test, __test_pass, "test_pass hw_req=none");
139 vsf_test_add(test, __test_fail, "test_fail hw_req=none");
140 vsf_test_add(test, __test_wdt, "test_wdt hw_req=none");
141 vsf_test_add(test, __test_unalign, "test_invalid_address hw_req=none");
142 #endif
143
144 // 6. Here the test will start running
145 vsf_test_run_tests(test);
146
147 return 0;
148 }
149*/
150
151#ifndef __VSF_TEST_H__
152# define __VSF_TEST_H__
153
154# if VSF_USE_TEST == ENABLED
155
156/*============================ MACROS ========================================*/
157
159# ifndef VSF_TEST_CFG_LONGJMP
160# define VSF_TEST_CFG_LONGJMP ENABLED
161# endif
162
164# ifndef VSF_TEST_CFG_INTERNAL_TIMEOUT_MS
165# define VSF_TEST_CFG_INTERNAL_TIMEOUT_MS 1000
166# endif
167
169# ifndef VSF_TEST_CFG_EXTERNAL_TIMEOUT_MS
170# define VSF_TEST_CFG_EXTERNAL_TIMEOUT_MS 1500
171# endif
172
174# ifndef VSF_TEST_CFG_USE_HAL_WDT
175# define VSF_TEST_CFG_USE_HAL_WDT DISABLED
176# endif
177
179# ifndef VSF_TEST_CFG_USE_STDIO_DATA_SYNC
180# define VSF_TEST_CFG_USE_STDIO_DATA_SYNC DISABLED
181# endif
182
189# define VSF_TEST_ASSERT(__t, __v) \
190 do { \
191 if (!(__v)) { \
192 __vsf_test_longjmp(__t, VSF_TEST_RESULT_FAIL, __FILE__, \
193 __LINE__, __FUNCTION__, #__v); \
194 } \
195 } while (0)
196
206# define VSF_TEST_ADD_EX(__FN, __CFG, __TYPE, ...) \
207 {.jmp_fn = __FN, .cfg_str = __CFG, .type = __TYPE, __VA_ARGS__}
208
217# define VSF_TEST_ADD_BOOL_FN(__FN, __CFG, ...) \
218 {.jmp_fn = __FN, \
219 .cfg_str = __CFG, \
220 .type = VSF_TEST_TYPE_BOOL_FN, \
221 __VA_ARGS__}
222
231# define VSF_TEST_ADD(__FN, __CFG, ...) \
232 {.jmp_fn = __FN, \
233 .cfg_str = __CFG, \
234 .type = VSF_TEST_TYPE_LONGJMP_FN, \
235 __VA_ARGS__}
236
237/*============================ TYPES =========================================*/
238
239typedef enum vsf_test_status_t {
243
244typedef enum vsf_test_req_t {
248
249typedef enum vsf_test_result_t {
259
261typedef enum vsf_test_type_t {
269
270typedef void vsf_test_reboot_t(void);
271
279 void (*feed)(vsf_test_wdt_t *wdt);
284};
285
292
297
300
304
310
326
330
335
338
348
354
357 struct {
358 const char *function_name;
359 const char *file_name;
360 const char *condition;
363};
364
365typedef bool vsf_test_bool_fn_t(void);
366typedef void vsf_test_jmp_fn_t(void);
367
368typedef struct vsf_test_case_t {
369 union {
374
379 };
380 char *cfg_str; // json or other configuration format
381
387
392
393typedef struct vsf_test_t {
398 struct {
402
407
414 struct {
421
425
426# if VSF_TEST_CFG_LONGJMP == ENABLED
428# endif
429
431 struct {
437
438/*============================ INCLUDES ======================================*/
439/*============================ PROTOTYPES ====================================*/
440
447extern void vsf_test_init(vsf_test_t *test, vsf_test_case_t *tc_array,
448 uint32_t array_size);
449
456extern bool vsf_test_add_ex(vsf_test_t *test, vsf_test_case_t *test_case);
457
465extern bool vsf_test_add(vsf_test_t *test, vsf_test_jmp_fn_t *jmp_fn,
466 char *cfg);
467
475extern bool vsf_test_add_bool_fn(vsf_test_t *test, vsf_test_bool_fn_t *b_fn,
476 char *cfg);
477
483extern void vsf_test_run_tests(vsf_test_t *test);
484
497extern void __vsf_test_longjmp(vsf_test_t *test, vsf_test_result_t result,
498 const char *file_name, uint32_t line,
499 const char *function_name,
500 const char *condition);
501
512extern void vsf_test_reboot(vsf_test_t *test, vsf_test_result_t result,
513 const char *file_name, uint32_t line,
514 const char *function_name,
515 const char *additional_str);
516
517/*============================ LOCAL VARIABLES ===============================*/
518/*============================ GLOBAL VARIABLES ==============================*/
519
520/*============================ INCLUDES ======================================*/
521
524
525# endif
526#endif
527/* EOF */
struct ieee80211_ext_chansw_ie data
Definition ieee80211.h:80
unsigned uint32_t
Definition stdint.h:9
unsigned char uint8_t
Definition stdint.h:5
Definition vsf_test.h:368
vsf_test_bool_fn_t * b_fn
Definition vsf_test.h:373
uint8_t expect_wdt
Definition vsf_test.h:390
vsf_test_jmp_fn_t * jmp_fn
Definition vsf_test.h:378
uint8_t type
Definition vsf_test.h:386
char * cfg_str
Definition vsf_test.h:380
Definition vsf_test.h:315
char * request_str
Definition vsf_test.h:342
const char * file_name
Definition vsf_test.h:359
const char * function_name
Definition vsf_test.h:358
void(* sync)(vsf_test_data_t *data, vsf_test_data_cmd_t cmd)
Definition vsf_test.h:325
uint32_t line
Definition vsf_test.h:361
struct vsf_test_data_t::@106 error
void(* init)(vsf_test_data_t *data)
Definition vsf_test.h:319
uint32_t req_continue
Definition vsf_test.h:347
uint32_t status
Test status, vsf_test_status_t.
Definition vsf_test.h:337
uint32_t idx
Definition vsf_test.h:334
const char * condition
Definition vsf_test.h:360
uint32_t result
Definition vsf_test.h:353
Definition vsf_test.h:393
vsf_test_wdt_t external
Definition vsf_test.h:405
uint32_t offset
Definition vsf_test.h:433
vsf_test_data_t data
Definition vsf_test.h:424
vsf_test_reboot_t * internal
Use the chip's internal reset, possibly a hot reset.
Definition vsf_test.h:416
struct vsf_test_t::@110 reboot
struct vsf_test_t::@111 test_case
Information for each test.
struct vsf_test_t::@109 wdt
jmp_buf * jmp_buf
Definition vsf_test.h:427
uint32_t size
Definition vsf_test.h:432
vsf_test_reboot_t * external
Definition vsf_test.h:419
vsf_test_wdt_t internal
Definition vsf_test.h:401
vsf_test_case_t * array
Definition vsf_test.h:434
Definition vsf_test.h:273
uint32_t timeout_ms
Definition vsf_test.h:283
void(* feed)(vsf_test_wdt_t *wdt)
The feed function will be called once after each test is completed.
Definition vsf_test.h:279
void(* init)(vsf_test_wdt_t *wdt, uint32_t timeout_ms)
Definition vsf_test.h:277
void vsf_test_reboot_t(void)
Definition vsf_test.h:270
vsf_test_req_t
Definition vsf_test.h:244
@ VSF_TEST_REQ_NO_SUPPORT
Definition vsf_test.h:245
@ VSF_TEST_REQ_SUPPORT
Definition vsf_test.h:246
void vsf_test_reboot(vsf_test_t *test, vsf_test_result_t result, const char *file_name, uint32_t line, const char *function_name, const char *additional_str)
reboot, usually called inside an exception.
Definition vsf_test.c:108
void vsf_test_run_tests(vsf_test_t *test)
Run all tests. Should be called after all use cases have been initialized.
Definition vsf_test.c:138
void __vsf_test_longjmp(vsf_test_t *test, vsf_test_result_t result, const char *file_name, uint32_t line, const char *function_name, const char *condition)
rong jump. the user does not need to directly call this API
Definition vsf_test.c:92
vsf_test_data_cmd_t
Device commands during data synchronization.
Definition vsf_test.h:287
@ VSF_TEST_TESECASE_REQUEST_WRITE
Send test request information, requires an assist device response.
Definition vsf_test.h:299
@ VSF_TEST_TESTCASE_INDEX_READ
Reading the current index requires a response from the assist device.
Definition vsf_test.h:294
@ VSF_TEST_DONE
Definition vsf_test.h:308
@ VSF_TEST_TESTCASE_RESULT_WRITE
Definition vsf_test.h:303
@ VSF_TEST_STATUS_READ
Reading the current state requires a response from the assist device.
Definition vsf_test.h:289
@ VSF_TEST_STATUS_WRITE
Write current state, no assist device response required.
Definition vsf_test.h:291
@ VSF_TEST_TESTCASE_INDEX_WRITE
Write current index, no assist device response required.
Definition vsf_test.h:296
bool vsf_test_add(vsf_test_t *test, vsf_test_jmp_fn_t *jmp_fn, char *cfg)
Add to add a test case of VSF_TEST_TYPE_LONGJMP_FN type.
Definition vsf_test.c:64
void vsf_test_jmp_fn_t(void)
Definition vsf_test.h:366
vsf_test_result_t
Definition vsf_test.h:249
@ VSF_TEST_RESULT_ASSIST_FAIL
Definition vsf_test.h:256
@ VSF_TEST_RESULT_PASS
Definition vsf_test.h:250
@ VSF_TEST_RESULT_FAIL
Definition vsf_test.h:253
@ VSF_TEST_RESULT_ASSERT_FAIL
Definition vsf_test.h:254
@ VSF_TEST_RESULT_SKIP
Definition vsf_test.h:251
@ VSF_TEST_RESULT_WDT_FAIL
Definition vsf_test.h:255
@ VSF_TEST_RESULT_FAULT_HANDLER_FAIL
Definition vsf_test.h:257
@ VSF_TEST_RESULT_WDT_PASS
Definition vsf_test.h:252
bool vsf_test_bool_fn_t(void)
Definition vsf_test.h:365
bool vsf_test_add_ex(vsf_test_t *test, vsf_test_case_t *test_case)
Add to add a test case of any type.
Definition vsf_test.c:50
vsf_test_status_t
Definition vsf_test.h:239
@ VSF_TEST_STATUS_IDLE
Definition vsf_test.h:240
@ VSF_TEST_STATUS_RUNNING
Definition vsf_test.h:241
void vsf_test_init(vsf_test_t *test, vsf_test_case_t *tc_array, uint32_t array_size)
initialize vsf test
Definition vsf_test.c:32
bool vsf_test_add_bool_fn(vsf_test_t *test, vsf_test_bool_fn_t *b_fn, char *cfg)
Add to add a test case of VSF_TEST_TYPE_BOOL_FN type.
Definition vsf_test.c:79
vsf_test_type_t
Test the type of the function,.
Definition vsf_test.h:261
@ VSF_TEST_TYPE_LONGJMP_FN
Definition vsf_test.h:264
@ VSF_TEST_TYPE_BOOL_FN
Definition vsf_test.h:267