1/* refstring.c: Reference counted strings
2 *
3 * Copyright 2018 Emmanuele Bassi
4 *
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.
9 *
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.
14 *
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/>.
17 */
18
19#include <glib.h>
20#include <string.h>
21
22/* test_refstring_base: Test the base API of GRefString */
23static void
24test_refstring_base (void)
25{
26 char *s = g_ref_string_new (str: "hello, world");
27
28 g_test_message (format: "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"));
32
33 g_assert_true (g_ref_string_acquire (s) == s);
34 g_ref_string_release (str: s);
35
36 g_ref_string_release (str: s);
37}
38
39/* test_refstring_length: Test the _len variant */
40static void
41test_refstring_length (void)
42{
43 char buf[] = {'h', 'e', 'l', 'l', 'o'}; /* no NUL */
44 char *s = g_ref_string_new_len (str: buf, len: 5);
45
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 (str: s);
50}
51
52/* test_refstring_length: Test the _len variant with no size set */
53static void
54test_refstring_length_auto (void)
55{
56 char *s = g_ref_string_new_len (str: "hello", len: -1);
57 g_assert_cmpstr (s, ==, "hello");
58 g_assert_cmpuint (g_ref_string_length (s), ==, strlen ("hello"));
59 g_ref_string_release (str: s);
60}
61
62/* test_refstring_length_nuls: Test the _len variant */
63static void
64test_refstring_length_nuls (void)
65{
66 char buf[] = {'h', 'e', '\0', 'l', 'o'}; /* no NUL */
67 char *s = g_ref_string_new_len (str: buf, len: 5);
68
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 (str: s);
73}
74
75/* test_refstring_intern: Test the interning API of GRefString */
76static void
77test_refstring_intern (void)
78{
79 char *s = g_ref_string_new_intern (str: "hello, world");
80 char *p;
81
82 g_test_message (format: "s = '%s' (%p)", s, s);
83 g_assert_cmpstr (s, ==, "hello, world");
84
85 p = g_ref_string_new_intern (str: "hello, world");
86 g_test_message (format: "p = s = '%s' (%p)", p, p);
87 g_assert_true (s == p);
88
89 g_test_message (format: "releasing p[%p] ('%s')", p, p);
90 g_ref_string_release (str: p);
91
92 p = g_ref_string_new_intern (str: "goodbye, world");
93 g_test_message (format: "p = '%s' (%p)", p, p);
94 g_assert_false (s == p);
95
96 g_test_message (format: "releasing p[%p] ('%s')", p, p);
97 g_ref_string_release (str: p);
98
99 g_test_message (format: "releasing s[%p] ('%s')", s, s);
100 g_ref_string_release (str: s);
101}
102
103int
104main (int argc,
105 char *argv[])
106{
107 g_test_init (argc: &argc, argv: &argv, NULL);
108
109 g_test_add_func (testpath: "/refstring/base", test_func: test_refstring_base);
110 g_test_add_func (testpath: "/refstring/length", test_func: test_refstring_length);
111 g_test_add_func (testpath: "/refstring/length-auto", test_func: test_refstring_length_auto);
112 g_test_add_func (testpath: "/refstring/length-nuls", test_func: test_refstring_length_nuls);
113 g_test_add_func (testpath: "/refstring/intern", test_func: test_refstring_intern);
114
115 return g_test_run ();
116}
117

source code of gtk/subprojects/glib/glib/tests/refstring.c