xref: /netbsd-src/external/bsd/jemalloc.old/dist/test/unit/hooks.c (revision 8e33eff89e26cf71871ead62f0d5063e1313c33a)
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_HOOK(func_to_hook, hooks_libc_hook)
16 
17 TEST_BEGIN(unhooked_call) {
18 	hooks_libc_hook = NULL;
19 	hook_called = false;
20 	assert_d_eq(3, func_to_hook(1, 2), "Hooking changed return value.");
21 	assert_false(hook_called, "Nulling out hook didn't take.");
22 }
23 TEST_END
24 
25 TEST_BEGIN(hooked_call) {
26 	hooks_libc_hook = &hook;
27 	hook_called = false;
28 	assert_d_eq(3, func_to_hook(1, 2), "Hooking changed return value.");
29 	assert_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