Branch data Line data Source code
1 : : /* GLIB - Library of useful routines for C programming
2 : : * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 : : *
4 : : * SPDX-License-Identifier: LGPL-2.1-or-later
5 : : *
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.
10 : : *
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.
15 : : *
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/>.
18 : : */
19 : :
20 : : /*
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/.
25 : : */
26 : :
27 : : #include <string.h>
28 : : #include <glib.h>
29 : :
30 : : /* Test conversions between offsets and pointers */
31 : :
32 : 1 : static void test_utf8 (gconstpointer d)
33 : : {
34 : : gint num_chars;
35 : : const gchar **p;
36 : : gint i, j;
37 : 1 : const gchar *string = d;
38 : :
39 : 1 : g_assert (g_utf8_validate (string, -1, NULL));
40 : :
41 : 1 : num_chars = g_utf8_strlen (string, -1);
42 : :
43 : 1 : p = (const gchar **) g_malloc (num_chars * sizeof (gchar *));
44 : :
45 : 1 : p[0] = string;
46 [ + + ]: 762 : for (i = 1; i < num_chars; i++)
47 : 761 : p[i] = g_utf8_next_char (p[i-1]);
48 : :
49 [ + + ]: 763 : for (i = 0; i < num_chars; i++)
50 [ + + ]: 581406 : for (j = 0; j < num_chars; j++)
51 : : {
52 : 580644 : g_assert (g_utf8_offset_to_pointer (p[i], j - i) == p[j]);
53 : 580644 : g_assert (g_utf8_pointer_to_offset (p[i], p[j]) == j - i);
54 : : }
55 : :
56 : 1 : g_free (p);
57 : 1 : }
58 : :
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";
68 : :
69 : : static void
70 : 1 : test_length (void)
71 : : {
72 : 1 : g_assert (g_utf8_strlen ("1234", -1) == 4);
73 : 1 : g_assert (g_utf8_strlen ("1234", 0) == 0);
74 : 1 : g_assert (g_utf8_strlen ("1234", 1) == 1);
75 : 1 : g_assert (g_utf8_strlen ("1234", 2) == 2);
76 : 1 : g_assert (g_utf8_strlen ("1234", 3) == 3);
77 : 1 : g_assert (g_utf8_strlen ("1234", 4) == 4);
78 : 1 : g_assert (g_utf8_strlen ("1234", 5) == 4);
79 : :
80 : 1 : g_assert (g_utf8_strlen (longline, -1) == 762);
81 : 1 : g_assert (g_utf8_strlen (longline, strlen (longline)) == 762);
82 : 1 : g_assert (g_utf8_strlen (longline, 1024) == 762);
83 : :
84 : 1 : g_assert (g_utf8_strlen (NULL, 0) == 0);
85 : :
86 : 1 : g_assert (g_utf8_strlen ("a\340\250\201c", -1) == 3);
87 : 1 : g_assert (g_utf8_strlen ("a\340\250\201c", 1) == 1);
88 : 1 : g_assert (g_utf8_strlen ("a\340\250\201c", 2) == 1);
89 : 1 : g_assert (g_utf8_strlen ("a\340\250\201c", 3) == 1);
90 : 1 : g_assert (g_utf8_strlen ("a\340\250\201c", 4) == 2);
91 : 1 : g_assert (g_utf8_strlen ("a\340\250\201c", 5) == 3);
92 : 1 : }
93 : :
94 : : static void
95 : 1 : test_find (void)
96 : : {
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)
101 : : */
102 : : #define TEST_STR "\340\254\213\360\220\244\200\101\341\272\266\0\101"
103 : 1 : const gsize str_size = sizeof TEST_STR;
104 : 1 : const gchar *str = TEST_STR;
105 : 1 : const gchar str_array[] = TEST_STR;
106 : 1 : const gchar * volatile str_volatile = TEST_STR;
107 : : #undef TEST_STR
108 : 1 : gchar *str_copy = g_malloc (str_size);
109 : : const gchar *p;
110 : : const gchar *q;
111 : 1 : memcpy (str_copy, str, str_size);
112 : :
113 : : #define TEST_SET(STR) \
114 : : G_STMT_START { \
115 : : p = STR + (str_size - 1); \
116 : : \
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); \
130 : : g_assert_null (q); \
131 : : \
132 : : p = STR + 4; \
133 : : q = g_utf8_find_prev_char (STR, p); \
134 : : g_assert (q == STR + 3); \
135 : : \
136 : : p = STR + 2; \
137 : : q = g_utf8_find_prev_char (STR, p); \
138 : : g_assert (q == STR); \
139 : : \
140 : : p = STR + 2; \
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); \
145 : : \
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); \
149 : : g_assert_null (q); \
150 : : \
151 : : q = g_utf8_find_next_char (STR, STR); \
152 : : g_assert_null (q); \
153 : : \
154 : : q = g_utf8_find_next_char (STR + strlen (STR), NULL); \
155 : : g_assert (q == STR + strlen (STR) + 1); \
156 : : \
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'); \
162 : : \
163 : : q = g_utf8_find_next_char (STR + 10, STR + 11); \
164 : : g_assert_null (q); \
165 : : } G_STMT_END
166 : :
167 [ - + - + : 1 : TEST_SET(str_array);
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + ]
168 [ - + - + : 1 : TEST_SET(str_copy);
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + ]
169 [ - + - + : 1 : 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 */
175 : :
176 : : #undef TEST_SET
177 : :
178 : 1 : g_free (str_copy);
179 : 1 : }
180 : :
181 : 1 : int main (int argc, char *argv[])
182 : : {
183 : 1 : g_test_init (&argc, &argv, NULL);
184 : :
185 : 1 : g_test_add_data_func ("/utf8/offsets", longline, test_utf8);
186 : 1 : g_test_add_func ("/utf8/lengths", test_length);
187 : 1 : g_test_add_func ("/utf8/find", test_find);
188 : :
189 : 1 : return g_test_run ();
190 : : }
|