backup_helper_test.cpp revision c825d3ebd6ca66e65e63fdc76f032e08aa2a8e22
1#include <utils/backup_helpers.h>
2
3#include <stdio.h>
4#include <string.h>
5
6#if TEST_BACKUP_HELPERS
7
8// ============================================================
9// ============================================================
10typedef int (*test_func)();
11
12struct Test {
13    const char* name;
14    test_func func;
15    int result;
16    bool run;
17};
18
19Test TESTS[] = {
20    { "backup_helper_test_empty", backup_helper_test_empty, 0, false },
21    { "backup_helper_test_four", backup_helper_test_four, 0, false },
22    { "backup_helper_test_files", backup_helper_test_files, 0, false },
23    { 0, NULL, 0, false}
24};
25
26int
27main(int argc, const char** argv)
28{
29    Test* t;
30
31    if (argc == 1) {
32        t = TESTS;
33        while (t->name) {
34            t->run = true;
35            t++;
36        }
37    } else {
38        t = TESTS;
39        while (t->name) {
40            for (int i=1; i<argc; i++) {
41                if (0 == strcmp(t->name, argv[i])) {
42                    t->run = true;
43                }
44            }
45            t++;
46        }
47    }
48
49    int testCount = 0;
50    t = TESTS;
51    while (t->name) {
52        if (t->run) {
53            testCount++;
54        }
55        t++;
56    }
57
58
59    int failed = 0;
60    int i = 1;
61    t = TESTS;
62    while (t->name) {
63        if (t->run) {
64            printf("===== Running %s (%d of %d) ==============================\n",
65                    t->name, i, testCount);
66            fflush(stdout);
67            fflush(stderr);
68            t->result = t->func();
69            if (t->result != 0) {
70                failed++;
71                printf("failed\n");
72            } else {
73                printf("passed\n");
74            }
75            i++;
76        }
77        t++;
78    }
79
80    printf("=================================================================\n");
81    if (failed == 0) {
82        printf("All %d test(s) passed\n", testCount);
83    } else {
84        printf("Tests failed: (%d of %d)\n", failed, testCount);
85        t = TESTS;
86        while (t->name) {
87            if (t->run) {
88                if (t->result != 0) {
89                    printf("  %s\n", t->name);
90                }
91            }
92            t++;
93        }
94    }
95}
96
97#else
98int
99main(int argc, char** argv)
100{
101    printf ("test_backup_helper built without the tests\n");
102    return 0;
103}
104#endif
105