Skip to content

Commit

Permalink
iord-map: detect node relinking
Browse files Browse the repository at this point in the history
Signed-off-by: László Várady <[email protected]>
  • Loading branch information
MrAnno committed Nov 19, 2024
1 parent 9c7ee99 commit 3b42dc0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 8 additions & 2 deletions lib/adt/iord_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,13 @@ iord_map_foreach(IOrdMap *self, IOrdMapForeachFunc func, gpointer user_data)
gboolean
iord_map_insert(IOrdMap *self, gpointer key, gpointer value)
{
IOrdMapNode *key_node = iord_map_node_from_container(key, self->key_container_offset);
g_assert(!key_node->next || iv_list_empty(key_node));

gpointer orig_key, old_value;
if (!g_hash_table_lookup_extended(self->ht, key, &orig_key, &old_value))
{
iv_list_add_tail(iord_map_node_from_container(key, self->key_container_offset), &self->keys);
iv_list_add_tail(key_node, &self->keys);
goto finish;
}

Expand All @@ -128,10 +131,13 @@ iord_map_insert(IOrdMap *self, gpointer key, gpointer value)
gboolean
iord_map_prepend(IOrdMap *self, gpointer key, gpointer value)
{
IOrdMapNode *key_node = iord_map_node_from_container(key, self->key_container_offset);
g_assert(!key_node->next || iv_list_empty(key_node));

gpointer orig_key, old_value;
if (!g_hash_table_lookup_extended(self->ht, key, &orig_key, &old_value))
{
iv_list_add(iord_map_node_from_container(key, self->key_container_offset), &self->keys);
iv_list_add(key_node, &self->keys);
goto finish;
}

Expand Down
6 changes: 4 additions & 2 deletions lib/adt/tests/test_iord_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Test(iord_map, insert_lookup_and_contains)
IOrdMap *map = iord_map_new(g_str_hash, g_str_equal, offsetof(TestKey, n), offsetof(TestData, n));

TestKey key = { "key" };
TestKey key_same = { "key" };
TestData value = { .a = 1, .b = 2, .c = 3 };
TestData value2 = { .a = 4, .b = 5, .c = 6 };

Expand All @@ -97,7 +98,7 @@ Test(iord_map, insert_lookup_and_contains)
TestData *actual = iord_map_lookup(map, &key);
assert_value_equals(actual, &value);

cr_assert_not(iord_map_insert(map, &key, &value2));
cr_assert_not(iord_map_insert(map, &key_same, &value2));
actual = iord_map_lookup(map, &key);
assert_value_equals(actual, &value2);

Expand Down Expand Up @@ -194,6 +195,7 @@ Test(iord_map, reinsert_moves_key_to_end)
IOrdMap *map = iord_map_new(g_str_hash, g_str_equal, offsetof(TestKey, n), offsetof(TestData, n));

TestKey keys[] = {{"key1"}, {"key2"}, {"key3"}, {"key4"}, {"key5"}};
TestKey keys_reinsert = {"key1"};
TestData values[] = {{1}, {2}, {3}, {4}, {5}};
TestData new_value = {9};

Expand All @@ -202,7 +204,7 @@ Test(iord_map, reinsert_moves_key_to_end)
iord_map_insert(map, &keys[i], &values[i]);
}

iord_map_insert(map, &keys[0], &new_value);
iord_map_insert(map, &keys_reinsert, &new_value);

IOrdMapNode *actual_keys = iord_map_get_keys(map);

Expand Down

0 comments on commit 3b42dc0

Please sign in to comment.