xref: /netbsd-src/external/bsd/jemalloc/dist/test/unit/test_hooks.c (revision 32d1c65c71fbdb65a012e8392a62a757dd6853e9)
1 #include "test/jemalloc_test.h"
2 
3 static bool hook_called = false;
4 
5 static void
6 hook() {
7 	hook_called = true;
8 }
9 
10 static int
11 func_to_hook(int arg1, int arg2) {
12 	return arg1 + arg2;
13 }
14 
15 #define func_to_hook JEMALLOC_TEST_HOOK(func_to_hook, test_hooks_libc_hook)
16 
17 TEST_BEGIN(unhooked_call) {
18 	test_hooks_libc_hook = NULL;
19 	hook_called = false;
20 	expect_d_eq(3, func_to_hook(1, 2), "Hooking changed return value.");
21 	expect_false(hook_called, "Nulling out hook didn't take.");
22 }
23 TEST_END
24 
25 TEST_BEGIN(hooked_call) {
26 	test_hooks_libc_hook = &hook;
27 	hook_called = false;
28 	expect_d_eq(3, func_to_hook(1, 2), "Hooking changed return value.");
29 	expect_true(hook_called, "Hook should have executed.");
30 }
31 TEST_END
32 
33 int
34 main(void) {
35 	return test(
36 	    unhooked_call,
37 	    hooked_call);
38 }
39