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, see <http://www.gnu.org/licenses/>.
27 GtkTextBuffer *buffer;
31 buffer = gtk_text_buffer_new (NULL);
32 gtk_text_buffer_set_text (buffer, "This is some foo text", -1);
34 /* search from start forward */
35 gtk_text_buffer_get_start_iter (buffer, &it);
36 res = gtk_text_iter_forward_search (&it, "", 0, &s, &e, NULL);
38 g_assert_cmpint (gtk_text_iter_get_offset (&s), ==, gtk_text_iter_get_offset (&e));
39 g_assert_cmpint (gtk_text_iter_get_offset (&s), ==, 1);
41 /* search from end backward */
42 gtk_text_buffer_get_end_iter (buffer, &it);
43 res = gtk_text_iter_backward_search (&it, "", 0, &s, &e, NULL);
45 g_assert_cmpint (gtk_text_iter_get_offset (&s), ==, gtk_text_iter_get_offset (&e));
46 g_assert_cmpint (gtk_text_iter_get_offset (&s), ==, 20);
50 check_found_forward (const gchar *haystack,
52 GtkTextSearchFlags flags,
55 const gchar *expected_string)
57 GtkTextBuffer *buffer;
62 buffer = gtk_text_buffer_new (NULL);
64 gtk_text_buffer_set_text (buffer, haystack, -1);
66 /* TODO: add test with limit before, after and in the middle
67 of expected start and end */
69 /* search from start forward */
70 gtk_text_buffer_get_start_iter (buffer, &i);
71 res = gtk_text_iter_forward_search (&i, needle, flags, &s, &e, NULL);
73 g_assert_cmpint (expected_start, ==, gtk_text_iter_get_offset (&s));
74 g_assert_cmpint (expected_end, ==, gtk_text_iter_get_offset (&e));
75 text = gtk_text_iter_get_text (&s, &e);
76 g_assert_cmpstr (expected_string, ==, text);
79 g_object_unref (buffer);
83 check_found_backward (const gchar *haystack,
85 GtkTextSearchFlags flags,
88 const gchar *expected_string)
90 GtkTextBuffer *buffer;
95 buffer = gtk_text_buffer_new (NULL);
97 gtk_text_buffer_set_text (buffer, haystack, -1);
99 /* search from end backward */
100 gtk_text_buffer_get_end_iter (buffer, &i);
101 res = gtk_text_iter_backward_search (&i, needle, flags, &s, &e, NULL);
103 g_assert_cmpint (expected_start, ==, gtk_text_iter_get_offset (&s));
104 g_assert_cmpint (expected_end, ==, gtk_text_iter_get_offset (&e));
105 text = gtk_text_iter_get_text (&s, &e);
106 g_assert_cmpstr (expected_string, ==, text);
109 g_object_unref (buffer);
113 check_not_found (const gchar *haystack,
115 GtkTextSearchFlags flags)
117 GtkTextBuffer *buffer;
121 buffer = gtk_text_buffer_new (NULL);
123 gtk_text_buffer_set_text (buffer, haystack, -1);
125 /* search from start forward */
126 gtk_text_buffer_get_start_iter (buffer, &i);
127 res = gtk_text_iter_forward_search (&i, needle, flags, &s, &e, NULL);
128 g_assert (res == FALSE);
130 /* search from end backward */
131 gtk_text_buffer_get_end_iter (buffer, &i);
132 res = gtk_text_iter_backward_search (&i, needle, flags, &s, &e, NULL);
133 g_assert (res == FALSE);
135 g_object_unref (buffer);
139 test_full_buffer (void)
141 check_found_forward ("foo", "foo", 0, 0, 3, "foo");
142 check_found_backward ("foo", "foo", 0, 0, 3, "foo");
143 check_found_forward ("foo", "foo", GTK_TEXT_SEARCH_CASE_INSENSITIVE, 0, 3, "foo");
144 check_found_backward ("foo", "foo", GTK_TEXT_SEARCH_CASE_INSENSITIVE, 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");
153 check_found_forward ("This is some foo text", "foo", 0, 13, 16, "foo");
154 check_found_backward ("This is some foo text", "foo", 0, 13, 16, "foo");
155 check_not_found ("This is some foo text", "Foo", 0);
157 /* different matches for forward and backward */
158 check_found_forward ("This is some foo foo text", "foo", 0, 13, 16, "foo");
159 check_found_backward ("This is some foo foo text", "foo", 0, 17, 20, "foo");
161 /* new lines in the haystack */
162 check_found_forward ("This is some\nfoo text", "foo", 0, 13, 16, "foo");
163 check_found_backward ("This is some\nfoo text", "foo", 0, 13, 16, "foo");
164 check_found_forward ("This is some foo\nfoo text", "foo", 0, 13, 16, "foo");
165 check_found_backward ("This is some foo\nfoo text", "foo", 0, 17, 20, "foo");
166 check_not_found ("This is some\nfoo text", "Foo", 0);
169 check_found_forward ("This is some\ntext foo", "foo", 0, 18, 21, "foo");
170 check_found_backward ("This is some\ntext foo", "foo", 0, 18, 21, "foo");
171 check_not_found ("This is some\ntext foo", "Foo", 0);
173 /* multiple lines in the needle */
174 check_found_forward ("This is some foo\nfoo text", "foo\nfoo", 0, 13, 20, "foo\nfoo");
175 check_found_backward ("This is some foo\nfoo text", "foo\nfoo", 0, 13, 20, "foo\nfoo");
176 check_not_found ("This is some foo\nfoo text", "Foo\nfoo", 0);
180 test_search_caseless (void)
182 GtkTextSearchFlags flags;
184 flags = GTK_TEXT_SEARCH_CASE_INSENSITIVE;
187 check_found_forward ("This is some foo text", "foo", flags, 13, 16, "foo");
188 check_found_forward ("This is some foo text", "Foo", flags, 13, 16, "foo");
189 check_found_forward ("This is some Foo text", "foo", flags, 13, 16, "Foo");
190 check_found_backward ("This is some foo text", "foo", flags, 13, 16, "foo");
191 check_found_backward ("This is some foo text", "Foo", flags, 13, 16, "foo");
192 check_found_backward ("This is some Foo text", "foo", flags, 13, 16, "Foo");
194 /* check also that different composition of utf8 characters
195 (e.g. accented letters) match */
197 /* different matches for forward and backward */
198 check_found_forward ("This is some foo foo text", "foo", flags, 13, 16, "foo");
199 check_found_forward ("This is some foo foo text", "Foo", flags, 13, 16, "foo");
200 check_found_forward ("This is some Foo foo text", "foo", flags, 13, 16, "Foo");
201 check_found_forward ("This is some \303\200 \303\240 text", "\303\240", flags, 13, 14, "\303\200");
202 check_found_forward ("This is some \303\200 \303\240 text", "\303\200", flags, 13, 14, "\303\200");
203 check_found_forward ("This is some \303\200 \303\240 text", "a\u0300", flags, 13, 14, "\303\200");
204 check_found_backward ("This is some foo foo text", "foo", flags, 17, 20, "foo");
205 check_found_backward ("This is some foo foo text", "Foo", flags, 17, 20, "foo");
206 check_found_backward ("This is some foo Foo text", "foo", flags, 17, 20, "Foo");
207 check_found_backward ("This is some \303\200 \303\240 text", "\303\240", flags, 15, 16, "\303\240");
208 check_found_backward ("This is some \303\200 \303\240 text", "\303\200", flags, 15, 16, "\303\240");
209 check_found_backward ("This is some \303\200 \303\240 text", "a\u0300", flags, 15, 16, "\303\240");
211 /* new lines in the haystack */
212 check_found_forward ("This is some\nfoo text", "foo", flags, 13, 16, "foo");
213 check_found_forward ("This is some\nfoo text", "Foo", flags, 13, 16, "foo");
214 check_found_forward ("This is some\nFoo text", "foo", flags, 13, 16, "Foo");
215 check_found_forward ("This is some\n\303\200 text", "\303\240", flags, 13, 14, "\303\200");
216 check_found_forward ("This is some\n\303\200 text", "a\u0300", flags, 13, 14, "\303\200");
217 check_found_backward ("This is some\nfoo text", "foo", flags, 13, 16, "foo");
218 check_found_backward ("This is some\nfoo text", "Foo", flags, 13, 16, "foo");
219 check_found_backward ("This is some\nFoo text", "foo", flags, 13, 16, "Foo");
220 check_found_backward ("This is some\n\303\200 text", "\303\240", flags, 13, 14, "\303\200");
221 check_found_backward ("This is some\n\303\200 text", "a\u0300", flags, 13, 14, "\303\200");
222 check_found_forward ("This is some foo\nfoo text", "foo", flags, 13, 16, "foo");
223 check_found_forward ("This is some foo\nfoo text", "Foo", flags, 13, 16, "foo");
224 check_found_forward ("This is some Foo\nfoo text", "foo", flags, 13, 16, "Foo");
225 check_found_forward ("This is some \303\200\n\303\200 text", "\303\240", flags, 13, 14, "\303\200");
226 check_found_forward ("This is some \303\200\n\303\200 text", "a\u0300", flags, 13, 14, "\303\200");
227 check_found_backward ("This is some foo\nfoo text", "foo", flags, 17, 20, "foo");
228 check_found_backward ("This is some foo\nfoo text", "Foo", flags, 17, 20, "foo");
229 check_found_backward ("This is some foo\nFoo text", "foo", flags, 17, 20, "Foo");
230 check_found_backward ("This is some \303\200\n\303\200 text", "\303\240", flags, 15, 16, "\303\200");
231 check_found_backward ("This is some \303\200\n\303\200 text", "a\u0300", flags, 15, 16, "\303\200");
234 check_found_forward ("This is some\ntext foo", "foo", flags, 18, 21, "foo");
235 check_found_forward ("This is some\ntext foo", "Foo", flags, 18, 21, "foo");
236 check_found_forward ("This is some\ntext Foo", "foo", flags, 18, 21, "Foo");
237 check_found_forward ("This is some\ntext \303\200", "\303\240", flags, 18, 19, "\303\200");
238 check_found_forward ("This is some\ntext \303\200", "a\u0300", flags, 18, 19, "\303\200");
239 check_found_backward ("This is some\ntext foo", "foo", flags, 18, 21, "foo");
240 check_found_backward ("This is some\ntext foo", "Foo", flags, 18, 21, "foo");
241 check_found_backward ("This is some\ntext Foo", "foo", flags, 18, 21, "Foo");
242 check_found_backward ("This is some\ntext \303\200", "\303\240", flags, 18, 19, "\303\200");
243 check_found_backward ("This is some\ntext \303\200", "a\u0300", flags, 18, 19, "\303\200");
245 /* multiple lines in the needle */
246 check_found_forward ("This is some foo\nfoo text", "foo\nfoo", flags, 13, 20, "foo\nfoo");
247 check_found_forward ("This is some foo\nfoo text", "Foo\nFoo", flags, 13, 20, "foo\nfoo");
248 check_found_forward ("This is some Foo\nFoo text", "foo\nfoo", flags, 13, 20, "Foo\nFoo");
249 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");
250 check_found_forward ("This is some \303\200\n\303\200 text", "a\u0300\na\u0300", flags, 13, 16, "\303\200\n\303\200");
251 check_found_backward ("This is some foo\nfoo text", "foo\nfoo", flags, 13, 20, "foo\nfoo");
252 check_found_backward ("This is some foo\nfoo text", "Foo\nFoo", flags, 13, 20, "foo\nfoo");
253 check_found_backward ("This is some Foo\nFoo text", "foo\nfoo", flags, 13, 20, "Foo\nFoo");
254 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");
255 check_found_backward ("This is some \303\200\n\303\200 text", "a\u0300\na\u0300", flags, 13, 16, "\303\200\n\303\200");
259 main (int argc, char** argv)
261 gtk_test_init (&argc, &argv);
263 g_test_add_func ("/TextIter/Search Empty", test_empty_search);
264 g_test_add_func ("/TextIter/Search Full Buffer", test_full_buffer);
265 g_test_add_func ("/TextIter/Search", test_search);
266 g_test_add_func ("/TextIter/Search Caseless", test_search_caseless);