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