ticker.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "test/jemalloc_test.h"
  2. #include "jemalloc/internal/ticker.h"
  3. TEST_BEGIN(test_ticker_tick) {
  4. #define NREPS 2
  5. #define NTICKS 3
  6. ticker_t ticker;
  7. int32_t i, j;
  8. ticker_init(&ticker, NTICKS);
  9. for (i = 0; i < NREPS; i++) {
  10. for (j = 0; j < NTICKS; j++) {
  11. assert_u_eq(ticker_read(&ticker), NTICKS - j,
  12. "Unexpected ticker value (i=%d, j=%d)", i, j);
  13. assert_false(ticker_tick(&ticker),
  14. "Unexpected ticker fire (i=%d, j=%d)", i, j);
  15. }
  16. assert_u32_eq(ticker_read(&ticker), 0,
  17. "Expected ticker depletion");
  18. assert_true(ticker_tick(&ticker),
  19. "Expected ticker fire (i=%d)", i);
  20. assert_u32_eq(ticker_read(&ticker), NTICKS,
  21. "Expected ticker reset");
  22. }
  23. #undef NTICKS
  24. }
  25. TEST_END
  26. TEST_BEGIN(test_ticker_ticks) {
  27. #define NTICKS 3
  28. ticker_t ticker;
  29. ticker_init(&ticker, NTICKS);
  30. assert_u_eq(ticker_read(&ticker), NTICKS, "Unexpected ticker value");
  31. assert_false(ticker_ticks(&ticker, NTICKS), "Unexpected ticker fire");
  32. assert_u_eq(ticker_read(&ticker), 0, "Unexpected ticker value");
  33. assert_true(ticker_ticks(&ticker, NTICKS), "Expected ticker fire");
  34. assert_u_eq(ticker_read(&ticker), NTICKS, "Unexpected ticker value");
  35. assert_true(ticker_ticks(&ticker, NTICKS + 1), "Expected ticker fire");
  36. assert_u_eq(ticker_read(&ticker), NTICKS, "Unexpected ticker value");
  37. #undef NTICKS
  38. }
  39. TEST_END
  40. TEST_BEGIN(test_ticker_copy) {
  41. #define NTICKS 3
  42. ticker_t ta, tb;
  43. ticker_init(&ta, NTICKS);
  44. ticker_copy(&tb, &ta);
  45. assert_u_eq(ticker_read(&tb), NTICKS, "Unexpected ticker value");
  46. assert_true(ticker_ticks(&tb, NTICKS + 1), "Expected ticker fire");
  47. assert_u_eq(ticker_read(&tb), NTICKS, "Unexpected ticker value");
  48. ticker_tick(&ta);
  49. ticker_copy(&tb, &ta);
  50. assert_u_eq(ticker_read(&tb), NTICKS - 1, "Unexpected ticker value");
  51. assert_true(ticker_ticks(&tb, NTICKS), "Expected ticker fire");
  52. assert_u_eq(ticker_read(&tb), NTICKS, "Unexpected ticker value");
  53. #undef NTICKS
  54. }
  55. TEST_END
  56. int
  57. main(void) {
  58. return test(
  59. test_ticker_tick,
  60. test_ticker_ticks,
  61. test_ticker_copy);
  62. }