Merge branch 'wip/nielsdg/g-list-store-find' into 'master'

gliststore: Add item lookup functions

Closes #1011

See merge request GNOME/glib!1096
This commit is contained in:
Philip Withnall
2019-11-27 11:22:56 +00:00
4 changed files with 163 additions and 0 deletions

View File

@@ -805,6 +805,72 @@ test_store_past_end (void)
g_object_unref (store);
}
static gboolean
list_model_casecmp_action_by_name (gconstpointer a,
gconstpointer b)
{
return g_ascii_strcasecmp (g_action_get_name (G_ACTION (a)),
g_action_get_name (G_ACTION (b))) == 0;
}
/* Test if find() and find_with_equal_func() works */
static void
test_store_find (void)
{
GListStore *store;
guint position = 100;
const gchar *item_strs[4] = { "aaa", "bbb", "xxx", "ccc" };
GSimpleAction *items[4] = { NULL, };
GSimpleAction *other_item;
guint i;
store = g_list_store_new (G_TYPE_SIMPLE_ACTION);
for (i = 0; i < G_N_ELEMENTS (item_strs); i++)
items[i] = g_simple_action_new (item_strs[i], NULL);
/* Shouldn't crash on an empty list, or change the position pointer */
g_assert_false (g_list_store_find (store, items[0], NULL));
g_assert_false (g_list_store_find (store, items[0], &position));
g_assert_cmpint (position, ==, 100);
for (i = 0; i < G_N_ELEMENTS (item_strs); i++)
g_list_store_append (store, items[i]);
/* Check whether it could still find the the elements */
for (i = 0; i < G_N_ELEMENTS (item_strs); i++)
{
g_assert_true (g_list_store_find (store, items[i], &position));
g_assert_cmpint (position, ==, i);
/* Shouldn't try to write to position pointer if NULL given */
g_assert_true (g_list_store_find (store, items[i], NULL));
}
/* try to find element not part of the list */
other_item = g_simple_action_new ("111", NULL);
g_assert_false (g_list_store_find (store, other_item, NULL));
g_clear_object (&other_item);
/* Re-add item; find() should only return the first position */
g_list_store_append (store, items[0]);
g_assert_true (g_list_store_find (store, items[0], &position));
g_assert_cmpint (position, ==, 0);
/* try to find element which should only work with custom equality check */
other_item = g_simple_action_new ("XXX", NULL);
g_assert_false (g_list_store_find (store, other_item, NULL));
g_assert_true (g_list_store_find_with_equal_func (store,
other_item,
list_model_casecmp_action_by_name,
&position));
g_assert_cmpint (position, ==, 2);
g_clear_object (&other_item);
for (i = 0; i < G_N_ELEMENTS (item_strs); i++)
g_clear_object(&items[i]);
g_clear_object (&store);
}
int main (int argc, char *argv[])
{
g_test_init (&argc, &argv, NULL);
@@ -837,6 +903,7 @@ int main (int argc, char *argv[])
g_test_add_func ("/glistmodel/store/items-changed",
test_store_signal_items_changed);
g_test_add_func ("/glistmodel/store/past-end", test_store_past_end);
g_test_add_func ("/glistmodel/store/find", test_store_find);
return g_test_run ();
}