blob: c205614a82b6cba3df2038bd520b1ccf9c113dd0 [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 */
26};
27
28struct test_result_t {
29 enum test_status_t val; /*!< Test result \ref test_status_t */
30 const char *info_msg; /*!< Information message to show in case of
31 * failure
32 */
33 const char *filename; /*!< Filename where the failure has occured */
34 uint32_t line; /*!< Line where the failure has occured */
35};
36
37/**
38 * \brief Runs the test.
39 *
40 * \param[out] ret Test result value
41 */
42typedef void TEST_FUN(struct test_result_t *ret);
43
44struct test_t {
45 TEST_FUN * const test; /*!< Test function to call */
46 const char *name; /*!< Test name */
47 const char *desc; /*!< Test description */
Kevin Peng62a87112020-07-07 15:07:46 +080048};
49
50struct test_suite_t;
51
52/**
53 * \brief Registers test in the testsuite structure and sets the name.
54 *
55 * \param[in] p_test_suite Pointer to the p_test_suite_location.
56 */
57typedef void TESTSUITE_REG(struct test_suite_t *p_test_suite);
58
59struct test_suite_t {
60 TESTSUITE_REG * const freg; /*!< Function to set all follow fields
61 * of the current test suite
62 */
63 struct test_t *test_list; /*!< List of tests */
64 uint32_t list_size; /*!< List size */
65 const char *name; /*!< Test suite name */
66 enum test_status_t val; /*!< Test suite result \ref test_result_t */
67};
68
Kevin Peng62a87112020-07-07 15:07:46 +080069/**
70 * \brief Translates the test suite error into a string.
71 *
72 * \param[in] err Error value \ref test_suite_err_t
73 *
74 * \returns error as string.
75 */
76const char *test_err_to_str(enum test_suite_err_t err);
77
78/**
79 * \brief Sets test suite parameters.
80 *
81 * \param[in] name Test suite name
82 * \param[in] test_list Pointer to the test list
83 * \param[in] size Test list size
84 * \param[in,out] p_ts Pointer to test suite object to fill in the
85 * parameters
86 *
87 * \returns Returns error code as specified in \ref test_suite_err_t
88 */
89enum test_suite_err_t set_testsuite(const char *name,
90 struct test_t *test_list, uint32_t size,
91 struct test_suite_t *p_ts);
92
93/**
94 * \brief Runs the given test suite.
95 *
96 * \param[in,out] test_suite Test suite to run the list of tests and
97 * store test results.
98 *
99 * \returns Returns error code as specified in \ref test_suite_err_t
100 */
101enum test_suite_err_t run_testsuite(struct test_suite_t *test_suite);
102
103/**
104 * \brief Prints all test in the the given test suite.
105 *
106 * \param[in] ts Test suite to print the list of tests
107 */
108void show_tests(const struct test_suite_t *ts);
109
110/**
111 * \brief Sets test failure state and information in the \ref test_result_t
112 * structure.
113 *
114 * \param[in] info_msg Information message to show
115 * \param[in] filename Filename where the error has ocurred
116 * \param[in] line Line in the file where the error has ocurred
117 * \param[out] ret Pointer to \ref test_result_t structure to
118 * set the values
119 *
120 * \note: If info_msg is "" or , info message is not shown. If filename is "",
121 * filename and line are not shown.
122 */
123void set_test_failed(const char *info_msg, const char *filename, uint32_t line,
124 struct test_result_t *ret);
125
126#define TEST_FAIL(info_msg) set_test_failed(info_msg, __FILE__, __LINE__, ret)
127
Kevin Peng62a87112020-07-07 15:07:46 +0800128#ifdef __cplusplus
129}
130#endif
131
132#endif /* __TEST_FRAMEWORK_H__ */