[profiler] Split method_leave callback into a method_tail_call callback.
[mono.git] / eglib / test / test.c
index 8400cde0fa0b1281c6bcee9a8628bc575b898f8a..7c870e9c7665a3a074cf4763ca3ed33271b90716 100644 (file)
  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 
+#ifndef _GNU_SOURCE
 #define _GNU_SOURCE
+#endif
+
+#include <config.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
 #include <stdarg.h>
-#include <sys/time.h>
 #include <glib.h>
+#ifdef HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif
+#ifdef G_OS_WIN32
+#include <winsock2.h>
+#endif
 
 #include "test.h"
 
+extern gint global_passed, global_tests;
 static gchar *last_result = NULL;
 
 gboolean 
@@ -52,12 +63,14 @@ run_test(Test *test, gchar **result_out)
 }
 
 gboolean
-run_group(Group *group, gint iterations, gboolean quiet, gboolean time)
+run_group(Group *group, gint iterations, gboolean quiet, 
+       gboolean time, gchar *tests_to_run_s)
 {
        Test *tests = group->handler();
-       gint i, j, passed = 0;
+       gint i, j, passed = 0, total = 0;
        gdouble start_time_group, start_time_test;
-       
+       gchar **tests_to_run = NULL;
+
        if(!quiet) {
                if(iterations > 1) {
                        printf("[%s] (%dx)\n", group->name, iterations);
@@ -66,12 +79,36 @@ run_group(Group *group, gint iterations, gboolean quiet, gboolean time)
                }
        }
 
+       if(tests_to_run_s != NULL) {
+               tests_to_run = eg_strsplit(tests_to_run_s, ",", -1);
+       }
+
        start_time_group = get_timestamp();
 
        for(i = 0; tests[i].name != NULL; i++) {
-               gchar *result;
-               gboolean iter_pass;
-               
+               gchar *result = "";
+               gboolean iter_pass, run;
+       
+               iter_pass = FALSE;
+               if(tests_to_run != NULL) {
+                       gint j;
+                       run = FALSE;
+                       for(j = 0; tests_to_run[j] != NULL; j++) {
+                               if(strcmp(tests_to_run[j], tests[i].name) == 0) {
+                                       run = TRUE;
+                                       break;
+                               }
+                       }
+               } else {
+                       run = TRUE;
+               }
+
+               if(!run) {
+                       continue;
+               }
+       
+               total++;
+       
                if(!quiet) {
                        printf("  %s: ", tests[i].name);
                }
@@ -106,17 +143,24 @@ run_group(Group *group, gint iterations, gboolean quiet, gboolean time)
                }
        }
 
+       global_passed += passed;
+       global_tests += total;
+
        if(!quiet) {
-               gdouble pass_percentage = ((gdouble)passed / (gdouble)i) * 100.0;
+               gdouble pass_percentage = ((gdouble)passed / (gdouble)total) * 100.0;
                if(time) {
-                       printf("  %d / %d (%g%%, %g)\n", passed, i,
+                       printf("  %d / %d (%g%%, %g)\n", passed, total,
                                pass_percentage, get_timestamp() - start_time_group);
                } else {
-                       printf("  %d / %d (%g%%)\n", passed, i, pass_percentage);
+                       printf("  %d / %d (%g%%)\n", passed, total, pass_percentage);
                }
        }
 
-       return passed == i;
+       if(tests_to_run != NULL) {
+               eg_strfreev(tests_to_run);
+       }
+
+       return passed == total;
 }
 
 RESULT
@@ -126,8 +170,13 @@ FAILED(const gchar *format, ...)
        va_list args;
        gint n;
 
+#if !defined(HAVE_VASPRINTF) && !defined(_EGLIB_MAJOR)
+       /* We are linked against the real glib, no vasprintf */
+       g_assert_not_reached ();
+       return NULL;
+#else
        va_start(args, format);
-       n = vasprintf(&ret, format, args);
+       n = g_vasprintf(&ret, format, args);
        va_end(args);
 
        if(n == -1) {
@@ -137,13 +186,90 @@ FAILED(const gchar *format, ...)
 
        last_result = ret;
        return ret;
+#endif
 }
 
 gdouble
 get_timestamp()
 {
-       struct timeval tp;
-       gettimeofday(&tp, NULL);
-       return (gdouble)tp.tv_sec + (1.e-6) * tp.tv_usec;
+       /* FIXME: We should use g_get_current_time here */
+       GTimeVal res;
+       g_get_current_time (&res);
+       return res.tv_sec + (1.e-6) * res.tv_usec;
 }
 
+/* 
+ * Duplicating code here from EGlib to avoid g_strsplit skew between
+ * EGLib and GLib
+ */
+gchar ** 
+eg_strsplit (const gchar *string, const gchar *delimiter, gint max_tokens)
+{
+       gchar *string_c;
+       gchar *strtok_save, **vector;
+       gchar *token, *token_c;
+       gint size = 1;
+       size_t token_length;
+
+       g_return_val_if_fail(string != NULL, NULL);
+       g_return_val_if_fail(delimiter != NULL, NULL);
+       g_return_val_if_fail(delimiter[0] != 0, NULL);
+       
+       token_length = strlen(string);
+       string_c = (gchar *)g_malloc(token_length + 1);
+       memcpy(string_c, string, token_length);
+       string_c[token_length] = 0;
+       
+       vector = NULL;
+       token = (gchar *)strtok_r(string_c, delimiter, &strtok_save);
+
+       while(token != NULL) {
+               token_length = strlen(token);
+               token_c = (gchar *)g_malloc(token_length + 1);
+               memcpy(token_c, token, token_length);
+               token_c[token_length] = 0;
+
+               vector = vector == NULL ? 
+                       (gchar **)g_malloc(2 * sizeof(vector)) :
+                       (gchar **)g_realloc(vector, (size + 1) * sizeof(vector));
+       
+               vector[size - 1] = token_c;     
+               size++;
+
+               if(max_tokens > 0 && size >= max_tokens) {
+                       if(size > max_tokens) {
+                               break;
+                       }
+
+                       token = strtok_save;
+               } else {
+                       token = (gchar *)strtok_r(NULL, delimiter, &strtok_save);
+               }
+       }
+
+       if(vector != NULL && size > 0) {
+               vector[size - 1] = NULL;
+       }
+       
+       g_free(string_c);
+       string_c = NULL;
+
+       return vector;
+}
+
+void
+eg_strfreev (gchar **str_array)
+{
+       gchar **orig = str_array;
+       if (str_array == NULL)
+               return;
+       while (*str_array != NULL){
+               g_free (*str_array);
+               str_array++;
+       }
+       g_free (orig);
+}
+
+
+