LCOV - code coverage report
Current view: top level - tests - test_addr_main.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 49 56 87.5 %
Date: 2018-05-30 23:58:02 Functions: 9 9 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* test_addr_main.cpp
       2             :  * Copyright (c) 2011-2018  Made to Order Software Corp.  All Rights Reserved
       3             :  *
       4             :  * Project: https://snapwebsites.org/project/libaddr
       5             :  *
       6             :  * Permission is hereby granted, free of charge, to any
       7             :  * person obtaining a copy of this software and
       8             :  * associated documentation files (the "Software"), to
       9             :  * deal in the Software without restriction, including
      10             :  * without limitation the rights to use, copy, modify,
      11             :  * merge, publish, distribute, sublicense, and/or sell
      12             :  * copies of the Software, and to permit persons to whom
      13             :  * the Software is furnished to do so, subject to the
      14             :  * following conditions:
      15             :  *
      16             :  * The above copyright notice and this permission notice
      17             :  * shall be included in all copies or substantial
      18             :  * portions of the Software.
      19             :  *
      20             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
      21             :  * ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
      22             :  * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
      23             :  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
      24             :  * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
      25             :  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
      26             :  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
      27             :  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      28             :  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      29             :  * SOFTWARE.
      30             :  */
      31             : 
      32             : /** \file
      33             :  * \brief The main() function of the addr library tests.
      34             :  *
      35             :  * This file implements the main() function of the unit tests used to
      36             :  * verify the addr library.
      37             :  *
      38             :  * It defines any globals (none at this point) and a few basic command
      39             :  * line options such as --help and --version.
      40             :  */
      41             : 
      42             : // Tell catch we want it to add the runner code in this file.
      43             : #define CATCH_CONFIG_RUNNER
      44             : 
      45             : // self
      46             : //
      47             : #include "test_addr_main.h"
      48             : 
      49             : 
      50             : 
      51             : 
      52             : namespace unittest
      53             : {
      54             : 
      55             : int         g_tcp_port = -1;
      56             : 
      57             : }
      58             : 
      59             : 
      60             : namespace
      61             : {
      62             :     struct UnitTestCLData
      63             :     {
      64             :         bool        help = false;
      65             :         bool        version = false;
      66             :         int         seed = 0;
      67             :         int         tcp_port = -1;
      68             :     };
      69             : 
      70           1 :     void remove_from_args( std::vector<std::string> & vect, std::string const & long_opt, std::string const & short_opt )
      71             :     {
      72          11 :         auto iter = std::find_if( vect.begin(), vect.end(), [long_opt, short_opt]( std::string const & arg )
      73             :         {
      74           2 :             return arg == long_opt || arg == short_opt;
      75           3 :         });
      76             : 
      77           1 :         if( iter != vect.end() )
      78             :         {
      79           1 :             auto next_iter = iter;
      80           1 :             vect.erase( ++next_iter );
      81           1 :             vect.erase( iter );
      82             :         }
      83           1 :     }
      84             : }
      85             : // namespace
      86             : 
      87             : 
      88           2 : int test_addr_main(int argc, char * argv[])
      89             : {
      90           2 :     UnitTestCLData configData;
      91           3 :     Catch::Clara::CommandLine<UnitTestCLData> cli;
      92             : 
      93           4 :     cli["-?"]["-h"]["--help"]
      94           6 :         .describe("display usage information")
      95           2 :         .bind(&UnitTestCLData::help);
      96             : 
      97           4 :     cli["-S"]["--seed"]
      98           6 :         .describe("value to seed the randomizer, if not specified, randomize")
      99           6 :         .bind(&UnitTestCLData::seed, "seed");
     100             : 
     101             :     cli["--tcp-port"]
     102           4 :         .describe("define a TCP port we can connect to to test the get_from_socket() function")
     103           6 :         .bind(&UnitTestCLData::tcp_port, "port");
     104             : 
     105           4 :     cli["-V"]["--version"]
     106           6 :         .describe("print out the libaddr library version these unit tests pertain to")
     107           2 :         .bind(&UnitTestCLData::version);
     108             : 
     109           2 :     cli.parseInto( argc, argv, configData );
     110             : 
     111           2 :     if( configData.help )
     112             :     {
     113           0 :         cli.usage( std::cout, argv[0] );
     114           0 :         Catch::Session().run(argc, argv);
     115           0 :         exit(1);
     116             :     }
     117             : 
     118           2 :     if( configData.version )
     119             :     {
     120           1 :         std::cout << LIBADDR_VERSION_STRING << std::endl;
     121           1 :         exit(0);
     122             :     }
     123             : 
     124           2 :     std::vector<std::string> arg_list;
     125           4 :     for( int i = 0; i < argc; ++i )
     126             :     {
     127           3 :         arg_list.push_back( argv[i] );
     128             :     }
     129             : 
     130             :     // by default we get a different seed each time; that really helps
     131             :     // in detecting errors! (I know, I wrote loads of tests before)
     132             :     //
     133           1 :     unsigned int seed(static_cast<unsigned int>(time(NULL)));
     134           1 :     if( configData.seed != 0 )
     135             :     {
     136           0 :         seed = static_cast<unsigned int>(configData.seed);
     137           0 :         remove_from_args( arg_list, "--seed", "-S" );
     138             :     }
     139           1 :     srand(seed);
     140           1 :     std::cout << "libaddr[" << getpid() << "]:test: seed is " << seed << std::endl;
     141             : 
     142             :     // make a copy of the port specified on the command line
     143             :     //
     144           1 :     if( configData.tcp_port != -1 )
     145             :     {
     146           1 :         unittest::g_tcp_port = configData.tcp_port;
     147           1 :         remove_from_args( arg_list, "--tcp-port", "" );
     148             :     }
     149             : 
     150           2 :     std::vector<char *> new_argv;
     151           1 :     std::for_each( arg_list.begin(), arg_list.end(), [&new_argv]( const std::string& arg )
     152           1 :     {
     153           2 :         new_argv.push_back( const_cast<char *>(arg.c_str()) );
     154           2 :     });
     155             : 
     156           2 :     return Catch::Session().run( static_cast<int>(new_argv.size()), &new_argv[0] );
     157             : }
     158             : 
     159             : 
     160           2 : int main(int argc, char * argv[])
     161             : {
     162           2 :     int r(1);
     163             : 
     164             :     try
     165             :     {
     166           2 :         r = test_addr_main(argc, argv);
     167             :     }
     168           0 :     catch(std::logic_error const & e)
     169             :     {
     170           0 :         std::cerr << "fatal error: caught a logic error in libaddr unit tests: " << e.what() << "\n";
     171             :     }
     172             : 
     173           1 :     return r;
     174           6 : }
     175             : 
     176             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.12