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
20#ifndef __VSF_TEST_H__
21# define __VSF_TEST_H__
22
26
27/* example:
28
29 // 0. Include vsf header file
30 #include "vsf.h"
31
32 // 1. Defining vsf_test variable
33 static vsf_test_t __test = {
34 .wdt =
35 {
36 .internal =
37 {
38 // 1.1 If you are using a device that already supports
39 // vsf_hal_wdt, then you can just use vsf_test_hal_wdt_*
40 .init = vsf_test_hal_wdt_init,
41 .feed = vsf_test_hal_wdt_feed,
42 },
43 .external =
44 {
45 // 1.2 If you are using an assist device that can
46 // control the power pin or reset pin of the device,
47 // then you can use vsf_test_stdio_wdt_* to control it
48 .init = vsf_test_stdio_wdt_init,
49 .feed = vsf_test_stdio_wdt_feed,
50 },
51 },
52 .reboot =
53 {
54 // 1.3 Reset can be done using the functions provided by
55 // vsf_arch or the chip's APIs
56 .internal = vsf_arch_reset,
57
58 // 1.4 To control the reset or power pins of the device, we can
59 // use the stdio method to communicate.
60 .external = vsf_test_stdio_reboot,
61 },
62 .data = {
63 // We use stdio to communicate with assist devices for data
64 // persistence. This way we only need to implement the stdio stub
65 // function on the device to come.
66 .init = vsf_test_stdio_data_init,
67 .sync = vsf_test_stdio_data_sync,
68 }};
69 static vsf_test_t *test = &__test;
70
71 // 3. Optionally, call vsf_test_reboot in the callback function for all
72 // exceptions to abort the test and provide additional error messages if
73 // possible information.
74 // Here is an example of HardFault_Handler in Cortex-M:
75 void HardFault_Handler(void)
76 {
77 vsf_test_reboot(VSF_TEST_RESULT_FAULT_HANDLER_FAIL, __FILE__,
78 __LINE__, __FUNCTION__, "More Info");
79 }
80
81 // 4. Here are some test examples. including:
82 // - test succeeded
83 // - test failed
84 // - test with watchdog reset
85 // - test with an exception
86
87 // test succeeded
88 static void __test_pass(void)
89 {
90 int a = 100;
91 int b = 50 + 50;
92
93 VSF_TEST_ASSERT(a == b);
94 }
95
96 // test failed
97 static void __test_fail(void)
98 {
99 int a = 100;
100 int b = 50 + 50;
101
102 VSF_TEST_ASSERT(a != b);
103 }
104
105 // test with watchdog reset
106 static void __test_wdt(void)
107 {
108 while (1);
109 }
110
111 // test with an exception, current only support cortex-m
112 void __test_unalign(void)
113 {
114 #ifdef __CORTEX_M
115 SCB->CCR |= (1 << 3);
116 volatile uint32_t *p = (volatile uint32_t *)0x03;
117 uint32_t value = *p;
118 #else
119 # error "TODO"
120 #endif
121 }
122
123 int main(void)
124 {
125 // 5. Configure and initialize the test framework
126 vsf_test_cfg_t test_cfg = {
127 .wdt = {
128 .internal = {
129 .init = vsf_test_hal_wdt_init,
130 .feed = vsf_test_hal_wdt_feed,
131 },
132 },
133 .reboot = {
134 .internal = vsf_arch_reset,
135 },
136 .data = {
137 .init = vsf_test_stdio_data_init,
138 .sync = vsf_test_stdio_data_sync,
139 },
140 .restart_on_done = false, // Set to true to restart when test completes or errors occur
141 };
142 vsf_test_init(&test_cfg);
143
144 // 6. We support two styles of adding test cases.
145 // - The first is the static way. We can use macros to initialize test
146 // cases. It is more RAM efficient.
147 // - The second is the dynamic way of adding, which is closer to the
148 // traditional testing framework.
149 #if 1
150 vsf_test_add(__test_pass, "test_pass hw_req=none");
151 vsf_test_add(__test_fail, "test_fail hw_req=none");
152 vsf_test_add(__test_wdt, "test_wdt hw_req=none");
153 vsf_test_add(__test_unalign, "test_invalid_address hw_req=none");
154 #endif
155
156 // 7. Here the test will start running
157 vsf_test_run_tests();
158
159 return 0;
160 }
161*/
162
163
164
165# if VSF_USE_TEST == ENABLED
166
167/*============================ MACROS ========================================*/
168
170# ifndef VSF_TEST_CFG_LONGJMP
171# define VSF_TEST_CFG_LONGJMP ENABLED
172# endif
173
175# ifndef VSF_TEST_CFG_INTERNAL_TIMEOUT_MS
176# define VSF_TEST_CFG_INTERNAL_TIMEOUT_MS 1000
177# endif
178
180# ifndef VSF_TEST_CFG_EXTERNAL_TIMEOUT_MS
181# define VSF_TEST_CFG_EXTERNAL_TIMEOUT_MS 1500
182# endif
183
185# ifndef VSF_TEST_CFG_USE_HAL_WDT
186# define VSF_TEST_CFG_USE_HAL_WDT DISABLED
187# endif
188
190# ifndef VSF_TEST_CFG_USE_STDIO_DATA_SYNC
191# define VSF_TEST_CFG_USE_STDIO_DATA_SYNC DISABLED
192# endif
193
195# ifndef VSF_TEST_CFG_USE_FILE_DATA_SYNC
196# define VSF_TEST_CFG_USE_FILE_DATA_SYNC DISABLED
197# endif
198
200# ifndef VSF_TEST_CFG_USE_APPCFG_DATA_SYNC
201# define VSF_TEST_CFG_USE_APPCFG_DATA_SYNC DISABLED
202# endif
203
205# ifndef VSF_TEST_CFG_USE_TRACE
206# define VSF_TEST_CFG_USE_TRACE ENABLED
207# endif
208
210# ifndef VSF_TEST_CFG_ARRAY_SIZE
211# define VSF_TEST_CFG_ARRAY_SIZE 100
212# endif
213
219# define VSF_TEST_ASSERT(__v) \
220 do { \
221 if (!(__v)) { \
222 __vsf_test_longjmp(VSF_TEST_RESULT_FAIL, __FILE__, \
223 __LINE__, __FUNCTION__, #__v); \
224 } \
225 } while (0)
226
227/*============================ TYPES =========================================*/
228
229typedef enum vsf_test_status_t {
233
234typedef enum vsf_test_req_t {
238
239typedef enum vsf_test_result_t {
249
251typedef enum vsf_test_type_t {
259
260typedef void vsf_test_reboot_t(void);
261
269 void (*feed)(vsf_test_wdt_t *wdt);
274};
275
282
287
290
294
300
316
320
325
328
338
344
347 struct {
348 const char *function_name;
349 const char *file_name;
350 const char *condition;
353};
354
355typedef bool vsf_test_bool_fn_t(void);
356typedef void vsf_test_jmp_fn_t(void);
357
358typedef struct vsf_test_case_t {
359 union {
364
369 };
370 char *cfg_str; // json or other configuration format
371
377
381
387
389typedef struct vsf_test_cfg_t {
391 struct {
397
399 struct {
405
407 struct {
413
417
418typedef struct vsf_test_t {
423 struct {
427
432
439 struct {
446
450
451# if VSF_TEST_CFG_LONGJMP == ENABLED
453# endif
454
457
463
464/*============================ INCLUDES ======================================*/
465/*============================ PROTOTYPES ====================================*/
466
471extern void vsf_test_init(const vsf_test_cfg_t *cfg);
472
473/*============================ API USAGE GUIDE =============================*/
519extern bool vsf_test_add_ex(vsf_test_case_t *test_case);
520
527extern bool vsf_test_add_simple_case(vsf_test_jmp_fn_t *jmp_fn, char *cfg);
528
535extern bool vsf_test_add_bool_fn(vsf_test_bool_fn_t *b_fn, char *cfg);
536
544extern bool vsf_test_add_case(vsf_test_jmp_fn_t *fn, char *cfg, uint8_t expect_wdt);
545
553extern bool vsf_test_add_bool_fn_case(vsf_test_bool_fn_t *fn, char *cfg, uint8_t expect_wdt);
554
564extern bool vsf_test_add_ex_case(vsf_test_jmp_fn_t *fn, char *cfg,
566 uint8_t expect_wdt,
567 uint8_t expect_assert);
568
577 char *cfg,
578 uint8_t expect_wdt);
579
584extern void vsf_test_run_tests(void);
585
597extern void __vsf_test_longjmp(vsf_test_result_t result,
598 const char *file_name, uint32_t line,
599 const char *function_name,
600 const char *condition);
601
611extern void vsf_test_reboot(vsf_test_result_t result,
612 const char *file_name, uint32_t line,
613 const char *function_name,
614 const char *additional_str);
615
616/*============================ LOCAL VARIABLES ===============================*/
617/*============================ GLOBAL VARIABLES ==============================*/
618
619/*============================ INCLUDES ======================================*/
620
625
626# endif
627#endif
628/* 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:358
vsf_test_bool_fn_t * b_fn
Definition vsf_test.h:363
uint8_t expect_wdt
Definition vsf_test.h:380
uint8_t expect_assert
Definition vsf_test.h:385
vsf_test_jmp_fn_t * jmp_fn
Definition vsf_test.h:368
uint8_t type
Definition vsf_test.h:376
char * cfg_str
Definition vsf_test.h:370
Test framework configuration structure.
Definition vsf_test.h:389
struct vsf_test_cfg_t::@110 reboot
Reboot configuration.
vsf_test_wdt_t external
External watchdog configuration.
Definition vsf_test.h:395
bool restart_on_done
Restart from the beginning when test completes or errors occur.
Definition vsf_test.h:415
void(* init)(vsf_test_data_t *data)
Data initialization function.
Definition vsf_test.h:409
struct vsf_test_cfg_t::@109 wdt
Watchdog configuration.
void(* sync)(vsf_test_data_t *data, vsf_test_data_cmd_t cmd)
Data synchronization function.
Definition vsf_test.h:411
vsf_test_reboot_t * external
External reboot function (via reset pin or power pin)
Definition vsf_test.h:403
struct vsf_test_cfg_t::@111 data
Data persistence configuration.
vsf_test_wdt_t internal
Internal watchdog configuration.
Definition vsf_test.h:393
vsf_test_reboot_t * internal
Internal reboot function (chip's internal reset)
Definition vsf_test.h:401
Definition vsf_test.h:305
char * request_str
Definition vsf_test.h:332
const char * file_name
Definition vsf_test.h:349
const char * function_name
Definition vsf_test.h:348
void(* sync)(vsf_test_data_t *data, vsf_test_data_cmd_t cmd)
Definition vsf_test.h:315
uint32_t line
Definition vsf_test.h:351
struct vsf_test_data_t::@106 error
void(* init)(vsf_test_data_t *data)
Definition vsf_test.h:309
uint32_t req_continue
Definition vsf_test.h:337
uint32_t status
Test status, vsf_test_status_t.
Definition vsf_test.h:327
uint32_t idx
Definition vsf_test.h:324
const char * condition
Definition vsf_test.h:350
uint32_t result
Definition vsf_test.h:343
Definition vsf_test.h:418
struct vsf_test_t::@113 reboot
vsf_test_wdt_t external
Definition vsf_test.h:430
vsf_test_case_t test_case_array[VSF_TEST_CFG_ARRAY_SIZE]
Test case array.
Definition vsf_test.h:461
uint32_t test_case_count
Test case count (number of test cases added)
Definition vsf_test.h:459
vsf_test_data_t data
Definition vsf_test.h:449
vsf_test_reboot_t * internal
Use the chip's internal reset, possibly a hot reset.
Definition vsf_test.h:441
jmp_buf * jmp_buf
Definition vsf_test.h:452
bool restart_on_done
Restart from the beginning when test completes or errors occur.
Definition vsf_test.h:456
vsf_test_reboot_t * external
Definition vsf_test.h:444
struct vsf_test_t::@112 wdt
vsf_test_wdt_t internal
Definition vsf_test.h:426
Definition vsf_test.h:263
uint32_t timeout_ms
Definition vsf_test.h:273
void(* feed)(vsf_test_wdt_t *wdt)
The feed function will be called once after each test is completed.
Definition vsf_test.h:269
void(* init)(vsf_test_wdt_t *wdt, uint32_t timeout_ms)
Definition vsf_test.h:267
vk_av_control_type_t type
Definition vsf_audio.h:170
bool vsf_test_add_bool_fn(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:112
void vsf_test_reboot_t(void)
Definition vsf_test.h:260
bool vsf_test_add_expect_assert_case(vsf_test_jmp_fn_t *fn, char *cfg, uint8_t expect_wdt)
Add a test case that expects an assertion.
Definition vsf_test.c:166
bool vsf_test_add_case(vsf_test_jmp_fn_t *fn, char *cfg, uint8_t expect_wdt)
Add a test case of VSF_TEST_TYPE_LONGJMP_FN type.
Definition vsf_test.c:127
void __vsf_test_longjmp(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:180
vsf_test_req_t
Definition vsf_test.h:234
@ VSF_TEST_REQ_NO_SUPPORT
Definition vsf_test.h:235
@ VSF_TEST_REQ_SUPPORT
Definition vsf_test.h:236
bool vsf_test_add_simple_case(vsf_test_jmp_fn_t *jmp_fn, char *cfg)
Add a test case of VSF_TEST_TYPE_LONGJMP_FN type (simplified, expect_wdt=0)
Definition vsf_test.c:107
vsf_test_data_cmd_t
Device commands during data synchronization.
Definition vsf_test.h:277
@ VSF_TEST_TESECASE_REQUEST_WRITE
Send test request information, requires an assist device response.
Definition vsf_test.h:289
@ VSF_TEST_TESTCASE_INDEX_READ
Reading the current index requires a response from the assist device.
Definition vsf_test.h:284
@ VSF_TEST_DONE
Definition vsf_test.h:298
@ VSF_TEST_TESTCASE_RESULT_WRITE
Definition vsf_test.h:293
@ VSF_TEST_STATUS_READ
Reading the current state requires a response from the assist device.
Definition vsf_test.h:279
@ VSF_TEST_STATUS_WRITE
Write current state, no assist device response required.
Definition vsf_test.h:281
@ VSF_TEST_TESTCASE_INDEX_WRITE
Write current index, no assist device response required.
Definition vsf_test.h:286
void vsf_test_reboot(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:226
void vsf_test_jmp_fn_t(void)
Definition vsf_test.h:356
bool vsf_test_add_ex(vsf_test_case_t *test_case)
API Selection Guide.
Definition vsf_test.c:91
bool vsf_test_add_ex_case(vsf_test_jmp_fn_t *fn, char *cfg, vsf_test_type_t type, uint8_t expect_wdt, uint8_t expect_assert)
Add a test case of any type.
Definition vsf_test.c:151
vsf_test_result_t
Definition vsf_test.h:239
@ VSF_TEST_RESULT_ASSIST_FAIL
Definition vsf_test.h:246
@ VSF_TEST_RESULT_PASS
Definition vsf_test.h:240
@ VSF_TEST_RESULT_FAIL
Definition vsf_test.h:243
@ VSF_TEST_RESULT_ASSERT_FAIL
Definition vsf_test.h:244
@ VSF_TEST_RESULT_SKIP
Definition vsf_test.h:241
@ VSF_TEST_RESULT_WDT_FAIL
Definition vsf_test.h:245
@ VSF_TEST_RESULT_FAULT_HANDLER_FAIL
Definition vsf_test.h:247
@ VSF_TEST_RESULT_WDT_PASS
Definition vsf_test.h:242
bool vsf_test_bool_fn_t(void)
Definition vsf_test.h:355
void vsf_test_init(const vsf_test_cfg_t *cfg)
initialize vsf test
Definition vsf_test.c:61
vsf_test_status_t
Definition vsf_test.h:229
@ VSF_TEST_STATUS_IDLE
Definition vsf_test.h:230
@ VSF_TEST_STATUS_RUNNING
Definition vsf_test.h:231
bool vsf_test_add_bool_fn_case(vsf_test_bool_fn_t *fn, char *cfg, uint8_t expect_wdt)
Add a test case of VSF_TEST_TYPE_BOOL_FN type.
Definition vsf_test.c:139
void vsf_test_run_tests(void)
Run all tests. Should be called after all use cases have been initialized.
Definition vsf_test.c:259
#define VSF_TEST_CFG_ARRAY_SIZE
Definition vsf_test.h:211
vsf_test_type_t
Test the type of the function,.
Definition vsf_test.h:251
@ VSF_TEST_TYPE_LONGJMP_FN
Definition vsf_test.h:254
@ VSF_TEST_TYPE_BOOL_FN
Definition vsf_test.h:257
Generated from commit: vsfteam/vsf@b2e9e8a