[msvc] Update csproj files (#4136)
[mono.git] / eglib / test / driver.c
index dea1cd21d3a3882355c64a6c8573ce2b69c48b2e..806e8b66d3f2387f45078ae70af458bf19fca5a1 100644 (file)
  * 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 <stdio.h>
-#include <glib.h>
-#include <stddef.h>
-#include <sys/time.h>
-#include <getopt.h>
 
+#include <config.h>
 #include "test.h"
+
+#ifndef DRIVER_EXTERNAL_TESTS
 #include "tests.h"
+#endif
+
+#include <stdio.h>
+#ifdef HAVE_GETOPT_H
+#include <getopt.h>
+#endif
+
+#if defined(HAVE_UNISTD_H)
+#include <unistd.h>
+#endif
+
+#ifndef DRIVER_NAME
+#define DRIVER_NAME "EGlib"
+#endif
+
+typedef struct _StringArray {
+       gchar **strings;
+       gint length;
+} StringArray;
+
+static StringArray *
+string_array_append(StringArray *array, gchar *string)
+{
+       if(array == NULL) {
+               array = g_new0(StringArray, 1);
+               array->length = 1;
+               array->strings = g_malloc(sizeof(gchar *) * 2);
+       } else {
+               array->length++;
+               array->strings = g_realloc(array->strings, sizeof(gchar *) 
+                       * (array->length + 1));
+       }
+       
+       array->strings[array->length - 1] = string;
+       array->strings[array->length] = NULL;
+
+       return array;
+}
+
+gint global_passed = 0, global_tests = 0;
+
+static void
+string_array_free(StringArray *array)
+{
+       g_free(array->strings);
+       g_free(array);
+}
 
 static void print_help(char *s)
 {
        gint i;
        
-       printf("Usage: %s [options] [test1 test2 ... testN]\n\n", s);
-       printf(" options are:\n");
-       printf("   --help|-h          show this help\n");
-       printf("   --time|-t          time the tests\n");
-       printf("   --iterations|-i    number of times to run tests\n");
-       printf("   --quiet|-q         do not print test results; if -t\n");
-       printf("                      is passed, the time will print\n\n");
-       printf(" test1..testN  name of test to run (all run by default)\n\n");
-       printf(" available tests:\n");
+       printf("Usage: %s [OPTION]... [TESTGROUP]...\n\n", s);
+       printf("OPTIONS are:\n");
+       printf("  -h, --help          show this help\n");
+       printf("  -t, --time          time the tests\n");
+       printf("  -i, --iterations    number of times to run tests\n");
+       printf("  -q, --quiet         do not print test results; "
+               "final time always prints\n");
+       printf("  -n, --no-labels     print final time without labels, "
+               "nice for scripts\n");
+       printf("  -d, --debug         do not run tests, "
+               "debug the driver itself for valgrind\n\n");
+       printf("TESTGROUPS available:\n");
 
        for(i = 0; test_groups[i].name != NULL; i++) {
-               printf("   %s\n", test_groups[i].name);
+               if(test_groups[i].handler != fake_tests_init) {
+                       printf("  %s\n", test_groups[i].name);
+               }
        }
 
        printf("\n");
 }
 
+#ifdef DRIVER_EXTERNAL_MAIN
+gint run_tests_main(gint argc, gchar **argv)
+#else
 gint main(gint argc, gchar **argv)
+#endif
 {
        gint i, j, c, iterations = 1;
-       GList *tests_to_run = NULL;
-       double time_start, time_end;
-       struct timeval tp;
+       StringArray *tests_to_run = NULL;
+       gdouble time_start;
        gboolean report_time = FALSE;
        gboolean quiet = FALSE;
-       
+       gboolean global_failure = FALSE;
+       gboolean no_final_time_labels = FALSE;
+       gboolean debug = FALSE;
+
+#if HAVE_GETOPT_H
        static struct option long_options [] = {
-               {"help", no_argument, 0, 'h'},
-               {"time", no_argument, 0, 't'},
+               {"help",       no_argument,       0, 'h'},
+               {"time",       no_argument,       0, 't'},
+               {"quiet",      no_argument,       0, 'q'},
                {"iterations", required_argument, 0, 'i'},
+               {"debug",      no_argument,       0, 'd'},
+               {"no-labels",  no_argument,       0, 'n'},
                {0, 0, 0, 0}
        };
 
-       while((c = getopt_long(argc, argv, "htqi:", long_options, NULL)) != -1) {                       switch(c) {
+       while((c = getopt_long(argc, argv, "dhtqni:", long_options, NULL)) != -1) {                     switch(c) {
                        case 'h':
                                print_help(argv[0]);
                                return 1;
@@ -85,6 +144,12 @@ gint main(gint argc, gchar **argv)
                        case 'q':
                                quiet = TRUE;
                                break;
+                       case 'n':
+                               no_final_time_labels = TRUE;
+                               break;
+                       case 'd':
+                               debug = TRUE;
+                               break;
                }
        }
 
@@ -93,51 +158,98 @@ gint main(gint argc, gchar **argv)
                        continue;
                }
 
-               tests_to_run = g_list_append(tests_to_run, argv[i]);
+               tests_to_run = string_array_append(tests_to_run, argv[i]);
        }
+#endif
 
-       gettimeofday(&tp, NULL);
-       time_start = (double)tp.tv_sec + (1.e-6) * tp.tv_usec;
-
-       for(i = 0; i < iterations; i++) {
-               for(j = 0; test_groups[j].name != NULL; j++) {
-                       gboolean run = TRUE;
+       time_start = get_timestamp();
+       
+       for(j = 0; test_groups[j].name != NULL; j++) {
+               gboolean run = TRUE;
+               gchar *tests = NULL;
+               gchar *group = NULL;
+               
+               if(tests_to_run != NULL) {
+                       gint k;
+                       run = FALSE;
                        
-                       if(tests_to_run != NULL) {
-                               gint k, n;
-                               run = FALSE;
-                               for(k = 0, n = g_list_length(tests_to_run); k < n; k++) {
-                                       if(strcmp((char *)g_list_nth_data(tests_to_run, k), 
-                                               test_groups[j].name) == 0) {
-                                               run = TRUE;
+                       for(k = 0; k < tests_to_run->length; k++) {     
+                               gchar *user = tests_to_run->strings[k];
+                               const gchar *table = test_groups[j].name;
+                               size_t user_len = strlen(user);
+                               size_t table_len = strlen(table);
+                               
+                               if(strncmp(user, table, table_len) == 0) {
+                                       if(user_len > table_len && user[table_len] != ':') {
                                                break;
                                        }
+                                       
+                                       run = TRUE;
+                                       group = tests_to_run->strings[k];
+                                       break;
                                }
                        }
+               }
+       
+               if(run) {
+                       gboolean passed;
+                       gchar **split = NULL;
                        
-                       if(run) {
-                               gint total, passed;
-                               run_group(&(test_groups[j]), &total, &passed, quiet);
-                               if(!quiet) {
-                                       printf("  -- %d / %d (%g%%) --\n", passed, total,
-                                               ((gdouble)passed / (gdouble)total) * 100.0);
+                       if(debug && test_groups[j].handler != fake_tests_init) {
+                               printf("Skipping %s, in driver debug mode\n", 
+                                       test_groups[j].name);
+                               continue;
+                       } else if(!debug && test_groups[j].handler == fake_tests_init) {
+                               continue;
+                       }
+
+                       if(group != NULL) {
+                               split = eg_strsplit(group, ":", -1);    
+                               if(split != NULL) {
+                                       gint m;
+                                       for(m = 0; split[m] != NULL; m++) {
+                                               if(m == 1) {
+                                                       tests = strdup(split[m]);
+                                                       break;
+                                               }
+                                       }
+                                       eg_strfreev(split);
                                }
                        }
+                       
+                       passed = run_group(&(test_groups[j]), 
+                               iterations, quiet, report_time, tests);
+
+                       if(tests != NULL) {
+                               g_free(tests);
+                       }
+
+                       if(!passed && !global_failure) {
+                               global_failure = TRUE;
+                       }
                }
        }
        
-       gettimeofday(&tp, NULL);
-       time_end = (double)tp.tv_sec + (1.e-6) * tp.tv_usec;
+       if(!quiet) {
+               gdouble pass_percentage = ((gdouble)global_passed / (gdouble)global_tests) * 100.0;
+               printf("=============================\n");
+               printf("Overall result: %s : %d / %d (%g%%)\n", global_failure ? "FAILED" : "OK", global_passed, global_tests, pass_percentage);
+       }
        
        if(report_time) {
-               gdouble duration = time_end - time_start;
-               printf("Total Time: %gs\n", duration);
+               gdouble duration = get_timestamp() - time_start;
+               if(no_final_time_labels) {
+                       printf("%g\n", duration);
+               } else {
+                       printf("%s Total Time: %g\n", DRIVER_NAME, duration);
+               }
        }
 
        if(tests_to_run != NULL) {
-               g_list_free(tests_to_run);
+               string_array_free(tests_to_run);
        }
 
-       return 0;
+       return global_tests - global_passed;
 }
 
+