]> Dogcows Code - chaz/vimcoder/blob - src/com/dogcows/resources/C++Driver
generate TAP output with the default C++ templates
[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
15 const static double __EPSILON = 1e-9;
16 static double __time = 0.0;
17
18 static void __timer_start()
19 {
20 struct timeval tv;
21 if (gettimeofday(&tv, NULL) == 0)
22 {
23 __time = double(tv.tv_sec) + double(tv.tv_usec) * 0.000001;
24 }
25 }
26
27 static double __timer_stop()
28 {
29 double start = __time;
30 __timer_start();
31 return __time - start;
32 }
33
34
35 static void __eat_whitespace(std::istream& in)
36 {
37 while (in.good() && std::isspace(in.peek())) in.get();
38 }
39
40
41 std::ostream& operator << (std::ostream& out, const std::string& str)
42 {
43 out << '"' << str.c_str() << '"';
44 return out;
45 }
46
47 template <class T>
48 std::ostream& operator << (std::ostream& out, const std::vector<T>& vec)
49 {
50 out << '{';
51 if (0 < vec.size())
52 {
53 out << vec[0];
54 for (size_t i = 1; i < vec.size(); ++i) out << ", " << vec[i];
55 }
56 out << '}';
57 return out;
58 }
59
60 std::istream& operator >> (std::istream& in, std::string& str)
61 {
62 __eat_whitespace(in);
63
64 int c;
65 if (in.good() && (c = in.get()) == '"')
66 {
67 std::ostringstream s;
68 while (in.good() && (c = in.get()) != '"')
69 {
70 s.put(char(c));
71 }
72 str = s.str();
73 }
74
75 return in;
76 }
77
78 template <class T>
79 std::istream& operator >> (std::istream& in, std::vector<T>& vec)
80 {
81 __eat_whitespace(in);
82
83 int c;
84 if (in.good() && (c = in.get()) == '{')
85 {
86 __eat_whitespace(in);
87 vec.clear();
88 while (in.good() && (c = in.get()) != '}')
89 {
90 if (c != ',') in.putback(c);
91
92 T t;
93 in >> t;
94 __eat_whitespace(in);
95
96 vec.push_back(t);
97 }
98 }
99
100 return in;
101 }
102
103
104 template <class T>
105 bool __equals(const T& actual, const T& expected)
106 {
107 return actual == expected;
108 }
109
110 bool __equals(double actual, double expected)
111 {
112 if (std::abs(actual - expected) < __EPSILON)
113 {
114 return true;
115 }
116 else
117 {
118 double minimum = std::min(expected * (1.0 - __EPSILON), expected * (1.0 + __EPSILON));
119 double maximum = std::max(expected * (1.0 - __EPSILON), expected * (1.0 + __EPSILON));
120 return actual > minimum && actual < maximum;
121 }
122 }
123
124 bool __equals(const std::vector<double>& actual, const std::vector<double>& expected)
125 {
126 if (actual.size() != expected.size())
127 {
128 return false;
129 }
130
131 for (size_t i = 0; i < actual.size(); ++i)
132 {
133 if (!__equals(actual[i], expected[i]))
134 {
135 return false;
136 }
137 }
138
139 return true;
140 }
141
142
143 int main(int argc, char* argv[])
144 {
145 bool __abort_on_fail = false;
146 int __pass = 0;
147 int __fail = 0;
148
149 if (1 < argc) __abort_on_fail = true;
150
151 std::cout << "TAP version 13" << std::endl;
152 std::cout.flush();
153
154 std::ifstream __in("testcases.txt");
155 for(;;)
156 {
157 int __testnum = __pass + __fail + 1;
158
159 $RETURNTYPE$ __expected;
160 $METHODPARAMDECLARES$
161 __in >> __expected >> $METHODPARAMSTREAMIN$;
162 if (!__in.good()) break;
163
164 std::cout << "# input for test " << __testnum << ": " << $METHODPARAMSTREAMOUT$ << std::endl;
165 std::cout.flush();
166
167 __timer_start();
168
169 $CLASSNAME$ __object;
170 $RETURNTYPE$ __actual = __object.$METHODNAME$($METHODPARAMNAMES$);
171
172 double __t = __timer_stop();
173
174 std::cout << "# test completed in " << __t << " seconds" << std::endl;
175 std::cout.flush();
176
177 if (__equals(__actual, __expected))
178 {
179 std::cout << "ok";
180 ++__pass;
181 }
182 else
183 {
184 std::cout << "not ok";
185 ++__fail;
186 }
187
188 std::cout << " " << __testnum << " - " << __actual << " must equal " << __expected << std::endl;
189 std::cout.flush();
190
191 if (__abort_on_fail && 0 < __fail) std::abort();
192 }
193
194 std::cout << "1.." << (__pass + __fail) << std::endl
195 << "# passed: " << __pass << std::endl
196 << "# failed: " << __fail << std::endl;
197
198 if (__fail == 0)
199 {
200 std::cout << std::endl
201 << "# Nice! Don't forget to compile remotely before submitting." << std::endl;
202 }
203
204 return __fail;
205 }
206
207 // vim:ft=cpp:noet:ts=8
This page took 0.046074 seconds and 4 git commands to generate.