[glib/wip/refptr: 4/5] tests: Add units for the g_ref_* API



commit f2cd87afa337591311cac815caa75874d3806289
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Tue Jan 27 14:51:58 2015 +0000

    tests: Add units for the g_ref_* API
    
    https://bugzilla.gnome.org/show_bug.cgi?id=622721

 glib/tests/.gitignore  |    1 +
 glib/tests/Makefile.am |    1 +
 glib/tests/refcounts.c |   78 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 80 insertions(+), 0 deletions(-)
---
diff --git a/glib/tests/.gitignore b/glib/tests/.gitignore
index 33ef15e..b40541c 100644
--- a/glib/tests/.gitignore
+++ b/glib/tests/.gitignore
@@ -52,6 +52,7 @@ protocol
 queue
 rand
 rec-mutex
+refcounts
 regex
 rwlock
 scannerapi
diff --git a/glib/tests/Makefile.am b/glib/tests/Makefile.am
index 445040a..0b50734 100644
--- a/glib/tests/Makefile.am
+++ b/glib/tests/Makefile.am
@@ -76,6 +76,7 @@ test_programs = \
        queue                           \
        rand                            \
        rec-mutex                       \
+       refcounts                       \
        regex                           \
        rwlock                          \
        scannerapi                      \
diff --git a/glib/tests/refcounts.c b/glib/tests/refcounts.c
new file mode 100644
index 0000000..faf1117
--- /dev/null
+++ b/glib/tests/refcounts.c
@@ -0,0 +1,78 @@
+#include <glib.h>
+#include <string.h>
+
+typedef struct {
+  int x, y;
+  int width, height;
+} Rect;
+
+static gboolean check_free_did_run;
+
+static void
+check_rect_free (gpointer data)
+{
+  Rect *r = data;
+
+  g_assert_false (check_free_did_run);
+  g_assert_nonnull (r);
+
+  check_free_did_run = TRUE;
+}
+
+static void
+refs_generic (void)
+{
+  Rect *r = g_ref_pointer_new0 (Rect, check_rect_free);
+
+  check_free_did_run = FALSE;
+
+  g_assert_cmpint (r->x, ==, 0);
+  g_assert_cmpint (r->height, ==, 0);
+
+  g_ref_pointer_acquire (r);
+  r->y = 100;
+  g_assert_cmpint (r->y, ==, 100);
+  g_ref_pointer_release (r);
+
+  g_assert_false (check_free_did_run);
+  g_assert_cmpint (r->y, ==, 100);
+
+  g_ref_pointer_release (r);
+  g_assert_true (check_free_did_run);
+}
+
+static void
+refs_strings (void)
+{
+  char *orig = g_strdup ("hello");
+  const char *new = g_string_ref_new (orig);
+
+  g_assert_false (orig == new);
+  g_assert_cmpint (strlen (new), ==, strlen (orig));
+  g_assert_true (strcmp (orig, new) == 0);
+
+  memset (orig, 'a', strlen (orig)); 
+  g_assert_false (strcmp (orig, new) == 0);
+  g_assert_true (strcmp (new, "hello") == 0);
+
+  g_free (orig);
+
+  orig = g_strdup (new);
+  g_assert_false (orig == new);
+  g_assert_cmpint (strlen (new), ==, strlen (orig));
+  g_assert_true (strcmp (orig, new) == 0);
+
+  g_string_unref (new);
+  g_free (orig);
+}
+
+int
+main (int argc, char *argv[])
+{
+  g_test_init (&argc, &argv, NULL);
+
+  g_test_add_func ("/refs/generic", refs_generic);
+  g_test_add_func ("/refs/strings", refs_strings);
+
+  return g_test_run ();
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]