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).
13 struct oidmap_entry entry
;
14 char name
[FLEX_ARRAY
];
17 static const char *const key_val
[][2] = { { "11", "one" },
21 static void setup(void (*f
)(struct oidmap
*map
))
23 struct oidmap map
= OIDMAP_INIT
;
26 for (size_t i
= 0; i
< ARRAY_SIZE(key_val
); i
++){
27 struct test_entry
*entry
;
29 FLEX_ALLOC_STR(entry
, name
, key_val
[i
][1]);
30 if ((ret
= get_oid_arbitrary_hex(key_val
[i
][0], &entry
->entry
.oid
))) {
34 entry
= oidmap_put(&map
, entry
);
35 if (!check(entry
== NULL
))
44 static void t_replace(struct oidmap
*map
)
46 struct test_entry
*entry
, *prev
;
48 FLEX_ALLOC_STR(entry
, name
, "un");
49 if (get_oid_arbitrary_hex("11", &entry
->entry
.oid
))
51 prev
= oidmap_put(map
, entry
);
52 if (!check(prev
!= NULL
))
54 check_str(prev
->name
, "one");
57 FLEX_ALLOC_STR(entry
, name
, "deux");
58 if (get_oid_arbitrary_hex("22", &entry
->entry
.oid
))
60 prev
= oidmap_put(map
, entry
);
61 if (!check(prev
!= NULL
))
63 check_str(prev
->name
, "two");
67 static void t_get(struct oidmap
*map
)
69 struct test_entry
*entry
;
72 if (get_oid_arbitrary_hex("22", &oid
))
74 entry
= oidmap_get(map
, &oid
);
75 if (!check(entry
!= NULL
))
77 check_str(entry
->name
, "two");
79 if (get_oid_arbitrary_hex("44", &oid
))
81 check(oidmap_get(map
, &oid
) == NULL
);
83 if (get_oid_arbitrary_hex("11", &oid
))
85 entry
= oidmap_get(map
, &oid
);
86 if (!check(entry
!= NULL
))
88 check_str(entry
->name
, "one");
91 static void t_remove(struct oidmap
*map
)
93 struct test_entry
*entry
;
96 if (get_oid_arbitrary_hex("11", &oid
))
98 entry
= oidmap_remove(map
, &oid
);
99 if (!check(entry
!= NULL
))
101 check_str(entry
->name
, "one");
102 check(oidmap_get(map
, &oid
) == NULL
);
105 if (get_oid_arbitrary_hex("22", &oid
))
107 entry
= oidmap_remove(map
, &oid
);
108 if (!check(entry
!= NULL
))
110 check_str(entry
->name
, "two");
111 check(oidmap_get(map
, &oid
) == NULL
);
114 if (get_oid_arbitrary_hex("44", &oid
))
116 check(oidmap_remove(map
, &oid
) == NULL
);
119 static int key_val_contains(struct test_entry
*entry
, char seen
[])
121 for (size_t i
= 0; i
< ARRAY_SIZE(key_val
); i
++) {
122 struct object_id oid
;
124 if (get_oid_arbitrary_hex(key_val
[i
][0], &oid
))
127 if (oideq(&entry
->entry
.oid
, &oid
)) {
137 static void t_iterate(struct oidmap
*map
)
139 struct oidmap_iter iter
;
140 struct test_entry
*entry
;
141 char seen
[ARRAY_SIZE(key_val
)] = { 0 };
144 oidmap_iter_init(map
, &iter
);
145 while ((entry
= oidmap_iter_next(&iter
))) {
147 if (!check_int((ret
= key_val_contains(entry
, seen
)), ==, 0)) {
150 break; /* error message handled by get_oid_arbitrary_hex() */
152 test_msg("obtained entry was not given in the input\n"
153 " name: %s\n oid: %s\n",
154 entry
->name
, oid_to_hex(&entry
->entry
.oid
));
157 test_msg("duplicate entry detected\n"
158 " name: %s\n oid: %s\n",
159 entry
->name
, oid_to_hex(&entry
->entry
.oid
));
162 test_msg("BUG: invalid return value (%d) from key_val_contains()",
170 check_int(count
, ==, ARRAY_SIZE(key_val
));
171 check_int(hashmap_get_size(&map
->map
), ==, ARRAY_SIZE(key_val
));
174 int cmd_main(int argc UNUSED
, const char **argv UNUSED
)
176 TEST(setup(t_replace
), "replace works");
177 TEST(setup(t_get
), "get works");
178 TEST(setup(t_remove
), "remove works");
179 TEST(setup(t_iterate
), "iterate works");