hooks.c 752 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include "test/jemalloc_test.h"
  2. static bool hook_called = false;
  3. static void
  4. hook() {
  5. hook_called = true;
  6. }
  7. static int
  8. func_to_hook(int arg1, int arg2) {
  9. return arg1 + arg2;
  10. }
  11. #define func_to_hook JEMALLOC_HOOK(func_to_hook, hooks_libc_hook)
  12. TEST_BEGIN(unhooked_call) {
  13. hooks_libc_hook = NULL;
  14. hook_called = false;
  15. assert_d_eq(3, func_to_hook(1, 2), "Hooking changed return value.");
  16. assert_false(hook_called, "Nulling out hook didn't take.");
  17. }
  18. TEST_END
  19. TEST_BEGIN(hooked_call) {
  20. hooks_libc_hook = &hook;
  21. hook_called = false;
  22. assert_d_eq(3, func_to_hook(1, 2), "Hooking changed return value.");
  23. assert_true(hook_called, "Hook should have executed.");
  24. }
  25. TEST_END
  26. int
  27. main(void) {
  28. return test(
  29. unhooked_call,
  30. hooked_call);
  31. }