Hash :
2e0a3048
Author :
Date :
2019-01-23T10:48:55
oidmap: introduce high-level setter for key/value pairs Currently, one would use either `git_oidmap_insert` to insert key/value pairs into a map or `git_oidmap_put` to insert a key only. These function have historically been macros, which is why their syntax is kind of weird: instead of returning an error code directly, they instead have to be passed a pointer to where the return value shall be stored. This does not match libgit2's common idiom of directly returning error codes.Furthermore, `git_oidmap_put` is tightly coupled with implementation details of the map as it exposes the index of inserted entries. Introduce a new function `git_oidmap_set`, which takes as parameters the map, key and value and directly returns an error code. Convert all trivial callers of `git_oidmap_insert` and `git_oidmap_put` to make use of it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
#include "clar_libgit2.h"
#include "oidmap.h"
typedef struct {
git_oid oid;
size_t extra;
} oidmap_item;
#define NITEMS 0x0fff
void test_core_oidmap__basic(void)
{
git_oidmap *map;
oidmap_item items[NITEMS];
uint32_t i, j;
for (i = 0; i < NITEMS; ++i) {
items[i].extra = i;
for (j = 0; j < GIT_OID_RAWSZ / 4; ++j) {
items[i].oid.id[j * 4 ] = (unsigned char)i;
items[i].oid.id[j * 4 + 1] = (unsigned char)(i >> 8);
items[i].oid.id[j * 4 + 2] = (unsigned char)(i >> 16);
items[i].oid.id[j * 4 + 3] = (unsigned char)(i >> 24);
}
}
cl_git_pass(git_oidmap_new(&map));
for (i = 0; i < NITEMS; ++i) {
size_t pos;
int ret;
pos = git_oidmap_lookup_index(map, &items[i].oid);
cl_assert(!git_oidmap_valid_index(map, pos));
pos = git_oidmap_put(map, &items[i].oid, &ret);
cl_assert(ret != 0);
git_oidmap_set_value_at(map, pos, &items[i]);
}
for (i = 0; i < NITEMS; ++i) {
size_t pos;
pos = git_oidmap_lookup_index(map, &items[i].oid);
cl_assert(git_oidmap_valid_index(map, pos));
cl_assert_equal_p(git_oidmap_value_at(map, pos), &items[i]);
}
git_oidmap_free(map);
}
void test_core_oidmap__hash_collision(void)
{
git_oidmap *map;
oidmap_item items[NITEMS];
uint32_t i, j;
for (i = 0; i < NITEMS; ++i) {
uint32_t segment = i / 8;
int modi = i - (segment * 8);
items[i].extra = i;
for (j = 0; j < GIT_OID_RAWSZ / 4; ++j) {
items[i].oid.id[j * 4 ] = (unsigned char)modi;
items[i].oid.id[j * 4 + 1] = (unsigned char)(modi >> 8);
items[i].oid.id[j * 4 + 2] = (unsigned char)(modi >> 16);
items[i].oid.id[j * 4 + 3] = (unsigned char)(modi >> 24);
}
items[i].oid.id[ 8] = (unsigned char)i;
items[i].oid.id[ 9] = (unsigned char)(i >> 8);
items[i].oid.id[10] = (unsigned char)(i >> 16);
items[i].oid.id[11] = (unsigned char)(i >> 24);
}
cl_git_pass(git_oidmap_new(&map));
for (i = 0; i < NITEMS; ++i) {
size_t pos;
int ret;
pos = git_oidmap_lookup_index(map, &items[i].oid);
cl_assert(!git_oidmap_valid_index(map, pos));
pos = git_oidmap_put(map, &items[i].oid, &ret);
cl_assert(ret != 0);
git_oidmap_set_value_at(map, pos, &items[i]);
}
for (i = 0; i < NITEMS; ++i) {
size_t pos;
pos = git_oidmap_lookup_index(map, &items[i].oid);
cl_assert(git_oidmap_valid_index(map, pos));
cl_assert_equal_p(git_oidmap_value_at(map, pos), &items[i]);
}
git_oidmap_free(map);
}
void test_core_oidmap__get_succeeds_with_existing_keys(void)
{
git_oidmap *map;
oidmap_item items[NITEMS];
uint32_t i, j;
for (i = 0; i < NITEMS; ++i) {
uint32_t segment = i / 8;
int modi = i - (segment * 8);
items[i].extra = i;
for (j = 0; j < GIT_OID_RAWSZ / 4; ++j) {
items[i].oid.id[j * 4 ] = (unsigned char)modi;
items[i].oid.id[j * 4 + 1] = (unsigned char)(modi >> 8);
items[i].oid.id[j * 4 + 2] = (unsigned char)(modi >> 16);
items[i].oid.id[j * 4 + 3] = (unsigned char)(modi >> 24);
}
items[i].oid.id[ 8] = (unsigned char)i;
items[i].oid.id[ 9] = (unsigned char)(i >> 8);
items[i].oid.id[10] = (unsigned char)(i >> 16);
items[i].oid.id[11] = (unsigned char)(i >> 24);
}
cl_git_pass(git_oidmap_new(&map));
for (i = 0; i < NITEMS; ++i) {
int ret;
git_oidmap_insert(map, &items[i].oid, &items[i], &ret);
cl_assert(ret == 1);
}
for (i = 0; i < NITEMS; ++i)
cl_assert_equal_p(git_oidmap_get(map, &items[i].oid), &items[i]);
git_oidmap_free(map);
}
void test_core_oidmap__get_fails_with_nonexisting_key(void)
{
git_oidmap *map;
oidmap_item items[NITEMS];
uint32_t i, j;
for (i = 0; i < NITEMS; ++i) {
uint32_t segment = i / 8;
int modi = i - (segment * 8);
items[i].extra = i;
for (j = 0; j < GIT_OID_RAWSZ / 4; ++j) {
items[i].oid.id[j * 4 ] = (unsigned char)modi;
items[i].oid.id[j * 4 + 1] = (unsigned char)(modi >> 8);
items[i].oid.id[j * 4 + 2] = (unsigned char)(modi >> 16);
items[i].oid.id[j * 4 + 3] = (unsigned char)(modi >> 24);
}
items[i].oid.id[ 8] = (unsigned char)i;
items[i].oid.id[ 9] = (unsigned char)(i >> 8);
items[i].oid.id[10] = (unsigned char)(i >> 16);
items[i].oid.id[11] = (unsigned char)(i >> 24);
}
cl_git_pass(git_oidmap_new(&map));
/* Do _not_ add last OID to verify that we cannot look it up */
for (i = 0; i < NITEMS - 1; ++i) {
int ret;
git_oidmap_insert(map, &items[i].oid, &items[i], &ret);
cl_assert(ret == 1);
}
cl_assert_equal_p(git_oidmap_get(map, &items[NITEMS - 1].oid), NULL);
git_oidmap_free(map);
}
void test_core_oidmap__setting_oid_persists(void)
{
git_oid oids[] = {
{{ 0x01 }},
{{ 0x02 }},
{{ 0x03 }}
};
git_oidmap *map;
cl_git_pass(git_oidmap_new(&map));
cl_git_pass(git_oidmap_set(map, &oids[0], "one"));
cl_git_pass(git_oidmap_set(map, &oids[1], "two"));
cl_git_pass(git_oidmap_set(map, &oids[2], "three"));
cl_assert_equal_s(git_oidmap_get(map, &oids[0]), "one");
cl_assert_equal_s(git_oidmap_get(map, &oids[1]), "two");
cl_assert_equal_s(git_oidmap_get(map, &oids[2]), "three");
git_oidmap_free(map);
}
void test_core_oidmap__setting_existing_key_updates(void)
{
git_oid oids[] = {
{{ 0x01 }},
{{ 0x02 }},
{{ 0x03 }}
};
git_oidmap *map;
cl_git_pass(git_oidmap_new(&map));
cl_git_pass(git_oidmap_set(map, &oids[0], "one"));
cl_git_pass(git_oidmap_set(map, &oids[1], "two"));
cl_git_pass(git_oidmap_set(map, &oids[2], "three"));
cl_assert_equal_i(git_oidmap_size(map), 3);
cl_git_pass(git_oidmap_set(map, &oids[1], "other"));
cl_assert_equal_i(git_oidmap_size(map), 3);
cl_assert_equal_s(git_oidmap_get(map, &oids[1]), "other");
git_oidmap_free(map);
}