1 /*
2  * Copyright (c) 2017-2022, Arm Limited. All rights reserved.
3  *
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 
15 #include "test_log.h"
16 #include "test_framework_helpers.h"
17 #include "test_framework_error_codes.h"
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 enum test_status_t {
24     TEST_PASSED = 0,  /*!< Test has passed */
25     TEST_FAILED = 1,  /*!< Test has failed */
26     TEST_SKIPPED = 2, /*!< Test has skipped */
27 };
28 
29 struct 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  */
43 typedef void TEST_FUN(struct test_result_t *ret);
44 
45 struct test_t {
46     TEST_FUN * const test;         /*!< Test function to call */
47     const char *name;              /*!< Test name */
48     const char *desc;              /*!< Test description */
49 };
50 
51 struct 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  */
58 typedef void TESTSUITE_REG(struct test_suite_t *p_test_suite);
59 
60 struct 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 
70 /**
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  */
77 const 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  */
90 enum 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  */
102 enum 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  */
109 void 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  */
124 void 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 
129 #ifdef __cplusplus
130 }
131 #endif
132 
133 #endif /* __TEST_FRAMEWORK_H__ */
134