libqaeda

Unnamed repository; edit this file 'description' to name the repository.
Info | Log | Files | Refs | README | LICENSE

main.c (2729B)


      1 #include "tests.h"
      2 #include <memory.h>
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 
      7 /**
      8  * This is just a small macro which calculates the size of an array.
      9  */
     10 #define CWK_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
     11 
     12 struct cwk_test
     13 {
     14   const char *unit_name;
     15   const char *test_name;
     16   const char *full_name;
     17   int (*fn)(void);
     18 };
     19 
     20 #define XX(u, t) extern int u##_##t(void);
     21 UNIT_TESTS(XX)
     22 #undef XX
     23 
     24 static struct cwk_test tests[] = {
     25 #define XX(u, t)                                                               \
     26   {.unit_name = #u, .test_name = #t, .full_name = #u "/" #t, .fn = u##_##t},
     27   UNIT_TESTS(XX)
     28 #undef XX
     29 };
     30 
     31 static int call_test(struct cwk_test *test)
     32 {
     33   size_t i;
     34 
     35   printf(" Running '%s' ", test->full_name);
     36   for (i = strlen(test->full_name); i < 45; ++i) {
     37     fputs(".", stdout);
     38   }
     39 
     40   if (test->fn() == EXIT_FAILURE) {
     41     fputs(" FAILURE\n", stdout);
     42     return EXIT_FAILURE;
     43   }
     44 
     45   fputs(" SUCCESS\n", stdout);
     46   return EXIT_SUCCESS;
     47 }
     48 
     49 int main(int argc, char *argv[])
     50 {
     51   size_t i, count, failed, succeeded;
     52   const char *requested_unit_name, *requested_test_name;
     53   struct cwk_test *test;
     54   double rate;
     55 
     56   count = 0;
     57   failed = 0;
     58   if (argc < 2) {
     59     fputs("No unit specified. Running all tests.\n\n", stdout);
     60     for (i = 0; i < CWK_ARRAY_SIZE(tests); ++i) {
     61       test = &tests[i];
     62       ++count;
     63       if (call_test(test) == EXIT_FAILURE) {
     64         ++failed;
     65       }
     66     }
     67   } else if (argc < 3) {
     68     requested_unit_name = argv[1];
     69     printf("Running all unit tests of '%s'.\n\n", requested_unit_name);
     70     for (i = 0; i < CWK_ARRAY_SIZE(tests); ++i) {
     71       test = &tests[i];
     72       if (strcmp(test->unit_name, requested_unit_name) == 0) {
     73         ++count;
     74         if (call_test(test) == EXIT_FAILURE) {
     75           ++failed;
     76         }
     77       }
     78     }
     79   } else {
     80     requested_unit_name = argv[1];
     81     requested_test_name = argv[2];
     82     printf("Running a single test '%s/%s'.\n\n", requested_unit_name,
     83       requested_test_name);
     84     for (i = 0; i < CWK_ARRAY_SIZE(tests); ++i) {
     85       test = &tests[i];
     86       if (strcmp(test->unit_name, requested_unit_name) == 0 &&
     87           strcmp(test->test_name, requested_test_name) == 0) {
     88         ++count;
     89         if (call_test(test) == EXIT_FAILURE) {
     90           ++failed;
     91         }
     92       }
     93     }
     94   }
     95 
     96   if (count == 1) {
     97     fputs("\nThe test has been executed.\n", stdout);
     98   } else if (count > 0) {
     99     succeeded = count - failed;
    100     rate = (double)succeeded / (double)count * 100;
    101     printf("\n%zu/%zu (%.2f%%) of those tests succeeded.\n", succeeded, count,
    102       rate);
    103   } else {
    104     printf("\nNo tests found.\n");
    105     return EXIT_FAILURE;
    106   }
    107 
    108   if (failed > 0) {
    109     return EXIT_FAILURE;
    110   }
    111 
    112   return EXIT_SUCCESS;
    113 }