فهرست منبع

2006-08-25 Gonzalo Paniagua Javier <[email protected]>

	* test/tests.h:
	* test/timer.c:
	* test/Makefile.am:
	* TODO:
	* src/glib.h:
	* src/gtimer.c:
	* src/Makefile.am: implemented GTimer.


svn path=/trunk/mono/; revision=64346
Gonzalo Paniagua Javier 19 سال پیش
والد
کامیت
018401f033
8فایلهای تغییر یافته به همراه172 افزوده شده و 11 حذف شده
  1. 11 1
      eglib/ChangeLog
  2. 0 7
      eglib/TODO
  3. 2 1
      eglib/src/Makefile.am
  4. 15 1
      eglib/src/glib.h
  5. 95 0
      eglib/src/gtimer.c
  6. 2 1
      eglib/test/Makefile.am
  7. 2 0
      eglib/test/tests.h
  8. 45 0
      eglib/test/timer.c

+ 11 - 1
eglib/ChangeLog

@@ -1,3 +1,13 @@
+2006-08-25 Gonzalo Paniagua Javier <[email protected]>
+
+	* test/tests.h:
+	* test/timer.c:
+	* test/Makefile.am:
+	* TODO:
+	* src/glib.h:
+	* src/gtimer.c:
+	* src/Makefile.am: implemented GTimer.
+
 2006-08-25 Gonzalo Paniagua Javier <[email protected]>
 
 	* test/tests.h:
@@ -5,7 +15,7 @@
 	* test/spawn.c:
 	* src/gspawn.c:
 	* src/glib.h:
-	* src/Makefile.am: initial implemtation of g_spawn_command_line_sync.
+	* src/Makefile.am: initial implentation of g_spawn_command_line_sync.
 	Still need better error handling, but works for the most part.
 
 2006-08-25 Gonzalo Paniagua Javier <[email protected]>

+ 0 - 7
eglib/TODO

@@ -32,13 +32,6 @@ Important Groups:
 	      2 g_markup_parse_context_free
 	      2 g_markup_parse_context_end_parse
 
-	* Timers, used for the Mini built-in test suite
-	      5 g_timer_elapsed
-	      2 g_timer_stop
-	      2 g_timer_start
-	      2 g_timer_new
-	      1 g_timer_destroy
-		     
 	* Process launching
 	      1 g_spawn_async_with_pipes
 	      3 g_shell_quote

+ 2 - 1
eglib/src/Makefile.am

@@ -18,7 +18,8 @@ libeglib_la_SOURCES = \
 	gqueue.c	\
 	gpath.c		\
 	gshell.c	\
-	gspawn.c
+	gspawn.c	\
+	gtimer.c
 
 libeglib_la_CFLAGS = -Wall -Werror -D_FORTIFY_SOURCE=2
 

+ 15 - 1
eglib/src/glib.h

@@ -442,7 +442,6 @@ gchar  *g_get_current_dir      (void);
 const gchar *g_get_home_dir    (void);
 const gchar *g_get_tmp_dir     (void);
 const gchar *g_get_user_name   (void);
-#endif
 
 /*
  * Shell
@@ -455,3 +454,18 @@ gboolean g_shell_parse_argv (const gchar *command_line, gint *argcp, gchar ***ar
  */
 
 gboolean g_spawn_command_line_sync (const gchar *command_line, gchar **standard_output, gchar **standard_error, gint *exit_status, GError **error);
+
+
+/*
+ * Timer
+ */
+typedef struct _GTimer GTimer;
+
+GTimer *g_timer_new (void);
+void g_timer_destroy (GTimer *timer);
+gdouble g_timer_elapsed (GTimer *timer, gulong *microseconds);
+void g_timer_stop (GTimer *timer);
+void g_timer_start (GTimer *timer);
+
+#endif
+

+ 95 - 0
eglib/src/gtimer.c

@@ -0,0 +1,95 @@
+/*
+ * Timer
+ *
+ * Author:
+ *   Gonzalo Paniagua Javier ([email protected]
+ *
+ * (C) 2006 Novell, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <glib.h>
+#include <sys/time.h>
+
+struct _GTimer {
+	struct timeval start;
+	struct timeval stop;
+};
+
+GTimer *g_timer_new (void)
+{
+	GTimer *timer;
+
+	timer = g_new0 (GTimer, 1);
+	g_timer_start (timer);
+	return timer;
+}
+
+void
+g_timer_destroy (GTimer *timer)
+{
+	g_return_if_fail (timer != NULL);
+	g_free (timer);
+}
+
+void
+g_timer_start (GTimer *timer)
+{
+	g_return_if_fail (timer != NULL);
+	gettimeofday (&timer->start, NULL);
+	memset (&timer->stop, 0, sizeof (struct timeval));
+}
+
+void
+g_timer_stop (GTimer *timer)
+{
+	g_return_if_fail (timer != NULL);
+	gettimeofday (&timer->stop, NULL);
+}
+
+gdouble
+g_timer_elapsed (GTimer *timer, gulong *microseconds)
+{
+	struct timeval tv;
+	gulong seconds;
+	gulong usec;
+	gdouble result;
+
+	g_return_val_if_fail (timer != NULL, 0.0);
+
+	if (timer->stop.tv_sec == 0 && timer->stop.tv_usec == 0) {
+		gettimeofday (&tv, NULL);
+	} else {
+		tv = timer->stop;
+	}
+
+	usec = tv.tv_usec - timer->start.tv_usec;
+	seconds = tv.tv_sec - timer->start.tv_sec;
+	if (microseconds) {
+		if (usec < 0) {
+			usec += 1000000;
+			seconds--;
+		}
+		*microseconds = usec;
+	}
+	result = seconds * 1000000 + usec;
+	return (result / 1000000);
+}
+

+ 2 - 1
eglib/test/Makefile.am

@@ -17,7 +17,8 @@ SOURCES = \
 	path.c		\
 	queue.c		\
 	shell.c		\
-	spawn.c
+	spawn.c		\
+	timer.c
 
 test_eglib_SOURCES = $(SOURCES)
 test_glib_SOURCES = $(SOURCES)

+ 2 - 0
eglib/test/tests.h

@@ -13,6 +13,7 @@ DEFINE_TEST_GROUP_INIT_H(queue_tests_init);
 DEFINE_TEST_GROUP_INIT_H(path_tests_init);
 DEFINE_TEST_GROUP_INIT_H(shell_tests_init);
 DEFINE_TEST_GROUP_INIT_H(spawn_tests_init);
+DEFINE_TEST_GROUP_INIT_H(timer_tests_init);
 
 static Group test_groups [] = {	
 	{"string",    string_tests_init}, 
@@ -28,6 +29,7 @@ static Group test_groups [] = {
 	{"path",      path_tests_init},
 	{"shell",     shell_tests_init},
 	{"spawn",     spawn_tests_init},
+	{"timer",     timer_tests_init},
 	{NULL, NULL}
 };
 

+ 45 - 0
eglib/test/timer.c

@@ -0,0 +1,45 @@
+#include <glib.h>
+#include <string.h>
+#include <math.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include "test.h"
+
+RESULT
+test_timer ()
+{
+	GTimer *timer;
+	gdouble elapsed1, elapsed2;
+	gulong usec = 0;
+
+	timer = g_timer_new ();
+	sleep (1);
+	elapsed1 = g_timer_elapsed (timer, NULL);
+	if ((elapsed1 + 0.1) < 1.0)
+		return FAILED ("Elapsed time should be around 1s and was %f", elapsed1);
+
+	g_timer_stop (timer);
+	elapsed1 = g_timer_elapsed (timer, NULL);
+	elapsed2 = g_timer_elapsed (timer, &usec);
+	if (elapsed1 != elapsed2)
+		return FAILED ("The elapsed times are not equal %f - %f.", elapsed1, elapsed2);
+
+	elapsed2 *= 1000000;
+	while (elapsed2 > 1000000)
+		elapsed2 -= 1000000;
+
+	if (fabs (usec - elapsed2) > 100.0)
+		return FAILED ("usecs are wrong.");
+
+	g_timer_destroy (timer);
+	return OK;
+}
+
+static Test timer_tests [] = {
+	{"g_timer", test_timer},
+	{NULL, NULL}
+};
+
+DEFINE_TEST_GROUP_INIT(timer_tests_init, timer_tests)
+