test_handles.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Copyright (c) 2016-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #include "test.h"
  5. #include "util.h"
  6. #include "handles.h"
  7. typedef struct demo_t {
  8. HANDLE_ENTRY(demo, demo_t);
  9. int val;
  10. } demo_t;
  11. HANDLE_DECL(demo, demo_t, static)
  12. #define demo_handle_free(h) \
  13. FREE_AND_NULL(demo_handle_t, demo_handle_free_, (h))
  14. HANDLE_IMPL(demo, demo_t, static)
  15. static demo_t *
  16. demo_new(int val)
  17. {
  18. demo_t *d = tor_malloc_zero(sizeof(demo_t));
  19. d->val = val;
  20. return d;
  21. }
  22. static void
  23. demo_free(demo_t *d)
  24. {
  25. if (d == NULL)
  26. return;
  27. demo_handles_clear(d);
  28. tor_free(d);
  29. }
  30. static void
  31. test_handle_basic(void *arg)
  32. {
  33. (void) arg;
  34. demo_t *d1 = NULL, *d2 = NULL;
  35. demo_handle_t *wr1 = NULL, *wr2 = NULL, *wr3 = NULL, *wr4 = NULL;
  36. d1 = demo_new(9000);
  37. d2 = demo_new(9009);
  38. wr1 = demo_handle_new(d1);
  39. wr2 = demo_handle_new(d1);
  40. wr3 = demo_handle_new(d1);
  41. wr4 = demo_handle_new(d2);
  42. tt_assert(wr1);
  43. tt_assert(wr2);
  44. tt_assert(wr3);
  45. tt_assert(wr4);
  46. tt_ptr_op(demo_handle_get(wr1), OP_EQ, d1);
  47. tt_ptr_op(demo_handle_get(wr2), OP_EQ, d1);
  48. tt_ptr_op(demo_handle_get(wr3), OP_EQ, d1);
  49. tt_ptr_op(demo_handle_get(wr4), OP_EQ, d2);
  50. demo_handle_free(wr1);
  51. wr1 = NULL;
  52. tt_ptr_op(demo_handle_get(wr2), OP_EQ, d1);
  53. tt_ptr_op(demo_handle_get(wr3), OP_EQ, d1);
  54. tt_ptr_op(demo_handle_get(wr4), OP_EQ, d2);
  55. demo_free(d1);
  56. d1 = NULL;
  57. tt_ptr_op(demo_handle_get(wr2), OP_EQ, NULL);
  58. tt_ptr_op(demo_handle_get(wr3), OP_EQ, NULL);
  59. tt_ptr_op(demo_handle_get(wr4), OP_EQ, d2);
  60. demo_handle_free(wr2);
  61. wr2 = NULL;
  62. tt_ptr_op(demo_handle_get(wr3), OP_EQ, NULL);
  63. tt_ptr_op(demo_handle_get(wr4), OP_EQ, d2);
  64. demo_handle_free(wr3);
  65. wr3 = NULL;
  66. done:
  67. demo_handle_free(wr1);
  68. demo_handle_free(wr2);
  69. demo_handle_free(wr3);
  70. demo_handle_free(wr4);
  71. demo_free(d1);
  72. demo_free(d2);
  73. }
  74. #define HANDLE_TEST(name, flags) \
  75. { #name, test_handle_ ##name, (flags), NULL, NULL }
  76. struct testcase_t handle_tests[] = {
  77. HANDLE_TEST(basic, 0),
  78. END_OF_TESTCASES
  79. };