|
| 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 const char *const key_val[][2] = { { "11", "one" }, |
| 18 | + { "22", "two" }, |
| 19 | + { "33", "three" } }; |
| 20 | + |
| 21 | +static struct oidmap map; |
| 22 | + |
| 23 | +void test_oidmap__initialize(void) |
| 24 | +{ |
| 25 | + oidmap_init(&map, 0); |
| 26 | + |
| 27 | + for (size_t i = 0; i < ARRAY_SIZE(key_val); i++){ |
| 28 | + struct test_entry *entry; |
| 29 | + |
| 30 | + FLEX_ALLOC_STR(entry, name, key_val[i][1]); |
| 31 | + cl_parse_any_oid(key_val[i][0], &entry->entry.oid); |
| 32 | + cl_assert(oidmap_put(&map, entry) == NULL); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +void test_oidmap__cleanup(void) |
| 37 | +{ |
| 38 | + oidmap_free(&map, 1); |
| 39 | +} |
| 40 | + |
| 41 | +void test_oidmap__replace(void) |
| 42 | +{ |
| 43 | + struct test_entry *entry, *prev; |
| 44 | + |
| 45 | + FLEX_ALLOC_STR(entry, name, "un"); |
| 46 | + cl_parse_any_oid("11", &entry->entry.oid); |
| 47 | + prev = oidmap_put(&map, entry); |
| 48 | + cl_assert(prev != NULL); |
| 49 | + cl_assert_equal_s(prev->name, "one"); |
| 50 | + free(prev); |
| 51 | + |
| 52 | + FLEX_ALLOC_STR(entry, name, "deux"); |
| 53 | + cl_parse_any_oid("22", &entry->entry.oid); |
| 54 | + prev = oidmap_put(&map, entry); |
| 55 | + cl_assert(prev != NULL); |
| 56 | + cl_assert_equal_s(prev->name, "two"); |
| 57 | + free(prev); |
| 58 | +} |
| 59 | + |
| 60 | +void test_oidmap__get(void) |
| 61 | +{ |
| 62 | + struct test_entry *entry; |
| 63 | + struct object_id oid; |
| 64 | + |
| 65 | + cl_parse_any_oid("22", &oid); |
| 66 | + entry = oidmap_get(&map, &oid); |
| 67 | + cl_assert(entry != NULL); |
| 68 | + cl_assert_equal_s(entry->name, "two"); |
| 69 | + |
| 70 | + cl_parse_any_oid("44", &oid); |
| 71 | + cl_assert(oidmap_get(&map, &oid) == NULL); |
| 72 | + |
| 73 | + cl_parse_any_oid("11", &oid); |
| 74 | + entry = oidmap_get(&map, &oid); |
| 75 | + cl_assert(entry != NULL); |
| 76 | + cl_assert_equal_s(entry->name, "one"); |
| 77 | +} |
| 78 | + |
| 79 | +void test_oidmap__remove(void) |
| 80 | +{ |
| 81 | + struct test_entry *entry; |
| 82 | + struct object_id oid; |
| 83 | + |
| 84 | + cl_parse_any_oid("11", &oid); |
| 85 | + entry = oidmap_remove(&map, &oid); |
| 86 | + cl_assert(entry != NULL); |
| 87 | + cl_assert_equal_s(entry->name, "one"); |
| 88 | + cl_assert(oidmap_get(&map, &oid) == NULL); |
| 89 | + free(entry); |
| 90 | + |
| 91 | + cl_parse_any_oid("22", &oid); |
| 92 | + entry = oidmap_remove(&map, &oid); |
| 93 | + cl_assert(entry != NULL); |
| 94 | + cl_assert_equal_s(entry->name, "two"); |
| 95 | + cl_assert(oidmap_get(&map, &oid) == NULL); |
| 96 | + free(entry); |
| 97 | + |
| 98 | + cl_parse_any_oid("44", &oid); |
| 99 | + cl_assert(oidmap_remove(&map, &oid) == NULL); |
| 100 | +} |
| 101 | + |
| 102 | +static int key_val_contains(struct test_entry *entry, char seen[]) |
| 103 | +{ |
| 104 | + for (size_t i = 0; i < ARRAY_SIZE(key_val); i++) { |
| 105 | + struct object_id oid; |
| 106 | + |
| 107 | + cl_parse_any_oid(key_val[i][0], &oid); |
| 108 | + |
| 109 | + if (oideq(&entry->entry.oid, &oid)) { |
| 110 | + if (seen[i]) |
| 111 | + return 2; |
| 112 | + seen[i] = 1; |
| 113 | + return 0; |
| 114 | + } |
| 115 | + } |
| 116 | + return 1; |
| 117 | +} |
| 118 | + |
| 119 | +void test_oidmap__iterate(void) |
| 120 | +{ |
| 121 | + struct oidmap_iter iter; |
| 122 | + struct test_entry *entry; |
| 123 | + char seen[ARRAY_SIZE(key_val)] = { 0 }; |
| 124 | + int count = 0; |
| 125 | + |
| 126 | + oidmap_iter_init(&map, &iter); |
| 127 | + while ((entry = oidmap_iter_next(&iter))) { |
| 128 | + if (key_val_contains(entry, seen) != 0) { |
| 129 | + cl_failf("Unexpected entry: name = %s, oid = %s", |
| 130 | + entry->name, oid_to_hex(&entry->entry.oid)); |
| 131 | + } |
| 132 | + count++; |
| 133 | + } |
| 134 | + cl_assert_equal_i(count, ARRAY_SIZE(key_val)); |
| 135 | + cl_assert_equal_i(hashmap_get_size(&map.map), ARRAY_SIZE(key_val)); |
| 136 | +} |
0 commit comments