summaryrefslogtreecommitdiff
path: root/glib/tests
diff options
context:
space:
mode:
authorMatthias Clasen <mclasen@redhat.com>2023-01-17 17:56:41 -0500
committerMatthias Clasen <mclasen@redhat.com>2023-01-17 20:56:08 -0500
commit1e38e6f53f0c0549d3d75ba76899f53b7b28f2ea (patch)
treeddc27248e3944fe4af718e2ef4e909afe7d7aee2 /glib/tests
parent1aa530410990efbf23a108a51b62afd2d4ed9b20 (diff)
downloadglib-g-string-on-stack.tar.gz
GString: Support on-stack useg-string-on-stack
Move the preallocation into the GString struct, and add g_string_init and g_string_clear to enable on-stack use of GString.
Diffstat (limited to 'glib/tests')
-rw-r--r--glib/tests/string.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/glib/tests/string.c b/glib/tests/string.c
index 23f44015f..3136f690b 100644
--- a/glib/tests/string.c
+++ b/glib/tests/string.c
@@ -629,6 +629,44 @@ test_string_replace (void)
}
}
+static void
+test_string_on_stack (void)
+{
+ GString string;
+ char *s;
+
+ g_string_init (&string);
+
+ g_assert_cmpstr (string.str, ==, "");
+ g_assert_cmpint (string.len, ==, 0);
+ g_assert_true (string.str == string.buf);
+
+ g_string_append_printf (&string, "Three %s", "cheese");
+ g_assert_cmpstr (string.str, ==, "Three cheese");
+ g_string_append (&string, " and a big banana");
+ g_assert_cmpstr (string.str, ==, "Three cheese and a big banana");
+
+ g_string_assign (&string, "On a hot summer night, would you offer your throat to the wolf with red roses?");
+ g_assert_cmpstr (string.str, ==, "On a hot summer night, would you offer your throat to the wolf with red roses?");
+
+ g_string_clear (&string, TRUE);
+
+ g_assert_cmpstr (string.str, ==, "");
+ g_assert_cmpint (string.len, ==, 0);
+ g_assert_true (string.str == string.buf);
+
+ g_string_append_printf (&string, "Three %s", "cheese");
+ g_assert_cmpstr (string.str, ==, "Three cheese");
+ g_string_append (&string, " and a big banana");
+ g_assert_cmpstr (string.str, ==, "Three cheese and a big banana");
+
+ s = (g_string_clear) (&string, FALSE);
+
+ g_assert_cmpstr (s, ==, "Three cheese and a big banana");
+
+ g_free (s);
+}
+
int
main (int argc,
char *argv[])
@@ -655,6 +693,7 @@ main (int argc,
g_test_add_func ("/string/test-string-set-size", test_string_set_size);
g_test_add_func ("/string/test-string-to-bytes", test_string_to_bytes);
g_test_add_func ("/string/test-string-replace", test_string_replace);
+ g_test_add_func ("/string/test-string-on-stack", test_string_on_stack);
return g_test_run();
}