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