-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit_test.h
104 lines (84 loc) · 3.05 KB
/
unit_test.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* SPDX-FileCopyrightText: 2025 Eli Array Minkoff
*
* SPDX-License-Identifier: GPL-3.0-only */
#ifndef BFC_UNIT_TEST_H
#define BFC_UNIT_TEST_H 1
/* C99 */
#include <limits.h>
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
/* libLLVM */
#include <llvm-c/Disassembler.h> /* IWYU pragma: export */
/* CUNIT */
#include <CUnit/CUnit.h> /* IWYU pragma: export */
/* internal */
#include "err.h"
#include "types.h"
#ifdef UNIT_TEST_C
#define TEST_GLOBAL(decl) decl
#else /* UNIT_TEST_C */
#define TEST_GLOBAL(decl) extern decl
#endif /* UNIT_TEST_C */
typedef LLVMDisasmContextRef disasm_ref;
/* __BACKENDS__ add a declaration of the disasm_ref here */
TEST_GLOBAL(disasm_ref ARM64_DIS);
TEST_GLOBAL(disasm_ref RISCV64_DIS);
TEST_GLOBAL(disasm_ref S390X_DIS);
TEST_GLOBAL(disasm_ref X86_64_DIS);
TEST_GLOBAL(bf_err_id current_err);
enum test_status {
TEST_SET = -1,
NOT_TESTING = 0,
TEST_INTERCEPT = 1,
};
TEST_GLOBAL(enum test_status testing_err);
TEST_GLOBAL(jmp_buf etest_stack);
/* disassemble the contents of bytes, and return a sized_buf containing the
* diassembly - instructions are separated by newlines, and the disassembly as a
* whole is null-terminated. If any the provided bytes is unable to be fully
* disassembled, it returns a sized_buf with sz and capacity set to zero, and
* buf set to NULL.
*
* `bytes->sz` is set to zero by this process, but the allocation of
* `bytes->buf` is left as-is, so it can be reused. */
bool disassemble(disasm_ref ref, sized_buf *bytes, sized_buf *disasm);
/* utility macro to test if a sized_buf contains the expected disassembly.
* Clears both sb and dis, leaving the allocation behind for reuse if needed */
#define DISASM_TEST(code, dis, expected) \
if (disassemble(REF, &code, &dis)) { \
CU_ASSERT_STRING_EQUAL(dis.buf, expected); \
if (strcmp(dis.buf, expected)) { \
fprintf( \
stderr, \
"\n\n### EXPECTED ###\n%s\n\n### ACTUAL ###\n%s\n", \
expected, \
dis.buf \
); \
} \
} else { \
CU_FAIL("Failed to decompile bytes!"); \
}
/* utility macro to abort on CUnit error after running expr */
#define BF_ERRCHECKED(expr) \
expr; \
if (CU_get_error()) { \
fprintf(stderr, "%s\n", CU_get_error_msg()); \
exit(EXIT_FAILURE); \
}
/* utility macro to set up a CUnit suite with the current file name */
#define INIT_SUITE(suite_var) \
BF_ERRCHECKED(suite_var = CU_add_suite(__FILE__, NULL, NULL))
/* simple self-explanatory BF_ERRCHECKED wrapper around CU_ADD_TEST */
#define ADD_TEST(suite, test) BF_ERRCHECKED(CU_ADD_TEST(suite, test))
CU_pSuite register_util_tests(void);
CU_pSuite register_serialize_tests(void);
CU_pSuite register_optimize_tests(void);
CU_pSuite register_err_tests(void);
CU_pSuite register_compile_tests(void);
/* __BACKENDS__ add your test suite here */
CU_pSuite register_arm64_tests(void);
CU_pSuite register_riscv64_tests(void);
CU_pSuite register_s390x_tests(void);
CU_pSuite register_x86_64_tests(void);
#endif /* BFC_UNIT_TEST_H */