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