]> Pileus Git - ~andy/gtk/blob - gtk/tests/textiter.c
Merge branch 'bgo593793-filechooser-recent-folders-master'
[~andy/gtk] / gtk / tests / textiter.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser 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.
8  *
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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser 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.
18  */
19
20 #include "config.h"
21 #include <stdio.h>
22 #include <string.h>
23
24 #include <gtk/gtk.h>
25
26 static void
27 test_empty_search ()
28 {
29   GtkTextBuffer *buffer;
30   GtkTextIter it, s, e;
31   gboolean res;
32
33   buffer = gtk_text_buffer_new (NULL);
34   gtk_text_buffer_set_text (buffer, "This is some foo text", -1);
35
36   /* search from start forward */
37   gtk_text_buffer_get_start_iter (buffer, &it);
38   res = gtk_text_iter_forward_search (&it, "", 0, &s, &e, NULL);
39   g_assert (res);
40   g_assert_cmpint (gtk_text_iter_get_offset (&s), ==, gtk_text_iter_get_offset (&e));
41   g_assert_cmpint (gtk_text_iter_get_offset (&s), ==, 1);
42
43   /* search from end backward */
44   gtk_text_buffer_get_end_iter (buffer, &it);
45   res = gtk_text_iter_backward_search (&it, "", 0, &s, &e, NULL);
46   g_assert (res);
47   g_assert_cmpint (gtk_text_iter_get_offset (&s), ==, gtk_text_iter_get_offset (&e));
48   g_assert_cmpint (gtk_text_iter_get_offset (&s), ==, 20);
49 }
50
51 static void
52 check_found_forward (const gchar *haystack,
53                      const gchar *needle,
54                      GtkTextSearchFlags flags,
55                      int expected_start,
56                      int expected_end,
57                      const gchar *expected_string)
58 {
59   GtkTextBuffer *buffer;
60   GtkTextIter i, s, e;
61   gboolean res;
62   gchar *text;
63
64   buffer = gtk_text_buffer_new (NULL);
65
66   gtk_text_buffer_set_text (buffer, haystack, -1);
67
68   /* TODO: add test with limit before, after and in the middle
69      of expected start and end */
70
71   /* search from start forward */
72   gtk_text_buffer_get_start_iter (buffer, &i);
73   res = gtk_text_iter_forward_search (&i, needle, flags, &s, &e, NULL);
74   g_assert (res);
75   g_assert_cmpint (expected_start, ==, gtk_text_iter_get_offset (&s));
76   g_assert_cmpint (expected_end, ==, gtk_text_iter_get_offset (&e));
77   text = gtk_text_iter_get_text (&s, &e);
78   g_assert_cmpstr (expected_string, ==, text);
79   g_free (text);
80
81   g_object_unref (buffer);
82 }
83
84 static void
85 check_found_backward (const gchar *haystack,
86                       const gchar *needle,
87                       GtkTextSearchFlags flags,
88                       int expected_start,
89                       int expected_end,
90                       const gchar *expected_string)
91 {
92   GtkTextBuffer *buffer;
93   GtkTextIter i, s, e;
94   gboolean res;
95   gchar *text;
96
97   buffer = gtk_text_buffer_new (NULL);
98
99   gtk_text_buffer_set_text (buffer, haystack, -1);
100
101   /* search from end backward */
102   gtk_text_buffer_get_end_iter (buffer, &i);
103   res = gtk_text_iter_backward_search (&i, needle, flags, &s, &e, NULL);
104   g_assert (res);
105   g_assert_cmpint (expected_start, ==, gtk_text_iter_get_offset (&s));
106   g_assert_cmpint (expected_end, ==, gtk_text_iter_get_offset (&e));
107   text = gtk_text_iter_get_text (&s, &e);
108   g_assert_cmpstr (expected_string, ==, text);
109   g_free (text);
110
111   g_object_unref (buffer);
112 }
113
114 static void
115 check_not_found (const gchar *haystack,
116                  const gchar *needle,
117                  GtkTextSearchFlags flags)
118 {
119   GtkTextBuffer *buffer;
120   GtkTextIter i, s, e;
121   gboolean res;
122
123   buffer = gtk_text_buffer_new (NULL);
124
125   gtk_text_buffer_set_text (buffer, haystack, -1);
126
127   /* search from start forward */
128   gtk_text_buffer_get_start_iter (buffer, &i);
129   res = gtk_text_iter_forward_search (&i, needle, flags, &s, &e, NULL);
130   g_assert (res == FALSE);
131
132   /* search from end backward */
133   gtk_text_buffer_get_end_iter (buffer, &i);
134   res = gtk_text_iter_backward_search (&i, needle, flags, &s, &e, NULL);
135   g_assert (res == FALSE);
136
137   g_object_unref (buffer);
138 }
139
140 static void
141 test_full_buffer (void)
142 {
143   check_found_forward ("foo", "foo", 0, 0, 3, "foo");
144   check_found_backward ("foo", "foo", 0, 0, 3, "foo");
145   check_found_forward ("foo", "foo", GTK_TEXT_SEARCH_CASE_INSENSITIVE, 0, 3, "foo");
146   check_found_backward ("foo", "foo", GTK_TEXT_SEARCH_CASE_INSENSITIVE, 0, 3, "foo");
147   check_found_forward ("foo", "Foo", GTK_TEXT_SEARCH_CASE_INSENSITIVE, 0, 3, "foo");
148   check_found_backward ("foo", "Foo", GTK_TEXT_SEARCH_CASE_INSENSITIVE, 0, 3, "foo");
149 }
150
151 static void
152 test_search (void)
153 {
154   /* simple match */
155   check_found_forward ("This is some foo text", "foo", 0, 13, 16, "foo");
156   check_found_backward ("This is some foo text", "foo", 0, 13, 16, "foo");
157   check_not_found ("This is some foo text", "Foo", 0);
158
159   /* different matches for forward and backward */
160   check_found_forward ("This is some foo foo text", "foo", 0, 13, 16, "foo");
161   check_found_backward ("This is some foo foo text", "foo", 0, 17, 20, "foo");
162
163   /* new lines in the haystack */
164   check_found_forward ("This is some\nfoo text", "foo", 0, 13, 16, "foo");
165   check_found_backward ("This is some\nfoo text", "foo", 0, 13, 16, "foo");
166   check_found_forward ("This is some foo\nfoo text", "foo", 0, 13, 16, "foo");
167   check_found_backward ("This is some foo\nfoo text", "foo", 0, 17, 20, "foo");
168   check_not_found ("This is some\nfoo text", "Foo", 0);
169
170   /* end of buffer */
171   check_found_forward ("This is some\ntext foo", "foo", 0, 18, 21, "foo");
172   check_found_backward ("This is some\ntext foo", "foo", 0, 18, 21, "foo");
173   check_not_found ("This is some\ntext foo", "Foo", 0);
174
175   /* multiple lines in the needle */
176   check_found_forward ("This is some foo\nfoo text", "foo\nfoo", 0, 13, 20, "foo\nfoo");
177   check_found_backward ("This is some foo\nfoo text", "foo\nfoo", 0, 13, 20, "foo\nfoo");
178   check_not_found ("This is some foo\nfoo text", "Foo\nfoo", 0);
179 }
180
181 static void
182 test_search_caseless (void)
183 {
184   GtkTextSearchFlags flags;
185
186   flags = GTK_TEXT_SEARCH_CASE_INSENSITIVE;
187
188   /* simple match */
189   check_found_forward ("This is some foo text", "foo", flags, 13, 16, "foo");
190   check_found_forward ("This is some foo text", "Foo", flags, 13, 16, "foo");
191   check_found_forward ("This is some Foo text", "foo", flags, 13, 16, "Foo");
192   check_found_backward ("This is some foo text", "foo", flags, 13, 16, "foo");
193   check_found_backward ("This is some foo text", "Foo", flags, 13, 16, "foo");
194   check_found_backward ("This is some Foo text", "foo", flags, 13, 16, "Foo");
195
196   /* check also that different composition of utf8 characters
197      (e.g. accented letters) match */
198
199   /* different matches for forward and backward */
200   check_found_forward ("This is some foo foo text", "foo", flags, 13, 16, "foo");
201   check_found_forward ("This is some foo foo text", "Foo", flags, 13, 16, "foo");
202   check_found_forward ("This is some Foo foo text", "foo", flags, 13, 16, "Foo");
203   check_found_forward ("This is some \303\200 \303\240 text", "\303\240", flags, 13, 14, "\303\200");
204   check_found_forward ("This is some \303\200 \303\240 text", "\303\200", flags, 13, 14, "\303\200");
205   check_found_forward ("This is some \303\200 \303\240 text", "a\u0300", flags, 13, 14, "\303\200");
206   check_found_backward ("This is some foo foo text", "foo", flags, 17, 20, "foo");
207   check_found_backward ("This is some foo foo text", "Foo", flags, 17, 20, "foo");
208   check_found_backward ("This is some foo Foo text", "foo", flags, 17, 20, "Foo");
209   check_found_backward ("This is some \303\200 \303\240 text", "\303\240", flags, 15, 16, "\303\240");
210   check_found_backward ("This is some \303\200 \303\240 text", "\303\200", flags, 15, 16, "\303\240");
211   check_found_backward ("This is some \303\200 \303\240 text", "a\u0300", flags, 15, 16, "\303\240");
212
213   /* new lines in the haystack */
214   check_found_forward ("This is some\nfoo text", "foo", flags, 13, 16, "foo");
215   check_found_forward ("This is some\nfoo text", "Foo", flags, 13, 16, "foo");
216   check_found_forward ("This is some\nFoo text", "foo", flags, 13, 16, "Foo");
217   check_found_forward ("This is some\n\303\200 text", "\303\240", flags, 13, 14, "\303\200");
218   check_found_forward ("This is some\n\303\200 text", "a\u0300", flags, 13, 14, "\303\200");
219   check_found_backward ("This is some\nfoo text", "foo", flags, 13, 16, "foo");
220   check_found_backward ("This is some\nfoo text", "Foo", flags, 13, 16, "foo");
221   check_found_backward ("This is some\nFoo text", "foo", flags, 13, 16, "Foo");
222   check_found_backward ("This is some\n\303\200 text", "\303\240", flags, 13, 14, "\303\200");
223   check_found_backward ("This is some\n\303\200 text", "a\u0300", flags, 13, 14, "\303\200");
224   check_found_forward ("This is some foo\nfoo text", "foo", flags, 13, 16, "foo");
225   check_found_forward ("This is some foo\nfoo text", "Foo", flags, 13, 16, "foo");
226   check_found_forward ("This is some Foo\nfoo text", "foo", flags, 13, 16, "Foo");
227   check_found_forward ("This is some \303\200\n\303\200 text", "\303\240", flags, 13, 14, "\303\200");
228   check_found_forward ("This is some \303\200\n\303\200 text", "a\u0300", flags, 13, 14, "\303\200");
229   check_found_backward ("This is some foo\nfoo text", "foo", flags, 17, 20, "foo");
230   check_found_backward ("This is some foo\nfoo text", "Foo", flags, 17, 20, "foo");
231   check_found_backward ("This is some foo\nFoo text", "foo", flags, 17, 20, "Foo");
232   check_found_backward ("This is some \303\200\n\303\200 text", "\303\240", flags, 15, 16, "\303\200");
233   check_found_backward ("This is some \303\200\n\303\200 text", "a\u0300", flags, 15, 16, "\303\200");
234
235   /* end of buffer */
236   check_found_forward ("This is some\ntext foo", "foo", flags, 18, 21, "foo");
237   check_found_forward ("This is some\ntext foo", "Foo", flags, 18, 21, "foo");
238   check_found_forward ("This is some\ntext Foo", "foo", flags, 18, 21, "Foo");
239   check_found_forward ("This is some\ntext \303\200", "\303\240", flags, 18, 19, "\303\200");
240   check_found_forward ("This is some\ntext \303\200", "a\u0300", flags, 18, 19, "\303\200");
241   check_found_backward ("This is some\ntext foo", "foo", flags, 18, 21, "foo");
242   check_found_backward ("This is some\ntext foo", "Foo", flags, 18, 21, "foo");
243   check_found_backward ("This is some\ntext Foo", "foo", flags, 18, 21, "Foo");
244   check_found_backward ("This is some\ntext \303\200", "\303\240", flags, 18, 19, "\303\200");
245   check_found_backward ("This is some\ntext \303\200", "a\u0300", flags, 18, 19, "\303\200");
246
247   /* multiple lines in the needle */
248   check_found_forward ("This is some foo\nfoo text", "foo\nfoo", flags, 13, 20, "foo\nfoo");
249   check_found_forward ("This is some foo\nfoo text", "Foo\nFoo", flags, 13, 20, "foo\nfoo");
250   check_found_forward ("This is some Foo\nFoo text", "foo\nfoo", flags, 13, 20, "Foo\nFoo");
251   check_found_forward ("This is some \303\200\n\303\200 text", "\303\240\n\303\240", flags, 13, 16, "\303\200\n\303\200");
252   check_found_forward ("This is some \303\200\n\303\200 text", "a\u0300\na\u0300", flags, 13, 16, "\303\200\n\303\200");
253   check_found_backward ("This is some foo\nfoo text", "foo\nfoo", flags, 13, 20, "foo\nfoo");
254   check_found_backward ("This is some foo\nfoo text", "Foo\nFoo", flags, 13, 20, "foo\nfoo");
255   check_found_backward ("This is some Foo\nFoo text", "foo\nfoo", flags, 13, 20, "Foo\nFoo");
256   check_found_backward ("This is some \303\200\n\303\200 text", "\303\240\n\303\240", flags, 13, 16, "\303\200\n\303\200");
257   check_found_backward ("This is some \303\200\n\303\200 text", "a\u0300\na\u0300", flags, 13, 16, "\303\200\n\303\200");
258 }
259
260 int
261 main (int argc, char** argv)
262 {
263   gtk_test_init (&argc, &argv);
264
265   g_test_add_func ("/TextIter/Search Empty", test_empty_search);
266   g_test_add_func ("/TextIter/Search Full Buffer", test_full_buffer);
267   g_test_add_func ("/TextIter/Search", test_search);
268   g_test_add_func ("/TextIter/Search Caseless", test_search_caseless);
269
270   return g_test_run();
271 }