Branch data Line data Source code
1 : : /* Unit tests for gstrfuncs
2 : : * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 : : *
4 : : * SPDX-License-Identifier: LicenseRef-old-glib-tests
5 : : *
6 : : * This work is provided "as is"; redistribution and modification
7 : : * in whole or in part, in any medium, physical or electronic is
8 : : * permitted without restriction.
9 : : *
10 : : * This work is distributed in the hope that it will be useful,
11 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 : : *
14 : : * In no event shall the authors or contributors be liable for any
15 : : * direct, indirect, incidental, special, exemplary, or consequential
16 : : * damages (including, but not limited to, procurement of substitute
17 : : * goods or services; loss of use, data, or profits; or business
18 : : * interruption) however caused and on any theory of liability, whether
19 : : * in contract, strict liability, or tort (including negligence or
20 : : * otherwise) arising in any way out of the use of this software, even
21 : : * if advised of the possibility of such damage.
22 : : */
23 : :
24 : : #ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
25 : : #define GLIB_DISABLE_DEPRECATION_WARNINGS
26 : : #endif
27 : :
28 : : #define _XOPEN_SOURCE 600
29 : : #include <ctype.h>
30 : : #include <errno.h>
31 : : #include <locale.h>
32 : : #include <math.h>
33 : : #include <stdarg.h>
34 : : #include <stdio.h>
35 : : #include <stdlib.h>
36 : : #include <string.h>
37 : : #include "glib.h"
38 : :
39 : : #if defined (_MSC_VER) && (_MSC_VER <= 1800)
40 : : #define isnan(x) _isnan(x)
41 : :
42 : : #ifndef NAN
43 : : static const unsigned long __nan[2] = {0xffffffff, 0x7fffffff};
44 : : #define NAN (*(const float *) __nan)
45 : : #endif
46 : :
47 : : #ifndef INFINITY
48 : : #define INFINITY HUGE_VAL
49 : : #endif
50 : :
51 : : #endif
52 : :
53 : : #define GLIB_TEST_STRING "el dorado "
54 : :
55 : : #define FOR_ALL_CTYPE(macro) \
56 : : macro(isalnum) \
57 : : macro(isalpha) \
58 : : macro(iscntrl) \
59 : : macro(isdigit) \
60 : : macro(isgraph) \
61 : : macro(islower) \
62 : : macro(isprint) \
63 : : macro(ispunct) \
64 : : macro(isspace) \
65 : : macro(isupper) \
66 : : macro(isxdigit)
67 : :
68 : : #define DEFINE_CALL_CTYPE(function) \
69 : : static int \
70 : : call_##function (int c) \
71 : : { \
72 : : return function (c); \
73 : : }
74 : :
75 : : #define DEFINE_CALL_G_ASCII_CTYPE(function) \
76 : : static gboolean \
77 : : call_g_ascii_##function (gchar c) \
78 : : { \
79 : : return g_ascii_##function (c); \
80 : : }
81 : :
82 : 1408 : FOR_ALL_CTYPE (DEFINE_CALL_CTYPE)
83 : 2816 : FOR_ALL_CTYPE (DEFINE_CALL_G_ASCII_CTYPE)
84 : :
85 : : static void
86 : 11 : test_is_function (const char *name,
87 : : gboolean (* ascii_function) (gchar),
88 : : int (* c_library_function) (int),
89 : : gboolean (* unicode_function) (gunichar))
90 : : {
91 : : int c;
92 : :
93 [ + + ]: 1419 : for (c = 0; c <= 0x7F; c++)
94 : : {
95 : 1408 : gboolean ascii_result = ascii_function ((gchar)c);
96 : 1408 : gboolean c_library_result = c_library_function (c) != 0;
97 : 1408 : gboolean unicode_result = unicode_function ((gunichar) c);
98 [ + + - + ]: 1408 : if (ascii_result != c_library_result && c != '\v')
99 : : {
100 : 0 : g_error ("g_ascii_%s returned %d and %s returned %d for 0x%X",
101 : : name, ascii_result, name, c_library_result, c);
102 : : }
103 [ - + ]: 1408 : if (ascii_result != unicode_result)
104 : : {
105 : 0 : g_error ("g_ascii_%s returned %d and g_unichar_%s returned %d for 0x%X",
106 : : name, ascii_result, name, unicode_result, c);
107 : : }
108 : : }
109 [ + + ]: 1419 : for (c = 0x80; c <= 0xFF; c++)
110 : : {
111 : 1408 : gboolean ascii_result = ascii_function ((gchar)c);
112 [ - + ]: 1408 : if (ascii_result)
113 : : {
114 : 0 : g_error ("g_ascii_%s returned TRUE for 0x%X", name, c);
115 : : }
116 : : }
117 : 11 : }
118 : :
119 : : static void
120 : 2 : test_to_function (const char *name,
121 : : gchar (* ascii_function) (gchar),
122 : : int (* c_library_function) (int),
123 : : gunichar (* unicode_function) (gunichar))
124 : : {
125 : : int c;
126 : :
127 [ + + ]: 258 : for (c = 0; c <= 0x7F; c++)
128 : : {
129 : 256 : int ascii_result = (guchar) ascii_function ((gchar) c);
130 : 256 : int c_library_result = c_library_function (c);
131 : 256 : int unicode_result = unicode_function ((gunichar) c);
132 [ - + ]: 256 : if (ascii_result != c_library_result)
133 : : {
134 : 0 : g_error ("g_ascii_%s returned 0x%X and %s returned 0x%X for 0x%X",
135 : : name, ascii_result, name, c_library_result, c);
136 : : }
137 [ - + ]: 256 : if (ascii_result != unicode_result)
138 : : {
139 : 0 : g_error ("g_ascii_%s returned 0x%X and g_unichar_%s returned 0x%X for 0x%X",
140 : : name, ascii_result, name, unicode_result, c);
141 : : }
142 : : }
143 [ + + ]: 258 : for (c = 0x80; c <= 0xFF; c++)
144 : : {
145 : 256 : int ascii_result = (guchar) ascii_function ((gchar) c);
146 [ - + ]: 256 : if (ascii_result != c)
147 : : {
148 : 0 : g_error ("g_ascii_%s returned 0x%X for 0x%X",
149 : : name, ascii_result, c);
150 : : }
151 : : }
152 : 2 : }
153 : :
154 : : static void
155 : 2 : test_digit_function (const char *name,
156 : : int (* ascii_function) (gchar),
157 : : int (* unicode_function) (gunichar))
158 : : {
159 : : int c;
160 : :
161 [ + + ]: 258 : for (c = 0; c <= 0x7F; c++)
162 : : {
163 : 256 : int ascii_result = ascii_function ((gchar) c);
164 : 256 : int unicode_result = unicode_function ((gunichar) c);
165 [ - + ]: 256 : if (ascii_result != unicode_result)
166 : : {
167 : 0 : g_error ("g_ascii_%s_value returned %d and g_unichar_%s_value returned %d for 0x%X",
168 : : name, ascii_result, name, unicode_result, c);
169 : : }
170 : : }
171 [ + + ]: 258 : for (c = 0x80; c <= 0xFF; c++)
172 : : {
173 : 256 : int ascii_result = ascii_function ((gchar) c);
174 [ - + ]: 256 : if (ascii_result != -1)
175 : : {
176 : 0 : g_error ("g_ascii_%s_value returned %d for 0x%X",
177 : : name, ascii_result, c);
178 : : }
179 : : }
180 : 2 : }
181 : :
182 : : static void
183 : 1 : test_is_to_digit (void)
184 : : {
185 : : #define TEST_IS(name) test_is_function (#name, call_g_ascii_##name, call_##name, g_unichar_##name);
186 : :
187 : 1 : FOR_ALL_CTYPE(TEST_IS)
188 : :
189 : : #undef TEST_IS
190 : :
191 : : #define TEST_TO(name) test_to_function (#name, g_ascii_##name, name, g_unichar_##name)
192 : :
193 : 1 : TEST_TO (tolower);
194 : 1 : TEST_TO (toupper);
195 : :
196 : : #undef TEST_TO
197 : :
198 : : #define TEST_DIGIT(name) test_digit_function (#name, g_ascii_##name##_value, g_unichar_##name##_value)
199 : :
200 : 1 : TEST_DIGIT (digit);
201 : 1 : TEST_DIGIT (xdigit);
202 : :
203 : : #undef TEST_DIGIT
204 : 1 : }
205 : :
206 : : /* Testing g_memdup() function with various positive and negative cases */
207 : : static void
208 : 1 : test_memdup (void)
209 : : {
210 : : G_GNUC_BEGIN_IGNORE_DEPRECATIONS
211 : :
212 : 1 : gchar *str_dup = NULL;
213 : 1 : const gchar *str = "The quick brown fox jumps over the lazy dog";
214 : :
215 : : /* Testing negative cases */
216 : 1 : g_assert_null (g_memdup (NULL, 1024));
217 : 1 : g_assert_null (g_memdup (str, 0));
218 : 1 : g_assert_null (g_memdup (NULL, 0));
219 : :
220 : : /* Testing normal usage cases */
221 : 1 : str_dup = g_memdup (str, strlen (str) + 1);
222 : 1 : g_assert_nonnull (str_dup);
223 : 1 : g_assert_cmpstr (str, ==, str_dup);
224 : :
225 : 1 : g_free (str_dup);
226 : :
227 : : G_GNUC_END_IGNORE_DEPRECATIONS
228 : 1 : }
229 : :
230 : : /* Testing g_memdup2() function with various positive and negative cases */
231 : : static void
232 : 1 : test_memdup2 (void)
233 : : {
234 : 1 : gchar *str_dup = NULL;
235 : 1 : const gchar *str = "The quick brown fox jumps over the lazy dog";
236 : :
237 : : /* Testing negative cases */
238 : 1 : g_assert_null (g_memdup2 (NULL, 1024));
239 : 1 : g_assert_null (g_memdup2 (str, 0));
240 : 1 : g_assert_null (g_memdup2 (NULL, 0));
241 : :
242 : : /* Testing normal usage cases */
243 : 1 : str_dup = g_memdup2 (str, strlen (str) + 1);
244 : 1 : g_assert_nonnull (str_dup);
245 : 1 : g_assert_cmpstr (str, ==, str_dup);
246 : :
247 : 1 : g_free (str_dup);
248 : 1 : }
249 : :
250 : : /* Testing g_strpcpy() function with various positive and negative cases */
251 : : static void
252 : 1 : test_stpcpy (void)
253 : : {
254 : 1 : gchar *str = "The quick brown fox jumps over the lazy dog";
255 : 1 : gchar str_cpy[45], *str_cpy_end = NULL;
256 : :
257 [ + - ]: 1 : if (g_test_undefined ())
258 : : {
259 : : /* Testing degenerated cases */
260 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
261 : : "*assertion*!= NULL*");
262 : 1 : str_cpy_end = g_stpcpy (str_cpy, NULL);
263 : 1 : g_test_assert_expected_messages ();
264 : :
265 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
266 : : "*assertion*!= NULL*");
267 : 1 : str_cpy_end = g_stpcpy (NULL, str);
268 : 1 : g_test_assert_expected_messages ();
269 : : }
270 : :
271 : : /* Testing normal usage cases */
272 : 1 : str_cpy_end = g_stpcpy (str_cpy, str);
273 : : g_assert_nonnull (str_cpy);
274 : 1 : g_assert_true (str_cpy + strlen (str) == str_cpy_end);
275 : 1 : g_assert_cmpstr (str, ==, str_cpy);
276 : 1 : g_assert_cmpstr (str, ==, str_cpy_end - strlen (str));
277 : 1 : }
278 : :
279 : : /* Testing g_strlcpy() function with various positive and negative cases */
280 : : static void
281 : 1 : test_strlcpy (void)
282 : : {
283 : 1 : gchar *str = "The quick brown fox jumps over the lazy dog";
284 : : gchar str_cpy[45];
285 : 1 : gsize str_cpy_size = 0;
286 : :
287 [ + - ]: 1 : if (g_test_undefined ())
288 : : {
289 : : /* Testing degenerated cases */
290 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
291 : : "*assertion*!= NULL*");
292 : 1 : str_cpy_size = g_strlcpy (str_cpy, NULL, 0);
293 : 1 : g_test_assert_expected_messages ();
294 : : /* Returned 0 because g_strlcpy() failed */
295 : 1 : g_assert_cmpint (str_cpy_size, ==, 0);
296 : :
297 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
298 : : "*assertion*!= NULL*");
299 : 1 : str_cpy_size = g_strlcpy (NULL, str, 0);
300 : 1 : g_test_assert_expected_messages ();
301 : : /* Returned 0 because g_strlcpy() failed */
302 : 1 : g_assert_cmpint (str_cpy_size, ==, 0);
303 : : }
304 : :
305 : 1 : str_cpy_size = g_strlcpy (str_cpy, "", 0);
306 : 1 : g_assert_cmpint (str_cpy_size, ==, strlen (""));
307 : :
308 : : /* Testing normal usage cases.
309 : : * Note that the @dest_size argument to g_strlcpy() is normally meant to be
310 : : * set to `sizeof (dest)`. We set it to various values `≤ sizeof (str_cpy)`
311 : : * for testing purposes. */
312 : 1 : str_cpy_size = g_strlcpy (str_cpy, str, strlen (str) + 1);
313 : : g_assert_nonnull (str_cpy);
314 : 1 : g_assert_cmpstr (str, ==, str_cpy);
315 : 1 : g_assert_cmpint (str_cpy_size, ==, strlen (str));
316 : :
317 : 1 : str_cpy_size = g_strlcpy (str_cpy, str, strlen (str));
318 : : g_assert_nonnull (str_cpy);
319 : 1 : g_assert_cmpstr ("The quick brown fox jumps over the lazy do", ==, str_cpy);
320 : 1 : g_assert_cmpint (str_cpy_size, ==, strlen (str));
321 : :
322 : 1 : str_cpy_size = g_strlcpy (str_cpy, str, strlen (str) - 15);
323 : : g_assert_nonnull (str_cpy);
324 : 1 : g_assert_cmpstr ("The quick brown fox jumps o", ==, str_cpy);
325 : 1 : g_assert_cmpint (str_cpy_size, ==, strlen (str));
326 : :
327 : 1 : str_cpy_size = g_strlcpy (str_cpy, str, 0);
328 : : g_assert_nonnull (str_cpy);
329 : 1 : g_assert_cmpstr ("The quick brown fox jumps o", ==, str_cpy);
330 : 1 : g_assert_cmpint (str_cpy_size, ==, strlen (str));
331 : :
332 : 1 : str_cpy_size = g_strlcpy (str_cpy, str, strlen (str) + 15);
333 : : g_assert_nonnull (str_cpy);
334 : 1 : g_assert_cmpstr (str, ==, str_cpy);
335 : 1 : g_assert_cmpint (str_cpy_size, ==, strlen (str));
336 : 1 : }
337 : :
338 : : /* Testing g_strlcat() function with various positive and negative cases */
339 : : static void
340 : 1 : test_strlcat (void)
341 : : {
342 : 1 : gchar *str = "The quick brown fox jumps over the lazy dog";
343 : 1 : gchar str_cpy[60] = { 0 };
344 : 1 : gsize str_cpy_size = 0;
345 : :
346 [ + - ]: 1 : if (g_test_undefined ())
347 : : {
348 : : /* Testing degenerated cases */
349 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
350 : : "*assertion*!= NULL*");
351 : 1 : str_cpy_size = g_strlcat (str_cpy, NULL, 0);
352 : 1 : g_test_assert_expected_messages ();
353 : : /* Returned 0 because g_strlcpy() failed */
354 : 1 : g_assert_cmpint (str_cpy_size, ==, 0);
355 : :
356 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
357 : : "*assertion*!= NULL*");
358 : 1 : str_cpy_size = g_strlcat (NULL, str, 0);
359 : 1 : g_test_assert_expected_messages ();
360 : : /* Returned 0 because g_strlcpy() failed */
361 : 1 : g_assert_cmpint (str_cpy_size, ==, 0);
362 : : }
363 : :
364 : 1 : str_cpy_size = g_strlcat (str_cpy, "", 0);
365 : 1 : g_assert_cmpint (str_cpy_size, ==, strlen (""));
366 : :
367 : : /* Testing normal usage cases.
368 : : * Note that the @dest_size argument to g_strlcat() is normally meant to be
369 : : * set to `sizeof (dest)`. We set it to various values `≤ sizeof (str_cpy)`
370 : : * for testing purposes. */
371 : 1 : g_assert_cmpuint (strlen (str) + 1, <=, sizeof (str_cpy));
372 : 1 : str_cpy_size = g_strlcat (str_cpy, str, strlen (str) + 1);
373 : 1 : g_assert_cmpstr (str, ==, str_cpy);
374 : 1 : g_assert_cmpint (str_cpy_size, ==, strlen (str));
375 : :
376 : 1 : g_assert_cmpuint (strlen (str), <=, sizeof (str_cpy));
377 : 1 : str_cpy_size = g_strlcat (str_cpy, str, strlen (str));
378 : 1 : g_assert_cmpstr (str, ==, str_cpy);
379 : 1 : g_assert_cmpint (str_cpy_size, ==, 2 * strlen (str));
380 : :
381 : 1 : g_assert_cmpuint (strlen (str) - 15, <=, sizeof (str_cpy));
382 : 1 : str_cpy_size = g_strlcat (str_cpy, str, strlen (str) - 15);
383 : 1 : g_assert_cmpstr (str, ==, str_cpy);
384 : 1 : g_assert_cmpint (str_cpy_size, ==, 2 * strlen (str) - 15);
385 : :
386 : 1 : g_assert_cmpuint (0, <=, sizeof (str_cpy));
387 : 1 : str_cpy_size = g_strlcat (str_cpy, str, 0);
388 : 1 : g_assert_cmpstr (str, ==, str_cpy);
389 : 1 : g_assert_cmpint (str_cpy_size, ==, strlen (str));
390 : :
391 : 1 : g_assert_cmpuint (strlen (str) + 15, <=, sizeof (str_cpy));
392 : 1 : str_cpy_size = g_strlcat (str_cpy, str, strlen (str) + 15);
393 : 1 : g_assert_cmpstr ("The quick brown fox jumps over the lazy dogThe quick brow",
394 : : ==, str_cpy);
395 : 1 : g_assert_cmpint (str_cpy_size, ==, 2 * strlen (str));
396 : 1 : }
397 : :
398 : : /* Testing g_ascii_strdown() function with various positive and negative cases */
399 : : static void
400 : 1 : test_ascii_strdown (void)
401 : : {
402 : 1 : const gchar *str_down = "the quick brown fox jumps over the lazy dog.";
403 : 1 : const gchar *str_up = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.";
404 : : gchar* str;
405 : :
406 [ + - ]: 1 : if (g_test_undefined ())
407 : : {
408 : : /* Testing degenerated cases */
409 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
410 : : "*assertion*!= NULL*");
411 : 1 : str = g_ascii_strdown (NULL, 0);
412 : 1 : g_test_assert_expected_messages ();
413 : : }
414 : :
415 : 1 : str = g_ascii_strdown ("", 0);
416 : 1 : g_assert_nonnull (str);
417 : 1 : g_assert_cmpstr (str, ==, "");
418 : 1 : g_free (str);
419 : :
420 : 1 : str = g_ascii_strdown ("", -1);
421 : 1 : g_assert_nonnull (str);
422 : 1 : g_assert_cmpstr (str, ==, "");
423 : 1 : g_free (str);
424 : :
425 : : /* Testing normal usage cases */
426 : 1 : str = g_ascii_strdown (str_down, strlen (str_down));
427 : 1 : g_assert_nonnull (str);
428 : 1 : g_assert_cmpstr (str, ==, str_down);
429 : 1 : g_free (str);
430 : :
431 : 1 : str = g_ascii_strdown (str_up, strlen (str_up));
432 : 1 : g_assert_nonnull (str);
433 : 1 : g_assert_cmpstr (str, ==, str_down);
434 : 1 : g_free (str);
435 : :
436 : 1 : str = g_ascii_strdown (str_up, -1);
437 : 1 : g_assert_nonnull (str);
438 : 1 : g_assert_cmpstr (str, ==, str_down);
439 : 1 : g_free (str);
440 : :
441 : 1 : str = g_ascii_strdown (str_up, 0);
442 : 1 : g_assert_nonnull (str);
443 : 1 : g_assert_cmpstr (str, ==, "");
444 : 1 : g_free (str);
445 : 1 : }
446 : :
447 : : /* Testing g_ascii_strup() function with various positive and negative cases */
448 : : static void
449 : 1 : test_ascii_strup (void)
450 : : {
451 : 1 : const gchar *str_down = "the quick brown fox jumps over the lazy dog.";
452 : 1 : const gchar *str_up = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.";
453 : : gchar* str;
454 : :
455 [ + - ]: 1 : if (g_test_undefined ())
456 : : {
457 : : /* Testing degenerated cases */
458 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
459 : : "*assertion*!= NULL*");
460 : 1 : str = g_ascii_strup (NULL, 0);
461 : 1 : g_test_assert_expected_messages ();
462 : : }
463 : :
464 : 1 : str = g_ascii_strup ("", 0);
465 : 1 : g_assert_nonnull (str);
466 : 1 : g_assert_cmpstr (str, ==, "");
467 : 1 : g_free (str);
468 : :
469 : 1 : str = g_ascii_strup ("", -1);
470 : 1 : g_assert_nonnull (str);
471 : 1 : g_assert_cmpstr (str, ==, "");
472 : 1 : g_free (str);
473 : :
474 : : /* Testing normal usage cases */
475 : 1 : str = g_ascii_strup (str_up, strlen (str_up));
476 : 1 : g_assert_nonnull (str);
477 : 1 : g_assert_cmpstr (str, ==, str_up);
478 : 1 : g_free (str);
479 : :
480 : 1 : str = g_ascii_strup (str_down, strlen (str_down));
481 : 1 : g_assert_nonnull (str);
482 : 1 : g_assert_cmpstr (str, ==, str_up);
483 : 1 : g_free (str);
484 : :
485 : 1 : str = g_ascii_strup (str_down, -1);
486 : 1 : g_assert_nonnull (str);
487 : 1 : g_assert_cmpstr (str, ==, str_up);
488 : 1 : g_free (str);
489 : :
490 : 1 : str = g_ascii_strup (str_down, 0);
491 : 1 : g_assert_nonnull (str);
492 : 1 : g_assert_cmpstr (str, ==, "");
493 : 1 : g_free (str);
494 : 1 : }
495 : :
496 : : /* Testing g_strdup() function with various positive and negative cases */
497 : : static void
498 : 1 : test_strdup (void)
499 : : {
500 : : gchar *str;
501 : :
502 : 1 : g_assert_null ((g_strdup) (NULL));
503 : :
504 : 1 : str = (g_strdup) (GLIB_TEST_STRING);
505 : 1 : g_assert_nonnull (str);
506 : 1 : g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
507 : :
508 : 1 : char *other_str = (g_strdup) (str);
509 : 1 : g_free (str);
510 : :
511 : 1 : g_assert_nonnull (other_str);
512 : 1 : g_assert_cmpstr (other_str, ==, GLIB_TEST_STRING);
513 : 1 : g_clear_pointer (&other_str, g_free);
514 : :
515 : 1 : str = (g_strdup) ("");
516 : 1 : g_assert_cmpint (str[0], ==, '\0');
517 : 1 : g_assert_cmpstr (str, ==, "");
518 : 1 : g_clear_pointer (&str, g_free);
519 : 1 : }
520 : :
521 : : static void
522 : 1 : test_strdup_inline (void)
523 : : {
524 : : gchar *str;
525 : :
526 : : #if G_GNUC_CHECK_VERSION (2, 0)
527 : : #ifndef g_strdup
528 : : #error g_strdup() should be defined as a macro in this platform!
529 : : #endif
530 : : #else
531 : : g_test_incomplete ("g_strdup() is not inlined in this platform");
532 : : #endif
533 : :
534 : : /* Testing inline version of g_strdup() function with various positive and
535 : : * negative cases */
536 : :
537 : 1 : g_assert_null (g_strdup (NULL));
538 : :
539 : 1 : str = g_strdup (GLIB_TEST_STRING);
540 : 1 : g_assert_nonnull (str);
541 : 1 : g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
542 : :
543 : 1 : char *other_str = g_strdup (str);
544 : 1 : g_clear_pointer (&str, g_free);
545 : :
546 : 1 : g_assert_nonnull (other_str);
547 : 1 : g_assert_cmpstr (other_str, ==, GLIB_TEST_STRING);
548 : 1 : g_clear_pointer (&other_str, g_free);
549 : :
550 : 1 : str = g_strdup ("");
551 : 1 : g_assert_cmpint (str[0], ==, '\0');
552 : 1 : g_assert_cmpstr (str, ==, "");
553 : 1 : g_clear_pointer (&str, g_free);
554 : 1 : }
555 : :
556 : : /* Testing g_strndup() function with various positive and negative cases */
557 : : static void
558 : 1 : test_strndup (void)
559 : : {
560 : : gchar *str;
561 : :
562 : 1 : str = g_strndup (NULL, 3);
563 : 1 : g_assert_null (str);
564 : :
565 : 1 : str = g_strndup ("aaaa", 5);
566 : 1 : g_assert_nonnull (str);
567 : 1 : g_assert_cmpstr (str, ==, "aaaa");
568 : 1 : g_free (str);
569 : :
570 : 1 : str = g_strndup ("aaaa", 2);
571 : 1 : g_assert_nonnull (str);
572 : 1 : g_assert_cmpstr (str, ==, "aa");
573 : 1 : g_free (str);
574 : 1 : }
575 : :
576 : : /* Testing g_strdup_printf() function with various positive and negative cases */
577 : : static void
578 : 1 : test_strdup_printf (void)
579 : : {
580 : : gchar *str;
581 : :
582 : 1 : str = g_strdup_printf ("%05d %-5s", 21, "test");
583 : 1 : g_assert_nonnull (str);
584 : 1 : g_assert_cmpstr (str, ==, "00021 test ");
585 : 1 : g_free (str);
586 : 1 : }
587 : :
588 : : /* Testing g_strdupv() function with various positive and negative cases */
589 : : static void
590 : 1 : test_strdupv (void)
591 : : {
592 : 1 : gchar *vec[] = { "Foo", "Bar", NULL };
593 : : gchar **copy;
594 : :
595 : 1 : copy = g_strdupv (NULL);
596 : 1 : g_assert_null (copy);
597 : :
598 : 1 : copy = g_strdupv (vec);
599 : 1 : g_assert_nonnull (copy);
600 : 3 : g_assert_cmpstrv (copy, vec);
601 : 1 : g_strfreev (copy);
602 : 1 : }
603 : :
604 : : /* Testing g_strfill() function with various positive and negative cases */
605 : : static void
606 : 1 : test_strnfill (void)
607 : : {
608 : : gchar *str;
609 : :
610 : 1 : str = g_strnfill (0, 'a');
611 : 1 : g_assert_nonnull (str);
612 : 1 : g_assert_true (*str == '\0');
613 : 1 : g_free (str);
614 : :
615 : 1 : str = g_strnfill (5, 'a');
616 : 1 : g_assert_nonnull (str);
617 : 1 : g_assert_cmpstr (str, ==, "aaaaa");
618 : 1 : g_free (str);
619 : 1 : }
620 : :
621 : : /* Testing g_strconcat() function with various positive and negative cases */
622 : : static void
623 : 1 : test_strconcat (void)
624 : : {
625 : : gchar *str;
626 : :
627 : 1 : str = g_strconcat (GLIB_TEST_STRING, NULL);
628 : 1 : g_assert_nonnull (str);
629 : 1 : g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
630 : 1 : g_free (str);
631 : :
632 : 1 : str = g_strconcat (GLIB_TEST_STRING,
633 : : GLIB_TEST_STRING,
634 : : GLIB_TEST_STRING,
635 : : NULL);
636 : 1 : g_assert_nonnull (str);
637 : 1 : g_assert_cmpstr (str, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING);
638 : 1 : g_free (str);
639 : :
640 : 1 : g_assert_null (g_strconcat (NULL, "bla", NULL));
641 : 1 : }
642 : :
643 : : /* Testing g_strjoinv() function with various positive and negative cases */
644 : : static void
645 : 1 : test_strjoinv (void)
646 : : {
647 : 1 : gchar *strings[] = { "string1", "string2", NULL };
648 : 1 : gchar *empty_strings[] = { NULL };
649 : : gchar *str;
650 : :
651 [ + - ]: 1 : if (g_test_undefined ())
652 : : {
653 : : /* Testing degenerated cases */
654 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
655 : : "*assertion*!= NULL*");
656 : 1 : str = g_strjoinv (NULL, NULL);
657 : 1 : g_test_assert_expected_messages ();
658 : : }
659 : :
660 : 1 : str = g_strjoinv (":", strings);
661 : 1 : g_assert_nonnull (str);
662 : 1 : g_assert_cmpstr (str, ==, "string1:string2");
663 : 1 : g_free (str);
664 : :
665 : 1 : str = g_strjoinv (NULL, strings);
666 : 1 : g_assert_nonnull (str);
667 : 1 : g_assert_cmpstr (str, ==, "string1string2");
668 : 1 : g_free (str);
669 : :
670 : 1 : str = g_strjoinv (NULL, empty_strings);
671 : 1 : g_assert_nonnull (str);
672 : 1 : g_assert_cmpstr (str, ==, "");
673 : 1 : g_free (str);
674 : 1 : }
675 : :
676 : : /* Testing g_strjoin() function with various positive and negative cases */
677 : : static void
678 : 1 : test_strjoin (void)
679 : : {
680 : : gchar *str;
681 : :
682 : 1 : str = g_strjoin (NULL, NULL);
683 : 1 : g_assert_nonnull (str);
684 : 1 : g_assert_true (*str == '\0');
685 : 1 : g_free (str);
686 : :
687 : 1 : str = g_strjoin (":", NULL);
688 : 1 : g_assert_nonnull (str);
689 : 1 : g_assert_true (*str == '\0');
690 : 1 : g_free (str);
691 : :
692 : 1 : str = g_strjoin (NULL, GLIB_TEST_STRING, NULL);
693 : 1 : g_assert_nonnull (str);
694 : 1 : g_assert_cmpstr (str, ==, GLIB_TEST_STRING);
695 : 1 : g_free (str);
696 : :
697 : 1 : str = g_strjoin (NULL,
698 : : GLIB_TEST_STRING,
699 : : GLIB_TEST_STRING,
700 : : GLIB_TEST_STRING,
701 : : NULL);
702 : 1 : g_assert_nonnull (str);
703 : 1 : g_assert_cmpstr (str, ==, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING);
704 : 1 : g_free (str);
705 : :
706 : 1 : str = g_strjoin (":",
707 : : GLIB_TEST_STRING,
708 : : GLIB_TEST_STRING,
709 : : GLIB_TEST_STRING,
710 : : NULL);
711 : 1 : g_assert_nonnull (str);
712 : 1 : g_assert_cmpstr (str, ==, GLIB_TEST_STRING ":" GLIB_TEST_STRING ":" GLIB_TEST_STRING);
713 : 1 : g_free (str);
714 : 1 : }
715 : :
716 : : /* Testing g_strcanon() function with various positive and negative cases */
717 : : static void
718 : 1 : test_strcanon (void)
719 : : {
720 : : gchar *str;
721 : :
722 [ + - ]: 1 : if (g_test_undefined ())
723 : : {
724 : : gchar *ret;
725 : :
726 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
727 : : "*assertion*!= NULL*");
728 : 1 : str = g_strcanon (NULL, "ab", 'y');
729 : 1 : g_test_assert_expected_messages ();
730 : 1 : g_assert_null (str);
731 : :
732 : 1 : str = g_strdup ("abxabxab");
733 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
734 : : "*assertion*!= NULL*");
735 : 1 : ret = g_strcanon (str, NULL, 'y');
736 : 1 : g_test_assert_expected_messages ();
737 : 1 : g_assert_null (ret);
738 : 1 : g_free (str);
739 : : }
740 : :
741 : 1 : str = g_strdup ("abxabxab");
742 : 1 : str = g_strcanon (str, "ab", 'y');
743 : 1 : g_assert_nonnull (str);
744 : 1 : g_assert_cmpstr (str, ==, "abyabyab");
745 : 1 : g_free (str);
746 : 1 : }
747 : :
748 : : /* Testing g_strcompress() and g_strescape() functions with various cases */
749 : : static void
750 : 1 : test_strcompress_strescape (void)
751 : : {
752 : : gchar *str;
753 : : gchar *tmp;
754 : :
755 : : /* test compress */
756 [ + - ]: 1 : if (g_test_undefined ())
757 : : {
758 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
759 : : "*assertion*!= NULL*");
760 : 1 : str = g_strcompress (NULL);
761 : 1 : g_test_assert_expected_messages ();
762 : 1 : g_assert_null (str);
763 : :
764 : : /* trailing slashes are not allowed */
765 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
766 : : "*trailing \\*");
767 : 1 : str = g_strcompress ("abc\\");
768 : 1 : g_test_assert_expected_messages ();
769 : 1 : g_assert_cmpstr (str, ==, "abc");
770 : 1 : g_free (str);
771 : : }
772 : :
773 : 1 : str = g_strcompress ("abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\003\\177\\234\\313\\12345z");
774 : 1 : g_assert_nonnull (str);
775 : 1 : g_assert_cmpstr (str, ==, "abc\\\"\b\f\n\r\t\v\003\177\234\313\12345z");
776 : 1 : g_free (str);
777 : :
778 : : /* test escape */
779 [ + - ]: 1 : if (g_test_undefined ())
780 : : {
781 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
782 : : "*assertion*!= NULL*");
783 : 1 : str = g_strescape (NULL, NULL);
784 : 1 : g_test_assert_expected_messages ();
785 : 1 : g_assert_null (str);
786 : : }
787 : :
788 : 1 : str = g_strescape ("abc\\\"\b\f\n\r\t\v\003\177\234\313", NULL);
789 : 1 : g_assert_nonnull (str);
790 : 1 : g_assert_cmpstr (str, ==, "abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\003\\177\\234\\313");
791 : 1 : g_free (str);
792 : :
793 : 1 : str = g_strescape ("abc\\\"\b\f\n\r\t\v\003\177\234\313",
794 : : "\b\f\001\002\003\004");
795 : 1 : g_assert_nonnull (str);
796 : 1 : g_assert_cmpstr (str, ==, "abc\\\\\\\"\b\f\\n\\r\\t\\v\003\\177\\234\\313");
797 : 1 : g_free (str);
798 : :
799 : : /* round trip */
800 : 1 : tmp = g_strescape ("abc\\\"\b\f\n\r\t\v\003\177\234\313", NULL);
801 : 1 : str = g_strcompress (tmp);
802 : 1 : g_assert_nonnull (str);
803 : 1 : g_assert_cmpstr (str, ==, "abc\\\"\b\f\n\r\t\v\003\177\234\313");
804 : 1 : g_free (str);
805 : 1 : g_free (tmp);
806 : :
807 : : /* Unicode round trip */
808 : 1 : str = g_strescape ("héllø there⸘", NULL);
809 : 1 : g_assert_nonnull (str);
810 : 1 : g_assert_cmpstr (str, ==, "h\\303\\251ll\\303\\270 there\\342\\270\\230");
811 : 1 : tmp = g_strcompress (str);
812 : 1 : g_assert_nonnull (tmp);
813 : 1 : g_assert_cmpstr (tmp, ==, "héllø there⸘");
814 : 1 : g_free (tmp);
815 : 1 : g_free (str);
816 : :
817 : : /* Test expanding invalid escapes */
818 : 1 : str = g_strcompress ("\\11/ \\118 \\8aa \\19");
819 : 1 : g_assert_nonnull (str);
820 : 1 : g_assert_cmpstr (str, ==, "\t/ \t8 8aa \0019");
821 : 1 : g_free (str);
822 : 1 : }
823 : :
824 : : /* Testing g_ascii_strcasecmp() and g_ascii_strncasecmp() */
825 : : static void
826 : 1 : test_ascii_strcasecmp (void)
827 : : {
828 : : gboolean res;
829 : :
830 [ + - ]: 1 : if (g_test_undefined ())
831 : : {
832 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
833 : : "*assertion*!= NULL*");
834 : 1 : res = g_ascii_strcasecmp ("foo", NULL);
835 : 1 : g_test_assert_expected_messages ();
836 : 1 : g_assert_false (res);
837 : :
838 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
839 : : "*assertion*!= NULL*");
840 : 1 : res = g_ascii_strcasecmp (NULL, "foo");
841 : 1 : g_test_assert_expected_messages ();
842 : 1 : g_assert_false (res);
843 : :
844 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
845 : : "*assertion*!= NULL*");
846 : 1 : res = g_ascii_strncasecmp ("foo", NULL, 0);
847 : 1 : g_test_assert_expected_messages ();
848 : 1 : g_assert_false (res);
849 : :
850 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
851 : : "*assertion*!= NULL*");
852 : 1 : res = g_ascii_strncasecmp (NULL, "foo", 0);
853 : 1 : g_test_assert_expected_messages ();
854 : 1 : g_assert_false (res);
855 : : }
856 : :
857 : 1 : res = g_ascii_strcasecmp ("FroboZZ", "frobozz");
858 : 1 : g_assert_cmpint (res, ==, 0);
859 : :
860 : 1 : res = g_ascii_strcasecmp ("frobozz", "frobozz");
861 : 1 : g_assert_cmpint (res, ==, 0);
862 : :
863 : 1 : res = g_ascii_strcasecmp ("frobozz", "FROBOZZ");
864 : 1 : g_assert_cmpint (res, ==, 0);
865 : :
866 : 1 : res = g_ascii_strcasecmp ("FROBOZZ", "froboz");
867 : 1 : g_assert_cmpint (res, !=, 0);
868 : :
869 : 1 : res = g_ascii_strcasecmp ("", "");
870 : 1 : g_assert_cmpint (res, ==, 0);
871 : :
872 : 1 : res = g_ascii_strcasecmp ("!#%&/()", "!#%&/()");
873 : 1 : g_assert_cmpint (res, ==, 0);
874 : :
875 : 1 : res = g_ascii_strcasecmp ("a", "b");
876 : 1 : g_assert_cmpint (res, <, 0);
877 : :
878 : 1 : res = g_ascii_strcasecmp ("a", "B");
879 : 1 : g_assert_cmpint (res, <, 0);
880 : :
881 : 1 : res = g_ascii_strcasecmp ("A", "b");
882 : 1 : g_assert_cmpint (res, <, 0);
883 : :
884 : 1 : res = g_ascii_strcasecmp ("A", "B");
885 : 1 : g_assert_cmpint (res, <, 0);
886 : :
887 : 1 : res = g_ascii_strcasecmp ("b", "a");
888 : 1 : g_assert_cmpint (res, >, 0);
889 : :
890 : 1 : res = g_ascii_strcasecmp ("b", "A");
891 : 1 : g_assert_cmpint (res, >, 0);
892 : :
893 : 1 : res = g_ascii_strcasecmp ("B", "a");
894 : 1 : g_assert_cmpint (res, >, 0);
895 : :
896 : 1 : res = g_ascii_strcasecmp ("B", "A");
897 : 1 : g_assert_cmpint (res, >, 0);
898 : :
899 : : /* g_ascii_strncasecmp() */
900 : 1 : res = g_ascii_strncasecmp ("", "", 10);
901 : 1 : g_assert_cmpint (res, ==, 0);
902 : :
903 : 1 : res = g_ascii_strncasecmp ("Frob0ZZ", "frob0zz", strlen ("frobozz"));
904 : 1 : g_assert_cmpint (res, ==, 0);
905 : :
906 : 1 : res = g_ascii_strncasecmp ("Frob0ZZ", "frobozz", strlen ("frobozz"));
907 : 1 : g_assert_cmpint (res, !=, 0);
908 : :
909 : 1 : res = g_ascii_strncasecmp ("frob0ZZ", "FroB0zz", strlen ("frobozz"));
910 : 1 : g_assert_cmpint (res, ==, 0);
911 : :
912 : 1 : res = g_ascii_strncasecmp ("Frob0ZZ", "froB0zz", strlen ("frobozz") - 5);
913 : 1 : g_assert_cmpint (res, ==, 0);
914 : :
915 : 1 : res = g_ascii_strncasecmp ("Frob0ZZ", "froB0zz", strlen ("frobozz") + 5);
916 : 1 : g_assert_cmpint (res, ==, 0);
917 : 1 : }
918 : :
919 : : static void
920 : 7 : do_test_strchug (const gchar *str, const gchar *expected)
921 : : {
922 : : gchar *tmp;
923 : : gboolean res;
924 : :
925 : 7 : tmp = g_strdup (str);
926 : :
927 : 7 : g_strchug (tmp);
928 : 7 : res = (strcmp (tmp, expected) == 0);
929 : 7 : g_free (tmp);
930 : :
931 : 7 : g_assert_cmpint (res, ==, TRUE);
932 : 7 : }
933 : :
934 : : /* Testing g_strchug() function with various positive and negative cases */
935 : : static void
936 : 1 : test_strchug (void)
937 : : {
938 [ + - ]: 1 : if (g_test_undefined ())
939 : : {
940 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
941 : : "*assertion*!= NULL*");
942 : 1 : g_strchug (NULL);
943 : 1 : g_test_assert_expected_messages ();
944 : : }
945 : :
946 : 1 : do_test_strchug ("", "");
947 : 1 : do_test_strchug (" ", "");
948 : 1 : do_test_strchug ("\t\r\n ", "");
949 : 1 : do_test_strchug (" a", "a");
950 : 1 : do_test_strchug (" a", "a");
951 : 1 : do_test_strchug ("a a", "a a");
952 : 1 : do_test_strchug (" a a", "a a");
953 : 1 : }
954 : :
955 : : static void
956 : 7 : do_test_strchomp (const gchar *str, const gchar *expected)
957 : : {
958 : : gchar *tmp;
959 : : gboolean res;
960 : :
961 : 7 : tmp = g_strdup (str);
962 : :
963 : 7 : g_strchomp (tmp);
964 : 7 : res = (strcmp (tmp, expected) == 0);
965 : 7 : g_free (tmp);
966 : :
967 : 7 : g_assert_cmpint (res, ==, TRUE);
968 : 7 : }
969 : :
970 : : /* Testing g_strchomp() function with various positive and negative cases */
971 : : static void
972 : 1 : test_strchomp (void)
973 : : {
974 [ + - ]: 1 : if (g_test_undefined ())
975 : : {
976 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
977 : : "*assertion*!= NULL*");
978 : 1 : g_strchomp (NULL);
979 : 1 : g_test_assert_expected_messages ();
980 : : }
981 : :
982 : 1 : do_test_strchomp ("", "");
983 : 1 : do_test_strchomp (" ", "");
984 : 1 : do_test_strchomp (" \t\r\n", "");
985 : 1 : do_test_strchomp ("a ", "a");
986 : 1 : do_test_strchomp ("a ", "a");
987 : 1 : do_test_strchomp ("a a", "a a");
988 : 1 : do_test_strchomp ("a a ", "a a");
989 : 1 : }
990 : :
991 : : /* Testing g_str_tokenize_and_fold() functions */
992 : : static void
993 : 1 : test_str_tokenize_and_fold (void)
994 : : {
995 : 1 : const gchar *local_str = "en_GB";
996 : 1 : const gchar *sample = "The quick brown fox¸ jumps over the lazy dog.";
997 : 1 : const gchar *special_cases = "quıck QUİCK QUİı QUıİ İıck ıİCK àìøş";
998 : : gchar **tokens, **alternates;
999 : : gchar
1000 : 1 : *expected_tokens[] = \
1001 : : {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog", NULL},
1002 : 1 : *expected_tokens_alt[] = \
1003 : : { "quick", "quick", "quii", "quii", "iick", "iick", "àìøş", NULL};
1004 : :
1005 [ + - ]: 1 : if (g_test_undefined ())
1006 : : {
1007 : : /* Testing degenerated cases */
1008 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1009 : : "*assertion*!= NULL*");
1010 : 1 : tokens = g_str_tokenize_and_fold (NULL, local_str, NULL);
1011 : 1 : g_test_assert_expected_messages ();
1012 : : }
1013 : :
1014 : 1 : tokens = g_str_tokenize_and_fold (special_cases, local_str, &alternates);
1015 : 1 : g_assert_cmpint (g_strv_length (tokens), ==,
1016 : : g_strv_length (expected_tokens_alt));
1017 : 1 : g_assert_true (g_strv_equal ((const gchar * const *) tokens,
1018 : : (const gchar * const *) expected_tokens_alt));
1019 : 1 : g_strfreev (tokens);
1020 : 1 : g_strfreev (alternates);
1021 : :
1022 : 1 : tokens = g_str_tokenize_and_fold (sample, local_str, &alternates);
1023 : 1 : g_assert_cmpint (g_strv_length (tokens), ==, g_strv_length (expected_tokens));
1024 : 1 : g_assert_true (g_strv_equal ((const gchar * const *) tokens,
1025 : : (const gchar * const *) expected_tokens));
1026 : 1 : g_strfreev (tokens);
1027 : 1 : g_strfreev (alternates);
1028 : :
1029 : 1 : tokens = g_str_tokenize_and_fold (sample, local_str, NULL);
1030 : 1 : g_assert_cmpint (g_strv_length (tokens), ==, g_strv_length (expected_tokens));
1031 : 1 : g_assert_true (g_strv_equal ((const gchar * const *) tokens,
1032 : : (const gchar * const *) expected_tokens));
1033 : 1 : g_strfreev (tokens);
1034 : :
1035 : 1 : tokens = g_str_tokenize_and_fold (sample, NULL, &alternates);
1036 : 1 : g_assert_cmpint (g_strv_length (tokens), ==, g_strv_length (expected_tokens));
1037 : 1 : g_assert_true (g_strv_equal ((const gchar * const *) tokens,
1038 : : (const gchar * const *) expected_tokens));
1039 : 1 : g_strfreev (tokens);
1040 : 1 : g_strfreev (alternates);
1041 : 1 : }
1042 : :
1043 : : /* Testing g_strreverse() function with various positive and negative cases */
1044 : : static void
1045 : 1 : test_strreverse (void)
1046 : : {
1047 : : gchar *str;
1048 : : gchar *p;
1049 : :
1050 [ + - ]: 1 : if (g_test_undefined ())
1051 : : {
1052 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1053 : : "*assertion*!= NULL*");
1054 : 1 : str = g_strreverse (NULL);
1055 : 1 : g_test_assert_expected_messages ();
1056 : 1 : g_assert_null (str);
1057 : : }
1058 : :
1059 : 1 : str = p = g_strdup ("abcde");
1060 : 1 : str = g_strreverse (str);
1061 : 1 : g_assert_nonnull (str);
1062 : 1 : g_assert_true (p == str);
1063 : 1 : g_assert_cmpstr (str, ==, "edcba");
1064 : 1 : g_free (str);
1065 : 1 : }
1066 : :
1067 : : /* Testing g_strncasecmp() functions */
1068 : : static void
1069 : 1 : test_strncasecmp (void)
1070 : : {
1071 : 1 : g_assert_cmpint (g_strncasecmp ("abc1", "ABC2", 3), ==, 0);
1072 : 1 : g_assert_cmpint (g_strncasecmp ("abc1", "ABC2", 4), !=, 0);
1073 : 1 : }
1074 : :
1075 : : static void
1076 : 1 : test_strstr (void)
1077 : : {
1078 : : gchar *haystack;
1079 : : gchar *res;
1080 : :
1081 : 1 : haystack = g_strdup ("FooBarFooBarFoo");
1082 : :
1083 [ + - ]: 1 : if (g_test_undefined ())
1084 : : {
1085 : : /* Testing degenerated cases */
1086 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1087 : : "*assertion*!= NULL*");
1088 : 1 : res = g_strstr_len (NULL, 0, "xxx");
1089 : 1 : g_test_assert_expected_messages ();
1090 : :
1091 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1092 : : "*assertion*!= NULL*");
1093 : 1 : res = g_strstr_len ("xxx", 0, NULL);
1094 : 1 : g_test_assert_expected_messages ();
1095 : : }
1096 : :
1097 : : /* strstr_len */
1098 : 1 : res = g_strstr_len (haystack, 6, "xxx");
1099 : 1 : g_assert_null (res);
1100 : :
1101 : 1 : res = g_strstr_len (haystack, 6, "FooBarFooBarFooBar");
1102 : 1 : g_assert_null (res);
1103 : :
1104 : 1 : res = g_strstr_len (haystack, 3, "Bar");
1105 : 1 : g_assert_null (res);
1106 : :
1107 : 1 : res = g_strstr_len (haystack, 6, "");
1108 : 1 : g_assert_true (res == haystack);
1109 : 1 : g_assert_cmpstr (res, ==, "FooBarFooBarFoo");
1110 : :
1111 : 1 : res = g_strstr_len (haystack, 6, "Bar");
1112 : 1 : g_assert_true (res == haystack + 3);
1113 : 1 : g_assert_cmpstr (res, ==, "BarFooBarFoo");
1114 : :
1115 : 1 : res = g_strstr_len (haystack, -1, "Bar");
1116 : 1 : g_assert_true (res == haystack + 3);
1117 : 1 : g_assert_cmpstr (res, ==, "BarFooBarFoo");
1118 : :
1119 : : /* strrstr */
1120 [ + - ]: 1 : if (g_test_undefined ())
1121 : : {
1122 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1123 : : "*assertion*!= NULL*");
1124 : 1 : res = g_strrstr (NULL, "xxx");
1125 : 1 : g_test_assert_expected_messages ();
1126 : :
1127 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1128 : : "*assertion*!= NULL*");
1129 : 1 : res = g_strrstr ("xxx", NULL);
1130 : 1 : g_test_assert_expected_messages ();
1131 : : }
1132 : :
1133 : 1 : res = g_strrstr (haystack, "xxx");
1134 : 1 : g_assert_null (res);
1135 : :
1136 : 1 : res = g_strrstr (haystack, "FooBarFooBarFooBar");
1137 : 1 : g_assert_null (res);
1138 : :
1139 : 1 : res = g_strrstr (haystack, "");
1140 : 1 : g_assert_true (res == haystack);
1141 : 1 : g_assert_cmpstr (res, ==, "FooBarFooBarFoo");
1142 : :
1143 : 1 : res = g_strrstr (haystack, "Bar");
1144 : 1 : g_assert_true (res == haystack + 9);
1145 : 1 : g_assert_cmpstr (res, ==, "BarFoo");
1146 : :
1147 : : /* strrstr_len */
1148 [ + - ]: 1 : if (g_test_undefined ())
1149 : : {
1150 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1151 : : "*assertion*!= NULL*");
1152 : 1 : res = g_strrstr_len (NULL, 14, "xxx");
1153 : 1 : g_test_assert_expected_messages ();
1154 : :
1155 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1156 : : "*assertion*!= NULL*");
1157 : 1 : res = g_strrstr_len ("xxx", 14, NULL);
1158 : 1 : g_test_assert_expected_messages ();
1159 : : }
1160 : :
1161 : 1 : res = g_strrstr_len (haystack, 14, "xxx");
1162 : 1 : g_assert_null (res);
1163 : :
1164 : 1 : res = g_strrstr_len (haystack, 14, "FooBarFooBarFooBar");
1165 : 1 : g_assert_null (res);
1166 : :
1167 : 1 : res = g_strrstr_len (haystack, 3, "Bar");
1168 : 1 : g_assert_null (res);
1169 : :
1170 : 1 : res = g_strrstr_len (haystack, 14, "BarFoo");
1171 : 1 : g_assert_true (res == haystack + 3);
1172 : 1 : g_assert_cmpstr (res, ==, "BarFooBarFoo");
1173 : :
1174 : 1 : res = g_strrstr_len (haystack, 15, "BarFoo");
1175 : 1 : g_assert_true (res == haystack + 9);
1176 : 1 : g_assert_cmpstr (res, ==, "BarFoo");
1177 : :
1178 : 1 : res = g_strrstr_len (haystack, -1, "BarFoo");
1179 : 1 : g_assert_true (res == haystack + 9);
1180 : 1 : g_assert_cmpstr (res, ==, "BarFoo");
1181 : :
1182 : : /* test case for strings with \0 in the middle */
1183 : 1 : *(haystack + 7) = '\0';
1184 : 1 : res = g_strstr_len (haystack, 15, "BarFoo");
1185 : 1 : g_assert_null (res);
1186 : :
1187 : 1 : g_free (haystack);
1188 : 1 : }
1189 : :
1190 : : /* Testing g_strtod() function with various positive and negative cases */
1191 : : static void
1192 : 1 : test_strtod (void)
1193 : : {
1194 : 1 : gchar *str_end = NULL;
1195 : 1 : double value = 0.0;
1196 : 1 : const double gold_ratio = 1.61803398874989484;
1197 : 1 : const gchar *gold_ratio_str = "1.61803398874989484";
1198 : 1 : const gchar *minus_gold_ratio_str = "-1.61803398874989484";
1199 : :
1200 [ + - ]: 1 : if (g_test_undefined ())
1201 : : {
1202 : : /* Testing degenerated cases */
1203 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1204 : : "*assertion*!= NULL*");
1205 : 1 : value = g_strtod (NULL, NULL);
1206 : 1 : g_test_assert_expected_messages ();
1207 : 1 : g_assert_cmpfloat (value, ==, 0.0);
1208 : : }
1209 : :
1210 : 1 : g_assert_cmpfloat (g_strtod ("\x00\x00\x00\x00", NULL), ==, 0.0);
1211 : 1 : g_assert_cmpfloat (g_strtod ("\x00\x00\x00\x00", &str_end), ==, 0.0);
1212 : 1 : g_assert_cmpstr (str_end, ==, "");
1213 : 1 : g_assert_cmpfloat (g_strtod ("\xff\xff\xff\xff", NULL), ==, 0.0);
1214 : 1 : g_assert_cmpfloat (g_strtod ("\xff\xff\xff\xff", &str_end), ==, 0.0);
1215 : 1 : g_assert_cmpstr (str_end, ==, "\xff\xff\xff\xff");
1216 : :
1217 : : /* Testing normal usage cases */
1218 : 1 : g_assert_cmpfloat (g_strtod (gold_ratio_str, NULL), ==, gold_ratio);
1219 : 1 : g_assert_cmpfloat (g_strtod (gold_ratio_str, &str_end), ==, gold_ratio);
1220 : 1 : g_assert_true (str_end == gold_ratio_str + strlen (gold_ratio_str));
1221 : 1 : g_assert_cmpfloat (g_strtod (minus_gold_ratio_str, NULL), ==, -gold_ratio);
1222 : 1 : g_assert_cmpfloat (g_strtod (minus_gold_ratio_str, &str_end), ==, -gold_ratio);
1223 : 1 : g_assert_true (str_end == minus_gold_ratio_str + strlen (minus_gold_ratio_str));
1224 : 1 : }
1225 : :
1226 : : /* Testing g_strdelimit() function */
1227 : : static void
1228 : 1 : test_strdelimit (void)
1229 : : {
1230 : 1 : const gchar *const_string = "ABCDE<*>Q";
1231 : : gchar *string;
1232 : :
1233 [ + - ]: 1 : if (g_test_undefined ())
1234 : : {
1235 : : /* Testing degenerated cases */
1236 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1237 : : "*assertion*!= NULL*");
1238 : 1 : string = g_strdelimit (NULL, "ABCDE", 'N');
1239 : 1 : g_test_assert_expected_messages ();
1240 : : }
1241 : :
1242 : 1 : string = g_strdelimit (g_strdup (const_string), "<>", '?');
1243 : 1 : g_assert_cmpstr (string, ==, "ABCDE?*?Q");
1244 : 1 : g_free (string);
1245 : :
1246 : 1 : string = g_strdelimit (g_strdup (const_string), NULL, '?');
1247 : 1 : g_assert_cmpstr (string, ==, "ABCDE?*?Q");
1248 : 1 : g_free (string);
1249 : 1 : }
1250 : :
1251 : : /* Testing g_str_has_prefix() function avoiding the optimizing macro */
1252 : : static void
1253 : 1 : test_has_prefix (void)
1254 : : {
1255 [ + - ]: 1 : if (g_test_undefined ())
1256 : : {
1257 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1258 : : "*assertion*!= NULL*");
1259 : 1 : g_assert_false ((g_str_has_prefix) ("foo", NULL));
1260 : 1 : g_test_assert_expected_messages ();
1261 : :
1262 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1263 : : "*assertion*!= NULL*");
1264 : 1 : g_assert_false ((g_str_has_prefix) (NULL, "foo"));
1265 : 1 : g_test_assert_expected_messages ();
1266 : : }
1267 : :
1268 : : /* Having a string smaller than the prefix */
1269 : 1 : g_assert_false ((g_str_has_prefix) ("aa", "aaa"));
1270 : :
1271 : : /* Negative tests */
1272 : 1 : g_assert_false ((g_str_has_prefix) ("foo", "bar"));
1273 : 1 : g_assert_false ((g_str_has_prefix) ("foo", "foobar"));
1274 : 1 : g_assert_false ((g_str_has_prefix) ("foobar", "bar"));
1275 : :
1276 : : /* Positive tests */
1277 : 1 : g_assert_true ((g_str_has_prefix) ("foobar", "foo"));
1278 : 1 : g_assert_true ((g_str_has_prefix) ("foo", ""));
1279 : 1 : g_assert_true ((g_str_has_prefix) ("foo", "foo"));
1280 : 1 : g_assert_true ((g_str_has_prefix) ("", ""));
1281 : 1 : }
1282 : :
1283 : : /* Testing g_str_has_prefix() optimized macro */
1284 : : static void
1285 : 1 : test_has_prefix_macro (void)
1286 : : {
1287 : : #if G_GNUC_CHECK_VERSION (2, 0)
1288 : : #ifndef g_str_has_prefix
1289 : : #error g_str_has_prefix() should be defined as a macro in this platform!
1290 : : #endif
1291 : : #else
1292 : : g_test_incomplete ("g_str_has_prefix() is not inlined in this platform");
1293 : : #endif
1294 : :
1295 [ + - ]: 1 : if (g_test_undefined ())
1296 : : {
1297 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1298 : : "*assertion*!= NULL*");
1299 : 1 : g_assert_false (g_str_has_prefix ("foo", NULL));
1300 : 1 : g_test_assert_expected_messages ();
1301 : :
1302 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1303 : : "*assertion*!= NULL*");
1304 : 1 : g_assert_false (g_str_has_prefix (NULL, "foo"));
1305 : 1 : g_test_assert_expected_messages ();
1306 : : }
1307 : :
1308 : : /* Having a string smaller than the prefix */
1309 : 1 : g_assert_false (g_str_has_prefix ("aa", "aaa"));
1310 : :
1311 : : /* Negative tests */
1312 : 1 : g_assert_false (g_str_has_prefix ("foo", "bar"));
1313 : 1 : g_assert_false (g_str_has_prefix ("foo", "foobar"));
1314 : 1 : g_assert_false (g_str_has_prefix ("foobar", "bar"));
1315 : :
1316 : : /* Positive tests */
1317 : 1 : g_assert_true (g_str_has_prefix ("foobar", "foo"));
1318 : 1 : g_assert_true (g_str_has_prefix ("foo", ""));
1319 : 1 : g_assert_true (g_str_has_prefix ("foo", "foo"));
1320 : 1 : g_assert_true (g_str_has_prefix ("", ""));
1321 : :
1322 : : /* Testing the nested G_UNLIKELY */
1323 : 1 : g_assert_false (G_UNLIKELY (g_str_has_prefix ("foo", "aaa")));
1324 : 1 : }
1325 : :
1326 : : /* Testing g_str_has_suffix() function avoiding the optimizing macro */
1327 : : static void
1328 : 1 : test_has_suffix (void)
1329 : : {
1330 [ + - ]: 1 : if (g_test_undefined ())
1331 : : {
1332 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1333 : : "*assertion*!= NULL*");
1334 : 1 : g_assert_false ((g_str_has_suffix) ("foo", NULL));
1335 : 1 : g_test_assert_expected_messages ();
1336 : :
1337 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1338 : : "*assertion*!= NULL*");
1339 : 1 : g_assert_false ((g_str_has_suffix) (NULL, "foo"));
1340 : 1 : g_test_assert_expected_messages ();
1341 : : }
1342 : :
1343 : : /* Having a string smaller than the suffix */
1344 : 1 : g_assert_false ((g_str_has_suffix) ("aa", "aaa"));
1345 : :
1346 : : /* Negative tests */
1347 : 1 : g_assert_false ((g_str_has_suffix) ("foo", "bar"));
1348 : 1 : g_assert_false ((g_str_has_suffix) ("bar", "foobar"));
1349 : 1 : g_assert_false ((g_str_has_suffix) ("foobar", "foo"));
1350 : :
1351 : : /* Positive tests */
1352 : 1 : g_assert_true ((g_str_has_suffix) ("foobar", "bar"));
1353 : 1 : g_assert_true ((g_str_has_suffix) ("foo", ""));
1354 : 1 : g_assert_true ((g_str_has_suffix) ("foo", "foo"));
1355 : 1 : g_assert_true ((g_str_has_suffix) ("", ""));
1356 : 1 : }
1357 : :
1358 : : /* Testing g_str_has_prefix() optimized macro */
1359 : : static void
1360 : 1 : test_has_suffix_macro (void)
1361 : : {
1362 : : #if G_GNUC_CHECK_VERSION (2, 0)
1363 : : #ifndef g_str_has_suffix
1364 : : #error g_str_has_suffix() should be defined as a macro in this platform!
1365 : : #endif
1366 : : #else
1367 : : g_test_incomplete ("g_str_has_suffix() is not inlined in this platform");
1368 : : #endif
1369 : :
1370 [ + - ]: 1 : if (g_test_undefined ())
1371 : : {
1372 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1373 : : "*assertion*!= NULL*");
1374 : 1 : g_assert_false (g_str_has_suffix ("foo", NULL));
1375 : 1 : g_test_assert_expected_messages ();
1376 : :
1377 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1378 : : "*assertion*!= NULL*");
1379 : 1 : g_assert_false (g_str_has_suffix (NULL, "foo"));
1380 : 1 : g_test_assert_expected_messages ();
1381 : : }
1382 : :
1383 : : /* Having a string smaller than the suffix */
1384 : 1 : g_assert_false (g_str_has_suffix ("aa", "aaa"));
1385 : :
1386 : : /* Negative tests */
1387 : 1 : g_assert_false (g_str_has_suffix ("foo", "bar"));
1388 : 1 : g_assert_false (g_str_has_suffix ("bar", "foobar"));
1389 : 1 : g_assert_false (g_str_has_suffix ("foobar", "foo"));
1390 : :
1391 : : /* Positive tests */
1392 : 1 : g_assert_true (g_str_has_suffix ("foobar", "bar"));
1393 : 1 : g_assert_true (g_str_has_suffix ("foo", ""));
1394 : 1 : g_assert_true (g_str_has_suffix ("foo", "foo"));
1395 : 1 : g_assert_true (g_str_has_suffix ("", ""));
1396 : 1 : }
1397 : :
1398 : : static void
1399 : 90 : strv_check (gchar **strv, ...)
1400 : : {
1401 : 90 : gboolean ok = TRUE;
1402 : 90 : gint i = 0;
1403 : : va_list list;
1404 : :
1405 : 90 : va_start (list, strv);
1406 [ + - ]: 303 : while (ok)
1407 : : {
1408 : 303 : const gchar *str = va_arg (list, const char *);
1409 [ + + ]: 303 : if (strv[i] == NULL)
1410 : : {
1411 : 90 : g_assert_null (str);
1412 : 90 : break;
1413 : : }
1414 [ - + ]: 213 : if (str == NULL)
1415 : : {
1416 : 0 : ok = FALSE;
1417 : : }
1418 : : else
1419 : : {
1420 : 213 : g_assert_cmpstr (strv[i], ==, str);
1421 : : }
1422 : 213 : i++;
1423 : : }
1424 : 90 : va_end (list);
1425 : :
1426 : 90 : g_strfreev (strv);
1427 : 90 : }
1428 : :
1429 : : /* Testing g_strsplit() function with various positive and negative cases */
1430 : : static void
1431 : 1 : test_strsplit (void)
1432 : : {
1433 : 1 : gchar **string = NULL;
1434 : :
1435 [ + - ]: 1 : if (g_test_undefined ())
1436 : : {
1437 : : /* Testing degenerated cases */
1438 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1439 : : "*assertion*!= NULL*");
1440 : 1 : string = g_strsplit (NULL, ",", 0);
1441 : 1 : g_test_assert_expected_messages ();
1442 : 1 : g_assert_null (string);
1443 : :
1444 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1445 : : "*assertion*!= NULL*");
1446 : 1 : string = g_strsplit ("x", NULL, 0);
1447 : 1 : g_test_assert_expected_messages ();
1448 : 1 : g_assert_null (string);
1449 : :
1450 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1451 : : "*assertion \'delimiter[0] != \'\\0\'*");
1452 : 1 : string = g_strsplit ("x", "", 0);
1453 : 1 : g_test_assert_expected_messages ();
1454 : 1 : g_assert_null (string);
1455 : : }
1456 : :
1457 : 1 : strv_check (g_strsplit ("", ",", 0), NULL);
1458 : 1 : strv_check (g_strsplit ("x", ",", 0), "x", NULL);
1459 : 1 : strv_check (g_strsplit ("x,y", ",", 0), "x", "y", NULL);
1460 : 1 : strv_check (g_strsplit ("x,y,", ",", 0), "x", "y", "", NULL);
1461 : 1 : strv_check (g_strsplit (",x,y", ",", 0), "", "x", "y", NULL);
1462 : 1 : strv_check (g_strsplit (",x,y,", ",", 0), "", "x", "y", "", NULL);
1463 : 1 : strv_check (g_strsplit ("x,y,z", ",", 0), "x", "y", "z", NULL);
1464 : 1 : strv_check (g_strsplit ("x,y,z,", ",", 0), "x", "y", "z", "", NULL);
1465 : 1 : strv_check (g_strsplit (",x,y,z", ",", 0), "", "x", "y", "z", NULL);
1466 : 1 : strv_check (g_strsplit (",x,y,z,", ",", 0), "", "x", "y", "z", "", NULL);
1467 : 1 : strv_check (g_strsplit (",,x,,y,,z,,", ",", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
1468 : 1 : strv_check (g_strsplit (",,x,,y,,z,,", ",,", 0), "", "x", "y", "z", "", NULL);
1469 : :
1470 : 1 : strv_check (g_strsplit ("", ",", 1), NULL);
1471 : 1 : strv_check (g_strsplit ("x", ",", 1), "x", NULL);
1472 : 1 : strv_check (g_strsplit ("x,y", ",", 1), "x,y", NULL);
1473 : 1 : strv_check (g_strsplit ("x,y,", ",", 1), "x,y,", NULL);
1474 : 1 : strv_check (g_strsplit (",x,y", ",", 1), ",x,y", NULL);
1475 : 1 : strv_check (g_strsplit (",x,y,", ",", 1), ",x,y,", NULL);
1476 : 1 : strv_check (g_strsplit ("x,y,z", ",", 1), "x,y,z", NULL);
1477 : 1 : strv_check (g_strsplit ("x,y,z,", ",", 1), "x,y,z,", NULL);
1478 : 1 : strv_check (g_strsplit (",x,y,z", ",", 1), ",x,y,z", NULL);
1479 : 1 : strv_check (g_strsplit (",x,y,z,", ",", 1), ",x,y,z,", NULL);
1480 : 1 : strv_check (g_strsplit (",,x,,y,,z,,", ",", 1), ",,x,,y,,z,,", NULL);
1481 : 1 : strv_check (g_strsplit (",,x,,y,,z,,", ",,", 1), ",,x,,y,,z,,", NULL);
1482 : :
1483 : 1 : strv_check (g_strsplit ("", ",", 2), NULL);
1484 : 1 : strv_check (g_strsplit ("x", ",", 2), "x", NULL);
1485 : 1 : strv_check (g_strsplit ("x,y", ",", 2), "x", "y", NULL);
1486 : 1 : strv_check (g_strsplit ("x,y,", ",", 2), "x", "y,", NULL);
1487 : 1 : strv_check (g_strsplit (",x,y", ",", 2), "", "x,y", NULL);
1488 : 1 : strv_check (g_strsplit (",x,y,", ",", 2), "", "x,y,", NULL);
1489 : 1 : strv_check (g_strsplit ("x,y,z", ",", 2), "x", "y,z", NULL);
1490 : 1 : strv_check (g_strsplit ("x,y,z,", ",", 2), "x", "y,z,", NULL);
1491 : 1 : strv_check (g_strsplit (",x,y,z", ",", 2), "", "x,y,z", NULL);
1492 : 1 : strv_check (g_strsplit (",x,y,z,", ",", 2), "", "x,y,z,", NULL);
1493 : 1 : strv_check (g_strsplit (",,x,,y,,z,,", ",", 2), "", ",x,,y,,z,,", NULL);
1494 : 1 : strv_check (g_strsplit (",,x,,y,,z,,", ",,", 2), "", "x,,y,,z,,", NULL);
1495 : 1 : }
1496 : :
1497 : : /* Testing function g_strsplit_set() */
1498 : : static void
1499 : 1 : test_strsplit_set (void)
1500 : : {
1501 : 1 : gchar **string = NULL;
1502 : :
1503 [ + - ]: 1 : if (g_test_undefined ())
1504 : : {
1505 : : /* Testing degenerated cases */
1506 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1507 : : "*assertion*!= NULL*");
1508 : 1 : string = g_strsplit_set (NULL, ",/", 0);
1509 : 1 : g_test_assert_expected_messages ();
1510 : 1 : g_assert_null (string);
1511 : :
1512 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1513 : : "*assertion*!= NULL*");
1514 : 1 : string = g_strsplit_set ("", NULL, 0);
1515 : 1 : g_test_assert_expected_messages ();
1516 : 1 : g_assert_null (string);
1517 : : }
1518 : :
1519 : 1 : strv_check (g_strsplit_set ("", ",/", 0), NULL);
1520 : 1 : strv_check (g_strsplit_set (":def/ghi:", ":/", -1), "", "def", "ghi", "", NULL);
1521 : 1 : strv_check (g_strsplit_set ("abc:def/ghi", ":/", -1), "abc", "def", "ghi", NULL);
1522 : 1 : strv_check (g_strsplit_set (",;,;,;,;", ",;", -1), "", "", "", "", "", "", "", "", "", NULL);
1523 : 1 : strv_check (g_strsplit_set (",,abc.def", ".,", -1), "", "", "abc", "def", NULL);
1524 : :
1525 : 1 : strv_check (g_strsplit_set (",x.y", ",.", 0), "", "x", "y", NULL);
1526 : 1 : strv_check (g_strsplit_set (".x,y,", ",.", 0), "", "x", "y", "", NULL);
1527 : 1 : strv_check (g_strsplit_set ("x,y.z", ",.", 0), "x", "y", "z", NULL);
1528 : 1 : strv_check (g_strsplit_set ("x.y,z,", ",.", 0), "x", "y", "z", "", NULL);
1529 : 1 : strv_check (g_strsplit_set (",x.y,z", ",.", 0), "", "x", "y", "z", NULL);
1530 : 1 : strv_check (g_strsplit_set (",x,y,z,", ",.", 0), "", "x", "y", "z", "", NULL);
1531 : 1 : strv_check (g_strsplit_set (",.x,,y,;z..", ".,;", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
1532 : 1 : strv_check (g_strsplit_set (",,x,,y,,z,,", ",,", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
1533 : :
1534 : 1 : strv_check (g_strsplit_set ("x,y.z", ",.", 1), "x,y.z", NULL);
1535 : 1 : strv_check (g_strsplit_set ("x.y,z,", ",.", 1), "x.y,z,", NULL);
1536 : 1 : strv_check (g_strsplit_set (",x,y,z", ",.", 1), ",x,y,z", NULL);
1537 : 1 : strv_check (g_strsplit_set (",x,y.z,", ",.", 1), ",x,y.z,", NULL);
1538 : 1 : strv_check (g_strsplit_set (",,x,.y,,z,,", ",.", 1), ",,x,.y,,z,,", NULL);
1539 : 1 : strv_check (g_strsplit_set (",.x,,y,,z,,", ",,..", 1), ",.x,,y,,z,,", NULL);
1540 : :
1541 : 1 : strv_check (g_strsplit_set ("", ",", 0), NULL);
1542 : 1 : strv_check (g_strsplit_set ("x", ",", 0), "x", NULL);
1543 : 1 : strv_check (g_strsplit_set ("x,y", ",", 0), "x", "y", NULL);
1544 : 1 : strv_check (g_strsplit_set ("x,y,", ",", 0), "x", "y", "", NULL);
1545 : 1 : strv_check (g_strsplit_set (",x,y", ",", 0), "", "x", "y", NULL);
1546 : 1 : strv_check (g_strsplit_set (",x,y,", ",", 0), "", "x", "y", "", NULL);
1547 : 1 : strv_check (g_strsplit_set ("x,y,z", ",", 0), "x", "y", "z", NULL);
1548 : 1 : strv_check (g_strsplit_set ("x,y,z,", ",", 0), "x", "y", "z", "", NULL);
1549 : 1 : strv_check (g_strsplit_set (",x,y,z", ",", 0), "", "x", "y", "z", NULL);
1550 : 1 : strv_check (g_strsplit_set (",x,y,z,", ",", 0), "", "x", "y", "z", "", NULL);
1551 : 1 : strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 0), "", "", "x", "", "y", "", "z", "", "", NULL);
1552 : :
1553 : 1 : strv_check (g_strsplit_set ("", ",", 1), NULL);
1554 : 1 : strv_check (g_strsplit_set ("x", ",", 1), "x", NULL);
1555 : 1 : strv_check (g_strsplit_set ("x,y", ",", 1), "x,y", NULL);
1556 : 1 : strv_check (g_strsplit_set ("x,y,", ",", 1), "x,y,", NULL);
1557 : 1 : strv_check (g_strsplit_set (",x,y", ",", 1), ",x,y", NULL);
1558 : 1 : strv_check (g_strsplit_set (",x,y,", ",", 1), ",x,y,", NULL);
1559 : 1 : strv_check (g_strsplit_set ("x,y,z", ",", 1), "x,y,z", NULL);
1560 : 1 : strv_check (g_strsplit_set ("x,y,z,", ",", 1), "x,y,z,", NULL);
1561 : 1 : strv_check (g_strsplit_set (",x,y,z", ",", 1), ",x,y,z", NULL);
1562 : 1 : strv_check (g_strsplit_set (",x,y,z,", ",", 1), ",x,y,z,", NULL);
1563 : 1 : strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 1), ",,x,,y,,z,,", NULL);
1564 : 1 : strv_check (g_strsplit_set (",,x,,y,,z,,", ",,", 1), ",,x,,y,,z,,", NULL);
1565 : :
1566 : 1 : strv_check (g_strsplit_set ("", ",", 2), NULL);
1567 : 1 : strv_check (g_strsplit_set ("x", ",", 2), "x", NULL);
1568 : 1 : strv_check (g_strsplit_set ("x,y", ",", 2), "x", "y", NULL);
1569 : 1 : strv_check (g_strsplit_set ("x,y,", ",", 2), "x", "y,", NULL);
1570 : 1 : strv_check (g_strsplit_set (",x,y", ",", 2), "", "x,y", NULL);
1571 : 1 : strv_check (g_strsplit_set (",x,y,", ",", 2), "", "x,y,", NULL);
1572 : 1 : strv_check (g_strsplit_set ("x,y,z", ",", 2), "x", "y,z", NULL);
1573 : 1 : strv_check (g_strsplit_set ("x,y,z,", ",", 2), "x", "y,z,", NULL);
1574 : 1 : strv_check (g_strsplit_set (",x,y,z", ",", 2), "", "x,y,z", NULL);
1575 : 1 : strv_check (g_strsplit_set (",x,y,z,", ",", 2), "", "x,y,z,", NULL);
1576 : 1 : strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 2), "", ",x,,y,,z,,", NULL);
1577 : :
1578 : 1 : strv_check (g_strsplit_set (",,x,.y,..z,,", ",.", 3), "", "", "x,.y,..z,,", NULL);
1579 : 1 : }
1580 : :
1581 : : /* Testing g_strv_length() function with various positive and negative cases */
1582 : : static void
1583 : 1 : test_strv_length (void)
1584 : : {
1585 : : gchar **strv;
1586 : : guint l;
1587 : :
1588 [ + - ]: 1 : if (g_test_undefined ())
1589 : : {
1590 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1591 : : "*assertion*!= NULL*");
1592 : 1 : l = g_strv_length (NULL);
1593 : 1 : g_test_assert_expected_messages ();
1594 : 1 : g_assert_cmpint (l, ==, 0);
1595 : : }
1596 : :
1597 : 1 : strv = g_strsplit ("1,2,3,4", ",", -1);
1598 : 1 : l = g_strv_length (strv);
1599 : 1 : g_assert_cmpuint (l, ==, 4);
1600 : 1 : g_strfreev (strv);
1601 : 1 : }
1602 : :
1603 : : static char *locales[] = {"sv_SE", "en_US", "fa_IR", "C", "ru_RU"};
1604 : :
1605 : : static void
1606 : 27 : check_strtod_string (gchar *number,
1607 : : double res,
1608 : : gboolean check_end,
1609 : : gsize correct_len)
1610 : : {
1611 : : double d;
1612 : : gsize l;
1613 : : gchar *dummy;
1614 : :
1615 : : /* we try a copy of number, with some free space for malloc before that.
1616 : : * This is supposed to smash the some wrong pointer calculations. */
1617 : :
1618 : 27 : dummy = g_malloc (100000);
1619 : 27 : number = g_strdup (number);
1620 : 27 : g_free (dummy);
1621 : :
1622 [ + + ]: 162 : for (l = 0; l < G_N_ELEMENTS (locales); l++)
1623 : : {
1624 : 135 : gchar *end = "(unset)";
1625 : :
1626 : 135 : setlocale (LC_NUMERIC, locales[l]);
1627 : 135 : d = g_ascii_strtod (number, &end);
1628 : 135 : g_assert_true (isnan (res) ? isnan (d) : (d == res));
1629 : 135 : g_assert_true ((gsize) (end - number) ==
1630 : : (check_end ? correct_len : strlen (number)));
1631 : : }
1632 : :
1633 : 27 : g_free (number);
1634 : 27 : }
1635 : :
1636 : : static void
1637 : 5 : check_strtod_number (gdouble num, gchar *fmt, gchar *str)
1638 : : {
1639 : : gsize l;
1640 : : gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
1641 : :
1642 [ + + ]: 30 : for (l = 0; l < G_N_ELEMENTS (locales); l++)
1643 : : {
1644 : 25 : setlocale (LC_ALL, locales[l]);
1645 : 25 : g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, fmt, num);
1646 : 25 : g_assert_cmpstr (buf, ==, str);
1647 : : }
1648 : 5 : }
1649 : :
1650 : : /* Testing g_ascii_strtod() function with various positive and negative cases */
1651 : : static void
1652 : 1 : test_ascii_strtod (void)
1653 : : {
1654 : : gdouble d, our_nan, our_inf;
1655 : : char buffer[G_ASCII_DTOSTR_BUF_SIZE];
1656 : :
1657 : : #ifdef NAN
1658 : 1 : our_nan = NAN;
1659 : : #else
1660 : : /* Do this before any call to setlocale. */
1661 : : our_nan = atof ("NaN");
1662 : : #endif
1663 : 1 : g_assert_true (isnan (our_nan));
1664 : :
1665 : : #ifdef INFINITY
1666 : 1 : our_inf = INFINITY;
1667 : : #else
1668 : : our_inf = atof ("Infinity");
1669 : : #endif
1670 : 1 : g_assert_true (our_inf > 1 && our_inf == our_inf / 2);
1671 : :
1672 : : /* Testing degenerated cases */
1673 [ + - ]: 1 : if (g_test_undefined ())
1674 : : {
1675 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1676 : : "*assertion*!= NULL*");
1677 : 1 : d = g_ascii_strtod (NULL, NULL);
1678 : 1 : g_test_assert_expected_messages ();
1679 : : }
1680 : :
1681 : : /* Testing normal cases */
1682 : 1 : check_strtod_string ("123.123", 123.123, FALSE, 0);
1683 : 1 : check_strtod_string ("123.123e2", 123.123e2, FALSE, 0);
1684 : 1 : check_strtod_string ("123.123e-2", 123.123e-2, FALSE, 0);
1685 : 1 : check_strtod_string ("-123.123", -123.123, FALSE, 0);
1686 : 1 : check_strtod_string ("-123.123e2", -123.123e2, FALSE, 0);
1687 : 1 : check_strtod_string ("-123.123e-2", -123.123e-2, FALSE, 0);
1688 : 1 : check_strtod_string ("5.4", 5.4, TRUE, 3);
1689 : 1 : check_strtod_string ("5.4,5.5", 5.4, TRUE, 3);
1690 : 1 : check_strtod_string ("5,4", 5.0, TRUE, 1);
1691 : : #ifndef _MSC_VER
1692 : : /* hex strings for strtod() is a C99 feature which Visual C++ does not support */
1693 : 1 : check_strtod_string ("0xa.b", 10.6875, TRUE, 5);
1694 : 1 : check_strtod_string ("0xa.bP3", 85.5, TRUE, 7);
1695 : 1 : check_strtod_string ("0xa.bp+3", 85.5, TRUE, 8);
1696 : 1 : check_strtod_string ("0xa.bp-2", 2.671875, TRUE, 8);
1697 : 1 : check_strtod_string ("0xA.BG", 10.6875, TRUE, 5);
1698 : : #endif
1699 : : /* the following are for #156421 */
1700 : 1 : check_strtod_string ("1e1", 1e1, FALSE, 0);
1701 : : #ifndef _MSC_VER
1702 : : /* NAN/-nan/INF/-infinity strings for strtod() are C99 features which Visual C++ does not support */
1703 : 1 : check_strtod_string ("NAN", our_nan, FALSE, 0);
1704 : 1 : check_strtod_string ("-nan", -our_nan, FALSE, 0);
1705 : 1 : check_strtod_string ("INF", our_inf, FALSE, 0);
1706 : 1 : check_strtod_string ("-infinity", -our_inf, FALSE, 0);
1707 : : #endif
1708 : 1 : check_strtod_string ("-.75,0", -0.75, TRUE, 4);
1709 : :
1710 : : #ifndef _MSC_VER
1711 : : /* the values of d in the following 2 tests generate a C1064 compiler limit error */
1712 : 1 : d = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
1713 : 1 : g_assert_true (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
1714 : :
1715 : 1 : d = -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
1716 : 1 : g_assert_true (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
1717 : : #endif
1718 : :
1719 : 1 : d = pow (2.0, -1024.1);
1720 : 1 : g_assert_true (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
1721 : :
1722 : 1 : d = -pow (2.0, -1024.1);
1723 : 1 : g_assert_true (d == g_ascii_strtod (g_ascii_dtostr (buffer, sizeof (buffer), d), NULL));
1724 : :
1725 : : /* for #343899 */
1726 : 1 : check_strtod_string (" 0.75", 0.75, FALSE, 0);
1727 : 1 : check_strtod_string (" +0.75", 0.75, FALSE, 0);
1728 : 1 : check_strtod_string (" -0.75", -0.75, FALSE, 0);
1729 : 1 : check_strtod_string ("\f0.75", 0.75, FALSE, 0);
1730 : 1 : check_strtod_string ("\n0.75", 0.75, FALSE, 0);
1731 : 1 : check_strtod_string ("\r0.75", 0.75, FALSE, 0);
1732 : 1 : check_strtod_string ("\t0.75", 0.75, FALSE, 0);
1733 : :
1734 : : #if 0
1735 : : /* g_ascii_isspace() returns FALSE for vertical tab, see #59388 */
1736 : : check_strtod_string ("\v0.75", 0.75, FALSE, 0);
1737 : : #endif
1738 : :
1739 : : /* for #343899 */
1740 : 1 : check_strtod_number (0.75, "%0.2f", "0.75");
1741 : 1 : check_strtod_number (0.75, "%5.2f", " 0.75");
1742 : 1 : check_strtod_number (-0.75, "%0.2f", "-0.75");
1743 : 1 : check_strtod_number (-0.75, "%5.2f", "-0.75");
1744 : 1 : check_strtod_number (1e99, "%.0e", "1e+99");
1745 : 1 : }
1746 : :
1747 : : static void
1748 : 8 : check_uint64 (const gchar *str,
1749 : : const gchar *end,
1750 : : gint base,
1751 : : guint64 result,
1752 : : gint error)
1753 : : {
1754 : : guint64 actual;
1755 : 8 : gchar *endptr = NULL;
1756 : : gint err;
1757 : :
1758 : 8 : errno = 0;
1759 : 8 : actual = g_ascii_strtoull (str, &endptr, base);
1760 : 8 : err = errno;
1761 : :
1762 : 8 : g_assert_true (actual == result);
1763 : 8 : g_assert_cmpstr (end, ==, endptr);
1764 : 8 : g_assert_true (err == error);
1765 : 8 : }
1766 : :
1767 : : static void
1768 : 9 : check_int64 (const gchar *str,
1769 : : const gchar *end,
1770 : : gint base,
1771 : : gint64 result,
1772 : : gint error)
1773 : : {
1774 : : gint64 actual;
1775 : 9 : gchar *endptr = NULL;
1776 : : gint err;
1777 : :
1778 : 9 : errno = 0;
1779 : 9 : actual = g_ascii_strtoll (str, &endptr, base);
1780 : 9 : err = errno;
1781 : :
1782 : 9 : g_assert_true (actual == result);
1783 : 9 : g_assert_cmpstr (end, ==, endptr);
1784 : 9 : g_assert_true (err == error);
1785 : 9 : }
1786 : :
1787 : : static void
1788 : 1 : test_strtoll (void)
1789 : : {
1790 : 1 : check_uint64 ("0", "", 10, 0, 0);
1791 : 1 : check_uint64 ("+0", "", 10, 0, 0);
1792 : 1 : check_uint64 ("-0", "", 10, 0, 0);
1793 : 1 : check_uint64 ("18446744073709551615", "", 10, G_MAXUINT64, 0);
1794 : 1 : check_uint64 ("18446744073709551616", "", 10, G_MAXUINT64, ERANGE);
1795 : 1 : check_uint64 ("20xyz", "xyz", 10, 20, 0);
1796 : 1 : check_uint64 ("-1", "", 10, G_MAXUINT64, 0);
1797 : 1 : check_uint64 ("-FF4", "", 16, -((guint64) 0xFF4), 0);
1798 : :
1799 : 1 : check_int64 ("0", "", 10, 0, 0);
1800 : 1 : check_int64 ("9223372036854775807", "", 10, G_MAXINT64, 0);
1801 : 1 : check_int64 ("9223372036854775808", "", 10, G_MAXINT64, ERANGE);
1802 : 1 : check_int64 ("-9223372036854775808", "", 10, G_MININT64, 0);
1803 : 1 : check_int64 ("-9223372036854775809", "", 10, G_MININT64, ERANGE);
1804 : 1 : check_int64 ("32768", "", 10, 32768, 0);
1805 : 1 : check_int64 ("-32768", "", 10, -32768, 0);
1806 : 1 : check_int64 ("001", "", 10, 1, 0);
1807 : 1 : check_int64 ("-001", "", 10, -1, 0);
1808 : 1 : }
1809 : :
1810 : : /* Testing g_str_match_string() function with various cases */
1811 : : static void
1812 : 1 : test_str_match_string (void)
1813 : : {
1814 : 1 : gboolean result = TRUE;
1815 : 1 : const gchar *str = "The quick brown fox¸ jumps over the lazy dog.";
1816 : :
1817 [ + - ]: 1 : if (g_test_undefined ())
1818 : : {
1819 : : /* Testing degenerated cases */
1820 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1821 : : "*assertion*!= NULL*");
1822 : 1 : result = g_str_match_string (NULL, "AAA", TRUE);
1823 : 1 : g_test_assert_expected_messages ();
1824 : 1 : g_assert_false (result);
1825 : :
1826 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1827 : : "*assertion*!= NULL*");
1828 : 1 : result = g_str_match_string (str, NULL, TRUE);
1829 : 1 : g_test_assert_expected_messages ();
1830 : 1 : g_assert_false (result);
1831 : : }
1832 : :
1833 : 1 : g_assert_false (g_str_match_string (str, "AAA", TRUE));
1834 : 1 : g_assert_false (g_str_match_string (str, "AAA", FALSE));
1835 : 1 : }
1836 : :
1837 : : /* Testing functions bounds */
1838 : : static void
1839 : 1 : test_bounds (void)
1840 : : {
1841 : : GMappedFile *file, *before, *after;
1842 : : char buffer[4097];
1843 : : char *tmp, *tmp2;
1844 : : char **array;
1845 : : char *string;
1846 : 1 : const char * const strjoinv_0[] = { NULL };
1847 : 1 : const char * const strjoinv_1[] = { "foo", NULL };
1848 : :
1849 : : /* if we allocate the file between two others and then free those
1850 : : * other two, then hopefully we end up with unmapped memory on either
1851 : : * side.
1852 : : */
1853 : 1 : before = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1854 : :
1855 : : /* quick workaround until #549783 can be fixed */
1856 [ + - ]: 1 : if (before == NULL)
1857 : 1 : return;
1858 : :
1859 : 0 : file = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1860 : 0 : after = g_mapped_file_new ("4096-random-bytes", TRUE, NULL);
1861 : 0 : g_mapped_file_unref (before);
1862 : 0 : g_mapped_file_unref (after);
1863 : :
1864 : 0 : g_assert_nonnull (file);
1865 : 0 : g_assert_cmpint (g_mapped_file_get_length (file), ==, 4096);
1866 : 0 : string = g_mapped_file_get_contents (file);
1867 : :
1868 : : /* ensure they're all non-nul */
1869 : 0 : g_assert_null (memchr (string, '\0', 4096));
1870 : :
1871 : : /* test set 1: ensure that nothing goes past its maximum length, even in
1872 : : * light of a missing nul terminator.
1873 : : *
1874 : : * we try to test all of the 'n' functions here.
1875 : : */
1876 : 0 : tmp = g_strndup (string, 4096);
1877 : 0 : g_assert_cmpint (strlen (tmp), ==, 4096);
1878 : 0 : g_free (tmp);
1879 : :
1880 : : /* found no bugs in gnome, i hope :) */
1881 : 0 : g_assert_null (g_strstr_len (string, 4096, "BUGS"));
1882 : 0 : g_strstr_len (string, 4096, "B");
1883 : 0 : g_strstr_len (string, 4096, ".");
1884 : 0 : g_strstr_len (string, 4096, "");
1885 : :
1886 : 0 : g_strrstr_len (string, 4096, "BUGS");
1887 : 0 : g_strrstr_len (string, 4096, "B");
1888 : 0 : g_strrstr_len (string, 4096, ".");
1889 : 0 : g_strrstr_len (string, 4096, "");
1890 : :
1891 : 0 : tmp = g_ascii_strup (string, 4096);
1892 : 0 : tmp2 = g_ascii_strup (tmp, 4096);
1893 : 0 : g_assert_cmpint (g_ascii_strncasecmp (string, tmp, 4096), ==, 0);
1894 : 0 : g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, 4096), ==, 0);
1895 : 0 : g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, 4096), ==, 0);
1896 : 0 : g_free (tmp);
1897 : 0 : g_free (tmp2);
1898 : :
1899 : 0 : tmp = g_ascii_strdown (string, 4096);
1900 : 0 : tmp2 = g_ascii_strdown (tmp, 4096);
1901 : 0 : g_assert_cmpint (g_ascii_strncasecmp (string, tmp, 4096), ==, 0);
1902 : 0 : g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, 4096), ==, 0);
1903 : 0 : g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, 4096), ==, 0);
1904 : 0 : g_free (tmp);
1905 : 0 : g_free (tmp2);
1906 : :
1907 : 0 : tmp = g_markup_escape_text (string, 4096);
1908 : 0 : g_free (tmp);
1909 : :
1910 : : /* test set 2: ensure that nothing reads even one byte past a '\0'.
1911 : : */
1912 : 0 : g_assert_cmpint (string[4095], ==, '\n');
1913 : 0 : string[4095] = '\0';
1914 : :
1915 : 0 : tmp = g_strdup (string);
1916 : 0 : g_assert_cmpint (strlen (tmp), ==, 4095);
1917 : 0 : g_free (tmp);
1918 : :
1919 : 0 : tmp = g_strndup (string, 10000);
1920 : 0 : g_assert_cmpint (strlen (tmp), ==, 4095);
1921 : 0 : g_free (tmp);
1922 : :
1923 : 0 : g_stpcpy (buffer, string);
1924 : 0 : g_assert_cmpint (strlen (buffer), ==, 4095);
1925 : :
1926 : 0 : g_strstr_len (string, 10000, "BUGS");
1927 : 0 : g_strstr_len (string, 10000, "B");
1928 : 0 : g_strstr_len (string, 10000, ".");
1929 : 0 : g_strstr_len (string, 10000, "");
1930 : :
1931 : 0 : g_strrstr (string, "BUGS");
1932 : 0 : g_strrstr (string, "B");
1933 : 0 : g_strrstr (string, ".");
1934 : 0 : g_strrstr (string, "");
1935 : :
1936 : 0 : g_strrstr_len (string, 10000, "BUGS");
1937 : 0 : g_strrstr_len (string, 10000, "B");
1938 : 0 : g_strrstr_len (string, 10000, ".");
1939 : 0 : g_strrstr_len (string, 10000, "");
1940 : :
1941 [ # # # # : 0 : g_str_has_prefix (string, "this won't do very much...");
# # ]
1942 [ # # # # : 0 : g_str_has_suffix (string, "but maybe this will...");
# # ]
1943 [ # # # # : 0 : g_str_has_suffix (string, "HMMMM.");
# # ]
1944 [ # # # # : 0 : g_str_has_suffix (string, "MMMM.");
# # ]
1945 [ # # # # : 0 : g_str_has_suffix (string, "M.");
# # ]
1946 : :
1947 : 0 : g_strlcpy (buffer, string, sizeof buffer);
1948 : 0 : g_assert_cmpint (strlen (buffer), ==, 4095);
1949 : 0 : g_strlcpy (buffer, string, sizeof buffer);
1950 : 0 : buffer[0] = '\0';
1951 : 0 : g_strlcat (buffer, string, sizeof buffer);
1952 : 0 : g_assert_cmpint (strlen (buffer), ==, 4095);
1953 : :
1954 : 0 : tmp = g_strdup_printf ("<%s>", string);
1955 : 0 : g_assert_cmpint (strlen (tmp), ==, 4095 + 2);
1956 : 0 : g_free (tmp);
1957 : :
1958 : 0 : tmp = g_ascii_strdown (string, -1);
1959 : 0 : tmp2 = g_ascii_strdown (tmp, -1);
1960 : 0 : g_assert_cmpint (strlen (tmp), ==, strlen (tmp2));
1961 : 0 : g_assert_cmpint (strlen (string), ==, strlen (tmp));
1962 : 0 : g_assert_cmpint (g_ascii_strncasecmp (string, tmp, -1), ==, 0);
1963 : 0 : g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, -1), ==, 0);
1964 : 0 : g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, -1), ==, 0);
1965 : 0 : g_free (tmp);
1966 : 0 : g_free (tmp2);
1967 : :
1968 : 0 : tmp = g_ascii_strup (string, -1);
1969 : 0 : tmp2 = g_ascii_strup (string, -1);
1970 : 0 : g_assert_cmpint (strlen (tmp), ==, strlen (tmp2));
1971 : 0 : g_assert_cmpint (strlen (string), ==, strlen (tmp));
1972 : 0 : g_assert_cmpint (g_ascii_strncasecmp (string, tmp, -1), ==, 0);
1973 : 0 : g_assert_cmpint (g_ascii_strncasecmp (string, tmp2, -1), ==, 0);
1974 : 0 : g_assert_cmpint (g_ascii_strncasecmp (tmp, tmp2, -1), ==, 0);
1975 : 0 : g_free (tmp);
1976 : 0 : g_free (tmp2);
1977 : :
1978 : 0 : g_ascii_strcasecmp (string, string);
1979 : 0 : g_ascii_strncasecmp (string, string, 10000);
1980 : :
1981 : 0 : g_strreverse (string);
1982 : 0 : g_strreverse (string);
1983 : 0 : g_strchug (string);
1984 : 0 : g_strchomp (string);
1985 : 0 : g_strstrip (string);
1986 : 0 : g_assert_cmpint (strlen (string), ==, 4095);
1987 : :
1988 : 0 : g_strdelimit (string, "M", 'N');
1989 : 0 : g_strcanon (string, " N.", ':');
1990 : 0 : g_assert_cmpint (strlen (string), ==, 4095);
1991 : :
1992 : 0 : array = g_strsplit (string, ".", -1);
1993 : 0 : tmp = g_strjoinv (".", array);
1994 : 0 : g_strfreev (array);
1995 : :
1996 : 0 : g_assert_cmpmem (tmp, strlen (tmp), string, 4095);
1997 : 0 : g_free (tmp);
1998 : :
1999 : 0 : tmp = g_strjoinv ("/", (char **) strjoinv_0);
2000 : 0 : g_assert_cmpstr (tmp, ==, "");
2001 : 0 : g_free (tmp);
2002 : :
2003 : 0 : tmp = g_strjoinv ("/", (char **) strjoinv_1);
2004 : 0 : g_assert_cmpstr (tmp, ==, "foo");
2005 : 0 : g_free (tmp);
2006 : :
2007 : 0 : tmp = g_strconcat (string, string, string, NULL);
2008 : 0 : g_assert_cmpint (strlen (tmp), ==, 4095 * 3);
2009 : 0 : g_free (tmp);
2010 : :
2011 : 0 : tmp = g_strjoin ("!", string, string, NULL);
2012 : 0 : g_assert_cmpint (strlen (tmp), ==, 4095 + 1 + 4095);
2013 : 0 : g_free (tmp);
2014 : :
2015 : 0 : tmp = g_markup_escape_text (string, -1);
2016 : 0 : g_free (tmp);
2017 : :
2018 : 0 : tmp = g_markup_printf_escaped ("%s", string);
2019 : 0 : g_free (tmp);
2020 : :
2021 : 0 : tmp = g_strescape (string, NULL);
2022 : 0 : tmp2 = g_strcompress (tmp);
2023 : 0 : g_assert_cmpstr (string, ==, tmp2);
2024 : 0 : g_free (tmp2);
2025 : 0 : g_free (tmp);
2026 : :
2027 : 0 : g_mapped_file_unref (file);
2028 : : }
2029 : :
2030 : : /* Testing g_strip_context() function with various cases */
2031 : : static void
2032 : 1 : test_strip_context (void)
2033 : : {
2034 : : const gchar *msgid;
2035 : : const gchar *msgval;
2036 : : const gchar *s;
2037 : :
2038 : 1 : msgid = "blabla";
2039 : 1 : msgval = "bla";
2040 : 1 : s = g_strip_context (msgid, msgval);
2041 : 1 : g_assert_true (s == msgval);
2042 : :
2043 : 1 : msgid = msgval = "blabla";
2044 : 1 : s = g_strip_context (msgid, msgval);
2045 : 1 : g_assert_true (s == msgval);
2046 : :
2047 : 1 : msgid = msgval = "blabla|foo";
2048 : 1 : s = g_strip_context (msgid, msgval);
2049 : 1 : g_assert_true (s == msgval + 7);
2050 : :
2051 : 1 : msgid = msgval = "blabla||bar";
2052 : 1 : s = g_strip_context (msgid, msgval);
2053 : 1 : g_assert_true (s == msgval + 7);
2054 : 1 : }
2055 : :
2056 : : /* Test the strings returned by g_strerror() are valid and unique. On Windows,
2057 : : * fewer than 200 error numbers are used, so we expect some strings to
2058 : : * return a generic ‘unknown error code’ message. */
2059 : : static void
2060 : 1 : test_strerror (void)
2061 : : {
2062 : : GHashTable *strs;
2063 : : gint i;
2064 : : const gchar *str, *unknown_str;
2065 : :
2066 : 1 : setlocale (LC_ALL, "C");
2067 : :
2068 : 1 : unknown_str = g_strerror (-1);
2069 : 1 : strs = g_hash_table_new (g_str_hash, g_str_equal);
2070 [ + + ]: 200 : for (i = 1; i < 200; i++)
2071 : : {
2072 : : gboolean is_unknown;
2073 : 199 : str = g_strerror (i);
2074 : 199 : is_unknown = (strcmp (str, unknown_str) == 0);
2075 : 199 : g_assert_nonnull (str);
2076 : 199 : g_assert_true (g_utf8_validate (str, -1, NULL));
2077 : 199 : g_assert_true (!g_hash_table_contains (strs, str) || is_unknown);
2078 : 199 : g_hash_table_add (strs, (gpointer) str);
2079 : : }
2080 : :
2081 : 1 : g_hash_table_unref (strs);
2082 : 1 : }
2083 : :
2084 : : /* Testing g_strsignal() function with various cases */
2085 : : static void
2086 : 1 : test_strsignal (void)
2087 : : {
2088 : : gint i;
2089 : : const gchar *str;
2090 : :
2091 [ + + ]: 20 : for (i = 1; i < 20; i++)
2092 : : {
2093 : 19 : str = g_strsignal (i);
2094 : 19 : g_assert_nonnull (str);
2095 : 19 : g_assert_true (g_utf8_validate (str, -1, NULL));
2096 : : }
2097 : 1 : }
2098 : :
2099 : : /* Testing g_strup(), g_strdown() and g_strcasecmp() */
2100 : : static void
2101 : 1 : test_strup (void)
2102 : : {
2103 : 1 : gchar *s = NULL;
2104 : :
2105 [ + - ]: 1 : if (g_test_undefined ())
2106 : : {
2107 : : /* Testing degenerated cases */
2108 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2109 : : "*assertion*!= NULL*");
2110 : 1 : s = g_strup (NULL);
2111 : 1 : g_test_assert_expected_messages ();
2112 : :
2113 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2114 : : "*assertion*!= NULL*");
2115 : 1 : s = g_strdown (NULL);
2116 : 1 : g_test_assert_expected_messages ();
2117 : :
2118 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2119 : : "*assertion*!= NULL*");
2120 : 1 : g_strcasecmp (NULL, "ABCD");
2121 : 1 : g_test_assert_expected_messages ();
2122 : :
2123 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2124 : : "*assertion*!= NULL*");
2125 : 1 : g_strcasecmp ("abcd", NULL);
2126 : 1 : g_test_assert_expected_messages ();
2127 : : }
2128 : :
2129 : 1 : s = g_strdup ("lower UPPER");
2130 : 1 : g_assert_cmpstr (g_strup (s), ==, "LOWER UPPER");
2131 : 1 : g_assert_cmpstr (g_strdown (s), ==, "lower upper");
2132 : 1 : g_assert_true (g_strcasecmp ("lower", "LOWER") == 0);
2133 : 1 : g_free (s);
2134 : 1 : }
2135 : :
2136 : : /* Testing g_str_to_ascii() function with various cases */
2137 : : static void
2138 : 1 : test_transliteration (void)
2139 : : {
2140 : : gchar *out;
2141 : :
2142 : : /* ...to test the defaults */
2143 : 1 : setlocale (LC_ALL, "C");
2144 : :
2145 : : /* Test something trivial */
2146 : 1 : out = g_str_to_ascii ("hello", NULL);
2147 : 1 : g_assert_cmpstr (out, ==, "hello");
2148 : 1 : g_free (out);
2149 : :
2150 : : /* Test something above 0xffff */
2151 : 1 : out = g_str_to_ascii ("𝐀𝐀𝐀", NULL);
2152 : 1 : g_assert_cmpstr (out, ==, "AAA");
2153 : 1 : g_free (out);
2154 : :
2155 : : /* Test something with no good match */
2156 : 1 : out = g_str_to_ascii ("a ∧ ¬a", NULL);
2157 : 1 : g_assert_cmpstr (out, ==, "a ? ?a");
2158 : 1 : g_free (out);
2159 : :
2160 : : /* Make sure 'ö' is handled differently per locale */
2161 : 1 : out = g_str_to_ascii ("ö", NULL);
2162 : 1 : g_assert_cmpstr (out, ==, "o");
2163 : 1 : g_free (out);
2164 : :
2165 : 1 : out = g_str_to_ascii ("ö", "sv");
2166 : 1 : g_assert_cmpstr (out, ==, "o");
2167 : 1 : g_free (out);
2168 : :
2169 : 1 : out = g_str_to_ascii ("ö", "de");
2170 : 1 : g_assert_cmpstr (out, ==, "oe");
2171 : 1 : g_free (out);
2172 : :
2173 : : /* Make sure we can find a locale by a wide range of names */
2174 : 1 : out = g_str_to_ascii ("ö", "de_DE");
2175 : 1 : g_assert_cmpstr (out, ==, "oe");
2176 : 1 : g_free (out);
2177 : :
2178 : 1 : out = g_str_to_ascii ("ö", "de_DE.UTF-8");
2179 : 1 : g_assert_cmpstr (out, ==, "oe");
2180 : 1 : g_free (out);
2181 : :
2182 : 1 : out = g_str_to_ascii ("ö", "de_DE.UTF-8@euro");
2183 : 1 : g_assert_cmpstr (out, ==, "oe");
2184 : 1 : g_free (out);
2185 : :
2186 : 1 : out = g_str_to_ascii ("ö", "de@euro");
2187 : 1 : g_assert_cmpstr (out, ==, "oe");
2188 : 1 : g_free (out);
2189 : :
2190 : : /* Test some invalid locale names */
2191 : 1 : out = g_str_to_ascii ("ö", "de_DE@euro.UTF-8");
2192 : 1 : g_assert_cmpstr (out, ==, "o");
2193 : 1 : g_free (out);
2194 : :
2195 : 1 : out = g_str_to_ascii ("ö", "de@DE@euro");
2196 : 1 : g_assert_cmpstr (out, ==, "o");
2197 : 1 : g_free (out);
2198 : :
2199 : 1 : out = g_str_to_ascii ("ö", "doesnotexist");
2200 : 1 : g_assert_cmpstr (out, ==, "o");
2201 : 1 : g_free (out);
2202 : :
2203 : 1 : out = g_str_to_ascii ("ö", "thislocalenameistoolong");
2204 : 1 : g_assert_cmpstr (out, ==, "o");
2205 : 1 : g_free (out);
2206 : :
2207 : : /* Try a lookup of a locale with a variant */
2208 : 1 : out = g_str_to_ascii ("б", "sr_RS");
2209 : 1 : g_assert_cmpstr (out, ==, "b");
2210 : 1 : g_free (out);
2211 : :
2212 : 1 : out = g_str_to_ascii ("б", "sr_RS@latin");
2213 : 1 : g_assert_cmpstr (out, ==, "?");
2214 : 1 : g_free (out);
2215 : :
2216 : : /* Ukrainian contains the only multi-character mappings.
2217 : : * Try a string that contains one ('зг') along with a partial
2218 : : * sequence ('з') at the end.
2219 : : */
2220 : 1 : out = g_str_to_ascii ("Зліва направо, згори вниз", "uk");
2221 : 1 : g_assert_cmpstr (out, ==, "Zliva napravo, zghory vnyz");
2222 : 1 : g_free (out);
2223 : :
2224 : : /* Try out the other combinations */
2225 : 1 : out = g_str_to_ascii ("Зг", "uk");
2226 : 1 : g_assert_cmpstr (out, ==, "Zgh");
2227 : 1 : g_free (out);
2228 : :
2229 : 1 : out = g_str_to_ascii ("зГ", "uk");
2230 : 1 : g_assert_cmpstr (out, ==, "zGH");
2231 : 1 : g_free (out);
2232 : :
2233 : 1 : out = g_str_to_ascii ("ЗГ", "uk");
2234 : 1 : g_assert_cmpstr (out, ==, "ZGH");
2235 : 1 : g_free (out);
2236 : :
2237 : : /* And a non-combination */
2238 : 1 : out = g_str_to_ascii ("зя", "uk");
2239 : 1 : g_assert_cmpstr (out, ==, "zya");
2240 : 1 : g_free (out);
2241 : 1 : }
2242 : :
2243 : : static void
2244 : 1 : test_str_equal (void)
2245 : : {
2246 : 1 : const guchar *unsigned_a = (const guchar *) "a";
2247 : :
2248 : 1 : g_test_summary ("Test macro and function forms of g_str_equal()");
2249 : :
2250 : : /* Test function form. */
2251 : 1 : g_assert_true ((g_str_equal) ("a", "a"));
2252 : 1 : g_assert_false ((g_str_equal) ("a", "b"));
2253 : :
2254 : : /* Test macro form. */
2255 : : g_assert_true (g_str_equal ("a", "a"));
2256 : : g_assert_false (g_str_equal ("a", "b"));
2257 : :
2258 : : /* As g_str_equal() is defined for use with GHashTable, it takes gconstpointer
2259 : : * arguments, so can historically accept unsigned arguments. We need to
2260 : : * continue to support that. */
2261 : 1 : g_assert_true ((g_str_equal) (unsigned_a, "a"));
2262 : 1 : g_assert_false ((g_str_equal) (unsigned_a, "b"));
2263 : :
2264 : 1 : g_assert_true (g_str_equal (unsigned_a, "a"));
2265 : 1 : g_assert_false (g_str_equal (unsigned_a, "b"));
2266 : 1 : }
2267 : :
2268 : : /* Testing g_strv_contains() function with various cases */
2269 : : static void
2270 : 1 : test_strv_contains (void)
2271 : : {
2272 : 1 : gboolean result = TRUE;
2273 : 1 : const gchar *strv_simple[] = { "hello", "there", NULL };
2274 : 1 : const gchar *strv_dupe[] = { "dupe", "dupe", NULL };
2275 : 1 : const gchar *strv_empty[] = { NULL };
2276 : :
2277 [ + - ]: 1 : if (g_test_undefined ())
2278 : : {
2279 : : /* Testing degenerated cases */
2280 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2281 : : "*assertion*!= NULL*");
2282 : 1 : result = g_strv_contains (NULL, "hello");
2283 : 1 : g_test_assert_expected_messages ();
2284 : 1 : g_assert_false (result);
2285 : :
2286 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2287 : : "*assertion*!= NULL*");
2288 : 1 : result = g_strv_contains (strv_simple, NULL);
2289 : 1 : g_test_assert_expected_messages ();
2290 : 1 : g_assert_false (result);
2291 : : }
2292 : :
2293 : 1 : g_assert_true (g_strv_contains (strv_simple, "hello"));
2294 : 1 : g_assert_true (g_strv_contains (strv_simple, "there"));
2295 : 1 : g_assert_false (g_strv_contains (strv_simple, "non-existent"));
2296 : 1 : g_assert_false (g_strv_contains (strv_simple, ""));
2297 : :
2298 : 1 : g_assert_true (g_strv_contains (strv_dupe, "dupe"));
2299 : :
2300 : 1 : g_assert_false (g_strv_contains (strv_empty, "empty!"));
2301 : 1 : g_assert_false (g_strv_contains (strv_empty, ""));
2302 : 1 : }
2303 : :
2304 : : /* Test g_strv_equal() works for various inputs. */
2305 : : static void
2306 : 1 : test_strv_equal (void)
2307 : : {
2308 : 1 : gboolean result = TRUE;
2309 : 1 : const gchar *strv_empty[] = { NULL };
2310 : 1 : const gchar *strv_empty2[] = { NULL };
2311 : 1 : const gchar *strv_simple[] = { "hello", "you", NULL };
2312 : 1 : const gchar *strv_simple2[] = { "hello", "you", NULL };
2313 : 1 : const gchar *strv_simple_reordered[] = { "you", "hello", NULL };
2314 : 1 : const gchar *strv_simple_superset[] = { "hello", "you", "again", NULL };
2315 : 1 : const gchar *strv_another[] = { "not", "a", "coded", "message", NULL };
2316 : :
2317 [ + - ]: 1 : if (g_test_undefined ())
2318 : : {
2319 : : /* Testing degenerated cases */
2320 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2321 : : "*assertion*!= NULL*");
2322 : 1 : result = g_strv_equal (NULL, strv_simple2);
2323 : 1 : g_test_assert_expected_messages ();
2324 : 1 : g_assert_false (result);
2325 : :
2326 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2327 : : "*assertion*!= NULL*");
2328 : 1 : result = g_strv_equal (strv_simple, NULL);
2329 : 1 : g_test_assert_expected_messages ();
2330 : 1 : g_assert_false (result);
2331 : : }
2332 : :
2333 : 1 : g_assert_true (g_strv_equal (strv_empty, strv_empty));
2334 : 1 : g_assert_true (g_strv_equal (strv_empty, strv_empty2));
2335 : 1 : g_assert_true (g_strv_equal (strv_empty2, strv_empty));
2336 : 1 : g_assert_false (g_strv_equal (strv_empty, strv_simple));
2337 : 1 : g_assert_false (g_strv_equal (strv_simple, strv_empty));
2338 : 1 : g_assert_true (g_strv_equal (strv_simple, strv_simple));
2339 : 1 : g_assert_true (g_strv_equal (strv_simple, strv_simple2));
2340 : 1 : g_assert_true (g_strv_equal (strv_simple2, strv_simple));
2341 : 1 : g_assert_false (g_strv_equal (strv_simple, strv_simple_reordered));
2342 : 1 : g_assert_false (g_strv_equal (strv_simple_reordered, strv_simple));
2343 : 1 : g_assert_false (g_strv_equal (strv_simple, strv_simple_superset));
2344 : 1 : g_assert_false (g_strv_equal (strv_simple_superset, strv_simple));
2345 : 1 : g_assert_false (g_strv_equal (strv_simple, strv_another));
2346 : 1 : g_assert_false (g_strv_equal (strv_another, strv_simple));
2347 : 1 : }
2348 : :
2349 : : typedef enum
2350 : : {
2351 : : SIGNED,
2352 : : UNSIGNED
2353 : : } SignType;
2354 : :
2355 : : typedef struct
2356 : : {
2357 : : const gchar *str;
2358 : : SignType sign_type;
2359 : : guint base;
2360 : : gint min;
2361 : : gint max;
2362 : : gint expected;
2363 : : gboolean should_fail;
2364 : : GNumberParserError error_code;
2365 : : } TestData;
2366 : :
2367 : : const TestData test_data[] = {
2368 : : /* typical cases for signed */
2369 : : { "0", SIGNED, 10, -2, 2, 0, FALSE, 0 },
2370 : : { "+0", SIGNED, 10, -2, 2, 0, FALSE, 0 },
2371 : : { "-0", SIGNED, 10, -2, 2, 0, FALSE, 0 },
2372 : : { "-2", SIGNED, 10, -2, 2, -2, FALSE, 0 },
2373 : : {"-02", SIGNED, 10, -2, 2, -2, FALSE, 0 },
2374 : : { "2", SIGNED, 10, -2, 2, 2, FALSE, 0 },
2375 : : { "02", SIGNED, 10, -2, 2, 2, FALSE, 0 },
2376 : : { "+2", SIGNED, 10, -2, 2, 2, FALSE, 0 },
2377 : : {"+02", SIGNED, 10, -2, 2, 2, FALSE, 0 },
2378 : : { "3", SIGNED, 10, -2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS },
2379 : : { "+3", SIGNED, 10, -2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS },
2380 : : { "-3", SIGNED, 10, -2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS },
2381 : :
2382 : : /* typical cases for unsigned */
2383 : : { "-1", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2384 : : { "1", UNSIGNED, 10, 0, 2, 1, FALSE, 0 },
2385 : : { "+1", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2386 : : { "0", UNSIGNED, 10, 0, 2, 0, FALSE, 0 },
2387 : : { "+0", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2388 : : { "-0", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2389 : : { "2", UNSIGNED, 10, 0, 2, 2, FALSE, 0 },
2390 : : { "+2", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2391 : : { "3", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS },
2392 : : { "+3", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2393 : :
2394 : : /* min == max cases for signed */
2395 : : { "-2", SIGNED, 10, -2, -2, -2, FALSE, 0 },
2396 : : { "-1", SIGNED, 10, -2, -2, 0, TRUE, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS },
2397 : : { "-3", SIGNED, 10, -2, -2, 0, TRUE, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS },
2398 : :
2399 : : /* min == max cases for unsigned */
2400 : : { "2", UNSIGNED, 10, 2, 2, 2, FALSE, 0 },
2401 : : { "3", UNSIGNED, 10, 2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS },
2402 : : { "1", UNSIGNED, 10, 2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS },
2403 : :
2404 : : /* invalid inputs */
2405 : : { "", SIGNED, 10, -2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2406 : : { "", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2407 : : { "a", SIGNED, 10, -2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2408 : : { "a", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2409 : : { "1a", SIGNED, 10, -2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2410 : : { "1a", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2411 : : { "- 1", SIGNED, 10, -2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2412 : :
2413 : : /* leading/trailing whitespace */
2414 : : { " 1", SIGNED, 10, -2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2415 : : { " 1", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2416 : : { "1 ", SIGNED, 10, -2, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2417 : : { "1 ", UNSIGNED, 10, 0, 2, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2418 : :
2419 : : /* hexadecimal numbers */
2420 : : { "a", SIGNED, 16, 0, 15, 10, FALSE, 0 },
2421 : : { "a", UNSIGNED, 16, 0, 15, 10, FALSE, 0 },
2422 : : { "0a", UNSIGNED, 16, 0, 15, 10, FALSE, 0 },
2423 : : { "0xa", SIGNED, 16, 0, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2424 : : { "0xa", UNSIGNED, 16, 0, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2425 : : { "-0xa", SIGNED, 16, -15, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2426 : : { "-0xa", UNSIGNED, 16, 0, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2427 : : { "+0xa", SIGNED, 16, 0, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2428 : : { "+0xa", UNSIGNED, 16, 0, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2429 : : { "- 0xa", SIGNED, 16, -15, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2430 : : { "- 0xa", UNSIGNED, 16, 0, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2431 : : { "+ 0xa", SIGNED, 16, -15, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2432 : : { "+ 0xa", UNSIGNED, 16, 0, 15, 0, TRUE, G_NUMBER_PARSER_ERROR_INVALID },
2433 : : };
2434 : :
2435 : : /* Testing g_ascii_string_to_signed() and g_ascii_string_to_unsigned() functions */
2436 : : static void
2437 : 1 : test_ascii_string_to_number_usual (void)
2438 : : {
2439 : : gsize idx;
2440 : : gboolean result;
2441 : 1 : GError *error = NULL;
2442 : : const TestData *data;
2443 : : gint value;
2444 : 1 : gint64 value64 = 0;
2445 : 1 : guint64 valueu64 = 0;
2446 : :
2447 : : /*** g_ascii_string_to_signed() ***/
2448 : 1 : data = &test_data[0]; /* Setting data to signed data */
2449 : :
2450 [ + - ]: 1 : if (g_test_undefined ())
2451 : : {
2452 : : /* Testing degenerated cases */
2453 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2454 : : "*assertion*!= NULL*");
2455 : 1 : result = g_ascii_string_to_signed (NULL,
2456 : 1 : data->base,
2457 : 1 : data->min,
2458 : 1 : data->max,
2459 : : &value64,
2460 : : &error);
2461 : 1 : g_test_assert_expected_messages ();
2462 : 1 : g_assert_false (result);
2463 : :
2464 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2465 : : "*assertion \'base >= 2 && base <= 36\'*");
2466 : 1 : result = g_ascii_string_to_signed (data->str,
2467 : : 1,
2468 : 1 : data->min,
2469 : 1 : data->max,
2470 : : &value64,
2471 : : &error);
2472 : 1 : g_test_assert_expected_messages ();
2473 : 1 : g_assert_false (result);
2474 : :
2475 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2476 : : "*assertion \'base >= 2 && base <= 36\'*");
2477 : 1 : result = g_ascii_string_to_signed (data->str,
2478 : : 40,
2479 : 1 : data->min,
2480 : 1 : data->max,
2481 : : &value64,
2482 : : &error);
2483 : 1 : g_test_assert_expected_messages ();
2484 : 1 : g_assert_false (result);
2485 : :
2486 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2487 : : "*assertion \'min <= max\'*");
2488 : 1 : result = g_ascii_string_to_signed (data->str,
2489 : 1 : data->base,
2490 : 1 : data->max,
2491 : 1 : data->min,
2492 : : &value64,
2493 : : &error);
2494 : 1 : g_test_assert_expected_messages ();
2495 : 1 : g_assert_false (result);
2496 : : }
2497 : :
2498 : : /* Catching first part of (error == NULL || *error == NULL) */
2499 : 1 : result = g_ascii_string_to_signed (data->str,
2500 : 1 : data->base,
2501 : 1 : data->min,
2502 : 1 : data->max,
2503 : : &value64,
2504 : : NULL);
2505 : 1 : g_assert_true (result);
2506 : :
2507 : : /*** g_ascii_string_to_unsigned() ***/
2508 : 1 : data = &test_data[12]; /* Setting data to unsigned data */
2509 : :
2510 [ + - ]: 1 : if (g_test_undefined ())
2511 : : {
2512 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2513 : : "*assertion*!= NULL*");
2514 : 1 : result = g_ascii_string_to_unsigned (NULL,
2515 : 1 : data->base,
2516 : 1 : data->min,
2517 : 1 : data->max,
2518 : : &valueu64,
2519 : : &error);
2520 : 1 : g_test_assert_expected_messages ();
2521 : 1 : g_assert_false (result);
2522 : :
2523 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2524 : : "*assertion \'base >= 2 && base <= 36\'*");
2525 : 1 : result = g_ascii_string_to_unsigned (data->str,
2526 : : 1,
2527 : 1 : data->min,
2528 : 1 : data->max,
2529 : : &valueu64,
2530 : : &error);
2531 : 1 : g_test_assert_expected_messages ();
2532 : 1 : g_assert_false (result);
2533 : :
2534 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2535 : : "*assertion \'base >= 2 && base <= 36\'*");
2536 : 1 : result = g_ascii_string_to_unsigned (data->str,
2537 : : 40,
2538 : 1 : data->min,
2539 : 1 : data->max,
2540 : : &valueu64,
2541 : : &error);
2542 : 1 : g_test_assert_expected_messages ();
2543 : 1 : g_assert_false (result);
2544 : :
2545 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2546 : : "*assertion \'min <= max\'*");
2547 : 1 : result = g_ascii_string_to_unsigned (data->str,
2548 : 1 : data->base,
2549 : 1 : data->max,
2550 : 1 : data->min,
2551 : : &valueu64,
2552 : : &error);
2553 : 1 : g_test_assert_expected_messages ();
2554 : 1 : g_assert_false (result);
2555 : : }
2556 : :
2557 : : /* Catching first part of (error == NULL || *error == NULL) */
2558 : 1 : result = g_ascii_string_to_unsigned (data->str,
2559 : 1 : data->base,
2560 : 1 : data->min,
2561 : 1 : data->max,
2562 : : &valueu64,
2563 : : NULL);
2564 : 1 : g_assert_false (result);
2565 : :
2566 : : /* Testing usual cases */
2567 [ + + ]: 53 : for (idx = 0; idx < G_N_ELEMENTS (test_data); ++idx)
2568 : : {
2569 : 52 : data = &test_data[idx];
2570 : :
2571 [ + + - ]: 52 : switch (data->sign_type)
2572 : : {
2573 : 27 : case SIGNED:
2574 : : {
2575 : 27 : result = g_ascii_string_to_signed (data->str,
2576 : 27 : data->base,
2577 : 27 : data->min,
2578 : 27 : data->max,
2579 : : &value64,
2580 : : &error);
2581 : 27 : value = value64;
2582 : 27 : g_assert_cmpint (value, ==, value64);
2583 : 27 : break;
2584 : : }
2585 : :
2586 : 25 : case UNSIGNED:
2587 : : {
2588 : 25 : result = g_ascii_string_to_unsigned (data->str,
2589 : 25 : data->base,
2590 : 25 : data->min,
2591 : 25 : data->max,
2592 : : &valueu64,
2593 : : &error);
2594 : 25 : value = valueu64;
2595 : 25 : g_assert_cmpint (value, ==, valueu64);
2596 : 25 : break;
2597 : : }
2598 : :
2599 : 0 : default:
2600 : : g_assert_not_reached ();
2601 : : }
2602 : :
2603 [ + + ]: 52 : if (data->should_fail)
2604 : : {
2605 : 35 : g_assert_false (result);
2606 : 35 : g_assert_error (error, G_NUMBER_PARSER_ERROR, (gint) data->error_code);
2607 : 35 : g_clear_error (&error);
2608 : : }
2609 : : else
2610 : : {
2611 : 17 : g_assert_true (result);
2612 : 17 : g_assert_no_error (error);
2613 : 17 : g_assert_cmpint (value, ==, data->expected);
2614 : : }
2615 : : }
2616 : 1 : }
2617 : :
2618 : : /* Testing pathological cases for g_ascii_string_to_(un)signed() */
2619 : : static void
2620 : 1 : test_ascii_string_to_number_pathological (void)
2621 : : {
2622 : 1 : GError *error = NULL;
2623 : 1 : const gchar *crazy_high = "999999999999999999999999999999999999";
2624 : 1 : const gchar *crazy_low = "-999999999999999999999999999999999999";
2625 : 1 : const gchar *max_uint64 = "18446744073709551615";
2626 : 1 : const gchar *max_int64 = "9223372036854775807";
2627 : 1 : const gchar *min_int64 = "-9223372036854775808";
2628 : 1 : guint64 uvalue = 0;
2629 : 1 : gint64 svalue = 0;
2630 : :
2631 : 1 : g_assert_false (g_ascii_string_to_unsigned (crazy_high,
2632 : : 10,
2633 : : 0,
2634 : : G_MAXUINT64,
2635 : : NULL,
2636 : : &error));
2637 : 1 : g_assert_error (error, G_NUMBER_PARSER_ERROR, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS);
2638 : 1 : g_clear_error (&error);
2639 : 1 : g_assert_false (g_ascii_string_to_unsigned (crazy_low,
2640 : : 10,
2641 : : 0,
2642 : : G_MAXUINT64,
2643 : : NULL,
2644 : : &error));
2645 : : // crazy_low is a signed number so it is not a valid unsigned number
2646 : 1 : g_assert_error (error, G_NUMBER_PARSER_ERROR, G_NUMBER_PARSER_ERROR_INVALID);
2647 : 1 : g_clear_error (&error);
2648 : :
2649 : 1 : g_assert_false (g_ascii_string_to_signed (crazy_high,
2650 : : 10,
2651 : : G_MININT64,
2652 : : G_MAXINT64,
2653 : : NULL,
2654 : : &error));
2655 : 1 : g_assert_error (error, G_NUMBER_PARSER_ERROR, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS);
2656 : 1 : g_clear_error (&error);
2657 : 1 : g_assert_false (g_ascii_string_to_signed (crazy_low,
2658 : : 10,
2659 : : G_MININT64,
2660 : : G_MAXINT64,
2661 : : NULL,
2662 : : &error));
2663 : 1 : g_assert_error (error, G_NUMBER_PARSER_ERROR, G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS);
2664 : 1 : g_clear_error (&error);
2665 : :
2666 : 1 : g_assert_true (g_ascii_string_to_unsigned (max_uint64,
2667 : : 10,
2668 : : 0,
2669 : : G_MAXUINT64,
2670 : : &uvalue,
2671 : : &error));
2672 : 1 : g_assert_no_error (error);
2673 : 1 : g_assert_cmpint (uvalue, ==, G_MAXUINT64);
2674 : :
2675 : 1 : g_assert_true (g_ascii_string_to_signed (max_int64,
2676 : : 10,
2677 : : G_MININT64,
2678 : : G_MAXINT64,
2679 : : &svalue,
2680 : : &error));
2681 : 1 : g_assert_no_error (error);
2682 : 1 : g_assert_cmpint (svalue, ==, G_MAXINT64);
2683 : :
2684 : 1 : g_assert_true (g_ascii_string_to_signed (min_int64,
2685 : : 10,
2686 : : G_MININT64,
2687 : : G_MAXINT64,
2688 : : &svalue,
2689 : : &error));
2690 : 1 : g_assert_no_error (error);
2691 : 1 : g_assert_cmpint (svalue, ==, G_MININT64);
2692 : 1 : }
2693 : :
2694 : : static void
2695 : 1 : test_set_str (void)
2696 : : {
2697 : 1 : char *str = NULL;
2698 : 1 : const char *empty_str = "";
2699 : :
2700 : 1 : g_assert_false (g_set_str (&str, NULL));
2701 : 1 : g_assert_null (str);
2702 : :
2703 : 1 : g_assert_true (g_set_str (&str, empty_str));
2704 : 1 : g_assert_false (g_set_str (&str, empty_str));
2705 : 1 : g_assert_nonnull (str);
2706 : 1 : g_assert_true ((gpointer)str != (gpointer)empty_str);
2707 : 1 : g_assert_cmpstr (str, ==, empty_str);
2708 : :
2709 : 1 : g_assert_true (g_set_str (&str, NULL));
2710 : 1 : g_assert_null (str);
2711 : :
2712 : 1 : g_assert_true (g_set_str (&str, empty_str));
2713 : 1 : g_assert_true (g_set_str (&str, "test"));
2714 : 1 : g_assert_cmpstr (str, ==, "test");
2715 : :
2716 : 1 : g_assert_true (g_set_str (&str, &str[2]));
2717 : 1 : g_assert_cmpstr (str, ==, "st");
2718 : :
2719 : 1 : g_free (str);
2720 : 1 : }
2721 : :
2722 : : int
2723 : 1 : main (int argc,
2724 : : char *argv[])
2725 : : {
2726 : 1 : g_test_init (&argc, &argv, NULL);
2727 : :
2728 : 1 : g_test_add_func ("/strfuncs/ascii-strcasecmp", test_ascii_strcasecmp);
2729 : 1 : g_test_add_func ("/strfuncs/ascii-string-to-num/pathological", test_ascii_string_to_number_pathological);
2730 : 1 : g_test_add_func ("/strfuncs/ascii-string-to-num/usual", test_ascii_string_to_number_usual);
2731 : 1 : g_test_add_func ("/strfuncs/ascii_strdown", test_ascii_strdown);
2732 : 1 : g_test_add_func ("/strfuncs/ascii_strdup", test_ascii_strup);
2733 : 1 : g_test_add_func ("/strfuncs/ascii_strtod", test_ascii_strtod);
2734 : 1 : g_test_add_func ("/strfuncs/bounds-check", test_bounds);
2735 : 1 : g_test_add_func ("/strfuncs/has-prefix", test_has_prefix);
2736 : 1 : g_test_add_func ("/strfuncs/has-prefix-macro", test_has_prefix_macro);
2737 : 1 : g_test_add_func ("/strfuncs/has-suffix", test_has_suffix);
2738 : 1 : g_test_add_func ("/strfuncs/has-suffix-macro", test_has_suffix_macro);
2739 : 1 : g_test_add_func ("/strfuncs/memdup", test_memdup);
2740 : 1 : g_test_add_func ("/strfuncs/memdup2", test_memdup2);
2741 : 1 : g_test_add_func ("/strfuncs/set_str", test_set_str);
2742 : 1 : g_test_add_func ("/strfuncs/stpcpy", test_stpcpy);
2743 : 1 : g_test_add_func ("/strfuncs/str_match_string", test_str_match_string);
2744 : 1 : g_test_add_func ("/strfuncs/str_tokenize_and_fold", test_str_tokenize_and_fold);
2745 : 1 : g_test_add_func ("/strfuncs/strcanon", test_strcanon);
2746 : 1 : g_test_add_func ("/strfuncs/strchomp", test_strchomp);
2747 : 1 : g_test_add_func ("/strfuncs/strchug", test_strchug);
2748 : 1 : g_test_add_func ("/strfuncs/strcompress-strescape", test_strcompress_strescape);
2749 : 1 : g_test_add_func ("/strfuncs/strconcat", test_strconcat);
2750 : 1 : g_test_add_func ("/strfuncs/strdelimit", test_strdelimit);
2751 : 1 : g_test_add_func ("/strfuncs/strdup", test_strdup);
2752 : 1 : g_test_add_func ("/strfuncs/strdup/inline", test_strdup_inline);
2753 : 1 : g_test_add_func ("/strfuncs/strdup-printf", test_strdup_printf);
2754 : 1 : g_test_add_func ("/strfuncs/strdupv", test_strdupv);
2755 : 1 : g_test_add_func ("/strfuncs/strerror", test_strerror);
2756 : 1 : g_test_add_func ("/strfuncs/strip-context", test_strip_context);
2757 : 1 : g_test_add_func ("/strfuncs/strjoin", test_strjoin);
2758 : 1 : g_test_add_func ("/strfuncs/strjoinv", test_strjoinv);
2759 : 1 : g_test_add_func ("/strfuncs/strlcat", test_strlcat);
2760 : 1 : g_test_add_func ("/strfuncs/strlcpy", test_strlcpy);
2761 : 1 : g_test_add_func ("/strfuncs/strncasecmp", test_strncasecmp);
2762 : 1 : g_test_add_func ("/strfuncs/strndup", test_strndup);
2763 : 1 : g_test_add_func ("/strfuncs/strnfill", test_strnfill);
2764 : 1 : g_test_add_func ("/strfuncs/strreverse", test_strreverse);
2765 : 1 : g_test_add_func ("/strfuncs/strsignal", test_strsignal);
2766 : 1 : g_test_add_func ("/strfuncs/strsplit", test_strsplit);
2767 : 1 : g_test_add_func ("/strfuncs/strsplit-set", test_strsplit_set);
2768 : 1 : g_test_add_func ("/strfuncs/strstr", test_strstr);
2769 : 1 : g_test_add_func ("/strfuncs/strtod", test_strtod);
2770 : 1 : g_test_add_func ("/strfuncs/strtoull-strtoll", test_strtoll);
2771 : 1 : g_test_add_func ("/strfuncs/strup", test_strup);
2772 : 1 : g_test_add_func ("/strfuncs/strv-contains", test_strv_contains);
2773 : 1 : g_test_add_func ("/strfuncs/strv-equal", test_strv_equal);
2774 : 1 : g_test_add_func ("/strfuncs/strv-length", test_strv_length);
2775 : 1 : g_test_add_func ("/strfuncs/test-is-to-digit", test_is_to_digit);
2776 : 1 : g_test_add_func ("/strfuncs/transliteration", test_transliteration);
2777 : 1 : g_test_add_func ("/strfuncs/str-equal", test_str_equal);
2778 : :
2779 : 1 : return g_test_run();
2780 : : }
|