dpdk/app/test/test.h
<<
>>
Prefs
   1/* SPDX-License-Identifier: BSD-3-Clause
   2 * Copyright(c) 2010-2014 Intel Corporation
   3 */
   4
   5#ifndef _TEST_H_
   6#define _TEST_H_
   7
   8#include <errno.h>
   9#include <stddef.h>
  10#include <stdlib.h>
  11#include <sys/queue.h>
  12
  13#include <rte_hexdump.h>
  14#include <rte_common.h>
  15
  16#define TEST_SUCCESS EXIT_SUCCESS
  17#define TEST_FAILED  -1
  18#define TEST_SKIPPED  77
  19
  20/* Before including test.h file you can define
  21 * TEST_TRACE_FAILURE(_file, _line, _func) macro to better trace/debug test
  22 * failures. Mostly useful in test development phase. */
  23#ifndef TEST_TRACE_FAILURE
  24# define TEST_TRACE_FAILURE(_file, _line, _func)
  25#endif
  26
  27#include <rte_test.h>
  28
  29#define TEST_ASSERT RTE_TEST_ASSERT
  30
  31#define TEST_ASSERT_EQUAL RTE_TEST_ASSERT_EQUAL
  32
  33/* Compare two buffers (length in bytes) */
  34#define TEST_ASSERT_BUFFERS_ARE_EQUAL(a, b, len,  msg, ...) do {        \
  35        if (memcmp(a, b, len)) {                                        \
  36                printf("TestCase %s() line %d failed: "              \
  37                        msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
  38                TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
  39                return TEST_FAILED;                                  \
  40        }                                                        \
  41} while (0)
  42
  43/* Compare two buffers with offset (length and offset in bytes) */
  44#define TEST_ASSERT_BUFFERS_ARE_EQUAL_OFFSET(a, b, len, off, msg, ...) do { \
  45        const uint8_t *_a_with_off = (const uint8_t *)a + off;              \
  46        const uint8_t *_b_with_off = (const uint8_t *)b + off;              \
  47        TEST_ASSERT_BUFFERS_ARE_EQUAL(_a_with_off, _b_with_off, len, msg);  \
  48} while (0)
  49
  50/* Compare two buffers (length in bits) */
  51#define TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT(a, b, len, msg, ...) do {     \
  52        uint8_t _last_byte_a, _last_byte_b;                       \
  53        uint8_t _last_byte_mask, _last_byte_bits;                  \
  54        TEST_ASSERT_BUFFERS_ARE_EQUAL(a, b, (len >> 3), msg);     \
  55        if (len % 8) {                                              \
  56                _last_byte_bits = len % 8;                   \
  57                _last_byte_mask = ~((1 << (8 - _last_byte_bits)) - 1); \
  58                _last_byte_a = ((const uint8_t *)a)[len >> 3];            \
  59                _last_byte_b = ((const uint8_t *)b)[len >> 3];            \
  60                _last_byte_a &= _last_byte_mask;                     \
  61                _last_byte_b &= _last_byte_mask;                    \
  62                if (_last_byte_a != _last_byte_b) {                  \
  63                        printf("TestCase %s() line %d failed: "              \
  64                                msg "\n", __func__, __LINE__, ##__VA_ARGS__);\
  65                        TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
  66                        return TEST_FAILED;                                  \
  67                }                                                        \
  68        }                                                            \
  69} while (0)
  70
  71/* Compare two buffers with offset (length and offset in bits) */
  72#define TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT_OFFSET(a, b, len, off, msg, ...) do { \
  73        uint8_t _first_byte_a, _first_byte_b;                                 \
  74        uint8_t _first_byte_mask, _first_byte_bits;                           \
  75        uint32_t _len_without_first_byte = (off % 8) ?                       \
  76                                len - (8 - (off % 8)) :                       \
  77                                len;                                          \
  78        uint32_t _off_in_bytes = (off % 8) ? (off >> 3) + 1 : (off >> 3);     \
  79        const uint8_t *_a_with_off = (const uint8_t *)a + _off_in_bytes;      \
  80        const uint8_t *_b_with_off = (const uint8_t *)b + _off_in_bytes;      \
  81        TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT(_a_with_off, _b_with_off,           \
  82                                _len_without_first_byte, msg);                \
  83        if (off % 8) {                                                        \
  84                _first_byte_bits = 8 - (off % 8);                             \
  85                _first_byte_mask = (1 << _first_byte_bits) - 1;               \
  86                _first_byte_a = *(_a_with_off - 1);                           \
  87                _first_byte_b = *(_b_with_off - 1);                           \
  88                _first_byte_a &= _first_byte_mask;                            \
  89                _first_byte_b &= _first_byte_mask;                            \
  90                if (_first_byte_a != _first_byte_b) {                         \
  91                        printf("TestCase %s() line %d failed: "               \
  92                                msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
  93                        TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);     \
  94                        return TEST_FAILED;                                   \
  95                }                                                             \
  96        }                                                                     \
  97} while (0)
  98
  99#define TEST_ASSERT_NOT_EQUAL RTE_TEST_ASSERT_NOT_EQUAL
 100
 101#define TEST_ASSERT_SUCCESS RTE_TEST_ASSERT_SUCCESS
 102
 103#define TEST_ASSERT_FAIL RTE_TEST_ASSERT_FAIL
 104
 105#define TEST_ASSERT_NULL RTE_TEST_ASSERT_NULL
 106
 107#define TEST_ASSERT_NOT_NULL RTE_TEST_ASSERT_NOT_NULL
 108
 109struct unit_test_case {
 110        int (*setup)(void);
 111        void (*teardown)(void);
 112        int (*testcase)(void);
 113        int (*testcase_with_data)(const void *data);
 114        const char *name;
 115        unsigned enabled;
 116        const void *data;
 117};
 118
 119#define TEST_CASE(fn) { NULL, NULL, fn, NULL, #fn, 1, NULL }
 120
 121#define TEST_CASE_NAMED(name, fn) { NULL, NULL, fn, NULL, name, 1, NULL }
 122
 123#define TEST_CASE_ST(setup, teardown, testcase) \
 124                { setup, teardown, testcase, NULL, #testcase, 1, NULL }
 125
 126#define TEST_CASE_WITH_DATA(setup, teardown, testcase, data) \
 127                { setup, teardown, NULL, testcase, #testcase, 1, data }
 128
 129#define TEST_CASE_NAMED_ST(name, setup, teardown, testcase) \
 130                { setup, teardown, NULL, testcase, name, 1, NULL }
 131
 132#define TEST_CASE_NAMED_WITH_DATA(name, setup, teardown, testcase, data) \
 133                { setup, teardown, NULL, testcase, name, 1, data }
 134
 135#define TEST_CASE_DISABLED(fn) { NULL, NULL, fn, NULL, #fn, 0, NULL }
 136
 137#define TEST_CASE_ST_DISABLED(setup, teardown, testcase) \
 138                { setup, teardown, testcase, NULL, #testcase, 0, NULL }
 139
 140#define TEST_CASES_END() { NULL, NULL, NULL, NULL, NULL, 0, NULL }
 141
 142static inline void
 143debug_hexdump(FILE *file, const char *title, const void *buf, size_t len)
 144{
 145        if (rte_log_get_global_level() == RTE_LOG_DEBUG)
 146                rte_hexdump(file, title, buf, len);
 147}
 148
 149struct unit_test_suite {
 150        const char *suite_name;
 151        int (*setup)(void);
 152        void (*teardown)(void);
 153        unsigned int total;
 154        unsigned int executed;
 155        unsigned int succeeded;
 156        unsigned int skipped;
 157        unsigned int failed;
 158        unsigned int unsupported;
 159        struct unit_test_suite **unit_test_suites;
 160        struct unit_test_case unit_test_cases[];
 161};
 162
 163int unit_test_suite_runner(struct unit_test_suite *suite);
 164extern int last_test_result;
 165
 166#define RECURSIVE_ENV_VAR "RTE_TEST_RECURSIVE"
 167
 168#include <cmdline_parse.h>
 169#include <cmdline_parse_string.h>
 170
 171extern const char *prgname;
 172
 173int commands_init(void);
 174int command_valid(const char *cmd);
 175
 176int test_mp_secondary(void);
 177int test_timer_secondary(void);
 178
 179int test_set_rxtx_conf(cmdline_fixed_string_t mode);
 180int test_set_rxtx_anchor(cmdline_fixed_string_t type);
 181int test_set_rxtx_sc(cmdline_fixed_string_t type);
 182
 183typedef int (test_callback)(void);
 184TAILQ_HEAD(test_commands_list, test_command);
 185struct test_command {
 186        TAILQ_ENTRY(test_command) next;
 187        const char *command;
 188        test_callback *callback;
 189};
 190
 191void add_test_command(struct test_command *t);
 192
 193/* Register a test function with its command string */
 194#define REGISTER_TEST_COMMAND(cmd, func) \
 195        static struct test_command test_struct_##cmd = { \
 196                .command = RTE_STR(cmd), \
 197                .callback = func, \
 198        }; \
 199        RTE_INIT(test_register_##cmd) \
 200        { \
 201                add_test_command(&test_struct_##cmd); \
 202        }
 203
 204#endif
 205