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