[libgweather/benzea/wip-variant-backend: 2/5] test: Add tree walk test




commit c0e291f2269e8fb98a42302ba29700c9bf81da6b
Author: Benjamin Berg <bberg redhat com>
Date:   Mon Apr 27 11:12:55 2020 +0200

    test: Add tree walk test

 libgweather/test_libgweather.c | 54 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
---
diff --git a/libgweather/test_libgweather.c b/libgweather/test_libgweather.c
index 3f95032c..883d5183 100644
--- a/libgweather/test_libgweather.c
+++ b/libgweather/test_libgweather.c
@@ -26,6 +26,7 @@
 #include <gweather-version.h>
 #include "gweather-location.h"
 #include "gweather-weather.h"
+#include "gweather-private.h"
 
 extern void _gweather_location_reset_world (void);
 
@@ -792,6 +793,58 @@ test_weather_loop_use_after_free (void)
     g_main_loop_unref (loop);
 }
 
+static void
+test_walk_world (void)
+{
+    g_autoptr(GWeatherLocation) cur = NULL, next = NULL;
+    gint visited = 0;
+
+    next = gweather_location_get_world ();
+    while (next) {
+       /* Update cur pointer. */
+       g_clear_pointer (&cur, gweather_location_unref);
+       cur = g_steal_pointer (&next);
+       visited += 1;
+       g_assert_cmpint (cur->ref_count, ==, 1);
+
+       /* Select next item, which is in this order:
+        *  1. The first child
+        *  2. Walk up the parent tree and try to find a sibbling
+        * Note that cur remains valid after the loop and points to the world
+        * again.
+        */
+       if ((next = gweather_location_next_child (cur, NULL)))
+           continue;
+
+       while (TRUE) {
+           g_autoptr(GWeatherLocation) child = NULL;
+           /* Move cur to the parent, keeping the child as reference. */
+           child = g_steal_pointer (&cur);
+           cur = gweather_location_get_parent (child);
+           if (!cur)
+               break;
+           g_assert_cmpint (cur->ref_count, ==, 1);
+           g_assert_cmpint (child->ref_count, ==, 1);
+
+           if ((next = gweather_location_next_child (cur, gweather_location_ref (child))))
+               break;
+       }
+    }
+
+    /* cur must be NULL at this point */
+    g_assert_null (cur);
+
+    /* Check that we visited a reasonable number of nodes.
+     * Due to implicit nearest nodes, this needs to be more than the number
+     * of DB entries. */
+    cur = gweather_location_get_world ();
+    g_assert_cmpint (visited, >, cur->db->locations->len);
+    g_clear_pointer (&cur, gweather_location_unref);
+
+    /* noop, but asserts we did not leak */
+    _gweather_location_reset_world ();
+}
+
 static void
 log_handler (const char *log_domain, GLogLevelFlags log_level, const char *message, gpointer user_data)
 {
@@ -826,6 +879,7 @@ main (int argc, char *argv[])
        g_test_add_func ("/weather/bad_duplicate_weather_stations", test_bad_duplicate_weather_stations);
        g_test_add_func ("/weather/duplicate_weather_stations", test_duplicate_weather_stations);
        g_test_add_func ("/weather/location-names", test_location_names);
+       g_test_add_func ("/weather/walk_world", test_walk_world);
 
        return g_test_run ();
 }


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