]> Dogcows Code - chaz/vimcoder/blob - src/com/dogcows/resources/C++Driver
initial commit
[chaz/vimcoder] / src / com / dogcows / resources / C++Driver
1
2 #include "$CLASSNAME$.cc"
3
4 #include <iostream>
5 #include <sstream>
6 #include <sys/time.h>
7
8 static bool __exit_on_fail = false;
9 static int __pass = 0;
10 static int __fail = 0;
11 static double __time = 0.0;
12
13 static void __timer_start()
14 {
15 struct timeval tv;
16 if (gettimeofday(&tv, NULL) == 0)
17 {
18 __time = (double)tv.tv_sec + (double)tv.tv_usec * 0.000001;
19 }
20 }
21
22 static double __timer_stop()
23 {
24 double start = __time;
25 __timer_start();
26 return __time - start;
27 }
28
29 template <class T>
30 std::string __encode(const T& in)
31 {
32 std::ostringstream s;
33 s << in;
34 return s.str();
35 }
36
37 std::string __encode(const std::string& in)
38 {
39 std::ostringstream s;
40 s << '"' << in << '"';
41 return s.str();
42 }
43
44 template <class T>
45 std::string __join(const std::vector<T>& in, const std::string& glue)
46 {
47 if (in.size() == 0) return "";
48 std::ostringstream s;
49 s << __encode(in[0]);
50 for (size_t i = 1; i < in.size(); ++i) s << glue << __encode(in[i]);
51 return s.str();
52 }
53
54 template <class T>
55 std::string __encode(const std::vector<T>& in)
56 {
57 std::ostringstream s;
58 s << "{ " << __join(in, ", ") << " }";
59 return s.str();
60 }
61
62 void __do_test(const std::string& expected, $METHODPARAMS$)
63 {
64 static int testNum = 0;
65 std::cout << "----------------------------------------" << std::endl
66 << "Test " << testNum++ << ": ";
67
68 __timer_start();
69
70 $CLASSNAME$ object;
71 $RETURNTYPE$ ret = object.$METHODNAME$($METHODPARAMNAMES$);
72
73 double t = __timer_stop();
74
75 std::string actual = __encode(ret);
76 if (actual == expected)
77 {
78 std::cout << "[PASS] in " << t << " seconds." << std::endl;
79 ++__pass;
80 }
81 else
82 {
83 std::cout << "[FAIL] in " << t << " seconds." << std::endl
84 << " Actual: " << actual << std::endl
85 << " Expected: " << expected << std::endl;
86 ++__fail;
87 if (__exit_on_fail) exit(1);
88 }
89 }
90
91 int main(int argc, char* argv[])
92 {
93 if (1 < argc) __exit_on_fail = true;
94
95 $TESTCASES$
96 std::cout << "========================================" << std::endl
97 << " Total Pass: " << __pass << std::endl
98 << " Total Fail: " << __fail << std::endl;
99
100 return __fail;
101 }
102
This page took 0.037139 seconds and 4 git commands to generate.