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