X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=src%2Fcom%2Fdogcows%2Fresources%2FC%2B%2BDriver;h=d08ded83b4928fece0f931c07dd65dcca23eb603;hb=a591f0fce8ebccd87f2e199479a0f91ad182f003;hp=9ca857968510527b6388536028a3a67e6dbf0a2d;hpb=540c07677f84c2680c808ecfb6be4ad6bb61237e;p=chaz%2Fvimcoder diff --git a/src/com/dogcows/resources/C++Driver b/src/com/dogcows/resources/C++Driver index 9ca8579..d08ded8 100644 --- a/src/com/dogcows/resources/C++Driver +++ b/src/com/dogcows/resources/C++Driver @@ -11,8 +11,6 @@ #include #include -using namespace std; - const static double __EPSILON = 1e-9; static double __time = 0.0; @@ -22,7 +20,7 @@ static void __timer_start() struct timeval tv; if (gettimeofday(&tv, NULL) == 0) { - __time = (double)tv.tv_sec + (double)tv.tv_usec * 0.000001; + __time = double(tv.tv_sec) + double(tv.tv_usec) * 0.000001; } } @@ -34,6 +32,12 @@ static double __timer_stop() } +static void __eat_whitespace(std::istream& in) +{ + while (in.good() && std::isspace(in.peek())) in.get(); +} + + std::ostream& operator << (std::ostream& out, const std::string& str) { out << '"' << str.c_str() << '"'; @@ -55,7 +59,7 @@ std::ostream& operator << (std::ostream& out, const std::vector& vec) std::istream& operator >> (std::istream& in, std::string& str) { - while (in.good() && std::isspace(in.peek())) in.get(); + __eat_whitespace(in); int c; if (in.good() && (c = in.get()) == '"') @@ -67,10 +71,6 @@ std::istream& operator >> (std::istream& in, std::string& str) } str = s.str(); } - else - { - in.putback(c); - } return in; } @@ -78,30 +78,29 @@ std::istream& operator >> (std::istream& in, std::string& str) template std::istream& operator >> (std::istream& in, std::vector& vec) { - while (in.good() && std::isspace(in.peek())) in.get(); + __eat_whitespace(in); int c; if (in.good() && (c = in.get()) == '{') { - while (in.good() && std::isspace(in.peek())) in.get(); - T t; + __eat_whitespace(in); vec.clear(); while (in.good() && (c = in.get()) != '}') { if (c != ',') in.putback(c); + + T t; in >> t; + __eat_whitespace(in); + vec.push_back(t); - while (in.good() && std::isspace(in.peek())) in.get(); } } - else - { - in.putback(c); - } return in; } + template bool __equals(const T& actual, const T& expected) { @@ -125,11 +124,17 @@ bool __equals(double actual, double expected) bool __equals(const std::vector& actual, const std::vector& expected) { if (actual.size() != expected.size()) + { return false; + } for (size_t i = 0; i < actual.size(); ++i) + { if (!__equals(actual[i], expected[i])) + { return false; + } + } return true; } @@ -137,58 +142,66 @@ bool __equals(const std::vector& actual, const std::vector& expe int main(int argc, char* argv[]) { - bool __exit_on_fail = false; + bool __abort_on_fail = false; int __pass = 0; int __fail = 0; - - if (1 < argc) __exit_on_fail = true; - + + if (1 < argc) __abort_on_fail = true; + + std::cout << "TAP version 13" << std::endl; + std::cout.flush(); + std::ifstream __in("testcases.txt"); for(;;) { + int __testnum = __pass + __fail + 1; + $RETURNTYPE$ __expected; $METHODPARAMDECLARES$ __in >> __expected >> $METHODPARAMSTREAMIN$; if (!__in.good()) break; - - std::cout << "----------------------------------------" << std::endl - << "Test " << (__pass + __fail) << ": "; + + std::cout << "# input for test " << __testnum << ": " << $METHODPARAMSTREAMOUT$ << std::endl; std::cout.flush(); - + __timer_start(); - - $CLASSNAME$ object; - $RETURNTYPE$ __actual = object.$METHODNAME$($METHODPARAMNAMES$); - + + $CLASSNAME$ __object; + $RETURNTYPE$ __actual = __object.$METHODNAME$($METHODPARAMNAMES$); + double __t = __timer_stop(); - + + std::cout << "# test completed in " << __t << " seconds" << std::endl; + std::cout.flush(); + if (__equals(__actual, __expected)) { - std::cout << "[PASS] in " << __t << " seconds." << std::endl; + std::cout << "ok"; ++__pass; } else { - std::cout << "[FAIL] in " << __t << " seconds." << std::endl - << "-> Input: " << $METHODPARAMSTREAMOUT$ << std::endl - << " Actual: " << __actual << std::endl - << " Expected: " << __expected << std::endl; + std::cout << "not ok"; ++__fail; - if (__exit_on_fail) exit(1); } + + std::cout << " " << __testnum << " - " << __actual << " must equal " << __expected << std::endl; + std::cout.flush(); + + if (__abort_on_fail && 0 < __fail) std::abort(); } - std::cout << "========================================" << std::endl - << " Total Pass: " << __pass << std::endl - << " Total Fail: " << __fail << std::endl; + std::cout << "1.." << (__pass + __fail) << std::endl + << "# passed: " << __pass << std::endl + << "# failed: " << __fail << std::endl; if (__fail == 0) { - std::cout << std::endl << "Nice! " - << "Don't forget to compile remotely before submitting." - << std::endl; + std::cout << std::endl + << "# Nice! Don't forget to compile remotely before submitting." << std::endl; } return __fail; } +// vim:ft=cpp:noet:ts=8