]> Pileus Git - ~andy/gtk/blob - gtk/tests/textiter.c
Change FSF Address
[~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, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "config.h"
19 #include <stdio.h>
20 #include <string.h>
21
22 #include <gtk/gtk.h>
23
24 static void
25 test_empty_search ()
26 {
27   GtkTextBuffer *buffer;
28   GtkTextIter it, s, e;
29   gboolean res;
30
31   buffer = gtk_text_buffer_new (NULL);
32   gtk_text_buffer_set_text (buffer, "This is some foo text", -1);
33
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);
37   g_assert (res);
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);
40
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);
44   g_assert (res);
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);
47 }
48
49 static void
50 check_found_forward (const gchar *haystack,
51                      const gchar *needle,
52                      GtkTextSearchFlags flags,
53                      int expected_start,
54                      int expected_end,
55                      const gchar *expected_string)
56 {
57   GtkTextBuffer *buffer;
58   GtkTextIter i, s, e;
59   gboolean res;
60   gchar *text;
61
62   buffer = gtk_text_buffer_new (NULL);
63
64   gtk_text_buffer_set_text (buffer, haystack, -1);
65
66   /* TODO: add test with limit before, after and in the middle
67      of expected start and end */
68
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);
72   g_assert (res);
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);
77   g_free (text);
78
79   g_object_unref (buffer);
80 }
81
82 static void
83 check_found_backward (const gchar *haystack,
84                       const gchar *needle,
85                       GtkTextSearchFlags flags,
86                       int expected_start,
87                       int expected_end,
88                       const gchar *expected_string)
89 {
90   GtkTextBuffer *buffer;
91   GtkTextIter i, s, e;
92   gboolean res;
93   gchar *text;
94
95   buffer = gtk_text_buffer_new (NULL);
96
97   gtk_text_buffer_set_text (buffer, haystack, -1);
98
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);
102   g_assert (res);
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);
107   g_free (text);
108
109   g_object_unref (buffer);
110 }
111
112 static void
113 check_not_found (const gchar *haystack,
114                  const gchar *needle,
115                  GtkTextSearchFlags flags)
116 {
117   GtkTextBuffer *buffer;
118   GtkTextIter i, s, e;
119   gboolean res;
120
121   buffer = gtk_text_buffer_new (NULL);
122
123   gtk_text_buffer_set_text (buffer, haystack, -1);
124
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);
129
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);
134
135   g_object_unref (buffer);
136 }
137
138 static void
139 test_full_buffer (void)
140 {
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");
147 }
148
149 static void
150 test_search (void)
151 {
152   /* simple match */
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);
156
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");
160
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);
167
168   /* end of buffer */
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);
172
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);
177 }
178
179 static void
180 test_search_caseless (void)
181 {
182   GtkTextSearchFlags flags;
183
184   flags = GTK_TEXT_SEARCH_CASE_INSENSITIVE;
185
186   /* simple match */
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");
193
194   /* check also that different composition of utf8 characters
195      (e.g. accented letters) match */
196
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");
210
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");
232
233   /* end of buffer */
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");
244
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");
256 }
257
258 int
259 main (int argc, char** argv)
260 {
261   gtk_test_init (&argc, &argv);
262
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);
267
268   return g_test_run();
269 }