blob: c01c34d43d5fc8af4fa5792b15f908a49121d6fb [file] [log] [blame]
Kevin Peng62a87112020-07-07 15:07:46 +08001/*
Jianliang Shen0f81aa02022-02-08 11:07:35 +08002 * Copyright (c) 2017-2022, Arm Limited. All rights reserved.
Kevin Peng62a87112020-07-07 15:07:46 +08003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#ifndef __TEST_FRAMEWORK_H__
9#define __TEST_FRAMEWORK_H__
10
11#include <stdarg.h>
12#include <stdint.h>
13#include <stdio.h>
14
David Hu1a74bc52021-08-19 11:17:42 +080015#include "test_log.h"
Kevin Peng62a87112020-07-07 15:07:46 +080016#include "test_framework_helpers.h"
Devaraj Ranganna7937ad52020-02-21 13:05:10 +000017#include "test_framework_error_codes.h"
Kevin Peng62a87112020-07-07 15:07:46 +080018
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23enum test_status_t {
24 TEST_PASSED = 0, /*!< Test has passed */
25 TEST_FAILED = 1, /*!< Test has failed */
Jianliang Shen5fcfd372022-02-08 13:30:59 +080026 TEST_SKIPPED = 2, /*!< Test has skipped */
Kevin Peng62a87112020-07-07 15:07:46 +080027};
28
29struct test_result_t {
30 enum test_status_t val; /*!< Test result \ref test_status_t */
31 const char *info_msg; /*!< Information message to show in case of
32 * failure
33 */
34 const char *filename; /*!< Filename where the failure has occured */
35 uint32_t line; /*!< Line where the failure has occured */
36};
37
38/**
39 * \brief Runs the test.
40 *
41 * \param[out] ret Test result value
42 */
43typedef void TEST_FUN(struct test_result_t *ret);
44
45struct test_t {
46 TEST_FUN * const test; /*!< Test function to call */
47 const char *name; /*!< Test name */
48 const char *desc; /*!< Test description */
Kevin Peng62a87112020-07-07 15:07:46 +080049};
50
51struct test_suite_t;
52
53/**
54 * \brief Registers test in the testsuite structure and sets the name.
55 *
56 * \param[in] p_test_suite Pointer to the p_test_suite_location.
57 */
58typedef void TESTSUITE_REG(struct test_suite_t *p_test_suite);
59
60struct test_suite_t {
61 TESTSUITE_REG * const freg; /*!< Function to set all follow fields
62 * of the current test suite
63 */
64 struct test_t *test_list; /*!< List of tests */
65 uint32_t list_size; /*!< List size */
66 const char *name; /*!< Test suite name */
67 enum test_status_t val; /*!< Test suite result \ref test_result_t */
68};
69
Kevin Peng62a87112020-07-07 15:07:46 +080070/**
71 * \brief Translates the test suite error into a string.
72 *
73 * \param[in] err Error value \ref test_suite_err_t
74 *
75 * \returns error as string.
76 */
77const char *test_err_to_str(enum test_suite_err_t err);
78
79/**
80 * \brief Sets test suite parameters.
81 *
82 * \param[in] name Test suite name
83 * \param[in] test_list Pointer to the test list
84 * \param[in] size Test list size
85 * \param[in,out] p_ts Pointer to test suite object to fill in the
86 * parameters
87 *
88 * \returns Returns error code as specified in \ref test_suite_err_t
89 */
90enum test_suite_err_t set_testsuite(const char *name,
91 struct test_t *test_list, uint32_t size,
92 struct test_suite_t *p_ts);
93
94/**
95 * \brief Runs the given test suite.
96 *
97 * \param[in,out] test_suite Test suite to run the list of tests and
98 * store test results.
99 *
100 * \returns Returns error code as specified in \ref test_suite_err_t
101 */
102enum test_suite_err_t run_testsuite(struct test_suite_t *test_suite);
103
104/**
105 * \brief Prints all test in the the given test suite.
106 *
107 * \param[in] ts Test suite to print the list of tests
108 */
109void show_tests(const struct test_suite_t *ts);
110
111/**
112 * \brief Sets test failure state and information in the \ref test_result_t
113 * structure.
114 *
115 * \param[in] info_msg Information message to show
116 * \param[in] filename Filename where the error has ocurred
117 * \param[in] line Line in the file where the error has ocurred
118 * \param[out] ret Pointer to \ref test_result_t structure to
119 * set the values
120 *
121 * \note: If info_msg is "" or , info message is not shown. If filename is "",
122 * filename and line are not shown.
123 */
124void set_test_failed(const char *info_msg, const char *filename, uint32_t line,
125 struct test_result_t *ret);
126
127#define TEST_FAIL(info_msg) set_test_failed(info_msg, __FILE__, __LINE__, ret)
128
Kevin Peng62a87112020-07-07 15:07:46 +0800129#ifdef __cplusplus
130}
131#endif
132
133#endif /* __TEST_FRAMEWORK_H__ */