| 1 |
// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
#ifndef DRGN_TEST_UTIL_H |
| 5 |
#define DRGN_TEST_UTIL_H |
| 6 |
|
| 7 |
#include <check.h> |
| 8 |
|
| 9 |
#include "../drgn_internal.h" |
| 10 |
#include "../pp.h" |
| 11 |
|
| 12 |
#define drgn_ck_err(err, code, message) drgn_ck_err_impl(err, PP_UNIQUE(_err), code, message) |
| 13 |
#define drgn_ck_err_impl(err, unique_err, _code, _message) do { \ |
| 14 |
struct drgn_error *unique_err = (err); \ |
| 15 |
ck_assert_msg(unique_err, "Assertion '%s != NULL' failed", #err); \ |
| 16 |
ck_assert_int_eq(unique_err->code, (_code)); \ |
| 17 |
ck_assert_str_eq(unique_err->message, (_message)); \ |
| 18 |
drgn_error_destroy(unique_err); \ |
| 19 |
} while (0) |
| 20 |
|
| 21 |
#define drgn_ck_no_err(err) drgn_ck_no_err_impl(err, PP_UNIQUE(_err)) |
| 22 |
#define drgn_ck_no_err_impl(err, unique_err) do { \ |
| 23 |
struct drgn_error *unique_err = (err); \ |
| 24 |
ck_assert_msg(!unique_err, "Assertion '!(%s)' failed: error: %s", #err, \ |
| 25 |
unique_err ? unique_err->message : ""); \ |
| 26 |
} while (0) |
| 27 |
|
| 28 |
// Copies of assert macros added in check 0.11.0. |
| 29 |
|
| 30 |
#ifndef ck_assert_ptr_null |
| 31 |
#ifndef _ck_assert_ptr_null |
| 32 |
#define _ck_assert_ptr_null(X, OP) do { \ |
| 33 |
const void* _ck_x = (X); \ |
| 34 |
ck_assert_msg(_ck_x OP NULL, \ |
| 35 |
"Assertion '%s' failed: %s == %#lx", \ |
| 36 |
#X" "#OP" NULL", \ |
| 37 |
#X, (unsigned long)(uintptr_t)_ck_x); \ |
| 38 |
} while (0) |
| 39 |
#endif |
| 40 |
|
| 41 |
#define ck_assert_ptr_null(X) _ck_assert_ptr_null(X, ==) |
| 42 |
#endif |
| 43 |
|
| 44 |
#ifndef ck_assert_ptr_nonnull |
| 45 |
#define ck_assert_ptr_nonnull(X) _ck_assert_ptr_null(X, !=) |
| 46 |
#endif |
| 47 |
|
| 48 |
#ifndef ck_assert_mem_eq |
| 49 |
#ifndef CK_MAX_ASSERT_MEM_PRINT_SIZE |
| 50 |
#define CK_MAX_ASSERT_MEM_PRINT_SIZE 64 |
| 51 |
#endif |
| 52 |
|
| 53 |
#ifndef _ck_assert_mem |
| 54 |
#define _ck_assert_mem(X, OP, Y, L) do { \ |
| 55 |
const uint8_t* _ck_x = (const uint8_t*)(X); \ |
| 56 |
const uint8_t* _ck_y = (const uint8_t*)(Y); \ |
| 57 |
size_t _ck_l = (L); \ |
| 58 |
char _ck_x_str[CK_MAX_ASSERT_MEM_PRINT_SIZE * 2 + 1]; \ |
| 59 |
char _ck_y_str[CK_MAX_ASSERT_MEM_PRINT_SIZE * 2 + 1]; \ |
| 60 |
static const char _ck_hexdigits[] = "0123456789abcdef"; \ |
| 61 |
size_t _ck_i; \ |
| 62 |
size_t _ck_maxl = (_ck_l > CK_MAX_ASSERT_MEM_PRINT_SIZE) ? CK_MAX_ASSERT_MEM_PRINT_SIZE : _ck_l; \ |
| 63 |
for (_ck_i = 0; _ck_i < _ck_maxl; _ck_i++) { \ |
| 64 |
_ck_x_str[_ck_i * 2 ] = _ck_hexdigits[(_ck_x[_ck_i] >> 4) & 0xF]; \ |
| 65 |
_ck_y_str[_ck_i * 2 ] = _ck_hexdigits[(_ck_y[_ck_i] >> 4) & 0xF]; \ |
| 66 |
_ck_x_str[_ck_i * 2 + 1] = _ck_hexdigits[_ck_x[_ck_i] & 0xF]; \ |
| 67 |
_ck_y_str[_ck_i * 2 + 1] = _ck_hexdigits[_ck_y[_ck_i] & 0xF]; \ |
| 68 |
} \ |
| 69 |
_ck_x_str[_ck_i * 2] = 0; \ |
| 70 |
_ck_y_str[_ck_i * 2] = 0; \ |
| 71 |
if (_ck_maxl != _ck_l) { \ |
| 72 |
_ck_x_str[_ck_i * 2 - 2] = '.'; \ |
| 73 |
_ck_y_str[_ck_i * 2 - 2] = '.'; \ |
| 74 |
_ck_x_str[_ck_i * 2 - 1] = '.'; \ |
| 75 |
_ck_y_str[_ck_i * 2 - 1] = '.'; \ |
| 76 |
} \ |
| 77 |
ck_assert_msg(0 OP memcmp(_ck_y, _ck_x, _ck_l), \ |
| 78 |
"Assertion '%s' failed: %s == \"%s\", %s == \"%s\"", #X" "#OP" "#Y, #X, _ck_x_str, #Y, _ck_y_str); \ |
| 79 |
} while (0) |
| 80 |
#endif |
| 81 |
|
| 82 |
#define ck_assert_mem_eq(X, Y, L) _ck_assert_mem(X, ==, Y, L) |
| 83 |
#endif |
| 84 |
|
| 85 |
#ifndef ck_assert_mem_ne |
| 86 |
#define ck_assert_mem_ne(X, Y, L) _ck_assert_mem(X, !=, Y, L) |
| 87 |
#endif |
| 88 |
|
| 89 |
#ifndef ck_assert_mem_lt |
| 90 |
#define ck_assert_mem_lt(X, Y, L) _ck_assert_mem(X, <, Y, L) |
| 91 |
#endif |
| 92 |
|
| 93 |
#ifndef ck_assert_mem_le |
| 94 |
#define ck_assert_mem_le(X, Y, L) _ck_assert_mem(X, <=, Y, L) |
| 95 |
#endif |
| 96 |
|
| 97 |
#ifndef ck_assert_mem_gt |
| 98 |
#define ck_assert_mem_gt(X, Y, L) _ck_assert_mem(X, >, Y, L) |
| 99 |
#endif |
| 100 |
|
| 101 |
#ifndef ck_assert_mem_ge |
| 102 |
#define ck_assert_mem_ge(X, Y, L) _ck_assert_mem(X, >=, Y, L) |
| 103 |
#endif |
| 104 |
|
| 105 |
#endif /* DRGN_TEST_UTIL_H */ |