]> Pileus Git - ~andy/gtk/blob - gtk/tests/treepath.c
03b96a0c9997f186819b4ff0e672652e5bdca227
[~andy/gtk] / gtk / tests / treepath.c
1 /* GtkTrePath tests.
2  *
3  * Copyright (C) 2011, Red Hat, Inc.
4  * Authors: Matthias Clasen <mclasen@redhat.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include <gtk/gtk.h>
23
24 static void
25 test_append (void)
26 {
27   GtkTreePath *p;
28   gint i;
29   gint *indices;
30
31   p = gtk_tree_path_new ();
32   for (i = 0; i < 100; i++)
33     {
34       g_assert_cmpint (gtk_tree_path_get_depth (p), ==, i);
35       gtk_tree_path_append_index (p, i);
36     }
37
38   indices = gtk_tree_path_get_indices (p);
39   for (i = 0; i < 100; i++)
40     g_assert_cmpint (indices[i], ==, i);
41
42   gtk_tree_path_free (p);
43 }
44
45 static void
46 test_prepend (void)
47 {
48   GtkTreePath *p;
49   gint i;
50   gint *indices;
51
52   p = gtk_tree_path_new ();
53   for (i = 0; i < 100; i++)
54     {
55       g_assert_cmpint (gtk_tree_path_get_depth (p), ==, i);
56       gtk_tree_path_prepend_index (p, i);
57     }
58
59   indices = gtk_tree_path_get_indices (p);
60   for (i = 0; i < 100; i++)
61     g_assert_cmpint (indices[i], ==, 99 - i);
62
63   gtk_tree_path_free (p);
64 }
65
66 static void
67 test_to_string (void)
68 {
69   const gchar *str = "0:1:2:3:4:5:6:7:8:9:10";
70   GtkTreePath *p;
71   gint *indices;
72   gchar *s;
73   gint i;
74
75   p = gtk_tree_path_new_from_string (str);
76   indices = gtk_tree_path_get_indices (p);
77   for (i = 0; i < 10; i++)
78     g_assert_cmpint (indices[i], ==, i);
79   s = gtk_tree_path_to_string (p);
80   g_assert_cmpstr (s, ==, str);
81
82   gtk_tree_path_free (p);
83   g_free (s);
84 }
85
86 static void
87 test_from_indices (void)
88 {
89   GtkTreePath *p;
90   gint *indices;
91   gint i;
92
93   p = gtk_tree_path_new_from_indices (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1);
94   g_assert_cmpint (gtk_tree_path_get_depth (p), ==, 10);
95   indices = gtk_tree_path_get_indices (p);
96   for (i = 0; i < 10; i++)
97     g_assert_cmpint (indices[i], ==, i);
98   gtk_tree_path_free (p);
99 }
100
101 static void
102 test_first (void)
103 {
104   GtkTreePath *p;
105   p = gtk_tree_path_new_first ();
106   g_assert_cmpint (gtk_tree_path_get_depth (p), ==, 1);
107   g_assert_cmpint (gtk_tree_path_get_indices (p)[0], ==, 0);
108   gtk_tree_path_free (p);
109 }
110
111 static void
112 test_navigation (void)
113 {
114   GtkTreePath *p;
115   GtkTreePath *q;
116   gint *pi;
117   gint *qi;
118   gint i;
119   gboolean res;
120
121   p = gtk_tree_path_new_from_indices (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1);
122   q = gtk_tree_path_copy (p);
123   g_assert (gtk_tree_path_compare (p, q) == 0);
124   gtk_tree_path_next (q);
125   pi = gtk_tree_path_get_indices (p);
126   qi = gtk_tree_path_get_indices (q);
127   for (i = 0; i < 9; i++)
128     g_assert_cmpint (pi[i], ==, qi[i]);
129   g_assert_cmpint (qi[9], ==, pi[9] + 1);
130
131   g_assert (!gtk_tree_path_is_ancestor (p, q));
132   g_assert (!gtk_tree_path_is_ancestor (q, p));
133   g_assert (!gtk_tree_path_is_descendant (p, q));
134   g_assert (!gtk_tree_path_is_descendant (q, p));
135
136   res = gtk_tree_path_prev (q);
137   g_assert (res);
138   g_assert (gtk_tree_path_compare (p, q) == 0);
139
140   g_assert (!gtk_tree_path_is_ancestor (p, q));
141   g_assert (!gtk_tree_path_is_ancestor (q, p));
142   g_assert (!gtk_tree_path_is_descendant (p, q));
143   g_assert (!gtk_tree_path_is_descendant (q, p));
144
145   gtk_tree_path_down (q);
146
147   g_assert (gtk_tree_path_compare (p, q) < 0);
148
149   g_assert (gtk_tree_path_is_ancestor (p, q));
150   g_assert (!gtk_tree_path_is_ancestor (q, p));
151   g_assert (!gtk_tree_path_is_descendant (p, q));
152   g_assert (gtk_tree_path_is_descendant (q, p));
153
154   res = gtk_tree_path_prev (q);
155   g_assert (!res);
156
157   res = gtk_tree_path_up (q);
158   g_assert (res);
159   g_assert (gtk_tree_path_compare (p, q) == 0);
160
161   g_assert_cmpint (gtk_tree_path_get_depth (q), ==, 10);
162   res = gtk_tree_path_up (q);
163   g_assert (res);
164   g_assert_cmpint (gtk_tree_path_get_depth (q), ==, 9);
165
166   gtk_tree_path_free (p);
167   gtk_tree_path_free (q);
168 }
169
170 int
171 main (int argc, char *argv[])
172 {
173   gtk_test_init (&argc, &argv, NULL);
174
175   g_test_add_func ("/tree-path/append", test_append);
176   g_test_add_func ("/tree-path/prepend", test_prepend);
177   g_test_add_func ("/tree-path/to-string", test_to_string);
178   g_test_add_func ("/tree-path/from-indices", test_from_indices);
179   g_test_add_func ("/tree-path/first", test_first);
180   g_test_add_func ("/tree-path/navigation", test_navigation);
181
182   return g_test_run ();
183 }