Merge branch 'g-clear-pointer-no-side-effects' into 'master'
[glib.git] / glib / tests / refstring.c
blob4d58eecb955b9e1099b75af7514a332e632f96b7
1 /* refstring.c: Reference counted strings
3 * Copyright 2018 Emmanuele Bassi
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include <glib.h>
20 #include <string.h>
22 /* test_refstring_base: Test the base API of GRefString */
23 static void
24 test_refstring_base (void)
26 char *s = g_ref_string_new ("hello, world");
28 g_test_message ("s = '%s' (%p)", s, s);
29 g_assert_cmpint (strcmp (s, "hello, world"), ==, 0);
30 g_assert_cmpint (strlen (s), ==, strlen ("hello, world"));
31 g_assert_cmpuint (g_ref_string_length (s), ==, strlen ("hello, world"));
33 g_assert_true (g_ref_string_acquire (s) == s);
34 g_ref_string_release (s);
36 g_ref_string_release (s);
39 /* test_refstring_length: Test the _len variant */
40 static void
41 test_refstring_length (void)
43 char buf[] = {'h', 'e', 'l', 'l', 'o'}; /* no NUL */
44 char *s = g_ref_string_new_len (buf, 5);
46 g_assert_cmpstr (s, ==, "hello");
47 g_assert_cmpint (strlen (s), ==, strlen ("hello"));
48 g_assert_cmpuint (g_ref_string_length (s), ==, strlen ("hello"));
49 g_ref_string_release (s);
52 /* test_refstring_length: Test the _len variant with no size set */
53 static void
54 test_refstring_length_auto (void)
56 char *s = g_ref_string_new_len ("hello", -1);
57 g_assert_cmpstr (s, ==, "hello");
58 g_assert_cmpuint (g_ref_string_length (s), ==, strlen ("hello"));
59 g_ref_string_release (s);
62 /* test_refstring_length_nuls: Test the _len variant */
63 static void
64 test_refstring_length_nuls (void)
66 char buf[] = {'h', 'e', '\0', 'l', 'o'}; /* no NUL */
67 char *s = g_ref_string_new_len (buf, 5);
69 g_assert_cmpstr (s, ==, "he");
70 g_assert_cmpint (memcmp (s, "he\0lo", 5), ==, 0);
71 g_assert_cmpuint (g_ref_string_length (s), ==, 5);
72 g_ref_string_release (s);
75 /* test_refstring_intern: Test the interning API of GRefString */
76 static void
77 test_refstring_intern (void)
79 char *s = g_ref_string_new_intern ("hello, world");
80 char *p;
82 g_test_message ("s = '%s' (%p)", s, s);
83 g_assert_cmpstr (s, ==, "hello, world");
85 p = g_ref_string_new_intern ("hello, world");
86 g_test_message ("p = s = '%s' (%p)", p, p);
87 g_assert_true (s == p);
89 g_test_message ("releasing p[%p] ('%s')", p, p);
90 g_ref_string_release (p);
92 p = g_ref_string_new_intern ("goodbye, world");
93 g_test_message ("p = '%s' (%p)", p, p);
94 g_assert_false (s == p);
96 g_test_message ("releasing p[%p] ('%s')", p, p);
97 g_ref_string_release (p);
99 g_test_message ("releasing s[%p] ('%s')", s, s);
100 g_ref_string_release (s);
104 main (int argc,
105 char *argv[])
107 g_test_init (&argc, &argv, NULL);
109 g_test_add_func ("/refstring/base", test_refstring_base);
110 g_test_add_func ("/refstring/length", test_refstring_length);
111 g_test_add_func ("/refstring/length-auto", test_refstring_length_auto);
112 g_test_add_func ("/refstring/length-nuls", test_refstring_length_nuls);
113 g_test_add_func ("/refstring/intern", test_refstring_intern);
115 return g_test_run ();