Imported Upstream version 2.73.3
[platform/upstream/glib.git] / glib / tests / utf8-pointer.c
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 static void test_utf8 (gconstpointer d)
33 {
34   gint num_chars;
35   const gchar **p;
36   gint i, j;
37   const gchar *string = d;
38
39   g_assert (g_utf8_validate (string, -1, NULL));
40
41   num_chars = g_utf8_strlen (string, -1);
42
43   p = (const gchar **) g_malloc (num_chars * sizeof (gchar *));
44
45   p[0] = string;
46   for (i = 1; i < num_chars; i++)
47     p[i] = g_utf8_next_char (p[i-1]);
48
49   for (i = 0; i < num_chars; i++)
50     for (j = 0; j < num_chars; j++)
51       {
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);
54       }
55
56   g_free (p);
57 }
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 test_length (void)
71 {
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);
79
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);
83
84   g_assert (g_utf8_strlen (NULL, 0) == 0);
85
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);
92 }
93
94 static void
95 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   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;
107 #undef TEST_STR
108   gchar *str_copy = g_malloc (str_size);
109   const gchar *p;
110   const gchar *q;
111   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   TEST_SET(str_array);
168   TEST_SET(str_copy);
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 */
175
176 #undef TEST_SET
177
178   g_free (str_copy);
179 }
180
181 int main (int argc, char *argv[])
182 {
183   g_test_init (&argc, &argv, NULL);
184
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);
188
189   return g_test_run ();
190 }