Home | History | Annotate | Download | only in backup
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include <utils/BackupHelpers.h>
     18 
     19 #include <stdio.h>
     20 #include <string.h>
     21 
     22 using namespace android;
     23 
     24 #if TEST_BACKUP_HELPERS
     25 
     26 // ============================================================
     27 // ============================================================
     28 typedef int (*test_func)();
     29 
     30 struct Test {
     31     const char* name;
     32     test_func func;
     33     int result;
     34     bool run;
     35 };
     36 
     37 Test TESTS[] = {
     38     { "backup_helper_test_empty", backup_helper_test_empty, 0, false },
     39     { "backup_helper_test_four", backup_helper_test_four, 0, false },
     40     { "backup_helper_test_files", backup_helper_test_files, 0, false },
     41     { "backup_helper_test_null_base", backup_helper_test_null_base, 0, false },
     42     { "backup_helper_test_missing_file", backup_helper_test_missing_file, 0, false },
     43     { "backup_helper_test_data_writer", backup_helper_test_data_writer, 0, false },
     44     { "backup_helper_test_data_reader", backup_helper_test_data_reader, 0, false },
     45     { 0, NULL, 0, false}
     46 };
     47 
     48 int
     49 main(int argc, const char** argv)
     50 {
     51     Test* t;
     52 
     53     if (argc == 1) {
     54         t = TESTS;
     55         while (t->name) {
     56             t->run = true;
     57             t++;
     58         }
     59     } else {
     60         t = TESTS;
     61         while (t->name) {
     62             for (int i=1; i<argc; i++) {
     63                 if (0 == strcmp(t->name, argv[i])) {
     64                     t->run = true;
     65                 }
     66             }
     67             t++;
     68         }
     69     }
     70 
     71     int testCount = 0;
     72     t = TESTS;
     73     while (t->name) {
     74         if (t->run) {
     75             testCount++;
     76         }
     77         t++;
     78     }
     79 
     80 
     81     int failed = 0;
     82     int i = 1;
     83     t = TESTS;
     84     while (t->name) {
     85         if (t->run) {
     86             printf("===== Running %s (%d of %d) ==============================\n",
     87                     t->name, i, testCount);
     88             fflush(stdout);
     89             fflush(stderr);
     90             t->result = t->func();
     91             if (t->result != 0) {
     92                 failed++;
     93                 printf("failed\n");
     94             } else {
     95                 printf("passed\n");
     96             }
     97             i++;
     98         }
     99         t++;
    100     }
    101 
    102     printf("=================================================================\n");
    103     if (failed == 0) {
    104         printf("All %d test(s) passed\n", testCount);
    105     } else {
    106         printf("Tests failed: (%d of %d)\n", failed, testCount);
    107         t = TESTS;
    108         while (t->name) {
    109             if (t->run) {
    110                 if (t->result != 0) {
    111                     printf("  %s\n", t->name);
    112                 }
    113             }
    114             t++;
    115         }
    116     }
    117 }
    118 
    119 #else
    120 int
    121 main(int argc, char** argv)
    122 {
    123     printf ("test_backup_helper built without the tests\n");
    124     return 0;
    125 }
    126 #endif
    127