blob: b27e2988a69339f3704472391c41d724437c86a6 [file] [log] [blame]
Marc Moreno Berengueffd3c462017-11-29 16:09:52 +00001/*
Jamie Foxc41ebb52017-12-20 13:42:23 +00002 * Copyright (c) 2017-2018, Arm Limited. All rights reserved.
Marc Moreno Berengueffd3c462017-11-29 16:09:52 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#ifndef __TEST_FRAMEWORK_H__
9#define __TEST_FRAMEWORK_H__
10
Jamie Foxc41ebb52017-12-20 13:42:23 +000011#include <stdarg.h>
Marc Moreno Berengueffd3c462017-11-29 16:09:52 +000012#include <stdint.h>
Antonio de Angelisa6f72162018-09-05 11:00:37 +010013#include <stdio.h>
Marc Moreno Berengueffd3c462017-11-29 16:09:52 +000014
Tamas Banc2074a72018-08-14 10:23:12 +010015#include "test_framework_helpers.h"
Marc Moreno Berengueffd3c462017-11-29 16:09:52 +000016
Jamie Foxc41ebb52017-12-20 13:42:23 +000017#ifdef __cplusplus
18extern "C" {
19#endif
20
Marc Moreno Berengueffd3c462017-11-29 16:09:52 +000021enum test_status_t {
22 TEST_PASSED = 0, /*!< Test has passed */
23 TEST_FAILED = 1, /*!< Test has failed */
24};
25
26struct test_result_t {
27 enum test_status_t val; /*!< Test result \ref test_status_t */
28 const char *info_msg; /*!< Information message to show in case of
29 * failure
30 */
31 const char *filename; /*!< Filename where the failure has occured */
32 uint32_t line; /*!< Line where the failure has occured */
33};
34
35/**
36 * \brief Runs the test.
37 *
38 * \param[out] ret Test result value
39 */
40typedef void TEST_FUN(struct test_result_t *ret);
41
42struct test_t {
43 TEST_FUN * const test; /*!< Test function to call */
44 const char *name; /*!< Test name */
45 const char *desc; /*!< Test description */
46 struct test_result_t ret; /*!< Test result */
47};
48
49struct test_suite_t;
50
51/**
52 * \brief Registers test in the testsuite structure and sets the name.
53 *
54 * \param[in] p_test_suite Pointer to the p_test_suite_location.
55 */
56typedef void TESTSUITE_REG(struct test_suite_t *p_test_suite);
57
58struct test_suite_t {
59 TESTSUITE_REG * const freg; /*!< Function to set all follow fields
60 * of the current test suite
61 */
62 struct test_t *test_list; /*!< List of tests */
63 uint32_t list_size; /*!< List size */
64 const char *name; /*!< Test suite name */
65 enum test_status_t val; /*!< Test suite result \ref test_result_t */
66};
67
68enum test_suite_err_t {
69 TEST_SUITE_ERR_NO_ERROR = 0, /*!< No error */
70 TEST_SUITE_ERR_INVALID_DATA = 1, /*!< Invalid test suite if any of the
71 * pointers is NULL
72 */
73 TEST_SUITE_ERR_INVALID_TEST_DATA = 2, /*!< Invalid test if any of the
74 * pointers is NULL
75 */
76 TEST_SUITE_ERR_TEST_FAILED = 3, /*!< Last executed test has failed */
77};
78
79/**
80 * \brief Translates the test suite error into a string.
81 *
82 * \param[in] err Error value \ref test_suite_err_t
83 *
84 * \returns error as string.
85 */
86const char *test_err_to_str(enum test_suite_err_t err);
87
88/**
89 * \brief Sets test suite parameters in the \ref structure.
90 *
91 * \param[in] name Test suite name
92 * \param[in] test_list Pointer to the test list
93 * \param[in] size Test list size
94 * \param[in/out] p_ts Pointer to test suite object to fill in the
95 * parameters
96 *
97 * \returns Returns error code as specified in \ref test_suite_err_t
98 */
99enum test_suite_err_t set_testsuite(const char *name,
100 struct test_t *test_list, uint32_t size,
101 struct test_suite_t *p_ts);
102
103/**
104 * \brief Runs the given test suite.
105 *
106 * \param[in/out] test_suite Test suite to run the list of tests and
107 * store test results.
108 *
109 * \returns Returns error code as specified in \ref test_suite_err_t
110 */
111enum test_suite_err_t run_testsuite(struct test_suite_t *test_suite);
112
113/**
114 * \brief Prints all test in the the given test suite.
115 *
116 * \param[in] ts Test suite to print the list of tests
117 */
118void show_tests(const struct test_suite_t *ts);
119
120/**
121 * \brief Sets test failure state and information in the \ref sst_test_result_t
122 * structure.
123 *
124 * \param[in] info_msg Information message to show
125 * \param[in] filename Filename where the error has ocurred
126 * \param[in] line Line in the file where the error has ocurred
127 * \param[out] ret Pointer to \ref sst_test_result_t structure to
128 * set the values
129 *
130 * \note: If info_msg is "" or , info message is not shown. If filename is "",
131 * filename and line are not shown.
132 */
133void set_test_failed(const char *info_msg, const char *filename, uint32_t line,
134 struct test_result_t *ret);
135
136#define TEST_FAIL(info_msg) set_test_failed(info_msg, __FILE__, __LINE__, ret)
137
Jamie Foxc41ebb52017-12-20 13:42:23 +0000138#define TEST_LOG(...) printf(__VA_ARGS__)
139
Marc Moreno Berengueffd3c462017-11-29 16:09:52 +0000140#ifdef __cplusplus
141}
142#endif
143
144#endif /* __TEST_FRAMEWORK_H__ */