]> Dogcows Code - chaz/vimcoder/blob - src/com/dogcows/resources/C++Driver
60ef873967713dd8bd18ec8331aa3f24df74c6cc
[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 std::ostream& operator << (std::ostream& out, const std::string& str)
38 {
39 out << '"' << str.c_str() << '"';
40 return out;
41 }
42
43 template <class T>
44 std::ostream& operator << (std::ostream& out, const std::vector<T>& vec)
45 {
46 out << '{';
47 if (0 < vec.size())
48 {
49 out << vec[0];
50 for (size_t i = 1; i < vec.size(); ++i) out << ", " << vec[i];
51 }
52 out << '}';
53 return out;
54 }
55
56 std::istream& operator >> (std::istream& in, std::string& str)
57 {
58 while (in.good() && std::isspace(in.peek())) in.get();
59
60 int c;
61 if (in.good() && (c = in.get()) == '"')
62 {
63 std::ostringstream s;
64 while (in.good() && (c = in.get()) != '"')
65 {
66 s.put(char(c));
67 }
68 str = s.str();
69 }
70
71 return in;
72 }
73
74 template <class T>
75 std::istream& operator >> (std::istream& in, std::vector<T>& vec)
76 {
77 while (in.good() && std::isspace(in.peek())) in.get();
78
79 int c;
80 if (in.good() && (c = in.get()) == '{')
81 {
82 while (in.good() && std::isspace(in.peek())) in.get();
83 T t;
84 vec.clear();
85 while (in.good() && (c = in.get()) != '}')
86 {
87 if (c != ',') in.putback(c);
88 in >> t;
89 vec.push_back(t);
90 while (in.good() && std::isspace(in.peek())) in.get();
91 }
92 }
93
94 return in;
95 }
96
97 template <class T>
98 bool __equals(const T& actual, const T& expected)
99 {
100 return actual == expected;
101 }
102
103 bool __equals(double actual, double expected)
104 {
105 if (std::abs(actual - expected) < __EPSILON)
106 {
107 return true;
108 }
109 else
110 {
111 double minimum = std::min(expected * (1.0 - __EPSILON), expected * (1.0 + __EPSILON));
112 double maximum = std::max(expected * (1.0 - __EPSILON), expected * (1.0 + __EPSILON));
113 return actual > minimum && actual < maximum;
114 }
115 }
116
117 bool __equals(const std::vector<double>& actual, const std::vector<double>& expected)
118 {
119 if (actual.size() != expected.size())
120 return false;
121
122 for (size_t i = 0; i < actual.size(); ++i)
123 if (!__equals(actual[i], expected[i]))
124 return false;
125
126 return true;
127 }
128
129
130 int main(int argc, char* argv[])
131 {
132 bool __exit_on_fail = false;
133 int __pass = 0;
134 int __fail = 0;
135
136 if (1 < argc) __exit_on_fail = true;
137
138 std::ifstream __in("testcases.txt");
139 for(;;)
140 {
141 $RETURNTYPE$ __expected;
142 $METHODPARAMDECLARES$
143 __in >> __expected >> $METHODPARAMSTREAMIN$;
144 if (!__in.good()) break;
145
146 std::cout << "----------------------------------------" << std::endl
147 << "Test " << (__pass + __fail) << ": ";
148 std::cout.flush();
149
150 __timer_start();
151
152 $CLASSNAME$ object;
153 $RETURNTYPE$ __actual = object.$METHODNAME$($METHODPARAMNAMES$);
154
155 double __t = __timer_stop();
156
157 if (__equals(__actual, __expected))
158 {
159 std::cout << "[PASS] in " << __t << " seconds." << std::endl;
160 ++__pass;
161 }
162 else
163 {
164 std::cout << "[FAIL] in " << __t << " seconds." << std::endl
165 << "-> Input: " << $METHODPARAMSTREAMOUT$ << std::endl
166 << " Actual: " << __actual << std::endl
167 << " Expected: " << __expected << std::endl;
168 ++__fail;
169 if (__exit_on_fail) exit(1);
170 }
171 }
172
173 std::cout << "========================================" << std::endl
174 << " Total Pass: " << __pass << std::endl
175 << " Total Fail: " << __fail << std::endl;
176
177 if (__fail == 0)
178 {
179 std::cout << std::endl << "Nice! "
180 << "Don't forget to compile remotely before submitting."
181 << std::endl;
182 }
183
184 return __fail;
185 }
186
This page took 0.037502 seconds and 3 git commands to generate.