test_handles.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. HANDLE_IMPL(demo, demo_t, static)
  13. static demo_t *
  14. demo_new(int val)
  15. {
  16. demo_t *d = tor_malloc_zero(sizeof(demo_t));
  17. d->val = val;
  18. return d;
  19. }
  20. static void
  21. demo_free(demo_t *d)
  22. {
  23. if (d == NULL)
  24. return;
  25. demo_handles_clear(d);
  26. tor_free(d);
  27. }
  28. static void
  29. test_handle_basic(void *arg)
  30. {
  31. (void) arg;
  32. demo_t *d1 = NULL, *d2 = NULL;
  33. demo_handle_t *wr1 = NULL, *wr2 = NULL, *wr3 = NULL, *wr4 = NULL;
  34. d1 = demo_new(9000);
  35. d2 = demo_new(9009);
  36. wr1 = demo_handle_new(d1);
  37. wr2 = demo_handle_new(d1);
  38. wr3 = demo_handle_new(d1);
  39. wr4 = demo_handle_new(d2);
  40. tt_assert(wr1);
  41. tt_assert(wr2);
  42. tt_assert(wr3);
  43. tt_assert(wr4);
  44. tt_ptr_op(demo_handle_get(wr1), OP_EQ, d1);
  45. tt_ptr_op(demo_handle_get(wr2), OP_EQ, d1);
  46. tt_ptr_op(demo_handle_get(wr3), OP_EQ, d1);
  47. tt_ptr_op(demo_handle_get(wr4), OP_EQ, d2);
  48. demo_handle_free(wr1);
  49. wr1 = NULL;
  50. tt_ptr_op(demo_handle_get(wr2), OP_EQ, d1);
  51. tt_ptr_op(demo_handle_get(wr3), OP_EQ, d1);
  52. tt_ptr_op(demo_handle_get(wr4), OP_EQ, d2);
  53. demo_free(d1);
  54. d1 = NULL;
  55. tt_ptr_op(demo_handle_get(wr2), OP_EQ, NULL);
  56. tt_ptr_op(demo_handle_get(wr3), OP_EQ, NULL);
  57. tt_ptr_op(demo_handle_get(wr4), OP_EQ, d2);
  58. demo_handle_free(wr2);
  59. wr2 = NULL;
  60. tt_ptr_op(demo_handle_get(wr3), OP_EQ, NULL);
  61. tt_ptr_op(demo_handle_get(wr4), OP_EQ, d2);
  62. demo_handle_free(wr3);
  63. wr3 = NULL;
  64. done:
  65. demo_handle_free(wr1);
  66. demo_handle_free(wr2);
  67. demo_handle_free(wr3);
  68. demo_handle_free(wr4);
  69. demo_free(d1);
  70. demo_free(d2);
  71. }
  72. #define HANDLE_TEST(name, flags) \
  73. { #name, test_handle_ ##name, (flags), NULL, NULL }
  74. struct testcase_t handle_tests[] = {
  75. HANDLE_TEST(basic, 0),
  76. END_OF_TESTCASES
  77. };