Raw
1 #include "unit-test.h"
2 #include "lib-oid.h"
3 #include "oidmap.h"
4 #include "hash.h"
5 #include "hex.h"
6
7 /*
8 * Elements we will put in oidmap structs are made of a key: the entry.oid
9 * field, which is of type struct object_id, and a value: the name field (could
10 * be a refname for example).
11 */
12 struct test_entry {
13 struct oidmap_entry entry;
14 char name[FLEX_ARRAY];
15 };
16
17 static int freed;
18
19 static void test_free_fn(void *p) {
20 freed++;
21 free(p);
22 }
23
24 static const char *const key_val[][2] = { { "11", "one" },
25 { "22", "two" },
26 { "33", "three" } };
27
28 static struct oidmap map;
29
30 void test_oidmap__initialize(void)
31 {
32 oidmap_init(&map, 0);
33
34 for (size_t i = 0; i < ARRAY_SIZE(key_val); i++){
35 struct test_entry *entry;
36
37 FLEX_ALLOC_STR(entry, name, key_val[i][1]);
38 cl_parse_any_oid(key_val[i][0], &entry->entry.oid);
39 cl_assert(oidmap_put(&map, entry) == NULL);
40 }
41 }
42
43 void test_oidmap__cleanup(void)
44 {
45 oidmap_clear(&map, 1);
46 }
47
48 void test_oidmap__replace(void)
49 {
50 struct test_entry *entry, *prev;
51
52 FLEX_ALLOC_STR(entry, name, "un");
53 cl_parse_any_oid("11", &entry->entry.oid);
54 prev = oidmap_put(&map, entry);
55 cl_assert(prev != NULL);
56 cl_assert_equal_s(prev->name, "one");
57 free(prev);
58
59 FLEX_ALLOC_STR(entry, name, "deux");
60 cl_parse_any_oid("22", &entry->entry.oid);
61 prev = oidmap_put(&map, entry);
62 cl_assert(prev != NULL);
63 cl_assert_equal_s(prev->name, "two");
64 free(prev);
65 }
66
67 void test_oidmap__get(void)
68 {
69 struct test_entry *entry;
70 struct object_id oid;
71
72 cl_parse_any_oid("22", &oid);
73 entry = oidmap_get(&map, &oid);
74 cl_assert(entry != NULL);
75 cl_assert_equal_s(entry->name, "two");
76
77 cl_parse_any_oid("44", &oid);
78 cl_assert(oidmap_get(&map, &oid) == NULL);
79
80 cl_parse_any_oid("11", &oid);
81 entry = oidmap_get(&map, &oid);
82 cl_assert(entry != NULL);
83 cl_assert_equal_s(entry->name, "one");
84 }
85
86 void test_oidmap__remove(void)
87 {
88 struct test_entry *entry;
89 struct object_id oid;
90
91 cl_parse_any_oid("11", &oid);
92 entry = oidmap_remove(&map, &oid);
93 cl_assert(entry != NULL);
94 cl_assert_equal_s(entry->name, "one");
95 cl_assert(oidmap_get(&map, &oid) == NULL);
96 free(entry);
97
98 cl_parse_any_oid("22", &oid);
99 entry = oidmap_remove(&map, &oid);
100 cl_assert(entry != NULL);
101 cl_assert_equal_s(entry->name, "two");
102 cl_assert(oidmap_get(&map, &oid) == NULL);
103 free(entry);
104
105 cl_parse_any_oid("44", &oid);
106 cl_assert(oidmap_remove(&map, &oid) == NULL);
107 }
108
109 static int key_val_contains(struct test_entry *entry, char seen[])
110 {
111 for (size_t i = 0; i < ARRAY_SIZE(key_val); i++) {
112 struct object_id oid;
113
114 cl_parse_any_oid(key_val[i][0], &oid);
115
116 if (oideq(&entry->entry.oid, &oid)) {
117 if (seen[i])
118 return 2;
119 seen[i] = 1;
120 return 0;
121 }
122 }
123 return 1;
124 }
125
126 void test_oidmap__iterate(void)
127 {
128 struct oidmap_iter iter;
129 struct test_entry *entry;
130 char seen[ARRAY_SIZE(key_val)] = { 0 };
131 int count = 0;
132
133 oidmap_iter_init(&map, &iter);
134 while ((entry = oidmap_iter_next(&iter))) {
135 if (key_val_contains(entry, seen) != 0) {
136 cl_failf("Unexpected entry: name = %s, oid = %s",
137 entry->name, oid_to_hex(&entry->entry.oid));
138 }
139 count++;
140 }
141 cl_assert_equal_i(count, ARRAY_SIZE(key_val));
142 cl_assert_equal_i(hashmap_get_size(&map.map), ARRAY_SIZE(key_val));
143 }
144
145 void test_oidmap__clear_without_free_callback(void)
146 {
147 struct oidmap local_map = OIDMAP_INIT;
148 struct test_entry *entry;
149
150 freed = 0;
151
152 FLEX_ALLOC_STR(entry, name, "one");
153 cl_parse_any_oid("11", &entry->entry.oid);
154 cl_assert(oidmap_put(&local_map, entry) == NULL);
155
156 oidmap_clear_with_free(&local_map, NULL);
157
158 cl_assert_equal_i(freed, 0);
159
160 free(entry);
161 }
162
163 void test_oidmap__clear_with_free_callback(void)
164 {
165 struct oidmap local_map = OIDMAP_INIT;
166 struct test_entry *entry;
167
168 freed = 0;
169
170 FLEX_ALLOC_STR(entry, name, "one");
171 cl_parse_any_oid("11", &entry->entry.oid);
172 cl_assert(oidmap_put(&local_map, entry) == NULL);
173
174 oidmap_clear_with_free(&local_map, test_free_fn);
175
176 cl_assert_equal_i(freed, 1);
177 }