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