-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
132 lines (105 loc) · 4.58 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Copyright (C) 2019 Jimmy Aguilar Mena
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include "argparser.hpp"
#ifdef _MSC_VER
#include <Windows.h>
#define sleep(X) Sleep(X * 1000)
#else
#include <unistd.h>
#endif
const std::string global_string = "Value for reportable in global variable";
int main(int argc, char *argv[])
{
argparser::init(argc, argv);
int v_int_1 = argparser::cl<int>("v_int_1");
std::string v_string_1 = argparser::cl<std::string>("v_string_1");
double v_double_1 = argparser::cl<double>("v_double_1");
int it = 1 + (std::string(argv[1]) == "-json" || std::string(argv[1]) == "-millis");
if (it > 1)
it += (std::string(argv[2]) == "-json" || std::string(argv[2]) == "-millis");
std::cout << "# Read mandatory args: "
<< v_int_1 << " "
<< v_string_1 << " "
<< v_double_1 << " "
<< std::endl;
assert(v_int_1 == std::atoi(argv[it++]));
assert(v_string_1 == std::string(argv[it++]));
assert(v_double_1 == std::atof(argv[it++]));
int o_int_1 = argparser::cl<int>("o_int_1", -1);
std::cout << "# Read: o_int_1 " << o_int_1 << std::endl;
assert(o_int_1 == ((argc > 4 ? atoi(argv[it++]) : -1)));
double o_double_1 = argparser::cl<double> ("o_double_1", 0.5);
std::cout << "# Read: o_double_1 " << o_double_1 << std::endl;
assert(o_double_1 == ((argc > 5 ? atof(argv[it++]) : 0.5)));
const std::string o_string_1 = argparser::cl<std::string>("o_string_1", std::string());
std::cout << "# Read: o_string_1 " << o_string_1 << std::endl;
assert(argc > 6
? (o_string_1 == std::string(argv[it++]))
: o_string_1.empty());
const std::string o_string_2 = argparser::cl<std::string> ("o_string_2", "");
std::cout << "# Read: o_string_2 " << o_string_2 << std::endl;
assert(o_string_2 == ((argc > 7 ? argv[it++] : "")));
const std::string o_string_3 = argparser::cl<std::string> ("o_string_3", "opt1 opt2");
std::cout << "# Read: o_string_3 " << o_string_3 << std::endl;
assert(o_string_3 == ((argc > 8 ? argv[it++] : "opt1 opt2")));
argparser::time t1("timer_1");
int r_int_1 = argparser::reportable<int>("r_int_1", argc);
std::cout << "# Report: r_int_1 " << r_int_1 << std::endl;
assert(r_int_1 == argc);
std::vector<std::string> rest = argparser::get_rest();
if (rest.size() > 0){
std::cout << "# ";
for (int i = 0; i < rest.size(); ++i) {
std::cout << "rest[" << i << "]: \"" << rest[i] << "\", ";
}
std::cout << std::endl;
}
double r_double_1 = argparser::reportable<double>("r_double_1", v_double_1 / o_double_1);
std::cout << "# Report: r_double_1 " << r_double_1 << std::endl;
assert(r_double_1 == v_double_1 / o_double_1);
const std::string r_string_1 = argparser::reportable<std::string>("r_string_1", "hello");
std::cout << "# Report: r_string_1 " << r_string_1 << std::endl;
assert(r_string_1 == "hello");
const std::string r_string_2 = argparser::reportable<std::string>("r_string_2", "hello world");
std::cout << "# Report: r_string_2 " << r_string_2 << std::endl;
assert(r_string_2 == "hello world");
const std::string r_string_3 = argparser::reportable<std::string>("r_string_3", global_string);
std::cout << "# Report: r_string_3 " << r_string_3 << std::endl;
assert(r_string_3 == global_string);
argparser::time t2("timer_2"); // New timer to trigger realloc
std::cout << "# Start sleep" << std::endl;
sleep(1);
std::cout << "# End sleep\n" << std::endl;
t2.stop();
const double diff_time1 = t1.getNS();
std::cout << "# Timer: T1 " << diff_time1 << std::endl;
assert(diff_time1 == 0.);
const double diff_time2 = t2.getNS();
std::cout << "# Timer: T2 " << diff_time2 << std::endl;
assert(diff_time2 >= 1E9);
t2.reset();
const double diff_time_reset = t2.getNS();
std::cout << "# Times: T2 (reset) " << diff_time_reset << std::endl;
assert(diff_time_reset == 0.);
std::cout << "Printing reset timer is: " << t2 << std::endl;
std::cout << "# Reporting args" << std::endl;
argparser::report<>();
std::cout << "\n# Call free args" << std::endl;
argparser::free();
return 0;
}