gtk/tests/testtreemodel.c
Anders Carlsson 04d65a6d3d Make gtk argument parsing use goption. Add gtk_get_option_group and
2004-09-05  Anders Carlsson  <andersca@gnome.org>

	* gdk/gdk.c: (gdk_arg_class_cb), (gdk_arg_name_cb),
	(gdk_add_option_entries_libgtk_only), (gdk_pre_parse_libgtk_only),
	(gdk_parse_args):
	* gdk/gdk.h:
	* gdk/gdkinternals.h:
	* gdk/linux-fb/gdkmain-fb.c: (_gdk_windowing_init):
	* gdk/win32/gdkmain-win32.c: (_gdk_windowing_init):
	* gdk/x11/gdkdisplay-x11.c: (gdk_display_open):
	* gdk/x11/gdkmain-x11.c: (_gdk_windowing_init):
	* gtk/gtkmain.c: (gtk_arg_debug_cb), (gtk_arg_no_debug_cb),
	(gtk_arg_module_cb), (gtk_arg_warnings_cb),
	(do_pre_parse_initialization), (do_post_parse_initialization),
	(pre_parse_hook), (post_parse_hook), (gtk_get_option_group),
	(gtk_init_with_args), (gtk_parse_args):
	* gtk/gtkmain.h:
	Make gtk argument parsing use goption. Add gtk_get_option_group and
	gtk_init_with_args.

	* tests/testtreemodel.c: (main):
	Use gtk_init_with_args.
2004-09-05 15:09:55 +00:00

309 lines
7.1 KiB
C

#include <malloc.h>
#include <gtk/gtk.h>
static gint repeats = 2;
static gint max_size = 8;
static GOptionEntry entries[] = {
{ "repeats", 'r', 0, G_OPTION_ARG_INT, &repeats, "Average over N repetitions", "N" },
{ "max-size", 'm', 0, G_OPTION_ARG_INT, &max_size, "Test up to 2^M items", "M" },
{ NULL }
};
typedef void (ClearFunc)(GtkTreeModel *model);
typedef void (InsertFunc)(GtkTreeModel *model,
gint items,
gint i);
static void
list_store_append (GtkTreeModel *model,
gint items,
gint i)
{
GtkListStore *store = GTK_LIST_STORE (model);
GtkTreeIter iter;
gchar *text;
text = g_strdup_printf ("row %d", i);
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, 0, i, 1, text, -1);
g_free (text);
}
static void
list_store_prepend (GtkTreeModel *model,
gint items,
gint i)
{
GtkListStore *store = GTK_LIST_STORE (model);
GtkTreeIter iter;
gchar *text;
text = g_strdup_printf ("row %d", i);
gtk_list_store_prepend (store, &iter);
gtk_list_store_set (store, &iter, 0, i, 1, text, -1);
g_free (text);
}
static void
list_store_insert (GtkTreeModel *model,
gint items,
gint i)
{
GtkListStore *store = GTK_LIST_STORE (model);
GtkTreeIter iter;
gchar *text;
gint n;
text = g_strdup_printf ("row %d", i);
n = g_random_int_range (0, i + 1);
gtk_list_store_insert (store, &iter, n);
gtk_list_store_set (store, &iter, 0, i, 1, text, -1);
g_free (text);
}
static gint
compare (GtkTreeModel *model,
GtkTreeIter *a,
GtkTreeIter *b,
gpointer data)
{
gchar *str_a, *str_b;
gint result;
gtk_tree_model_get (model, a, 1, &str_a, -1);
gtk_tree_model_get (model, b, 1, &str_b, -1);
result = strcmp (str_a, str_b);
g_free (str_a);
g_free (str_b);
return result;
}
static void
tree_store_append (GtkTreeModel *model,
gint items,
gint i)
{
GtkTreeStore *store = GTK_TREE_STORE (model);
GtkTreeIter iter;
gchar *text;
text = g_strdup_printf ("row %d", i);
gtk_tree_store_append (store, &iter, NULL);
gtk_tree_store_set (store, &iter, 0, i, 1, text, -1);
g_free (text);
}
static void
tree_store_prepend (GtkTreeModel *model,
gint items,
gint i)
{
GtkTreeStore *store = GTK_TREE_STORE (model);
GtkTreeIter iter;
gchar *text;
text = g_strdup_printf ("row %d", i);
gtk_tree_store_prepend (store, &iter, NULL);
gtk_tree_store_set (store, &iter, 0, i, 1, text, -1);
g_free (text);
}
static void
tree_store_insert_flat (GtkTreeModel *model,
gint items,
gint i)
{
GtkTreeStore *store = GTK_TREE_STORE (model);
GtkTreeIter iter;
gchar *text;
gint n;
text = g_strdup_printf ("row %d", i);
n = g_random_int_range (0, i + 1);
gtk_tree_store_insert (store, &iter, NULL, n);
gtk_tree_store_set (store, &iter, 0, i, 1, text, -1);
g_free (text);
}
typedef struct {
gint i;
gint n;
gboolean found;
GtkTreeIter iter;
} FindData;
static gboolean
find_nth (GtkTreeModel *model,
GtkTreePath *path,
GtkTreeIter *iter,
gpointer data)
{
FindData *fdata = (FindData *)data;
if (fdata->i >= fdata->n)
{
fdata->iter = *iter;
fdata->found = TRUE;
return TRUE;
}
fdata->i++;
return FALSE;
}
static void
tree_store_insert_deep (GtkTreeModel *model,
gint items,
gint i)
{
GtkTreeStore *store = GTK_TREE_STORE (model);
GtkTreeIter iter;
gchar *text;
FindData data;
gint n;
text = g_strdup_printf ("row %d", i);
data.n = g_random_int_range (0, items);
data.i = 0;
data.found = FALSE;
if (data.n < i)
gtk_tree_model_foreach (model, find_nth, &data);
gtk_tree_store_insert (store, &iter, data.found ? &(data.iter) : NULL, n);
gtk_tree_store_set (store, &iter, 0, i, 1, text, -1);
g_free (text);
}
static void
test_run (gchar *title,
GtkTreeModel *store,
ClearFunc *clear,
InsertFunc *insert)
{
GtkTreeIter iter;
gint i, k, d, items;
gchar *text;
GTimer *timer;
gdouble elapsed;
int uordblks_before, memused;
g_print ("%s (average over %d runs, time in milliseconds)\n"
"items \ttime \ttime/item \tused memory\n", title, repeats);
timer = g_timer_new ();
for (k = 0; k < max_size; k++)
{
items = 1 << k;
elapsed = 0.0;
for (d = 0; d < repeats; d++)
{
(*clear)(store);
#ifdef HAVE_MALLINFO
/* Peculiar location of this, btw. -- MW. */
uordblks_before = mallinfo().uordblks;
#endif
g_timer_reset (timer);
g_timer_start (timer);
for (i = 0; i < items; i++)
(*insert) (store, items, i);
g_timer_stop (timer);
elapsed += g_timer_elapsed (timer, NULL);
}
elapsed = elapsed * 1000 / repeats;
#ifdef HAVE_MALLINFO
memused = (mallinfo().uordblks - uordblks_before) / 1024;
#else
memused = 0;
#endif
g_print ("%d \t%f \t%f \t%dk\n",
items, elapsed, elapsed/items, memused);
}
}
int
main (int argc, char *argv[])
{
GtkTreeModel *model;
GOptionContext *context;
gtk_init_with_args (&argc, &argv, NULL, entries, NULL, NULL);
model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_INT, G_TYPE_STRING));
test_run ("list store append",
model,
(ClearFunc*)gtk_list_store_clear,
(InsertFunc*)list_store_append);
test_run ("list store prepend",
model,
(ClearFunc*)gtk_list_store_clear,
(InsertFunc*)list_store_prepend);
test_run ("list store insert",
model,
(ClearFunc*)gtk_list_store_clear,
(InsertFunc*)list_store_insert);
gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (model),
compare, NULL, NULL);
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model),
GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
GTK_SORT_ASCENDING);
test_run ("list store insert (sorted)",
model,
(ClearFunc*)gtk_list_store_clear,
(InsertFunc*)list_store_insert);
g_object_unref (model);
model = GTK_TREE_MODEL (gtk_tree_store_new (2, G_TYPE_INT, G_TYPE_STRING));
test_run ("tree store append",
model,
(ClearFunc*)gtk_tree_store_clear,
(InsertFunc*)tree_store_append);
test_run ("tree store prepend",
model,
(ClearFunc*)gtk_tree_store_clear,
(InsertFunc*)tree_store_prepend);
test_run ("tree store insert (flat)",
model,
(ClearFunc*)gtk_tree_store_clear,
(InsertFunc*)tree_store_insert_flat);
test_run ("tree store insert (deep)",
model,
(ClearFunc*)gtk_tree_store_clear,
(InsertFunc*)tree_store_insert_deep);
gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (model),
compare, NULL, NULL);
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model),
GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
GTK_SORT_ASCENDING);
test_run ("tree store insert (flat, sorted)",
model,
(ClearFunc*)gtk_tree_store_clear,
(InsertFunc*)tree_store_insert_flat);
test_run ("tree store insert (deep, sorted)",
model,
(ClearFunc*)gtk_tree_store_clear,
(InsertFunc*)tree_store_insert_deep);
return 0;
}