Hash :
18cf5698
Author :
Date :
2018-12-01T09:37:40
maps: provide high-level iteration interface Currently, our headers need to leak some implementation details of maps due to their direct use of indices in the implementation of their foreach macros. This makes it impossible to completely hide the map structures away, and also makes it impossible to include the khash implementation header in the C files of the respective map only. This is now being fixed by providing a high-level iteration interface `map_iterate`, which takes as inputs the map that shall be iterated over, an iterator as well as the locations where keys and values shall be put into. For simplicity's sake, the iterator is a simple `size_t` that shall initialized to `0` on the first call. All existing foreach macros are then adjusted to make use of this new function.
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
#include "clar_libgit2.h"
#include "strmap.h"
static git_strmap *g_table;
void test_core_strmap__initialize(void)
{
cl_git_pass(git_strmap_new(&g_table));
cl_assert(g_table != NULL);
}
void test_core_strmap__cleanup(void)
{
git_strmap_free(g_table);
}
void test_core_strmap__0(void)
{
cl_assert(git_strmap_size(g_table) == 0);
}
static void insert_strings(git_strmap *table, size_t count)
{
size_t i, j, over;
int err;
char *str;
for (i = 0; i < count; ++i) {
str = malloc(10);
for (j = 0; j < 10; ++j)
str[j] = 'a' + (i % 26);
str[9] = '\0';
/* if > 26, then encode larger value in first letters */
for (j = 0, over = i / 26; over > 0; j++, over = over / 26)
str[j] = 'A' + (over % 26);
git_strmap_insert(table, str, str, &err);
cl_assert(err >= 0);
}
cl_assert_equal_i(git_strmap_size(table), count);
}
void test_core_strmap__1(void)
{
int i;
char *str;
insert_strings(g_table, 20);
cl_assert(git_strmap_exists(g_table, "aaaaaaaaa"));
cl_assert(git_strmap_exists(g_table, "ggggggggg"));
cl_assert(!git_strmap_exists(g_table, "aaaaaaaab"));
cl_assert(!git_strmap_exists(g_table, "abcdefghi"));
i = 0;
git_strmap_foreach_value(g_table, str, { i++; free(str); });
cl_assert(i == 20);
}
void test_core_strmap__2(void)
{
size_t pos;
int i;
char *str;
insert_strings(g_table, 20);
cl_assert(git_strmap_exists(g_table, "aaaaaaaaa"));
cl_assert(git_strmap_exists(g_table, "ggggggggg"));
cl_assert(!git_strmap_exists(g_table, "aaaaaaaab"));
cl_assert(!git_strmap_exists(g_table, "abcdefghi"));
cl_assert(git_strmap_exists(g_table, "bbbbbbbbb"));
pos = git_strmap_lookup_index(g_table, "bbbbbbbbb");
cl_assert(git_strmap_valid_index(g_table, pos));
cl_assert_equal_s(git_strmap_value_at(g_table, pos), "bbbbbbbbb");
free(git_strmap_value_at(g_table, pos));
git_strmap_delete_at(g_table, pos);
cl_assert(!git_strmap_exists(g_table, "bbbbbbbbb"));
i = 0;
git_strmap_foreach_value(g_table, str, { i++; free(str); });
cl_assert_equal_i(i, 19);
}
void test_core_strmap__3(void)
{
int i;
char *str;
insert_strings(g_table, 10000);
i = 0;
git_strmap_foreach_value(g_table, str, { i++; free(str); });
cl_assert_equal_i(i, 10000);
}
void test_core_strmap__get_succeeds_with_existing_entries(void)
{
const char *keys[] = { "foo", "bar", "gobble" };
char *values[] = { "oof", "rab", "elbbog" };
int error;
size_t i;
for (i = 0; i < ARRAY_SIZE(keys); i++) {
git_strmap_insert(g_table, keys[i], values[i], &error);
cl_assert_equal_i(error, 1);
}
cl_assert_equal_s(git_strmap_get(g_table, "foo"), "oof");
cl_assert_equal_s(git_strmap_get(g_table, "bar"), "rab");
cl_assert_equal_s(git_strmap_get(g_table, "gobble"), "elbbog");
}
void test_core_strmap__get_returns_null_on_nonexisting_key(void)
{
const char *keys[] = { "foo", "bar", "gobble" };
char *values[] = { "oof", "rab", "elbbog" };
int error;
size_t i;
for (i = 0; i < ARRAY_SIZE(keys); i++) {
git_strmap_insert(g_table, keys[i], values[i], &error);
cl_assert_equal_i(error, 1);
}
cl_assert_equal_p(git_strmap_get(g_table, "other"), NULL);
}
void test_core_strmap__set_persists_key(void)
{
cl_git_pass(git_strmap_set(g_table, "foo", "oof"));
cl_assert_equal_s(git_strmap_get(g_table, "foo"), "oof");
}
void test_core_strmap__set_persists_multpile_keys(void)
{
cl_git_pass(git_strmap_set(g_table, "foo", "oof"));
cl_git_pass(git_strmap_set(g_table, "bar", "rab"));
cl_assert_equal_s(git_strmap_get(g_table, "foo"), "oof");
cl_assert_equal_s(git_strmap_get(g_table, "bar"), "rab");
}
void test_core_strmap__set_updates_existing_key(void)
{
cl_git_pass(git_strmap_set(g_table, "foo", "oof"));
cl_git_pass(git_strmap_set(g_table, "bar", "rab"));
cl_git_pass(git_strmap_set(g_table, "gobble", "elbbog"));
cl_assert_equal_i(git_strmap_size(g_table), 3);
cl_git_pass(git_strmap_set(g_table, "foo", "other"));
cl_assert_equal_i(git_strmap_size(g_table), 3);
cl_assert_equal_s(git_strmap_get(g_table, "foo"), "other");
}
void test_core_strmap__iteration(void)
{
struct {
char *key;
char *value;
int seen;
} entries[] = {
{ "foo", "oof" },
{ "bar", "rab" },
{ "gobble", "elbbog" },
};
const char *key, *value;
size_t i, n;
for (i = 0; i < ARRAY_SIZE(entries); i++)
cl_git_pass(git_strmap_set(g_table, entries[i].key, entries[i].value));
i = 0, n = 0;
while (git_strmap_iterate((void **) &value, g_table, &i, &key) == 0) {
size_t j;
for (j = 0; j < ARRAY_SIZE(entries); j++) {
if (strcmp(entries[j].key, key))
continue;
cl_assert_equal_i(entries[j].seen, 0);
cl_assert_equal_s(entries[j].value, value);
entries[j].seen++;
break;
}
n++;
}
for (i = 0; i < ARRAY_SIZE(entries); i++)
cl_assert_equal_i(entries[i].seen, 1);
cl_assert_equal_i(n, ARRAY_SIZE(entries));
}
void test_core_strmap__iterating_empty_map_stops_immediately(void)
{
size_t i = 0;
cl_git_fail_with(git_strmap_iterate(NULL, g_table, &i, NULL), GIT_ITEROVER);
}