1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2017 Intel Corporation 3 */ 4 5 #ifndef _MAIN_H_ 6 #define _MAIN_H_ 7 8 #include <stddef.h> 9 #include <sys/queue.h> 10 11 #include <rte_common.h> 12 #include <rte_hexdump.h> 13 #include <rte_log.h> 14 15 #define TEST_SUCCESS 0 16 #define TEST_FAILED -1 17 #define TEST_SKIPPED 1 18 19 #define MAX_BURST 512U 20 #define DEFAULT_BURST 32U 21 #define DEFAULT_OPS 64U 22 #define DEFAULT_ITER 6U 23 24 25 26 #define TEST_ASSERT(cond, msg, ...) do { \ 27 if (!(cond)) { \ 28 printf("TestCase %s() line %d failed: " \ 29 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \ 30 return TEST_FAILED; \ 31 } \ 32 } while (0) 33 34 /* Compare two buffers (length in bytes) */ 35 #define TEST_ASSERT_BUFFERS_ARE_EQUAL(a, b, len, msg, ...) do { \ 36 if (memcmp((a), (b), len)) { \ 37 printf("TestCase %s() line %d failed: " \ 38 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \ 39 rte_memdump(stdout, "Buffer A", (a), len); \ 40 rte_memdump(stdout, "Buffer B", (b), len); \ 41 return TEST_FAILED; \ 42 } \ 43 } while (0) 44 45 #define TEST_ASSERT_SUCCESS(val, msg, ...) do { \ 46 typeof(val) _val = (val); \ 47 if (!(_val == 0)) { \ 48 printf("TestCase %s() line %d failed (err %d): " \ 49 msg "\n", __func__, __LINE__, _val, \ 50 ##__VA_ARGS__); \ 51 return TEST_FAILED; \ 52 } \ 53 } while (0) 54 55 #define TEST_ASSERT_FAIL(val, msg, ...) \ 56 TEST_ASSERT_SUCCESS(!(val), msg, ##__VA_ARGS__) 57 58 #define TEST_ASSERT_NOT_NULL(val, msg, ...) do { \ 59 if ((val) == NULL) { \ 60 printf("TestCase %s() line %d failed (null): " \ 61 msg "\n", __func__, __LINE__, ##__VA_ARGS__); \ 62 return TEST_FAILED; \ 63 } \ 64 } while (0) 65 66 struct unit_test_case { 67 int (*setup)(void); 68 void (*teardown)(void); 69 int (*testcase)(void); 70 const char *name; 71 }; 72 73 #define TEST_CASE(testcase) {NULL, NULL, testcase, #testcase} 74 75 #define TEST_CASE_ST(setup, teardown, testcase) \ 76 {setup, teardown, testcase, #testcase} 77 78 #define TEST_CASES_END() {NULL, NULL, NULL, NULL} 79 80 struct unit_test_suite { 81 const char *suite_name; 82 int (*setup)(void); 83 void (*teardown)(void); 84 struct unit_test_case unit_test_cases[]; 85 }; 86 87 int unit_test_suite_runner(struct unit_test_suite *suite); 88 89 typedef int (test_callback)(void); 90 TAILQ_HEAD(test_commands_list, test_command); 91 struct test_command { 92 TAILQ_ENTRY(test_command) next; 93 const char *command; 94 test_callback *callback; 95 }; 96 97 void add_test_command(struct test_command *t); 98 99 /* Register a test function */ 100 #define REGISTER_TEST_COMMAND(name, testsuite) \ 101 static int test_func_##name(void) \ 102 { \ 103 return unit_test_suite_runner(&testsuite); \ 104 } \ 105 static struct test_command test_struct_##name = { \ 106 .command = RTE_STR(name), \ 107 .callback = test_func_##name, \ 108 }; \ 109 RTE_INIT(test_register_##name) \ 110 { \ 111 add_test_command(&test_struct_##name); \ 112 } 113 114 const char *get_vector_filename(void); 115 116 unsigned int get_num_ops(void); 117 118 unsigned int get_burst_sz(void); 119 120 unsigned int get_num_lcores(void); 121 122 double get_snr(void); 123 124 unsigned int get_iter_max(void); 125 126 bool get_init_device(void); 127 128 #endif 129