X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=src%2Fcom%2Fdogcows%2Fresources%2FC%2B%2BDriver;h=9ca857968510527b6388536028a3a67e6dbf0a2d;hb=540c07677f84c2680c808ecfb6be4ad6bb61237e;hp=718ced40bbce7d119631781e0779d426ace63ded;hpb=70fd7817176222991ace7906ecfd633c9b3eb272;p=chaz%2Fvimcoder diff --git a/src/com/dogcows/resources/C++Driver b/src/com/dogcows/resources/C++Driver index 718ced4..9ca8579 100644 --- a/src/com/dogcows/resources/C++Driver +++ b/src/com/dogcows/resources/C++Driver @@ -1,6 +1,8 @@ #include "$CLASSNAME$.cc" +#include +#include #include #include #include @@ -12,6 +14,7 @@ using namespace std; +const static double __EPSILON = 1e-9; static double __time = 0.0; static void __timer_start() @@ -90,7 +93,6 @@ std::istream& operator >> (std::istream& in, std::vector& vec) vec.push_back(t); while (in.good() && std::isspace(in.peek())) in.get(); } - } else { @@ -100,12 +102,44 @@ std::istream& operator >> (std::istream& in, std::vector& vec) return in; } +template +bool __equals(const T& actual, const T& expected) +{ + return actual == expected; +} + +bool __equals(double actual, double expected) +{ + if (std::abs(actual - expected) < __EPSILON) + { + return true; + } + else + { + double minimum = std::min(expected * (1.0 - __EPSILON), expected * (1.0 + __EPSILON)); + double maximum = std::max(expected * (1.0 - __EPSILON), expected * (1.0 + __EPSILON)); + return actual > minimum && actual < maximum; + } +} + +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; +} + int main(int argc, char* argv[]) { bool __exit_on_fail = false; - int __pass = 0; - int __fail = 0; + int __pass = 0; + int __fail = 0; if (1 < argc) __exit_on_fail = true; @@ -118,7 +152,7 @@ int main(int argc, char* argv[]) if (!__in.good()) break; std::cout << "----------------------------------------" << std::endl - << "Test " << (__pass + __fail) << ": "; + << "Test " << (__pass + __fail) << ": "; std::cout.flush(); __timer_start(); @@ -128,7 +162,7 @@ int main(int argc, char* argv[]) double __t = __timer_stop(); - if (__actual == __expected) + if (__equals(__actual, __expected)) { std::cout << "[PASS] in " << __t << " seconds." << std::endl; ++__pass; @@ -136,23 +170,23 @@ int main(int argc, char* argv[]) else { std::cout << "[FAIL] in " << __t << " seconds." << std::endl - << "-> Input: " << $METHODPARAMSTREAMOUT$ << std::endl - << " Actual: " << __actual << std::endl - << " Expected: " << __expected << std::endl; + << "-> Input: " << $METHODPARAMSTREAMOUT$ << std::endl + << " Actual: " << __actual << std::endl + << " Expected: " << __expected << std::endl; ++__fail; if (__exit_on_fail) exit(1); } } std::cout << "========================================" << std::endl - << " Total Pass: " << __pass << std::endl - << " Total Fail: " << __fail << 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; + << "Don't forget to compile remotely before submitting." + << std::endl; } return __fail;