Edit changelog a little for clarity and conciseness
[tor.git] / src / test / test_handles.c
blob536a478689f8ff3123fbf67665b35c4ffeb1b92c
1 /* Copyright (c) 2016, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #include "orconfig.h"
5 #include "test.h"
7 #include "util.h"
8 #include "handles.h"
10 typedef struct demo_t {
11 HANDLE_ENTRY(demo, demo_t);
12 int val;
13 } demo_t;
15 HANDLE_DECL(demo, demo_t, static)
16 HANDLE_IMPL(demo, demo_t, static)
18 static demo_t *
19 demo_new(int val)
21 demo_t *d = tor_malloc_zero(sizeof(demo_t));
22 d->val = val;
23 return d;
26 static void
27 demo_free(demo_t *d)
29 if (d == NULL)
30 return;
31 demo_handles_clear(d);
32 tor_free(d);
35 static void
36 test_handle_basic(void *arg)
38 (void) arg;
39 demo_t *d1 = NULL, *d2 = NULL;
40 demo_handle_t *wr1 = NULL, *wr2 = NULL, *wr3 = NULL, *wr4 = NULL;
42 d1 = demo_new(9000);
43 d2 = demo_new(9009);
45 wr1 = demo_handle_new(d1);
46 wr2 = demo_handle_new(d1);
47 wr3 = demo_handle_new(d1);
48 wr4 = demo_handle_new(d2);
50 tt_assert(wr1);
51 tt_assert(wr2);
52 tt_assert(wr3);
53 tt_assert(wr4);
55 tt_ptr_op(demo_handle_get(wr1), OP_EQ, d1);
56 tt_ptr_op(demo_handle_get(wr2), OP_EQ, d1);
57 tt_ptr_op(demo_handle_get(wr3), OP_EQ, d1);
58 tt_ptr_op(demo_handle_get(wr4), OP_EQ, d2);
60 demo_handle_free(wr1);
61 wr1 = NULL;
62 tt_ptr_op(demo_handle_get(wr2), OP_EQ, d1);
63 tt_ptr_op(demo_handle_get(wr3), OP_EQ, d1);
64 tt_ptr_op(demo_handle_get(wr4), OP_EQ, d2);
66 demo_free(d1);
67 d1 = NULL;
68 tt_ptr_op(demo_handle_get(wr2), OP_EQ, NULL);
69 tt_ptr_op(demo_handle_get(wr3), OP_EQ, NULL);
70 tt_ptr_op(demo_handle_get(wr4), OP_EQ, d2);
72 demo_handle_free(wr2);
73 wr2 = NULL;
74 tt_ptr_op(demo_handle_get(wr3), OP_EQ, NULL);
75 tt_ptr_op(demo_handle_get(wr4), OP_EQ, d2);
77 demo_handle_free(wr3);
78 wr3 = NULL;
79 done:
80 demo_handle_free(wr1);
81 demo_handle_free(wr2);
82 demo_handle_free(wr3);
83 demo_handle_free(wr4);
84 demo_free(d1);
85 demo_free(d2);
88 #define HANDLE_TEST(name, flags) \
89 { #name, test_handle_ ##name, (flags), NULL, NULL }
91 struct testcase_t handle_tests[] = {
92 HANDLE_TEST(basic, 0),
93 END_OF_TESTCASES