Add powerbox hook
[gtk-with-powerbox.git] / tests / testtreemodel.c
blob871dcf07017e6b1b394906664f09679c4b3cebf2
1 /* testtreemodel.c
2 * Copyright (C) 2004 Red Hat, Inc., Matthias Clasen <mclasen@redhat.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include "config.h"
22 #include <string.h>
24 #ifdef HAVE_MALLINFO
25 #include <malloc.h>
26 #endif
28 #include <gtk/gtk.h>
30 static gint repeats = 2;
31 static gint max_size = 8;
33 static GOptionEntry entries[] = {
34 { "repeats", 'r', 0, G_OPTION_ARG_INT, &repeats, "Average over N repetitions", "N" },
35 { "max-size", 'm', 0, G_OPTION_ARG_INT, &max_size, "Test up to 2^M items", "M" },
36 { NULL }
40 typedef void (ClearFunc)(GtkTreeModel *model);
41 typedef void (InsertFunc)(GtkTreeModel *model,
42 gint items,
43 gint i);
45 static void
46 list_store_append (GtkTreeModel *model,
47 gint items,
48 gint i)
50 GtkListStore *store = GTK_LIST_STORE (model);
51 GtkTreeIter iter;
52 gchar *text;
54 text = g_strdup_printf ("row %d", i);
55 gtk_list_store_append (store, &iter);
56 gtk_list_store_set (store, &iter, 0, i, 1, text, -1);
57 g_free (text);
60 static void
61 list_store_prepend (GtkTreeModel *model,
62 gint items,
63 gint i)
65 GtkListStore *store = GTK_LIST_STORE (model);
66 GtkTreeIter iter;
67 gchar *text;
69 text = g_strdup_printf ("row %d", i);
70 gtk_list_store_prepend (store, &iter);
71 gtk_list_store_set (store, &iter, 0, i, 1, text, -1);
72 g_free (text);
75 static void
76 list_store_insert (GtkTreeModel *model,
77 gint items,
78 gint i)
80 GtkListStore *store = GTK_LIST_STORE (model);
81 GtkTreeIter iter;
82 gchar *text;
83 gint n;
85 text = g_strdup_printf ("row %d", i);
86 n = g_random_int_range (0, i + 1);
87 gtk_list_store_insert (store, &iter, n);
88 gtk_list_store_set (store, &iter, 0, i, 1, text, -1);
89 g_free (text);
92 static gint
93 compare (GtkTreeModel *model,
94 GtkTreeIter *a,
95 GtkTreeIter *b,
96 gpointer data)
98 gchar *str_a, *str_b;
99 gint result;
101 gtk_tree_model_get (model, a, 1, &str_a, -1);
102 gtk_tree_model_get (model, b, 1, &str_b, -1);
104 result = strcmp (str_a, str_b);
106 g_free (str_a);
107 g_free (str_b);
109 return result;
112 static void
113 tree_store_append (GtkTreeModel *model,
114 gint items,
115 gint i)
117 GtkTreeStore *store = GTK_TREE_STORE (model);
118 GtkTreeIter iter;
119 gchar *text;
121 text = g_strdup_printf ("row %d", i);
122 gtk_tree_store_append (store, &iter, NULL);
123 gtk_tree_store_set (store, &iter, 0, i, 1, text, -1);
124 g_free (text);
127 static void
128 tree_store_prepend (GtkTreeModel *model,
129 gint items,
130 gint i)
132 GtkTreeStore *store = GTK_TREE_STORE (model);
133 GtkTreeIter iter;
134 gchar *text;
136 text = g_strdup_printf ("row %d", i);
137 gtk_tree_store_prepend (store, &iter, NULL);
138 gtk_tree_store_set (store, &iter, 0, i, 1, text, -1);
139 g_free (text);
142 static void
143 tree_store_insert_flat (GtkTreeModel *model,
144 gint items,
145 gint i)
147 GtkTreeStore *store = GTK_TREE_STORE (model);
148 GtkTreeIter iter;
149 gchar *text;
150 gint n;
152 text = g_strdup_printf ("row %d", i);
153 n = g_random_int_range (0, i + 1);
154 gtk_tree_store_insert (store, &iter, NULL, n);
155 gtk_tree_store_set (store, &iter, 0, i, 1, text, -1);
156 g_free (text);
159 typedef struct {
160 gint i;
161 gint n;
162 gboolean found;
163 GtkTreeIter iter;
164 } FindData;
166 static gboolean
167 find_nth (GtkTreeModel *model,
168 GtkTreePath *path,
169 GtkTreeIter *iter,
170 gpointer data)
172 FindData *fdata = (FindData *)data;
174 if (fdata->i >= fdata->n)
176 fdata->iter = *iter;
177 fdata->found = TRUE;
178 return TRUE;
181 fdata->i++;
183 return FALSE;
186 static void
187 tree_store_insert_deep (GtkTreeModel *model,
188 gint items,
189 gint i)
191 GtkTreeStore *store = GTK_TREE_STORE (model);
192 GtkTreeIter iter;
193 gchar *text;
194 FindData data;
196 text = g_strdup_printf ("row %d", i);
197 data.n = g_random_int_range (0, items);
198 data.i = 0;
199 data.found = FALSE;
200 if (data.n < i)
201 gtk_tree_model_foreach (model, find_nth, &data);
202 gtk_tree_store_insert (store, &iter, data.found ? &(data.iter) : NULL, data.n);
203 gtk_tree_store_set (store, &iter, 0, i, 1, text, -1);
204 g_free (text);
208 static void
209 test_run (gchar *title,
210 GtkTreeModel *store,
211 ClearFunc *clear,
212 InsertFunc *insert)
214 gint i, k, d, items;
215 GTimer *timer;
216 gdouble elapsed;
217 int uordblks_before = 0, memused;
219 g_print ("%s (average over %d runs, time in milliseconds)\n"
220 "items \ttime \ttime/item \tused memory\n", title, repeats);
222 timer = g_timer_new ();
224 for (k = 0; k < max_size; k++)
226 items = 1 << k;
227 elapsed = 0.0;
228 for (d = 0; d < repeats; d++)
230 (*clear)(store);
231 #ifdef HAVE_MALLINFO
232 /* Peculiar location of this, btw. -- MW. */
233 uordblks_before = mallinfo().uordblks;
234 #endif
235 g_timer_reset (timer);
236 g_timer_start (timer);
237 for (i = 0; i < items; i++)
238 (*insert) (store, items, i);
239 g_timer_stop (timer);
240 elapsed += g_timer_elapsed (timer, NULL);
243 elapsed = elapsed * 1000 / repeats;
244 #ifdef HAVE_MALLINFO
245 memused = (mallinfo().uordblks - uordblks_before) / 1024;
246 #else
247 memused = 0;
248 #endif
249 g_print ("%d \t%f \t%f \t%dk\n",
250 items, elapsed, elapsed/items, memused);
255 main (int argc, char *argv[])
257 GtkTreeModel *model;
259 gtk_init_with_args (&argc, &argv, NULL, entries, NULL, NULL);
261 model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_INT, G_TYPE_STRING));
263 test_run ("list store append",
264 model,
265 (ClearFunc*)gtk_list_store_clear,
266 (InsertFunc*)list_store_append);
268 test_run ("list store prepend",
269 model,
270 (ClearFunc*)gtk_list_store_clear,
271 (InsertFunc*)list_store_prepend);
273 test_run ("list store insert",
274 model,
275 (ClearFunc*)gtk_list_store_clear,
276 (InsertFunc*)list_store_insert);
278 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (model),
279 compare, NULL, NULL);
280 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model),
281 GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
282 GTK_SORT_ASCENDING);
284 test_run ("list store insert (sorted)",
285 model,
286 (ClearFunc*)gtk_list_store_clear,
287 (InsertFunc*)list_store_insert);
289 g_object_unref (model);
291 model = GTK_TREE_MODEL (gtk_tree_store_new (2, G_TYPE_INT, G_TYPE_STRING));
293 test_run ("tree store append",
294 model,
295 (ClearFunc*)gtk_tree_store_clear,
296 (InsertFunc*)tree_store_append);
298 test_run ("tree store prepend",
299 model,
300 (ClearFunc*)gtk_tree_store_clear,
301 (InsertFunc*)tree_store_prepend);
303 test_run ("tree store insert (flat)",
304 model,
305 (ClearFunc*)gtk_tree_store_clear,
306 (InsertFunc*)tree_store_insert_flat);
308 test_run ("tree store insert (deep)",
309 model,
310 (ClearFunc*)gtk_tree_store_clear,
311 (InsertFunc*)tree_store_insert_deep);
313 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (model),
314 compare, NULL, NULL);
315 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model),
316 GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
317 GTK_SORT_ASCENDING);
319 test_run ("tree store insert (flat, sorted)",
320 model,
321 (ClearFunc*)gtk_tree_store_clear,
322 (InsertFunc*)tree_store_insert_flat);
324 test_run ("tree store insert (deep, sorted)",
325 model,
326 (ClearFunc*)gtk_tree_store_clear,
327 (InsertFunc*)tree_store_insert_deep);
329 return 0;