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