1 /* 2 * Copyright (c) 2006-2021, RT-Thread Development Team 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Change Logs: 7 * Date Author Notes 8 * 2018-11-19 MurphyZhao the first version 9 */ 10 11 #ifndef __UTEST_H__ 12 #define __UTEST_H__ 13 14 #include <rtthread.h> 15 #include "utest_log.h" 16 #include "utest_assert.h" 17 18 #ifdef __cplusplus 19 extern "C" { 20 #endif 21 22 /** 23 * utest_error 24 * 25 * @brief Test result. 26 * 27 * @member UTEST_PASSED Test success. 28 * @member UTEST_FAILED Test failed. 29 * @member UTEST_PASSED Test skipped. 30 * 31 */ 32 enum utest_error 33 { 34 UTEST_PASSED = 0, 35 UTEST_FAILED = 1, 36 UTEST_SKIPPED = 2 37 }; 38 typedef enum utest_error utest_err_e; 39 40 /** 41 * utest 42 * 43 * @brief utest data structure. 44 * 45 * @member error Error number from enum `utest_error`. 46 * @member passed_num Total number of tests passed. 47 * @member failed_num Total number of tests failed. 48 * 49 */ 50 struct utest 51 { 52 utest_err_e error; 53 uint32_t passed_num; 54 uint32_t failed_num; 55 }; 56 typedef struct utest *utest_t; 57 58 /** 59 * utest_tc_export 60 * 61 * @brief utest testcase data structure. 62 * Will export the data to `UtestTcTab` section in flash. 63 * 64 * @member name Testcase name. 65 * @member run_timeout Testcase maximum test time (Time unit: seconds). 66 * @member init Necessary initialization before executing the test case function. 67 * @member tc Total number of tests failed. 68 * @member cleanup Total number of tests failed. 69 * 70 */ 71 struct utest_tc_export { 72 const char *name; 73 uint32_t run_timeout; 74 rt_err_t (*init)(void); 75 void (*tc)(void); 76 rt_err_t (*cleanup)(void); 77 }; 78 typedef struct utest_tc_export *utest_tc_export_t; 79 80 /** 81 * test_unit_func 82 * 83 * @brief Unit test handler function pointer. 84 * 85 */ 86 typedef void (*test_unit_func)(void); 87 88 /** 89 * utest_unit_run 90 * 91 * @brief Unit test function executor. 92 * No need for the user to call this function directly 93 * 94 * @param func Unit test function. 95 * @param unit_func_name Unit test function name. 96 * 97 * @return void 98 * 99 */ 100 void utest_unit_run(test_unit_func func, const char *unit_func_name); 101 102 /** 103 * utest_handle_get 104 * 105 * @brief Get the utest data structure handle. 106 * No need for the user to call this function directly 107 * 108 * @param void 109 * 110 * @return utest_t type. (struct utest *) 111 * 112 */ 113 utest_t utest_handle_get(void); 114 115 /** 116 * UTEST_NAME_MAX_LEN 117 * 118 * @brief Testcase name maximum length. 119 * 120 */ 121 #define UTEST_NAME_MAX_LEN (128u) 122 123 /** 124 * UTEST_TC_EXPORT 125 * 126 * @brief Export testcase function to `UtestTcTab` section in flash. 127 * Used in application layer. 128 * 129 * @param testcase The testcase function. 130 * @param name The testcase name. 131 * @param init The initialization function of the test case. 132 * @param cleanup The cleanup function of the test case. 133 * @param timeout Testcase maximum test time (Time unit: seconds). 134 * 135 * @return None 136 * 137 */ 138 #ifdef _MSC_VER 139 #pragma section("UtestTcTab$f",read) 140 #define UTEST_TC_EXPORT(testcase, name, init, cleanup, timeout) \ 141 __declspec(allocate("UtestTcTab$f")) \ 142 static const struct utest_tc_export _utest_testcase = \ 143 { \ 144 name, \ 145 timeout, \ 146 init, \ 147 testcase, \ 148 cleanup \ 149 } 150 #pragma comment(linker, "/merge:UtestTcTab=tctext") 151 #else 152 #define UTEST_TC_EXPORT(testcase, name, init, cleanup, timeout) \ 153 rt_used static const struct utest_tc_export _utest_testcase \ 154 rt_section("UtestTcTab") = \ 155 { \ 156 name, \ 157 timeout, \ 158 init, \ 159 testcase, \ 160 cleanup \ 161 } 162 #endif /* _MSC_VER */ 163 164 /** 165 * UTEST_UNIT_RUN 166 * 167 * @brief Unit test function executor. 168 * Used in `testcase` function in application. 169 * 170 * @param test_unit_func Unit test function 171 * 172 * @return None 173 * 174 */ 175 #define _UTEST_UNIT_RUN(test_unit_func) \ 176 do { \ 177 utest_unit_run(test_unit_func, #test_unit_func); \ 178 } while (0) 179 180 #define UTEST_UNIT_RUN(test_unit_func) _UTEST_UNIT_RUN(test_unit_func) 181 182 #ifdef __cplusplus 183 } 184 #endif 185 186 #endif /* __UTEST_H__ */ 187