]> Dogcows Code - chaz/vimcoder/blob - src/com/dogcows/resources/C++Driver
separate whitespace handling into its own function
[chaz/vimcoder] / src / com / dogcows / resources / C++Driver
1
2 #include "$CLASSNAME$.cc"
3
4 #include <algorithm>
5 #include <cmath>
6 #include <cstdlib>
7 #include <fstream>
8 #include <iostream>
9 #include <sstream>
10 #include <string>
11 #include <sys/time.h>
12 #include <vector>
13
14 using namespace std;
15
16
17 const static double __EPSILON = 1e-9;
18 static double __time = 0.0;
19
20 static void __timer_start()
21 {
22 struct timeval tv;
23 if (gettimeofday(&tv, NULL) == 0)
24 {
25 __time = double(tv.tv_sec) + double(tv.tv_usec) * 0.000001;
26 }
27 }
28
29 static double __timer_stop()
30 {
31 double start = __time;
32 __timer_start();
33 return __time - start;
34 }
35
36
37 static void __eat_whitespace(std::istream& in)
38 {
39 while (in.good() && std::isspace(in.peek())) in.get();
40 }
41
42
43 std::ostream& operator << (std::ostream& out, const std::string& str)
44 {
45 out << '"' << str.c_str() << '"';
46 return out;
47 }
48
49 template <class T>
50 std::ostream& operator << (std::ostream& out, const std::vector<T>& vec)
51 {
52 out << '{';
53 if (0 < vec.size())
54 {
55 out << vec[0];
56 for (size_t i = 1; i < vec.size(); ++i) out << ", " << vec[i];
57 }
58 out << '}';
59 return out;
60 }
61
62 std::istream& operator >> (std::istream& in, std::string& str)
63 {
64 __eat_whitespace(in);
65
66 int c;
67 if (in.good() && (c = in.get()) == '"')
68 {
69 std::ostringstream s;
70 while (in.good() && (c = in.get()) != '"')
71 {
72 s.put(char(c));
73 }
74 str = s.str();
75 }
76
77 return in;
78 }
79
80 template <class T>
81 std::istream& operator >> (std::istream& in, std::vector<T>& vec)
82 {
83 __eat_whitespace(in);
84
85 int c;
86 if (in.good() && (c = in.get()) == '{')
87 {
88 __eat_whitespace(in);
89 vec.clear();
90 while (in.good() && (c = in.get()) != '}')
91 {
92 if (c != ',') in.putback(c);
93
94 T t;
95 in >> t;
96 __eat_whitespace(in);
97
98 vec.push_back(t);
99 }
100 }
101
102 return in;
103 }
104
105
106 template <class T>
107 bool __equals(const T& actual, const T& expected)
108 {
109 return actual == expected;
110 }
111
112 bool __equals(double actual, double expected)
113 {
114 if (std::abs(actual - expected) < __EPSILON)
115 {
116 return true;
117 }
118 else
119 {
120 double minimum = std::min(expected * (1.0 - __EPSILON), expected * (1.0 + __EPSILON));
121 double maximum = std::max(expected * (1.0 - __EPSILON), expected * (1.0 + __EPSILON));
122 return actual > minimum && actual < maximum;
123 }
124 }
125
126 bool __equals(const std::vector<double>& actual, const std::vector<double>& expected)
127 {
128 if (actual.size() != expected.size())
129 return false;
130
131 for (size_t i = 0; i < actual.size(); ++i)
132 if (!__equals(actual[i], expected[i]))
133 return false;
134
135 return true;
136 }
137
138
139 int main(int argc, char* argv[])
140 {
141 bool __exit_on_fail = false;
142 int __pass = 0;
143 int __fail = 0;
144
145 if (1 < argc) __exit_on_fail = true;
146
147 std::ifstream __in("testcases.txt");
148 for(;;)
149 {
150 $RETURNTYPE$ __expected;
151 $METHODPARAMDECLARES$
152 __in >> __expected >> $METHODPARAMSTREAMIN$;
153 if (!__in.good()) break;
154
155 std::cout << "----------------------------------------" << std::endl
156 << "Test " << (__pass + __fail) << ": ";
157 std::cout.flush();
158
159 __timer_start();
160
161 $CLASSNAME$ object;
162 $RETURNTYPE$ __actual = object.$METHODNAME$($METHODPARAMNAMES$);
163
164 double __t = __timer_stop();
165
166 if (__equals(__actual, __expected))
167 {
168 std::cout << "[PASS] in " << __t << " seconds." << std::endl;
169 ++__pass;
170 }
171 else
172 {
173 std::cout << "[FAIL] in " << __t << " seconds." << std::endl
174 << "-> Input: " << $METHODPARAMSTREAMOUT$ << std::endl
175 << " Actual: " << __actual << std::endl
176 << " Expected: " << __expected << std::endl;
177 ++__fail;
178 if (__exit_on_fail) exit(1);
179 }
180 }
181
182 std::cout << "========================================" << std::endl
183 << " Total Pass: " << __pass << std::endl
184 << " Total Fail: " << __fail << std::endl;
185
186 if (__fail == 0)
187 {
188 std::cout << std::endl << "Nice! "
189 << "Don't forget to compile remotely before submitting."
190 << std::endl;
191 }
192
193 return __fail;
194 }
195
This page took 0.041113 seconds and 4 git commands to generate.