run.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <ccan/talloc_link/talloc_link.h>
  2. #include <ccan/tap/tap.h>
  3. #include <ccan/talloc_link/talloc_link.c>
  4. #include <stdlib.h>
  5. #include <err.h>
  6. static unsigned int destroy_count = 0;
  7. static int destroy_obj(void *obj)
  8. {
  9. destroy_count++;
  10. return 0;
  11. }
  12. int main(int argc, char *argv[])
  13. {
  14. void *obj, *p1, *p2, *p3;
  15. plan_tests(16);
  16. talloc_enable_leak_report();
  17. /* Single parent case. */
  18. p1 = talloc(NULL, char);
  19. obj = talloc_linked(p1, talloc(NULL, char));
  20. talloc_set_destructor(obj, destroy_obj);
  21. ok(destroy_count == 0, "destroy_count = %u", destroy_count);
  22. talloc_free(p1);
  23. ok(destroy_count == 1, "destroy_count = %u", destroy_count);
  24. /* Dual parent case. */
  25. p1 = talloc(NULL, char);
  26. obj = talloc_linked(p1, talloc(NULL, char));
  27. talloc_set_destructor(obj, destroy_obj);
  28. p2 = talloc(NULL, char);
  29. talloc_link(p2, obj);
  30. talloc_free(p1);
  31. ok(destroy_count == 1, "destroy_count = %u", destroy_count);
  32. talloc_free(p2);
  33. ok(destroy_count == 2, "destroy_count = %u", destroy_count);
  34. /* Triple parent case. */
  35. p1 = talloc(NULL, char);
  36. obj = talloc_linked(p1, talloc(NULL, char));
  37. talloc_set_destructor(obj, destroy_obj);
  38. p2 = talloc(NULL, char);
  39. p3 = talloc(NULL, char);
  40. talloc_link(p2, obj);
  41. talloc_link(p3, obj);
  42. talloc_free(p1);
  43. ok(destroy_count == 2, "destroy_count = %u", destroy_count);
  44. talloc_free(p2);
  45. ok(destroy_count == 2, "destroy_count = %u", destroy_count);
  46. talloc_free(p3);
  47. ok(destroy_count == 3, "destroy_count = %u", destroy_count);
  48. /* Single delink case. */
  49. p1 = talloc(NULL, char);
  50. obj = talloc_linked(p1, talloc(NULL, char));
  51. talloc_set_destructor(obj, destroy_obj);
  52. ok(destroy_count == 3, "destroy_count = %u", destroy_count);
  53. talloc_delink(p1, obj);
  54. ok(destroy_count == 4, "destroy_count = %u", destroy_count);
  55. talloc_free(p1);
  56. /* Double delink case. */
  57. p1 = talloc(NULL, char);
  58. obj = talloc_linked(p1, talloc(NULL, char));
  59. talloc_set_destructor(obj, destroy_obj);
  60. p2 = talloc(NULL, char);
  61. talloc_link(p2, obj);
  62. talloc_delink(p1, obj);
  63. ok(destroy_count == 4, "destroy_count = %u", destroy_count);
  64. talloc_delink(p2, obj);
  65. ok(destroy_count == 5, "destroy_count = %u", destroy_count);
  66. talloc_free(p1);
  67. talloc_free(p2);
  68. /* Delink and free. */
  69. p1 = talloc(NULL, char);
  70. obj = talloc_linked(p1, talloc(NULL, char));
  71. talloc_set_destructor(obj, destroy_obj);
  72. p2 = talloc(NULL, char);
  73. talloc_link(p2, obj);
  74. talloc_delink(p1, obj);
  75. ok(destroy_count == 5, "destroy_count = %u", destroy_count);
  76. talloc_free(p2);
  77. ok(destroy_count == 6, "destroy_count = %u", destroy_count);
  78. talloc_free(p1);
  79. /* Free and delink. */
  80. p1 = talloc(NULL, char);
  81. obj = talloc_linked(p1, talloc(NULL, char));
  82. talloc_set_destructor(obj, destroy_obj);
  83. p2 = talloc(NULL, char);
  84. talloc_link(p2, obj);
  85. talloc_free(p1);
  86. ok(destroy_count == 6, "destroy_count = %u", destroy_count);
  87. talloc_delink(p2, obj);
  88. ok(destroy_count == 7, "destroy_count = %u", destroy_count);
  89. talloc_free(p2);
  90. /* No leaks? */
  91. ok1(talloc_total_size(NULL) == 0);
  92. return exit_status();
  93. }