test_hooks.c 772 B

123456789101112131415161718192021222324252627282930313233343536373839
  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_TEST_HOOK(func_to_hook, test_hooks_libc_hook)
  12. TEST_BEGIN(unhooked_call) {
  13. test_hooks_libc_hook = NULL;
  14. hook_called = false;
  15. expect_d_eq(3, func_to_hook(1, 2), "Hooking changed return value.");
  16. expect_false(hook_called, "Nulling out hook didn't take.");
  17. }
  18. TEST_END
  19. TEST_BEGIN(hooked_call) {
  20. test_hooks_libc_hook = &hook;
  21. hook_called = false;
  22. expect_d_eq(3, func_to_hook(1, 2), "Hooking changed return value.");
  23. expect_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. }