2 * Copyright 2015 Lars Uebernickel
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 * Authors: Lars Uebernickel <lars@uebernic.de>
25 test_store_boundaries (void)
30 store
= g_list_store_new (G_TYPE_MENU_ITEM
);
32 item
= g_menu_item_new (NULL
, NULL
);
33 g_object_add_weak_pointer (G_OBJECT (item
), (gpointer
*) &item
);
35 /* remove an item from an empty list */
36 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_CRITICAL
, "*g_sequence*");
37 g_list_store_remove (store
, 0);
38 g_test_assert_expected_messages ();
40 /* don't allow inserting an item past the end ... */
41 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_CRITICAL
, "*g_sequence*");
42 g_list_store_insert (store
, 1, item
);
43 g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store
)), ==, 0);
44 g_test_assert_expected_messages ();
46 /* ... except exactly at the end */
47 g_list_store_insert (store
, 0, item
);
48 g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store
)), ==, 1);
50 /* remove a non-existing item at exactly the end of the list */
51 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_CRITICAL
, "*g_sequence*");
52 g_list_store_remove (store
, 1);
53 g_test_assert_expected_messages ();
55 g_list_store_remove (store
, 0);
56 g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store
)), ==, 0);
58 /* splice beyond the end of the list */
59 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_CRITICAL
, "*position*");
60 g_list_store_splice (store
, 1, 0, NULL
, 0);
61 g_test_assert_expected_messages ();
63 /* remove items from an empty list */
64 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_CRITICAL
, "*position*");
65 g_list_store_splice (store
, 0, 1, NULL
, 0);
66 g_test_assert_expected_messages ();
68 g_list_store_append (store
, item
);
69 g_list_store_splice (store
, 0, 1, (gpointer
*) &item
, 1);
70 g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store
)), ==, 1);
72 /* remove more items than exist */
73 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_CRITICAL
, "*position*");
74 g_list_store_splice (store
, 0, 5, NULL
, 0);
75 g_test_assert_expected_messages ();
76 g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store
)), ==, 1);
78 g_object_unref (store
);
79 g_object_unref (item
);
84 test_store_refcounts (void)
92 store
= g_list_store_new (G_TYPE_MENU_ITEM
);
94 g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store
)), ==, 0);
95 g_assert_null (g_list_model_get_item (G_LIST_MODEL (store
), 0));
97 n_items
= G_N_ELEMENTS (items
);
98 for (i
= 0; i
< n_items
; i
++)
100 items
[i
] = g_menu_item_new (NULL
, NULL
);
101 g_object_add_weak_pointer (G_OBJECT (items
[i
]), (gpointer
*) &items
[i
]);
102 g_list_store_append (store
, items
[i
]);
104 g_object_unref (items
[i
]);
105 g_assert_nonnull (items
[i
]);
108 g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store
)), ==, n_items
);
109 g_assert_null (g_list_model_get_item (G_LIST_MODEL (store
), n_items
));
111 tmp
= g_list_model_get_item (G_LIST_MODEL (store
), 3);
112 g_assert (tmp
== items
[3]);
113 g_object_unref (tmp
);
115 g_list_store_remove (store
, 4);
116 g_assert_null (items
[4]);
118 g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store
)), ==, n_items
);
119 g_assert_null (g_list_model_get_item (G_LIST_MODEL (store
), n_items
));
121 g_object_unref (store
);
122 for (i
= 0; i
< G_N_ELEMENTS (items
); i
++)
123 g_assert_null (items
[i
]);
127 make_random_string (void)
129 gchar
*str
= g_malloc (10);
132 for (i
= 0; i
< 9; i
++)
133 str
[i
] = g_test_rand_int_range ('a', 'z');
140 compare_items (gconstpointer a_p
,
144 GObject
*a_o
= (GObject
*) a_p
;
145 GObject
*b_o
= (GObject
*) b_p
;
147 gchar
*a
= g_object_get_data (a_o
, "key");
148 gchar
*b
= g_object_get_data (b_o
, "key");
150 g_assert (user_data
== GUINT_TO_POINTER(0x1234u
));
152 return strcmp (a
, b
);
156 insert_string (GListStore
*store
,
161 obj
= g_object_new (G_TYPE_OBJECT
, NULL
);
162 g_object_set_data_full (obj
, "key", g_strdup (str
), g_free
);
164 g_list_store_insert_sorted (store
, obj
, compare_items
, GUINT_TO_POINTER(0x1234u
));
166 g_object_unref (obj
);
170 test_store_sorted (void)
175 store
= g_list_store_new (G_TYPE_OBJECT
);
177 for (i
= 0; i
< 1000; i
++)
179 gchar
*str
= make_random_string ();
180 insert_string (store
, str
);
181 insert_string (store
, str
); /* multiple copies of the same are OK */
185 g_assert_cmpint (g_list_model_get_n_items (G_LIST_MODEL (store
)), ==, 2000);
187 for (i
= 0; i
< 1000; i
++)
191 /* should see our two copies */
192 a
= g_list_model_get_item (G_LIST_MODEL (store
), i
* 2);
193 b
= g_list_model_get_item (G_LIST_MODEL (store
), i
* 2 + 1);
195 g_assert (compare_items (a
, b
, GUINT_TO_POINTER(0x1234)) == 0);
202 c
= g_list_model_get_item (G_LIST_MODEL (store
), i
* 2 - 1);
206 g_assert (compare_items (b
, c
, GUINT_TO_POINTER(0x1234)) > 0);
207 g_assert (compare_items (a
, c
, GUINT_TO_POINTER(0x1234)) > 0);
216 g_object_unref (store
);
219 int main (int argc
, char *argv
[])
221 g_test_init (&argc
, &argv
, NULL
);
223 g_test_add_func ("/glistmodel/store/boundaries", test_store_boundaries
);
224 g_test_add_func ("/glistmodel/store/refcounts", test_store_refcounts
);
225 g_test_add_func ("/glistmodel/store/sorted", test_store_sorted
);
227 return g_test_run ();