X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fvimcoder;a=blobdiff_plain;f=src%2Fcom%2Fdogcows%2Fresources%2FC%2B%2BDriver;h=718ced40bbce7d119631781e0779d426ace63ded;hp=9f25901fbdc34f972dc761abade4a17df0a0a704;hb=70fd7817176222991ace7906ecfd633c9b3eb272;hpb=88f4c7d0844a4ce882caffb347f81fb70a0d2f98 diff --git a/src/com/dogcows/resources/C++Driver b/src/com/dogcows/resources/C++Driver index 9f25901..718ced4 100644 --- a/src/com/dogcows/resources/C++Driver +++ b/src/com/dogcows/resources/C++Driver @@ -1,14 +1,18 @@ #include "$CLASSNAME$.cc" +#include +#include #include #include +#include #include +#include -static bool __exit_on_fail = false; -static int __pass = 0; -static int __fail = 0; -static double __time = 0.0; +using namespace std; + + +static double __time = 0.0; static void __timer_start() { @@ -26,77 +30,131 @@ static double __timer_stop() return __time - start; } -template -std::string __encode(const T& in) -{ - std::ostringstream s; - s << in; - return s.str(); -} -std::string __encode(const std::string& in) +std::ostream& operator << (std::ostream& out, const std::string& str) { - std::ostringstream s; - s << '"' << in << '"'; - return s.str(); + out << '"' << str.c_str() << '"'; + return out; } template -std::string __join(const std::vector& in, const std::string& glue) +std::ostream& operator << (std::ostream& out, const std::vector& vec) { - if (in.size() == 0) return ""; - std::ostringstream s; - s << __encode(in[0]); - for (size_t i = 1; i < in.size(); ++i) s << glue << __encode(in[i]); - return s.str(); + out << '{'; + if (0 < vec.size()) + { + out << vec[0]; + for (size_t i = 1; i < vec.size(); ++i) out << ", " << vec[i]; + } + out << '}'; + return out; } -template -std::string __encode(const std::vector& in) +std::istream& operator >> (std::istream& in, std::string& str) { - std::ostringstream s; - s << "{ " << __join(in, ", ") << " }"; - return s.str(); + while (in.good() && std::isspace(in.peek())) in.get(); + + int c; + if (in.good() && (c = in.get()) == '"') + { + std::ostringstream s; + while (in.good() && (c = in.get()) != '"') + { + s.put(char(c)); + } + str = s.str(); + } + else + { + in.putback(c); + } + + return in; } -void __do_test(const std::string& expected, $METHODPARAMS$) +template +std::istream& operator >> (std::istream& in, std::vector& vec) { - static int testNum = 0; - std::cout << "----------------------------------------" << std::endl - << "Test " << testNum++ << ": "; + while (in.good() && std::isspace(in.peek())) in.get(); - __timer_start(); - - $CLASSNAME$ object; - $RETURNTYPE$ ret = object.$METHODNAME$($METHODPARAMNAMES$); - - double t = __timer_stop(); - - std::string actual = __encode(ret); - if (actual == expected) + int c; + if (in.good() && (c = in.get()) == '{') { - std::cout << "[PASS] in " << t << " seconds." << std::endl; - ++__pass; + while (in.good() && std::isspace(in.peek())) in.get(); + T t; + vec.clear(); + while (in.good() && (c = in.get()) != '}') + { + if (c != ',') in.putback(c); + in >> t; + vec.push_back(t); + while (in.good() && std::isspace(in.peek())) in.get(); + } + } else { - std::cout << "[FAIL] in " << t << " seconds." << std::endl - << " Actual: " << actual << std::endl - << " Expected: " << expected << std::endl; - ++__fail; - if (__exit_on_fail) exit(1); + in.putback(c); } + + return in; } + int main(int argc, char* argv[]) { + bool __exit_on_fail = false; + int __pass = 0; + int __fail = 0; + if (1 < argc) __exit_on_fail = true; + + std::ifstream __in("testcases.txt"); + for(;;) + { + $RETURNTYPE$ __expected; + $METHODPARAMDECLARES$ + __in >> __expected >> $METHODPARAMSTREAMIN$; + if (!__in.good()) break; + + std::cout << "----------------------------------------" << std::endl + << "Test " << (__pass + __fail) << ": "; + std::cout.flush(); + + __timer_start(); + + $CLASSNAME$ object; + $RETURNTYPE$ __actual = object.$METHODNAME$($METHODPARAMNAMES$); + + double __t = __timer_stop(); + + if (__actual == __expected) + { + std::cout << "[PASS] in " << __t << " seconds." << std::endl; + ++__pass; + } + else + { + std::cout << "[FAIL] in " << __t << " seconds." << std::endl + << "-> Input: " << $METHODPARAMSTREAMOUT$ << std::endl + << " Actual: " << __actual << std::endl + << " Expected: " << __expected << std::endl; + ++__fail; + if (__exit_on_fail) exit(1); + } + } -$TESTCASES$ std::cout << "========================================" << std::endl << " Total Pass: " << __pass << std::endl << " Total Fail: " << __fail << std::endl; + if (__fail == 0) + { + std::cout << std::endl << "Nice! " + << "Don't forget to compile remotely before submitting." + << std::endl; + } + return __fail; }