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_wdt_t __wdt_entries[] = {
34 // 1.1 Internal WDT (chip's own watchdog)
35 {
36 .init = vsf_test_hal_wdt_init,
37 .feed = vsf_test_hal_wdt_feed,
38 },
39 // 1.2 External WDT (assist device controlling power/reset pin)
40 {
41 .init = vsf_test_stdio_wdt_init,
42 .feed = vsf_test_stdio_wdt_feed,
43 },
44 };
45 static vsf_test_reboot_t *__reboot_entries[] = {
46 // 1.3 External reboot first (assist device)
47 vsf_test_stdio_reboot,
48 // 1.4 Internal reboot fallback (chip reset)
49 vsf_arch_reset,
50 };
51 static vsf_test_t __test = {
52 .wdt = {
53 .entries = __wdt_entries,
54 .count = dimof(__wdt_entries),
55 },
56 .reboot = {
57 .entries = __reboot_entries,
58 .count = dimof(__reboot_entries),
59 },
60 };
61 static vsf_test_t *test = &__test;
62
63 // 3. Optionally, call vsf_test_reboot in the callback function for all
64 // exceptions to abort the test and provide additional error messages if
65 // possible information.
66 // Here is an example of HardFault_Handler in Cortex-M:
67 void HardFault_Handler(void)
68 {
69 vsf_test_reboot(VSF_TEST_RESULT_FAULT_HANDLER_FAIL, __FILE__,
70 __LINE__, __FUNCTION__, "More Info");
71 }
72
73 // 4. Here are some test examples. including:
74 // - test succeeded
75 // - test failed
76 // - test with watchdog reset
77 // - test with an exception
78
79 // test succeeded
80 static void __test_pass(void)
81 {
82 int a = 100;
83 int b = 50 + 50;
84
85 VSF_TEST_ASSERT(a == b);
86 }
87
88 // test failed
89 static void __test_fail(void)
90 {
91 int a = 100;
92 int b = 50 + 50;
93
94 VSF_TEST_ASSERT(a != b);
95 }
96
97 // test with watchdog reset
98 static void __test_wdt(void)
99 {
100 while (1);
101 }
102
103 // test with an exception, current only support cortex-m
104 void __test_unalign(void)
105 {
106 #ifdef __CORTEX_M
107 SCB->CCR |= (1 << 3);
108 volatile uint32_t *p = (volatile uint32_t *)0x03;
109 uint32_t value = *p;
110 #else
111 # error "TODO"
112 #endif
113 }
114
115 int main(void)
116 {
117 // 5. Configure and initialize the test framework
118 static vsf_test_wdt_t __test_wdt_entries[] = {
119 {
120 .init = vsf_test_hal_wdt_init,
121 .feed = vsf_test_hal_wdt_feed,
122 },
123 };
124 static vsf_test_reboot_t *__test_reboot_entries[] = {
125 vsf_arch_reset,
126 };
127 __vsf_test.wdt.entries = __test_wdt_entries;
128 __vsf_test.wdt.count = dimof(__test_wdt_entries);
129 __vsf_test.reboot.entries = __test_reboot_entries;
130 __vsf_test.reboot.count = dimof(__test_reboot_entries);
131 __vsf_test.restart_on_done = false; // Set to true to restart when test completes or errors occur
132 vsf_test_run(NULL);
133
134 // 6. We support two styles of adding test cases.
135 // - The first is the static way. We can use macros to initialize test
136 // cases. It is more RAM efficient.
137 // - The second is the dynamic way of adding, which is closer to the
138 // traditional testing framework.
139 #if 1
140 vsf_test_add(__test_pass, "test_pass hw_req=none");
141 vsf_test_add(__test_fail, "test_fail hw_req=none");
142 vsf_test_add(__test_wdt, "test_wdt hw_req=none");
143 vsf_test_add(__test_unalign, "test_invalid_address hw_req=none");
144 #endif
145
146 // 7. vsf_test_run runs all tests and optionally starts the shell REPL.
147 // No explicit vsf_test_run_tests call is needed.
148
149 return 0;
150 }
151*/
152
153/*
154 * Protocol A — Shell REPL (host → device, real-time over UART):
155 * vsf-test suite --list → enumerate registered suites
156 * vsf-test run <name> → trigger execution
157 * vsf-test config shuffle <N> → set random seed
158 * Suite ack: <name> ← confirmation
159 * Pass: N, Fail: N, Skip: N ← per-suite summary
160 *
161 * Protocol B — Capture Markers (device → host, offline via LA decode):
162 * <suite>:CASE:<N> ← case start (framework)
163 * <suite>:CASE:<N>:READY ← DUT ready for host input (framework, optional)
164 * <suite>:CASE:<N>:DONE ← case end (framework)
165 * <suite>:END ← suite boundary (framework)
166 *
167 * Host-less standalone mode (data sync via assist device) is not supported
168 * in the current configuration. Legacy port files are preserved under
169 * component/test/vsf_test/port/legacy/ for reference.
170 */
171
172# if VSF_USE_TEST == ENABLED
173
174/*============================ MACROS ========================================*/
175
177# ifndef VSF_TEST_CFG_LONGJMP
178# define VSF_TEST_CFG_LONGJMP ENABLED
179# endif
180
182# ifndef VSF_TEST_CFG_INTERNAL_TIMEOUT_MS
183# define VSF_TEST_CFG_INTERNAL_TIMEOUT_MS 1000
184# endif
185
187# ifndef VSF_TEST_CFG_EXTERNAL_TIMEOUT_MS
188# define VSF_TEST_CFG_EXTERNAL_TIMEOUT_MS 1500
189# endif
190
192# ifndef VSF_TEST_CFG_USE_HAL_WDT
193# define VSF_TEST_CFG_USE_HAL_WDT DISABLED
194# endif
195
197# ifndef VSF_TEST_CFG_USE_TRACE
198# define VSF_TEST_CFG_USE_TRACE ENABLED
199# endif
200
203# ifndef VSF_TEST_CFG_EMIT_MARKERS
204# define VSF_TEST_CFG_EMIT_MARKERS DISABLED
205# endif
206
212# ifndef VSF_TEST_CFG_HW_CONFIG_LOG_ONLY
213# define VSF_TEST_CFG_HW_CONFIG_LOG_ONLY DISABLED
214# endif
215
220# ifndef VSF_TEST_MARKER_DELAY_MS
221# define VSF_TEST_MARKER_DELAY_MS 2
222# endif
223
226# ifndef VSF_TEST_CFG_BUSY_WAIT_CYCLES_PER_MS
227# define VSF_TEST_CFG_BUSY_WAIT_CYCLES_PER_MS 22000
228# endif
229
232# ifndef VSF_TEST_POLL_TICK_MS
233# define VSF_TEST_POLL_TICK_MS 1
234# endif
235
241# define VSF_TEST_ASSERT(__v) \
242 do { \
243 if (!(__v)) { \
244 vsf_test_assert(VSF_TEST_RESULT_FAIL, __FILE__, \
245 __LINE__, __FUNCTION__, #__v); \
246 } \
247 } while (0)
248
256# define VSF_TEST_ASSERT_INST(_inst, _pt, _field) \
257 do { \
258 VSF_TEST_ASSERT((_inst) != NULL); \
259 VSF_TEST_ASSERT((_inst)->peripheral_type == (_pt)); \
260 VSF_TEST_ASSERT((_inst)->fixture._field != NULL); \
261 VSF_TEST_ASSERT((_inst)->name != NULL); \
262 } while (0)
263
272# define VSF_TEST_ASSERT_ERR(__err, __expected, ...) \
273 do { \
274 vsf_err_t _vsf_err = (vsf_err_t)(__err); \
275 if (_vsf_err != (vsf_err_t)(__expected)) { \
276 VSF_TEST_TRACE_ERROR(__VA_ARGS__); \
277 } \
278 VSF_TEST_ASSERT(_vsf_err == (vsf_err_t)(__expected)); \
279 } while (0)
280
285# define VSF_TEST_ASSERT_ERR_NONE(__err, ...) \
286 VSF_TEST_ASSERT_ERR(__err, VSF_ERR_NONE, __VA_ARGS__)
287
294# define VSF_TEST_WAIT_FOR(__cond, __timeout_ms) \
295 do { \
296 uint32_t _vsf_t = (uint32_t)(__timeout_ms); \
297 while (!(__cond) && _vsf_t-- > 0) { \
298 vsf_test_busy_wait_ms(1); \
299 } \
300 } while (0)
301
308# define VSF_TEST_SPIN_FOR(__cond, __iterations) \
309 do { \
310 uint32_t _vsf_i = (uint32_t)(__iterations); \
311 while (!(__cond) && _vsf_i-- > 0) {} \
312 } while (0)
313
318extern void vsf_test_trace(uint8_t level, const char *format, ...);
319
320# define VSF_TEST_TRACE_ERROR(...) \
321 vsf_test_trace(VSF_TRACE_ERROR, __VA_ARGS__)
322# define VSF_TEST_TRACE_WARNING(...) \
323 vsf_test_trace(VSF_TRACE_WARNING, __VA_ARGS__)
324# define VSF_TEST_TRACE_INFO(...) \
325 vsf_test_trace(VSF_TRACE_INFO, __VA_ARGS__)
326# define VSF_TEST_TRACE_DEBUG(...) \
327 vsf_test_trace(VSF_TRACE_DEBUG, __VA_ARGS__)
328
329/*============================ INCLUDES ======================================*/
330
331# include "./vsf_test_shell.h"
332
333/*============================ TYPES =========================================*/
334
335typedef enum vsf_test_result_t {
340
359
360typedef void vsf_test_reboot_t(void);
361
363 public_member(
367 void (*init)(vsf_test_wdt_t *wdt, uint32_t timeout_ms);
369 void (*feed)(vsf_test_wdt_t *wdt);
373 uint32_t timeout_ms;
375};
376
378typedef void vsf_test_jmp_fn_t(const vsf_test_suite_t *suite, const vsf_test_case_t *tc, const vsf_test_inst_t *inst);
379
381
382
387
389 public_member(
390 uint8_t case_idx;
391 void *params;
392 )
393};
394
396 public_member(
397 const char *name;
398 vsf_test_jmp_fn_t *jmp_fn;
399 uint16_t case_count;
400 uint16_t params_stride;
401 vsf_peripheral_type_t peripheral_type;
402 uint8_t needs_ready;
404};
405
406/* Forward declarations of HAL types — only pointers are needed in the union. */
407typedef struct vsf_i2c_t vsf_i2c_t;
409typedef struct vsf_spi_t vsf_spi_t;
410typedef struct vsf_adc_t vsf_adc_t;
411typedef struct vsf_pwm_t vsf_pwm_t;
412typedef struct vsf_dma_t vsf_dma_t;
414typedef struct vsf_rtc_t vsf_rtc_t;
416typedef struct vsf_wdt_t vsf_wdt_t;
417typedef struct vsf_rng_t vsf_rng_t;
418typedef struct vsf_gpio_t vsf_gpio_t;
419
425
431
432typedef struct vsf_test_inst_t {
434 const char *name;
435 union {
436 void *raw;
453
454typedef struct vsf_test_t {
459 struct {
463
468 struct {
472
476
479
484 struct {
485 const char *function_name;
486 const char *file_name;
487 const char *condition;
490
491# if VSF_TEST_CFG_LONGJMP == ENABLED
493# endif
494
498
502
506
507/*============================ INCLUDES ======================================*/
508/*============================ PROTOTYPES ====================================*/
509
514extern void vsf_test_run(vsf_test_t *test);
515
527extern void vsf_test_assert(vsf_test_result_t result,
528 const char *file_name, uint32_t line,
529 const char *function_name,
530 const char *condition);
531
541extern void vsf_test_reboot(vsf_test_result_t result,
542 const char *file_name, uint32_t line,
543 const char *function_name,
544 const char *additional_str);
545
552extern void vsf_test_busy_wait_ms(uint32_t ms);
553extern void vsf_test_busy_wait_us(uint32_t us);
554
561extern void vsf_test_hw_setup(vsf_test_t *test);
562
570extern void vsf_test_hw_config(vsf_peripheral_type_t peripheral_type, const vsf_test_inst_t *inst, bool init);
571
572/* ========================== Test Suite primitive ========================== */
573
581extern vsf_test_result_t vsf_test_run_suite_case(const vsf_test_suite_t *suite, uint16_t local_idx, const vsf_test_inst_t *inst);
582
588extern void vsf_test_run_suite(const vsf_test_suite_t *suite);
589
590/*============================ LOCAL VARIABLES ===============================*/
591/*============================ GLOBAL VARIABLES ==============================*/
592
593# endif
594#endif
595/* EOF */
Definition vsf_test.h:395
Definition vsf_test.h:362
__le16 params
Definition ieee80211.h:108
#define vsf_class(__name)
Definition ooc_class.h:52
unsigned short uint16_t
Definition stdint.h:7
unsigned uint32_t
Definition stdint.h:9
unsigned char uint8_t
Definition stdint.h:5
Definition vsf_template_adc.h:755
Definition vsf_template_dma.h:906
Definition vsf_template_flash.h:418
Definition vsf_template_gpio.h:942
I2C instance structure for multi-class support.
Definition vsf_template_i2c.h:1056
Definition vsf_template_pwm.h:288
Definition vsf_template_rng.h:197
RTC instance structure, used for RTC Multi Class support, not needed in non Multi Class mode.
Definition vsf_template_rtc.h:453
SPI instance structure, used for SPI Multi Class support, not needed in non Multi Class mode.
Definition vsf_template_spi.h:1131
Definition vsf_test.h:427
vsf_usart_t * usart
Definition vsf_test.h:429
vsf_gpio_t * gpio
Definition vsf_test.h:428
Definition vsf_test.h:421
vsf_i2c_t * slave_i2c
Definition vsf_test.h:423
vsf_i2c_t * master_i2c
Definition vsf_test.h:422
Definition vsf_test.h:432
vsf_test_gpio_pinmux_ctx_t * gpio_pinmux
Definition vsf_test.h:450
vsf_flash_t * flash
Definition vsf_test.h:445
vsf_spi_t * spi
Definition vsf_test.h:439
union vsf_test_inst_t::@104 fixture
typed hardware fixture passed to test suites
vsf_usart_t * usart
Definition vsf_test.h:438
vsf_peripheral_type_t peripheral_type
which HAL interface this instance satisfies
Definition vsf_test.h:433
vsf_rtc_t * rtc
Definition vsf_test.h:444
const char * name
human-readable instance description for logs
Definition vsf_test.h:434
vsf_wdt_t * wdt
Definition vsf_test.h:446
vsf_test_i2c_slave_ctx_t * i2c_slave
Definition vsf_test.h:449
vsf_dma_t * dma
Definition vsf_test.h:442
vsf_rng_t * rng
Definition vsf_test.h:447
void * raw
generic fallback / pass-through
Definition vsf_test.h:436
vsf_gpio_t * gpio
Definition vsf_test.h:448
vsf_adc_t * adc
Definition vsf_test.h:440
vsf_i2c_t * i2c
Definition vsf_test.h:437
vsf_timer_t * timer
Definition vsf_test.h:443
vsf_pwm_t * pwm
Definition vsf_test.h:441
Definition vsf_test_shell.h:18
Definition vsf_test.h:454
uint8_t suite_count
Definition vsf_test.h:497
uint8_t instance_count
Definition vsf_test.h:501
uint8_t result
Definition vsf_test.h:483
const char * condition
Definition vsf_test.h:487
const vsf_test_case_t * current_case
Definition vsf_test.h:475
uint8_t count
Definition vsf_test.h:461
struct vsf_test_t::@105 wdt
uint32_t line
Definition vsf_test.h:488
jmp_buf * jmp_buf
Definition vsf_test.h:492
const char * function_name
Definition vsf_test.h:485
vsf_test_shell_t shell
Embedded shell REPL — started by vsf_test_run() after init.
Definition vsf_test.h:504
const vsf_test_inst_t * instances
Peripheral instances array and count — populated at compile time.
Definition vsf_test.h:500
const vsf_test_suite_t * current_suite
Current suite pointer — set before running cases in a suite.
Definition vsf_test.h:478
vsf_test_reboot_t ** entries
Definition vsf_test.h:469
struct vsf_test_t::@106 reboot
struct vsf_test_t::@107 error
const vsf_test_suite_t ** suites
Registered suites array and count — populated at compile time.
Definition vsf_test.h:496
vsf_test_wdt_t * entries
Definition vsf_test.h:460
const char * file_name
Definition vsf_test.h:486
Definition vsf_template_timer.h:780
USART instance structure Used for USART Multi Class support.
Definition vsf_template_usart.h:1138
WDT instance structure, used for WDT Multi Class support, not needed in non Multi Class mode.
Definition vsf_template_wdt.h:428
SDL_PixelFormat format
Definition vsf_sdl2_pixelformat.c:32
void vsf_test_reboot_t(void)
Definition vsf_test.h:360
void vsf_test_busy_wait_ms(uint32_t ms)
Busy-wait for approximately the given milliseconds. Useful for simple inter-step delays in test scena...
Definition vsf_test.c:194
void vsf_test_jmp_fn_t(const vsf_test_suite_t *suite, const vsf_test_case_t *tc, const vsf_test_inst_t *inst)
Definition vsf_test.h:378
void vsf_test_hw_setup(vsf_test_t *test)
Board-provided hardware init for the test framework. Fills wdt, reboot entries, peripheral instances,...
void vsf_test_assert(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:119
vsf_test_result_t vsf_test_run_suite_case(const vsf_test_suite_t *suite, uint16_t local_idx, const vsf_test_inst_t *inst)
Run a single test case within a suite. No setup/teardown is performed — the caller is responsible for...
Definition vsf_test.c:205
vsf_peripheral_type_t
Definition vsf_test.h:341
@ VSF_PERIPHERAL_TYPE_GPIO_PINMUX
Definition vsf_test.h:356
@ VSF_PERIPHERAL_TYPE_PWM
Definition vsf_test.h:348
@ VSF_PERIPHERAL_TYPE_SPI
Definition vsf_test.h:345
@ VSF_PERIPHERAL_TYPE_DMA
Definition vsf_test.h:353
@ VSF_PERIPHERAL_TYPE_TIMER
Definition vsf_test.h:349
@ VSF_PERIPHERAL_TYPE_USART
Definition vsf_test.h:344
@ VSF_PERIPHERAL_TYPE_ADC
Definition vsf_test.h:347
@ VSF_PERIPHERAL_TYPE_WDT
Definition vsf_test.h:351
@ VSF_PERIPHERAL_TYPE_ARCH
Definition vsf_test.h:357
@ VSF_PERIPHERAL_TYPE_I2C_SLAVE
Definition vsf_test.h:355
@ VSF_PERIPHERAL_TYPE_FLASH
Definition vsf_test.h:354
@ VSF_PERIPHERAL_TYPE_GPIO
Definition vsf_test.h:343
@ VSF_PERIPHERAL_TYPE_I2C
Definition vsf_test.h:346
@ VSF_PERIPHERAL_TYPE_NONE
Definition vsf_test.h:342
@ VSF_PERIPHERAL_TYPE_RNG
Definition vsf_test.h:352
@ VSF_PERIPHERAL_TYPE_RTC
Definition vsf_test.h:350
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:147
void vsf_test_run_suite(const vsf_test_suite_t *suite)
Run all cases in a suite. Calls setup before the first case and teardown after the last case....
Definition vsf_test.c:256
void vsf_test_hw_config(vsf_peripheral_type_t peripheral_type, const vsf_test_inst_t *inst, bool init)
GPIO config hook called before/after running test cases on an instance. Board override (weak): config...
Definition vsf_test.c:174
struct vsf_test_case_t vsf_test_case_t
Definition vsf_test.h:377
void vsf_test_busy_wait_us(uint32_t us)
Definition vsf_test.c:200
void vsf_test_trace(uint8_t level, const char *format,...)
Definition vsf_test.c:48
vsf_test_result_t
Definition vsf_test.h:335
@ VSF_TEST_RESULT_PASS
Definition vsf_test.h:336
@ VSF_TEST_RESULT_FAIL
Definition vsf_test.h:338
@ VSF_TEST_RESULT_SKIP
Definition vsf_test.h:337
void vsf_test_run(vsf_test_t *test)
initialize vsf test
Definition vsf_test.c:90
dcl_simple_class(vsf_test_suite_t) class vsf_test_case_t
Test Suite — pure logic, no HAL binding.
Definition vsf_test.h:380
Generated from commit: vsfteam/vsf@3b461d0