[glib/fix-gnulib-msvc-isnan: 19/37] test: Add string GValue tests
- From: Chun-wei Fan <fanchunwei src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/fix-gnulib-msvc-isnan: 19/37] test: Add string GValue tests
- Date: Tue, 9 Jun 2020 10:22:45 +0000 (UTC)
commit bc6da64996e865e8aaa8807893738dab60c70083
Author: Edward Hervey <edward centricular com>
Date: Fri May 15 17:25:09 2020 +0200
test: Add string GValue tests
Tests creation, duplication, ownership and copies
gobject/tests/value.c | 135 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 135 insertions(+)
---
diff --git a/gobject/tests/value.c b/gobject/tests/value.c
index b5a136486..1b2446c88 100644
--- a/gobject/tests/value.c
+++ b/gobject/tests/value.c
@@ -31,6 +31,140 @@ test_value_basic (void)
g_assert_false (G_VALUE_HOLDS_INT (&value));
}
+static void
+test_value_string (void)
+{
+ const gchar *static1 = "static1";
+ const gchar *static2 = "static2";
+ const gchar *storedstr;
+ const gchar *copystr;
+ gchar *str1, *str2;
+ GValue value = G_VALUE_INIT;
+ GValue copy = G_VALUE_INIT;
+
+ g_test_summary ("Test that G_TYPE_STRING GValue copy properly");
+
+ /*
+ * Regular strings (ownership not passed)
+ */
+
+ /* Create a regular string gvalue and make sure it copies the provided string */
+ g_value_init (&value, G_TYPE_STRING);
+ g_assert_true (G_VALUE_HOLDS_STRING (&value));
+
+ /* The string contents should be empty at this point */
+ storedstr = g_value_get_string (&value);
+ g_assert_true (storedstr == NULL);
+
+ g_value_set_string (&value, static1);
+ /* The contents should be a copy of the same string */
+ storedstr = g_value_get_string (&value);
+ g_assert_true (storedstr != static1);
+ g_assert_cmpstr (storedstr, ==, static1);
+ /* Check g_value_dup_string() provides a copy */
+ str1 = g_value_dup_string (&value);
+ g_assert_true (storedstr != str1);
+ g_assert_cmpstr (str1, ==, static1);
+ g_free (str1);
+
+ /* Copying a regular string gvalue should copy the contents */
+ g_value_init (©, G_TYPE_STRING);
+ g_value_copy (&value, ©);
+ copystr = g_value_get_string (©);
+ g_assert_true (copystr != storedstr);
+ g_assert_cmpstr (copystr, ==, static1);
+ g_value_unset (©);
+
+ /* Setting a new string should change the contents */
+ g_value_set_string (&value, static2);
+ /* The contents should be a copy of that *new* string */
+ storedstr = g_value_get_string (&value);
+ g_assert_true (storedstr != static2);
+ g_assert_cmpstr (storedstr, ==, static2);
+
+ /* Setting a static string over that should also change it (test for
+ * coverage and valgrind) */
+ g_value_set_static_string (&value, static1);
+ storedstr = g_value_get_string (&value);
+ g_assert_true (storedstr != static2);
+ g_assert_cmpstr (storedstr, ==, static1);
+
+ /* Giving a string directly (ownership passed) should replace the content */
+ str2 = g_strdup (static2);
+ g_value_take_string (&value, str2);
+ storedstr = g_value_get_string (&value);
+ g_assert_true (storedstr != static2);
+ g_assert_cmpstr (storedstr, ==, str2);
+
+ g_value_unset (&value);
+
+ /*
+ * Regular strings (ownership passed)
+ */
+
+ g_value_init (&value, G_TYPE_STRING);
+ g_assert_true (G_VALUE_HOLDS_STRING (&value));
+ str1 = g_strdup (static1);
+ g_value_take_string (&value, str1);
+ /* The contents should be the string we provided */
+ storedstr = g_value_get_string (&value);
+ g_assert_true (storedstr == str1);
+ /* But g_value_dup_string() should provide a copy */
+ str2 = g_value_dup_string (&value);
+ g_assert_true (storedstr != str2);
+ g_assert_cmpstr (str2, ==, static1);
+ g_free (str2);
+
+ /* Copying a regular string gvalue (even with ownership passed) should copy
+ * the contents */
+ g_value_init (©, G_TYPE_STRING);
+ g_value_copy (&value, ©);
+ copystr = g_value_get_string (©);
+ g_assert_true (copystr != storedstr);
+ g_assert_cmpstr (copystr, ==, static1);
+ g_value_unset (©);
+
+ /* Setting a new regular string should change the contents */
+ g_value_set_string (&value, static2);
+ /* The contents should be a copy of that *new* string */
+ storedstr = g_value_get_string (&value);
+ g_assert_true (storedstr != static2);
+ g_assert_cmpstr (storedstr, ==, static2);
+
+ g_value_unset (&value);
+
+ /*
+ * Static strings
+ */
+ g_value_init (&value, G_TYPE_STRING);
+ g_assert_true (G_VALUE_HOLDS_STRING (&value));
+ g_value_set_static_string (&value, static1);
+ /* The contents should be the string we provided */
+ storedstr = g_value_get_string (&value);
+ g_assert_true (storedstr == static1);
+ /* But g_value_dup_string() should provide a copy */
+ str2 = g_value_dup_string (&value);
+ g_assert_true (storedstr != str2);
+ g_assert_cmpstr (str2, ==, static1);
+ g_free (str2);
+
+ /* Copying a static string gvalue should *not* copy the contents */
+ g_value_init (©, G_TYPE_STRING);
+ g_value_copy (&value, ©);
+ copystr = g_value_get_string (©);
+ g_assert_true (copystr == static1);
+ g_value_unset (©);
+
+ /* Setting a new string should change the contents */
+ g_value_set_static_string (&value, static2);
+ /* The contents should be a copy of that *new* string */
+ storedstr = g_value_get_string (&value);
+ g_assert_true (storedstr != static1);
+ g_assert_cmpstr (storedstr, ==, static2);
+
+ g_value_unset (&value);
+}
+
static gint
cmpint (gconstpointer a, gconstpointer b)
{
@@ -89,6 +223,7 @@ main (int argc, char *argv[])
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/value/basic", test_value_basic);
+ g_test_add_func ("/value/string", test_value_string);
g_test_add_func ("/value/array/basic", test_valuearray_basic);
return g_test_run ();
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]