Line data Source code
1 : /* test_addr_main.cpp
2 : * Copyright (C) 2011-2017 Made to Order Software Corporation
3 : *
4 : * Project: http://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 : // Tell catch we want it to add the runner code in this file.
33 : #define CATCH_CONFIG_RUNNER
34 :
35 : #include "test_addr_main.h"
36 :
37 : #include "libaddr/addr.h"
38 : #include "libaddr/version.h"
39 :
40 : #include <sstream>
41 :
42 :
43 : namespace unittest
44 : {
45 : }
46 :
47 :
48 : namespace
49 : {
50 : struct UnitTestCLData
51 : {
52 : bool help = false;
53 : bool version = false;
54 : int seed = 0;
55 : };
56 :
57 0 : void remove_from_args( std::vector<std::string> & vect, std::string const & long_opt, std::string const & short_opt )
58 : {
59 0 : auto iter = std::find_if( vect.begin(), vect.end(), [long_opt, short_opt]( std::string const & arg )
60 : {
61 0 : return arg == long_opt || arg == short_opt;
62 0 : });
63 :
64 0 : if( iter != vect.end() )
65 : {
66 0 : auto next_iter = iter;
67 0 : vect.erase( ++next_iter );
68 0 : vect.erase( iter );
69 : }
70 0 : }
71 : }
72 : // namespace
73 :
74 :
75 2 : int test_addr_main(int argc, char * argv[])
76 : {
77 2 : UnitTestCLData configData;
78 3 : Catch::Clara::CommandLine<UnitTestCLData> cli;
79 :
80 4 : cli["-?"]["-h"]["--help"]
81 6 : .describe("display usage information")
82 2 : .bind(&UnitTestCLData::help);
83 :
84 4 : cli["-S"]["--seed"]
85 6 : .describe("value to seed the randomizer, if not specified, randomize")
86 6 : .bind(&UnitTestCLData::seed, "the seed value");
87 :
88 4 : cli["-V"]["--version"]
89 6 : .describe("print out the advgetopt library version these unit tests pertain to")
90 2 : .bind(&UnitTestCLData::version);
91 :
92 2 : cli.parseInto( argc, argv, configData );
93 :
94 2 : if( configData.help )
95 : {
96 0 : cli.usage( std::cout, argv[0] );
97 0 : Catch::Session().run(argc, argv);
98 0 : exit(1);
99 : }
100 :
101 2 : if( configData.version )
102 : {
103 1 : std::cout << LIBADDR_VERSION_STRING << std::endl;
104 1 : exit(0);
105 : }
106 :
107 2 : std::vector<std::string> arg_list;
108 2 : for( int i = 0; i < argc; ++i )
109 : {
110 1 : arg_list.push_back( argv[i] );
111 : }
112 :
113 : // by default we get a different seed each time; that really helps
114 : // in detecting errors! (I know, I wrote loads of tests before)
115 : //
116 1 : unsigned int seed(static_cast<unsigned int>(time(NULL)));
117 1 : if( configData.seed != 0 )
118 : {
119 0 : seed = static_cast<unsigned int>(configData.seed);
120 0 : remove_from_args( arg_list, "--seed", "-S" );
121 : }
122 1 : srand(seed);
123 1 : std::cout << "libaddr[" << getpid() << "]:test: seed is " << seed << std::endl;
124 :
125 2 : std::vector<char *> new_argv;
126 1 : std::for_each( arg_list.begin(), arg_list.end(), [&new_argv]( const std::string& arg )
127 1 : {
128 2 : new_argv.push_back( const_cast<char *>(arg.c_str()) );
129 2 : });
130 :
131 2 : return Catch::Session().run( static_cast<int>(new_argv.size()), &new_argv[0] );
132 : }
133 :
134 :
135 2 : int main(int argc, char * argv[])
136 : {
137 2 : int r(1);
138 :
139 : try
140 : {
141 2 : r = test_addr_main(argc, argv);
142 : }
143 0 : catch(std::logic_error const & e)
144 : {
145 0 : std::cerr << "fatal error: caught a logic error in libaddr unit tests: " << e.what() << "\n";
146 : }
147 :
148 1 : return r;
149 6 : }
150 :
151 : // vim: ts=4 sw=4 et
|