2018-11-30 16:12:35 +08:00
|
|
|
/*
|
2021-03-08 18:19:04 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2018-11-30 16:12:35 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2018-11-19 MurphyZhao the first version
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTEST_H__
|
|
|
|
#define __UTEST_H__
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
2020-08-29 22:45:22 +08:00
|
|
|
#include <stdint.h>
|
2018-11-30 16:12:35 +08:00
|
|
|
#include "utest_log.h"
|
2019-01-22 15:42:55 +08:00
|
|
|
#include "utest_assert.h"
|
2018-11-30 16:12:35 +08:00
|
|
|
|
2019-06-24 15:59:26 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2018-12-06 18:58:23 +08:00
|
|
|
/**
|
|
|
|
* utest_error
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
* @brief Test result.
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
* @member UTEST_PASSED Test success.
|
|
|
|
* @member UTEST_FAILED Test failed.
|
|
|
|
* @member UTEST_PASSED Test skipped.
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
*/
|
2018-11-30 16:12:35 +08:00
|
|
|
enum utest_error
|
|
|
|
{
|
|
|
|
UTEST_PASSED = 0,
|
|
|
|
UTEST_FAILED = 1,
|
|
|
|
UTEST_SKIPPED = 2
|
|
|
|
};
|
|
|
|
typedef enum utest_error utest_err_e;
|
|
|
|
|
2018-12-06 18:58:23 +08:00
|
|
|
/**
|
|
|
|
* utest
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
* @brief utest data structure.
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
* @member error Error number from enum `utest_error`.
|
|
|
|
* @member passed_num Total number of tests passed.
|
|
|
|
* @member failed_num Total number of tests failed.
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
*/
|
2018-11-30 16:12:35 +08:00
|
|
|
struct utest
|
|
|
|
{
|
|
|
|
utest_err_e error;
|
|
|
|
uint32_t passed_num;
|
|
|
|
uint32_t failed_num;
|
|
|
|
};
|
|
|
|
typedef struct utest *utest_t;
|
|
|
|
|
2018-12-06 18:58:23 +08:00
|
|
|
/**
|
|
|
|
* utest_tc_export
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
* @brief utest testcase data structure.
|
|
|
|
* Will export the data to `UtestTcTab` section in flash.
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
* @member name Testcase name.
|
2019-01-22 15:42:55 +08:00
|
|
|
* @member run_timeout Testcase maximum test time (Time unit: seconds).
|
2018-12-06 18:58:23 +08:00
|
|
|
* @member init Necessary initialization before executing the test case function.
|
|
|
|
* @member tc Total number of tests failed.
|
|
|
|
* @member cleanup Total number of tests failed.
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
*/
|
2018-11-30 16:12:35 +08:00
|
|
|
struct utest_tc_export {
|
|
|
|
const char *name;
|
2018-12-06 17:11:27 +08:00
|
|
|
uint32_t run_timeout;
|
2018-11-30 16:12:35 +08:00
|
|
|
rt_err_t (*init)(void);
|
|
|
|
void (*tc)(void);
|
|
|
|
rt_err_t (*cleanup)(void);
|
|
|
|
};
|
|
|
|
typedef struct utest_tc_export *utest_tc_export_t;
|
|
|
|
|
2018-12-06 18:58:23 +08:00
|
|
|
/**
|
|
|
|
* test_unit_func
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
* @brief Unit test handler function pointer.
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
*/
|
2018-11-30 16:12:35 +08:00
|
|
|
typedef void (*test_unit_func)(void);
|
|
|
|
|
2018-12-06 18:58:23 +08:00
|
|
|
/**
|
|
|
|
* utest_unit_run
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
* @brief Unit test function executor.
|
|
|
|
* No need for the user to call this function directly
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
* @param func Unit test function.
|
|
|
|
* @param unit_func_name Unit test function name.
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
* @return void
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
*/
|
2018-11-30 16:12:35 +08:00
|
|
|
void utest_unit_run(test_unit_func func, const char *unit_func_name);
|
2018-12-06 18:58:23 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* utest_handle_get
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
* @brief Get the utest data structure handle.
|
|
|
|
* No need for the user to call this function directly
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
* @param void
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
* @return utest_t type. (struct utest *)
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
*/
|
2018-11-30 16:12:35 +08:00
|
|
|
utest_t utest_handle_get(void);
|
|
|
|
|
2018-12-06 18:58:23 +08:00
|
|
|
/**
|
|
|
|
* UTEST_NAME_MAX_LEN
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
* @brief Testcase name maximum length.
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
*/
|
2018-11-30 16:12:35 +08:00
|
|
|
#define UTEST_NAME_MAX_LEN (128u)
|
|
|
|
|
2018-12-06 18:58:23 +08:00
|
|
|
/**
|
|
|
|
* UTEST_TC_EXPORT
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
* @brief Export testcase function to `UtestTcTab` section in flash.
|
|
|
|
* Used in application layer.
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
* @param testcase The testcase function.
|
|
|
|
* @param name The testcase name.
|
|
|
|
* @param init The initialization function of the test case.
|
|
|
|
* @param cleanup The cleanup function of the test case.
|
2019-01-22 15:42:55 +08:00
|
|
|
* @param timeout Testcase maximum test time (Time unit: seconds).
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
* @return None
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
*/
|
2018-12-06 17:11:27 +08:00
|
|
|
#define UTEST_TC_EXPORT(testcase, name, init, cleanup, timeout) \
|
2018-11-30 16:12:35 +08:00
|
|
|
RT_USED static const struct utest_tc_export _utest_testcase \
|
2021-02-09 23:25:46 +08:00
|
|
|
RT_SECTION("UtestTcTab") = \
|
2018-11-30 16:12:35 +08:00
|
|
|
{ \
|
2018-12-06 17:11:27 +08:00
|
|
|
name, \
|
|
|
|
timeout, \
|
|
|
|
init, \
|
|
|
|
testcase, \
|
|
|
|
cleanup \
|
2018-11-30 16:12:35 +08:00
|
|
|
}
|
|
|
|
|
2018-12-06 18:58:23 +08:00
|
|
|
/**
|
|
|
|
* UTEST_UNIT_RUN
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
* @brief Unit test function executor.
|
|
|
|
* Used in `testcase` function in application.
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
* @param test_unit_func Unit test function
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
* @return None
|
2021-03-08 18:19:04 +08:00
|
|
|
*
|
2018-12-06 18:58:23 +08:00
|
|
|
*/
|
2018-12-06 17:11:27 +08:00
|
|
|
#define UTEST_UNIT_RUN(test_unit_func) \
|
|
|
|
utest_unit_run(test_unit_func, #test_unit_func); \
|
2018-11-30 16:12:35 +08:00
|
|
|
if(utest_handle_get()->failed_num != 0) return;
|
|
|
|
|
2019-06-24 15:59:26 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-12-06 18:58:23 +08:00
|
|
|
#endif /* __UTEST_H__ */
|