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