run-debug.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #define DEBUG 1
  2. #include <ccan/likely/likely.c>
  3. #include <ccan/likely/likely.h>
  4. #include <ccan/tap/tap.h>
  5. #include <stdlib.h>
  6. static bool one_seems_likely(unsigned int val)
  7. {
  8. if (likely(val == 1))
  9. return true;
  10. return false;
  11. }
  12. static bool one_seems_unlikely(unsigned int val)
  13. {
  14. if (unlikely(val == 1))
  15. return true;
  16. return false;
  17. }
  18. static bool likely_one_unlikely_two(unsigned int val1, unsigned int val2)
  19. {
  20. /* Same line, check we don't get confused! */
  21. if (likely(val1 == 1) && unlikely(val2 == 2))
  22. return true;
  23. return false;
  24. }
  25. int main(int argc, char *argv[])
  26. {
  27. const char *bad;
  28. plan_tests(13);
  29. /* Correct guesses. */
  30. one_seems_likely(1);
  31. ok1(likely_stats(0, 90) == NULL);
  32. one_seems_unlikely(2);
  33. ok1(likely_stats(0, 90) == NULL);
  34. /* Incorrect guesses. */
  35. one_seems_likely(0);
  36. one_seems_likely(2);
  37. /* Hasn't been hit 4 times, so this fails */
  38. ok1(!likely_stats(4, 90));
  39. bad = likely_stats(3, 90);
  40. ok(strends(bad, "run-debug.c:9:likely(val == 1) correct 33% (1/3)"),
  41. "likely_stats returned %s", bad);
  42. /* Nothing else above 90% */
  43. ok1(!likely_stats(0, 90));
  44. /* This should get everything. */
  45. bad = likely_stats(0, 100);
  46. ok(strends(bad, "run-debug.c:16:unlikely(val == 1) correct 100% (1/1)"),
  47. "likely_stats returned %s", bad);
  48. /* Nothing left (table is actually cleared) */
  49. ok1(!likely_stats(0, 100));
  50. /* Make sure unlikely works */
  51. one_seems_unlikely(0);
  52. one_seems_unlikely(2);
  53. one_seems_unlikely(1);
  54. bad = likely_stats(0, 90);
  55. ok(strends(bad, "run-debug.c:16:unlikely(val == 1) correct 66% (2/3)"),
  56. "likely_stats returned %s", bad);
  57. ok1(!likely_stats(0, 100));
  58. likely_one_unlikely_two(1, 1);
  59. likely_one_unlikely_two(1, 1);
  60. likely_one_unlikely_two(1, 1);
  61. ok1(!likely_stats(0, 90));
  62. likely_one_unlikely_two(1, 2);
  63. bad = likely_stats(0, 90);
  64. ok(strends(bad, "run-debug.c:24:unlikely(val2 == 2) correct 75% (3/4)"),
  65. "likely_stats returned %s", bad);
  66. bad = likely_stats(0, 100);
  67. ok(strends(bad, "run-debug.c:24:likely(val1 == 1) correct 100% (4/4)"),
  68. "likely_stats returned %s", bad);
  69. ok1(!likely_stats(0, 100));
  70. exit(exit_status());
  71. }