1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
29 GtkTextBuffer *buffer;
33 buffer = gtk_text_buffer_new (NULL);
34 gtk_text_buffer_set_text (buffer, "This is some foo text", -1);
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);
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);
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);
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);
52 check_found_forward (const gchar *haystack,
54 GtkTextSearchFlags flags,
57 const gchar *expected_string)
59 GtkTextBuffer *buffer;
64 buffer = gtk_text_buffer_new (NULL);
66 gtk_text_buffer_set_text (buffer, haystack, -1);
68 /* TODO: add test with limit before, after and in the middle
69 of expected start and end */
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);
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);
81 g_object_unref (buffer);
85 check_found_backward (const gchar *haystack,
87 GtkTextSearchFlags flags,
90 const gchar *expected_string)
92 GtkTextBuffer *buffer;
97 buffer = gtk_text_buffer_new (NULL);
99 gtk_text_buffer_set_text (buffer, haystack, -1);
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);
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);
111 g_object_unref (buffer);
115 check_not_found (const gchar *haystack,
117 GtkTextSearchFlags flags)
119 GtkTextBuffer *buffer;
123 buffer = gtk_text_buffer_new (NULL);
125 gtk_text_buffer_set_text (buffer, haystack, -1);
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);
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);
137 g_object_unref (buffer);
141 test_full_buffer (void)
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");
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);
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");
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);
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);
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);
182 test_search_caseless (void)
184 GtkTextSearchFlags flags;
186 flags = GTK_TEXT_SEARCH_CASE_INSENSITIVE;
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");
196 /* check also that different composition of utf8 characters
197 (e.g. accented letters) match */
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");
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");
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");
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");
261 main (int argc, char** argv)
263 gtk_test_init (&argc, &argv);
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);