Implemented g_test_timer*().
authorTim Janik <timj@src.gnome.org>
Tue, 20 Nov 2007 15:00:30 +0000 (15:00 +0000)
committerTim Janik <timj@src.gnome.org>
Tue, 20 Nov 2007 15:00:30 +0000 (15:00 +0000)
* gtestframework.c: implemented g_test_timer*().

* tests/testing.c: added a g_test_timer*() test.

svn path=/trunk/; revision=5884

glib/gtestframework.c
glib/tests/testing.c

index 96b23df..c7df2a2 100644 (file)
@@ -63,6 +63,9 @@ static gchar      *test_run_output = NULL;
 static gchar      *test_run_seedstr = NULL;
 static GRand      *test_run_rand = NULL;
 static gchar      *test_run_name = "";
+static GTimer     *test_run_timer = NULL;
+static GTimer     *test_user_timer = NULL;
+static double      test_user_stamp = 0;
 static GSList     *test_paths = NULL;
 static GTestSuite *test_suite_root = NULL;
 static GSList     *test_run_free_queue = NULL;
@@ -218,6 +221,9 @@ g_test_init (int            *argc,
   test_run_seed (test_run_seedstr);
   if (test_run_seedstr == seedstr)
     g_printerr ("NOTE: random-seed: %s\n", test_run_seedstr);
+
+  /* misc setups */
+  test_run_timer = g_timer_new();
 }
 
 static void
@@ -284,6 +290,28 @@ g_test_rand_double_range (double          range_start,
   return g_rand_double_range (test_run_rand, range_start, range_end);
 }
 
+void
+g_test_timer_start (void)
+{
+  if (!test_user_timer)
+    test_user_timer = g_timer_new();
+  test_user_stamp = 0;
+  g_timer_start (test_user_timer);
+}
+
+double
+g_test_timer_elapsed (void)
+{
+  test_user_stamp = test_user_timer ? g_timer_elapsed (test_user_timer, NULL) : 0;
+  return test_user_stamp;
+}
+
+double
+g_test_timer_last (void)
+{
+  return test_user_stamp;
+}
+
 GTestSuite*
 g_test_get_root (void)
 {
@@ -411,7 +439,9 @@ g_test_queue_free (gpointer gfree_pointer)
 static int
 test_case_run (GTestCase *tc)
 {
-  gchar *old_name = test_run_name;
+  gchar *old_name;
+  g_timer_start (test_run_timer);
+  old_name = test_run_name;
   test_run_name = g_strconcat (old_name, "/", tc->name, NULL);
   if (test_run_list)
     g_print ("%s\n", test_run_name);
@@ -439,6 +469,8 @@ test_case_run (GTestCase *tc)
     }
   g_free (test_run_name);
   test_run_name = old_name;
+  g_timer_stop (test_run_timer);
+  /* FIXME: need reporting here */
   return 0;
 }
 
index a3e6a1f..bc221b0 100644 (file)
@@ -43,6 +43,19 @@ test_assertions (void)
   g_assert_cmpstr ("fzz", ==, "fzz");
 }
 
+/* test g_test_timer* API */
+static void
+test_timer (void)
+{
+  double ttime;
+  g_test_timer_start();
+  g_assert_cmpfloat (g_test_timer_last(), ==, 0);
+  g_usleep (25 * 1000);
+  ttime = g_test_timer_elapsed();
+  g_assert_cmpfloat (ttime, >, 0);
+  g_assert_cmpfloat (g_test_timer_last(), ==, ttime);
+}
+
 /* fork out for a failing test */
 static void
 test_fork_fail (void)
@@ -158,6 +171,7 @@ main (int   argc,
   g_test_add_func ("/random-generator/rand-2", test_rand2);
   g_test_add_func ("/misc/assertions", test_assertions);
   g_test_add ("/misc/primetoul", Fixturetest, fixturetest_setup, fixturetest_test, fixturetest_teardown);
+  g_test_add_func ("/misc/timer", test_timer);
   g_test_add_func ("/forking/fail assertion", test_fork_fail);
   g_test_add_func ("/forking/patterns", test_fork_patterns);
   g_test_add_func ("/forking/timeout", test_fork_timeout);