- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Utils/Testing/main.cpp

    00001
    00002 //
    00003 // Copyright (c) 2004           Model-based Embedded and Robotic Systems Group
    00004 // All Rights Reserved          Massachusetts Institute of Technology
    00005 //
    00006 // This software is provided as is. Model-based Embedded and Robotic Systems
    00007 // (MERS) group does not assume any responsibility.
    00008 //
    00009 // Organization:        Model-based Embedded and Robotic Systems Group
    00010 //                                      Massachusetts Institute of Technology
    00011 //
    00013
    00014 #include <tut.h>
    00015 #include <tut_reporter.h>
    00016 #include <iostream>
    00017 #include <Mers/Utils/Logging/logger.h>
    00018 #include <Mers/Utils/Containers/IntegerArray.h>
    00019
    00020 using std::cerr;
    00021 using std::cout;
    00022 using std::endl;
    00023
    00024 namespace tut
    00025 {
    00026   test_runner_singleton runner;
    00027 }
    00028
    00029
    00030 namespace OpMode {
    00031         enum OpMode {Unknown, Help, ListGroups, RunAllTests, RunGroup, RunTest};
    00032
    00033         const char * toString(OpMode opMode)
    00034         {
    00035                 switch(opMode) {
    00036                 case Unknown:
    00037                         return "unknown";
    00038                 case Help:
    00039                         return "help";
    00040                 case ListGroups:
    00041                         return "list";
    00042                 case RunAllTests:
    00043                         return "run all tests";
    00044                 case RunGroup:
    00045                         return "run a group test";
    00046                 case RunTest:
    00047                         return "run a single test";
    00048                 default:
    00049                         return "invalid mode";
    00050                 }
    00051         }
    00052 }
    00053
    00054 using OpMode::toString;
    00055
    00056 void printUsage(const char * prog)
    00057 {
    00058         cout << "TUT test suite." << endl;
    00059         cout << "Usage:" << endl;
    00060         cout << prog << " <options>" << endl;
    00061         cout << "       Runs all tests." << endl;
    00062         cout << prog << " <options> list" << endl;
    00063         cout << "       Lists all groups." << endl;
    00064         cout << prog << " <options> -g <group> [-t <test #>]" << endl;
    00065         cout << "       Runs the specified test in the specified group" << endl;
    00066         cout << "       (all tests in the group if no test is specified)." << endl;
    00067         cout << "       e.g. (group): " << prog << " -g std::auto_ptr" << endl;
    00068         cout << "       e.g. (test): " << prog << " -g std::auto_ptr -t 3" << endl;
    00069         cout << prog << " <options> --help" << endl;
    00070         cout << "       Displays this message." << endl;
    00071         cout << "<options>:" << endl;
    00072         cout << "       --log <log4cplus config file>: Specifies a config file to "
    00073                 "load," << endl << "           which is used to configure the log4cplus "
    00074                 "logger output." << endl << "           By default, log4cplus is "
    00075                 "configured to print to standard out." << endl;
    00076 }
    00077
    00078 int main(int argc,const char* argv[])
    00079 {
    00080   tut::reporter visi;
    00081
    00082         char const * progName = "main";
    00083         if(argc > 0)
    00084                 progName = argv[0];
    00085
    00086         OpMode::OpMode mode = OpMode::Unknown;
    00087
    00088         const char * configFile = NULL;
    00089         const char * groupName = NULL;
    00090         Mers::Utils::Containers::IntegerArray tests;
    00091
    00092         int x = 1;
    00093
    00094         bool error = false;
    00095
    00096         while(x < argc && !error) {
    00097                 if (strcmp(argv[x], "list") == 0) {
    00098                         if(mode != OpMode::Unknown) {
    00099                                 cerr << "Unexpected parameter '" << argv[x] << "' in mode: "
    00100                                                  << toString(mode) << endl;
    00101                                 error = true;
    00102                         } else {
    00103                                 mode = OpMode::ListGroups;
    00104                         }
    00105                 } else if (strcmp(argv[x], "--help") == 0 || strcmp(argv[x], "-h") == 0) {
    00106                         if(mode != OpMode::Unknown) {
    00107                                 cerr << "Warning, ignoring mode " << toString(mode)
    00108                                                  << ", displaying help instead." << endl;
    00109                                 error = true;
    00110                         }
    00111                         mode = OpMode::Help;
    00112                 } else if (strcmp(argv[x], "-g") == 0) {
    00113                         if (mode != OpMode::Unknown && mode != OpMode::RunTest) {
    00114                                 cerr << "Unexpected parameter '" << argv[x] << "' in mode: "
    00115                                                  << toString(mode) << endl;
    00116                                 error = true;
    00117                         } else {
    00118                                 x++;
    00119                                 if (x >= argc) {
    00120                                         cerr << "Missing argument to " << argv[x - 1] << endl;
    00121                                         error = true;
    00122                                 } else {
    00123                                         groupName = argv[x];
    00124                                         if(mode == OpMode::Unknown)
    00125                                                 mode = OpMode::RunGroup;
    00126                                 }
    00127                         }
    00128                 } else if (strcmp(argv[x], "-t") == 0) {
    00129                         if (mode != OpMode::Unknown && mode != OpMode::RunTest &&
    00130                                         mode != OpMode::RunGroup) {
    00131                                 cerr << "Unexpected parameter '" << argv[x] << "' in mode: "
    00132                                                  << toString(mode) << endl;
    00133                                 error = true;
    00134                         } else {
    00135                                 x++;
    00136                                 if (x >= argc) {
    00137                                         cerr << "Missing argument to " << argv[x - 1] << endl;
    00138                                         error = true;
    00139                                 } else {
    00140                                         char * end;
    00141                                         int test = strtol(argv[x], &end, 10);
    00142
    00143                                         if (end == argv[x] || *end != '\0') {
    00144                                                 cerr << "Invalid test number: " << argv[x] << endl;
    00145                                                 error = true;
    00146                                         } else {
    00147                                                 tests.pushBack(test);
    00148                                                 mode = OpMode::RunTest;
    00149                                         }
    00150                                 }
    00151                         }
    00152                 } else if (strcmp(argv[x], "--log") == 0) {
    00153                         x++;
    00154                         if (x >= argc) {
    00155                                 cerr << "Missing argument to " << argv[x - 1] << endl;
    00156                                 error = true;
    00157                         } else {
    00158                                 configFile = argv[x];
    00159                         }
    00160                 }
    00161                 x++;
    00162         }
    00163
    00164         if(mode == OpMode::Unknown)
    00165                 mode = OpMode::RunAllTests;
    00166
    00167         if(mode == OpMode::RunGroup || mode == OpMode::RunTest) {
    00168                 if(groupName == NULL || *groupName == '\0') {
    00169                         cerr << "Missing group (-g) for test" << endl;
    00170                         error = true;
    00171                 }
    00172         }
    00173
    00174         if(configFile != NULL && *configFile == '\0') {
    00175                 cerr << "Warning, invalid config file." << endl;
    00176                 configFile = NULL;
    00177         }
    00178
    00179   if(mode == OpMode::Help || error)
    00180   {
    00181                 printUsage(progName);
    00182                 return error;
    00183   }
    00184
    00185   tut::runner.get().set_callback(&visi);
    00186
    00187         // load our config file
    00188         if(configFile == NULL) {
    00189                 log4cplus::BasicConfigurator::doConfigure();
    00190         } else {
    00191                 log4cplus::PropertyConfigurator::doConfigure(configFile);
    00192         }
    00193
    00194   try
    00195   {
    00196                 switch(mode) {
    00197                 case OpMode::ListGroups:
    00198                         {
    00199                                 std::cout << "registered test groups:" << std::endl;
    00200                                 tut::groupnames gl = tut::runner.get().list_groups();
    00201                                 tut::groupnames::const_iterator i = gl.begin();
    00202                                 tut::groupnames::const_iterator e = gl.end();
    00203                                 while( i != e )
    00204                                 {
    00205                                         std::cout << "  " << *i << std::endl;
    00206                                         ++i;
    00207                                 }
    00208                         }
    00209                         break;
    00210                 case OpMode::RunAllTests:
    00211       tut::runner.get().run_tests();
    00212                         break;
    00213                 case OpMode::RunGroup:
    00214       tut::runner.get().run_tests(groupName);
    00215                         break;
    00216                 case OpMode::RunTest:
    00217                         {
    00218                                 for(unsigned int i = 0; i < tests.getLength(); i++) {
    00219                                         tut::runner.get().run_test(groupName, tests[i]);
    00220                                 }
    00221                         }
    00222                         break;
    00223                 default:
    00224                         cerr << "Invalid mode: " << toString(mode) << endl;
    00225     }
    00226   } catch( const std::exception& ex ) {
    00227     std::cerr << "tut raised ex: " << ex.what() << std::endl;
    00228   }
    00229
    00230   return 0;
    00231 }