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, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <gtk/gtk.h>
21 
22 static void
test_append(void)23 test_append (void)
24 {
25   GtkTreePath *p;
26   int i;
27   int *indices;
28 
29   p = gtk_tree_path_new ();
30   for (i = 0; i < 100; i++)
31     {
32       g_assert_cmpint (gtk_tree_path_get_depth (p), ==, i);
33       gtk_tree_path_append_index (p, i);
34     }
35 
36   indices = gtk_tree_path_get_indices (p);
37   for (i = 0; i < 100; i++)
38     g_assert_cmpint (indices[i], ==, i);
39 
40   gtk_tree_path_free (p);
41 }
42 
43 static void
test_prepend(void)44 test_prepend (void)
45 {
46   GtkTreePath *p;
47   int i;
48   int *indices;
49 
50   p = gtk_tree_path_new ();
51   for (i = 0; i < 100; i++)
52     {
53       g_assert_cmpint (gtk_tree_path_get_depth (p), ==, i);
54       gtk_tree_path_prepend_index (p, i);
55     }
56 
57   indices = gtk_tree_path_get_indices (p);
58   for (i = 0; i < 100; i++)
59     g_assert_cmpint (indices[i], ==, 99 - i);
60 
61   gtk_tree_path_free (p);
62 }
63 
64 static void
test_to_string(void)65 test_to_string (void)
66 {
67   const char *str = "0:1:2:3:4:5:6:7:8:9:10";
68   GtkTreePath *p;
69   int *indices;
70   char *s;
71   int i;
72 
73   p = gtk_tree_path_new_from_string (str);
74   indices = gtk_tree_path_get_indices (p);
75   for (i = 0; i < 10; i++)
76     g_assert_cmpint (indices[i], ==, i);
77   s = gtk_tree_path_to_string (p);
78   g_assert_cmpstr (s, ==, str);
79 
80   gtk_tree_path_free (p);
81   g_free (s);
82 }
83 
84 static void
test_from_indices(void)85 test_from_indices (void)
86 {
87   GtkTreePath *p;
88   int *indices;
89   int i;
90 
91   p = gtk_tree_path_new_from_indices (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1);
92   g_assert_cmpint (gtk_tree_path_get_depth (p), ==, 10);
93   indices = gtk_tree_path_get_indices (p);
94   for (i = 0; i < 10; i++)
95     g_assert_cmpint (indices[i], ==, i);
96   gtk_tree_path_free (p);
97 }
98 
99 static void
test_first(void)100 test_first (void)
101 {
102   GtkTreePath *p;
103   p = gtk_tree_path_new_first ();
104   g_assert_cmpint (gtk_tree_path_get_depth (p), ==, 1);
105   g_assert_cmpint (gtk_tree_path_get_indices (p)[0], ==, 0);
106   gtk_tree_path_free (p);
107 }
108 
109 static void
test_navigation(void)110 test_navigation (void)
111 {
112   GtkTreePath *p;
113   GtkTreePath *q;
114   int *pi;
115   int *qi;
116   int i;
117   gboolean res;
118 
119   p = gtk_tree_path_new_from_indices (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1);
120   q = gtk_tree_path_copy (p);
121   g_assert_true (gtk_tree_path_compare (p, q) == 0);
122   gtk_tree_path_next (q);
123   pi = gtk_tree_path_get_indices (p);
124   qi = gtk_tree_path_get_indices (q);
125   for (i = 0; i < 9; i++)
126     g_assert_cmpint (pi[i], ==, qi[i]);
127   g_assert_cmpint (qi[9], ==, pi[9] + 1);
128 
129   g_assert_false (gtk_tree_path_is_ancestor (p, q));
130   g_assert_false (gtk_tree_path_is_ancestor (q, p));
131   g_assert_false (gtk_tree_path_is_descendant (p, q));
132   g_assert_false (gtk_tree_path_is_descendant (q, p));
133 
134   res = gtk_tree_path_prev (q);
135   g_assert_true (res);
136   g_assert_true (gtk_tree_path_compare (p, q) == 0);
137 
138   g_assert_false (gtk_tree_path_is_ancestor (p, q));
139   g_assert_false (gtk_tree_path_is_ancestor (q, p));
140   g_assert_false (gtk_tree_path_is_descendant (p, q));
141   g_assert_false (gtk_tree_path_is_descendant (q, p));
142 
143   gtk_tree_path_down (q);
144 
145   g_assert_true (gtk_tree_path_compare (p, q) < 0);
146 
147   g_assert_true (gtk_tree_path_is_ancestor (p, q));
148   g_assert_false (gtk_tree_path_is_ancestor (q, p));
149   g_assert_false (gtk_tree_path_is_descendant (p, q));
150   g_assert_true (gtk_tree_path_is_descendant (q, p));
151 
152   res = gtk_tree_path_prev (q);
153   g_assert_false (res);
154 
155   res = gtk_tree_path_up (q);
156   g_assert_true (res);
157   g_assert_true (gtk_tree_path_compare (p, q) == 0);
158 
159   g_assert_cmpint (gtk_tree_path_get_depth (q), ==, 10);
160   res = gtk_tree_path_up (q);
161   g_assert_true (res);
162   g_assert_cmpint (gtk_tree_path_get_depth (q), ==, 9);
163 
164   gtk_tree_path_free (p);
165   gtk_tree_path_free (q);
166 }
167 
168 int
main(int argc,char * argv[])169 main (int argc, char *argv[])
170 {
171   gtk_test_init (&argc, &argv, NULL);
172 
173   g_test_add_func ("/tree-path/append", test_append);
174   g_test_add_func ("/tree-path/prepend", test_prepend);
175   g_test_add_func ("/tree-path/to-string", test_to_string);
176   g_test_add_func ("/tree-path/from-indices", test_from_indices);
177   g_test_add_func ("/tree-path/first", test_first);
178   g_test_add_func ("/tree-path/navigation", test_navigation);
179 
180   return g_test_run ();
181 }
182