]> Dogcows Code - chaz/vimcoder/blob - src/com/dogcows/resources/C++Driver
improved C++ driver template
[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 }
95 else
96 {
97 in.putback(c);
98 }
99
100 return in;
101 }
102
103
104 int main(int argc, char* argv[])
105 {
106 bool __exit_on_fail = false;
107 int __pass = 0;
108 int __fail = 0;
109
110 if (1 < argc) __exit_on_fail = true;
111
112 std::ifstream __in("testcases.txt");
113 for(;;)
114 {
115 $RETURNTYPE$ __expected;
116 $METHODPARAMDECLARES$
117 __in >> __expected >> $METHODPARAMSTREAMIN$;
118 if (!__in.good()) break;
119
120 std::cout << "----------------------------------------" << std::endl
121 << "Test " << (__pass + __fail) << ": ";
122 std::cout.flush();
123
124 __timer_start();
125
126 $CLASSNAME$ object;
127 $RETURNTYPE$ __actual = object.$METHODNAME$($METHODPARAMNAMES$);
128
129 double __t = __timer_stop();
130
131 if (__actual == __expected)
132 {
133 std::cout << "[PASS] in " << __t << " seconds." << std::endl;
134 ++__pass;
135 }
136 else
137 {
138 std::cout << "[FAIL] in " << __t << " seconds." << std::endl
139 << "-> Input: " << $METHODPARAMSTREAMOUT$ << std::endl
140 << " Actual: " << __actual << std::endl
141 << " Expected: " << __expected << std::endl;
142 ++__fail;
143 if (__exit_on_fail) exit(1);
144 }
145 }
146
147 std::cout << "========================================" << std::endl
148 << " Total Pass: " << __pass << std::endl
149 << " Total Fail: " << __fail << std::endl;
150
151 if (__fail == 0)
152 {
153 std::cout << std::endl << "Nice! "
154 << "Don't forget to compile remotely before submitting."
155 << std::endl;
156 }
157
158 return __fail;
159 }
160
This page took 0.042639 seconds and 4 git commands to generate.