1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * SPDX-License-Identifier: LGPL-2.1-or-later
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.1 of the License, or (at your option) any later version.
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.
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/>.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
30 /* Test conversions between offsets and pointers */
32 static void test_utf8 (gconstpointer d)
37 const gchar *string = d;
39 g_assert (g_utf8_validate (string, -1, NULL));
41 num_chars = g_utf8_strlen (string, -1);
43 p = (const gchar **) g_malloc (num_chars * sizeof (gchar *));
46 for (i = 1; i < num_chars; i++)
47 p[i] = g_utf8_next_char (p[i-1]);
49 for (i = 0; i < num_chars; i++)
50 for (j = 0; j < num_chars; j++)
52 g_assert (g_utf8_offset_to_pointer (p[i], j - i) == p[j]);
53 g_assert (g_utf8_pointer_to_offset (p[i], p[j]) == j - i);
59 gchar *longline = "asdasdas dsaf asfd as fdasdf asfd asdf as dfas dfasdf a"
60 "asd fasdf asdf asdf asd fasfd as fdasfd asdf as fdççççççççças ffsd asfd as fdASASASAs As"
61 "Asfdsf sdfg sdfg dsfg dfg sdfgsdfgsdfgsdfg sdfgsdfg sdfg sdfg sdf gsdfg sdfg sd"
62 "asd fasdf asdf asdf asd fasfd as fdaèèèèèèè òòòòòòòòòòòòsfd asdf as fdas ffsd asfd as fdASASASAs D"
63 "Asfdsf sdfg sdfg dsfg dfg sdfgsdfgsdfgsdfg sdfgsdfg sdfgùùùùùùùùùùùùùù sdfg sdf gsdfg sdfg sd"
64 "asd fasdf asdf asdf asd fasfd as fdasfd asd@@@@@@@f as fdas ffsd asfd as fdASASASAs D "
65 "Asfdsf sdfg sdfg dsfg dfg sdfgsdfgsdfgsdfg sdfgsdf€€€€€€€€€€€€€€€€€€g sdfg sdfg sdf gsdfg sdfg sd"
66 "asd fasdf asdf asdf asd fasfd as fdasfd asdf as fdas ffsd asfd as fdASASASAs D"
67 "Asfdsf sdfg sdfg dsfg dfg sdfgsdfgsdfgsdfg sdfgsdfg sdfg sdfg sdf gsdfg sdfg sd\n\nlalala\n";
72 g_assert (g_utf8_strlen ("1234", -1) == 4);
73 g_assert (g_utf8_strlen ("1234", 0) == 0);
74 g_assert (g_utf8_strlen ("1234", 1) == 1);
75 g_assert (g_utf8_strlen ("1234", 2) == 2);
76 g_assert (g_utf8_strlen ("1234", 3) == 3);
77 g_assert (g_utf8_strlen ("1234", 4) == 4);
78 g_assert (g_utf8_strlen ("1234", 5) == 4);
80 g_assert (g_utf8_strlen (longline, -1) == 762);
81 g_assert (g_utf8_strlen (longline, strlen (longline)) == 762);
82 g_assert (g_utf8_strlen (longline, 1024) == 762);
84 g_assert (g_utf8_strlen (NULL, 0) == 0);
86 g_assert (g_utf8_strlen ("a\340\250\201c", -1) == 3);
87 g_assert (g_utf8_strlen ("a\340\250\201c", 1) == 1);
88 g_assert (g_utf8_strlen ("a\340\250\201c", 2) == 1);
89 g_assert (g_utf8_strlen ("a\340\250\201c", 3) == 1);
90 g_assert (g_utf8_strlen ("a\340\250\201c", 4) == 2);
91 g_assert (g_utf8_strlen ("a\340\250\201c", 5) == 3);
97 /* U+0B0B Oriya Letter Vocalic R (\340\254\213)
98 * U+10900 Phoenician Letter Alf (\360\220\244\200)
99 * U+0041 Latin Capital Letter A (\101)
100 * U+1EB6 Latin Capital Letter A With Breve And Dot Below (\341\272\266)
102 #define TEST_STR "\340\254\213\360\220\244\200\101\341\272\266\0\101"
103 const gsize str_size = sizeof TEST_STR;
104 const gchar *str = TEST_STR;
105 const gchar str_array[] = TEST_STR;
106 const gchar * volatile str_volatile = TEST_STR;
108 gchar *str_copy = g_malloc (str_size);
111 memcpy (str_copy, str, str_size);
113 #define TEST_SET(STR) \
115 p = STR + (str_size - 1); \
117 q = g_utf8_find_prev_char (STR, p); \
118 g_assert (q == STR + 12); \
119 q = g_utf8_find_prev_char (STR, q); \
120 g_assert (q == STR + 11); \
121 q = g_utf8_find_prev_char (STR, q); \
122 g_assert (q == STR + 8); \
123 q = g_utf8_find_prev_char (STR, q); \
124 g_assert (q == STR + 7); \
125 q = g_utf8_find_prev_char (STR, q); \
126 g_assert (q == STR + 3); \
127 q = g_utf8_find_prev_char (STR, q); \
128 g_assert (q == STR); \
129 q = g_utf8_find_prev_char (STR, q); \
133 q = g_utf8_find_prev_char (STR, p); \
134 g_assert (q == STR + 3); \
137 q = g_utf8_find_prev_char (STR, p); \
138 g_assert (q == STR); \
141 q = g_utf8_find_next_char (p, NULL); \
142 g_assert (q == STR + 3); \
143 q = g_utf8_find_next_char (q, NULL); \
144 g_assert (q == STR + 7); \
146 q = g_utf8_find_next_char (p, STR + 6); \
147 g_assert (q == STR + 3); \
148 q = g_utf8_find_next_char (q, STR + 6); \
151 q = g_utf8_find_next_char (STR, STR); \
154 q = g_utf8_find_next_char (STR + strlen (STR), NULL); \
155 g_assert (q == STR + strlen (STR) + 1); \
157 /* Check return values when reaching the end of the string, \
158 * with @end set and unset. */ \
159 q = g_utf8_find_next_char (STR + 10, NULL); \
160 g_assert_nonnull (q); \
161 g_assert (*q == '\0'); \
163 q = g_utf8_find_next_char (STR + 10, STR + 11); \
169 TEST_SET(str_volatile);
170 /* Starting with GCC 8 tests on @str with "-O2 -flto" in CFLAGS fail due to
171 * (incorrect?) constant propagation of @str into @g_utf8_find_prev_char. It
172 * doesn't happen if @TEST_STR doesn't contain \0 in the middle but the tests
173 * should cover all corner cases.
174 * For instance, see https://gitlab.gnome.org/GNOME/glib/issues/1917 */
181 int main (int argc, char *argv[])
183 g_test_init (&argc, &argv, NULL);
185 g_test_add_data_func ("/utf8/offsets", longline, test_utf8);
186 g_test_add_func ("/utf8/lengths", test_length);
187 g_test_add_func ("/utf8/find", test_find);
189 return g_test_run ();