From 906292541a985d783221b6b73fd8d45d320f567a Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Tue, 31 May 2011 22:10:40 -0400 Subject: [PATCH] Add tests for GtkTreePath --- gtk/tests/Makefile.am | 4 + gtk/tests/treepath.c | 183 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 187 insertions(+) create mode 100644 gtk/tests/treepath.c diff --git a/gtk/tests/Makefile.am b/gtk/tests/Makefile.am index 17bbc38eab..32d741a572 100644 --- a/gtk/tests/Makefile.am +++ b/gtk/tests/Makefile.am @@ -109,6 +109,10 @@ TEST_PROGS += cellarea cellarea_SOURCES = cellarea.c cellarea_LDADD = $(progs_ldadd) +TEST_PROGS += treepath +treepath_SOURCES = treepath.c +treepath_LDADD = $(progs_ldadd) + EXTRA_DIST += \ file-chooser-test-dir/empty \ file-chooser-test-dir/text.txt diff --git a/gtk/tests/treepath.c b/gtk/tests/treepath.c new file mode 100644 index 0000000000..03b96a0c99 --- /dev/null +++ b/gtk/tests/treepath.c @@ -0,0 +1,183 @@ +/* GtkTrePath tests. + * + * Copyright (C) 2011, Red Hat, Inc. + * Authors: Matthias Clasen + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include + +static void +test_append (void) +{ + GtkTreePath *p; + gint i; + gint *indices; + + p = gtk_tree_path_new (); + for (i = 0; i < 100; i++) + { + g_assert_cmpint (gtk_tree_path_get_depth (p), ==, i); + gtk_tree_path_append_index (p, i); + } + + indices = gtk_tree_path_get_indices (p); + for (i = 0; i < 100; i++) + g_assert_cmpint (indices[i], ==, i); + + gtk_tree_path_free (p); +} + +static void +test_prepend (void) +{ + GtkTreePath *p; + gint i; + gint *indices; + + p = gtk_tree_path_new (); + for (i = 0; i < 100; i++) + { + g_assert_cmpint (gtk_tree_path_get_depth (p), ==, i); + gtk_tree_path_prepend_index (p, i); + } + + indices = gtk_tree_path_get_indices (p); + for (i = 0; i < 100; i++) + g_assert_cmpint (indices[i], ==, 99 - i); + + gtk_tree_path_free (p); +} + +static void +test_to_string (void) +{ + const gchar *str = "0:1:2:3:4:5:6:7:8:9:10"; + GtkTreePath *p; + gint *indices; + gchar *s; + gint i; + + p = gtk_tree_path_new_from_string (str); + indices = gtk_tree_path_get_indices (p); + for (i = 0; i < 10; i++) + g_assert_cmpint (indices[i], ==, i); + s = gtk_tree_path_to_string (p); + g_assert_cmpstr (s, ==, str); + + gtk_tree_path_free (p); + g_free (s); +} + +static void +test_from_indices (void) +{ + GtkTreePath *p; + gint *indices; + gint i; + + p = gtk_tree_path_new_from_indices (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1); + g_assert_cmpint (gtk_tree_path_get_depth (p), ==, 10); + indices = gtk_tree_path_get_indices (p); + for (i = 0; i < 10; i++) + g_assert_cmpint (indices[i], ==, i); + gtk_tree_path_free (p); +} + +static void +test_first (void) +{ + GtkTreePath *p; + p = gtk_tree_path_new_first (); + g_assert_cmpint (gtk_tree_path_get_depth (p), ==, 1); + g_assert_cmpint (gtk_tree_path_get_indices (p)[0], ==, 0); + gtk_tree_path_free (p); +} + +static void +test_navigation (void) +{ + GtkTreePath *p; + GtkTreePath *q; + gint *pi; + gint *qi; + gint i; + gboolean res; + + p = gtk_tree_path_new_from_indices (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1); + q = gtk_tree_path_copy (p); + g_assert (gtk_tree_path_compare (p, q) == 0); + gtk_tree_path_next (q); + pi = gtk_tree_path_get_indices (p); + qi = gtk_tree_path_get_indices (q); + for (i = 0; i < 9; i++) + g_assert_cmpint (pi[i], ==, qi[i]); + g_assert_cmpint (qi[9], ==, pi[9] + 1); + + g_assert (!gtk_tree_path_is_ancestor (p, q)); + g_assert (!gtk_tree_path_is_ancestor (q, p)); + g_assert (!gtk_tree_path_is_descendant (p, q)); + g_assert (!gtk_tree_path_is_descendant (q, p)); + + res = gtk_tree_path_prev (q); + g_assert (res); + g_assert (gtk_tree_path_compare (p, q) == 0); + + g_assert (!gtk_tree_path_is_ancestor (p, q)); + g_assert (!gtk_tree_path_is_ancestor (q, p)); + g_assert (!gtk_tree_path_is_descendant (p, q)); + g_assert (!gtk_tree_path_is_descendant (q, p)); + + gtk_tree_path_down (q); + + g_assert (gtk_tree_path_compare (p, q) < 0); + + g_assert (gtk_tree_path_is_ancestor (p, q)); + g_assert (!gtk_tree_path_is_ancestor (q, p)); + g_assert (!gtk_tree_path_is_descendant (p, q)); + g_assert (gtk_tree_path_is_descendant (q, p)); + + res = gtk_tree_path_prev (q); + g_assert (!res); + + res = gtk_tree_path_up (q); + g_assert (res); + g_assert (gtk_tree_path_compare (p, q) == 0); + + g_assert_cmpint (gtk_tree_path_get_depth (q), ==, 10); + res = gtk_tree_path_up (q); + g_assert (res); + g_assert_cmpint (gtk_tree_path_get_depth (q), ==, 9); + + gtk_tree_path_free (p); + gtk_tree_path_free (q); +} + +int +main (int argc, char *argv[]) +{ + gtk_test_init (&argc, &argv, NULL); + + g_test_add_func ("/tree-path/append", test_append); + g_test_add_func ("/tree-path/prepend", test_prepend); + g_test_add_func ("/tree-path/to-string", test_to_string); + g_test_add_func ("/tree-path/from-indices", test_from_indices); + g_test_add_func ("/tree-path/first", test_first); + g_test_add_func ("/tree-path/navigation", test_navigation); + + return g_test_run (); +}