]> Dogcows Code - chaz/vimcoder/blobdiff - src/com/dogcows/resources/C++Driver
separate whitespace handling into its own function
[chaz/vimcoder] / src / com / dogcows / resources / C++Driver
index 142fdac9d5745d375da6329e15e810427cbd0c87..24ab333609ffa5ed8a51c9c80f0b32943fe2fb19 100644 (file)
@@ -1,6 +1,8 @@
 
 #include "$CLASSNAME$.cc"
 
+#include <algorithm>
+#include <cmath>
 #include <cstdlib>
 #include <fstream>
 #include <iostream>
@@ -12,6 +14,7 @@
 using namespace std;
 
 
+const static double __EPSILON = 1e-9;
 static double __time = 0.0;
 
 static void __timer_start()
@@ -19,7 +22,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;
        }
 }
 
@@ -31,6 +34,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() << '"';
@@ -52,7 +61,7 @@ std::ostream& operator << (std::ostream& out, const std::vector<T>& 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()) == '"')
@@ -64,10 +73,6 @@ std::istream& operator >> (std::istream& in, std::string& str)
                }
                str = s.str();
        }
-       else
-       {
-               in.putback(c);
-       }
 
        return in;
 }
@@ -75,28 +80,59 @@ std::istream& operator >> (std::istream& in, std::string& str)
 template <class T>
 std::istream& operator >> (std::istream& in, std::vector<T>& 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();
                }
        }
+
+       return in;
+}
+
+
+template <class T>
+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
        {
-               in.putback(c);
+               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;
        }
+}
 
-       return in;
+bool __equals(const std::vector<double>& actual, const std::vector<double>& 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;
 }
 
 
@@ -105,9 +141,9 @@ 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(;;)
        {
@@ -115,19 +151,19 @@ int main(int argc, char* argv[])
                $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)
+
+               if (__equals(__actual, __expected))
                {
                        std::cout << "[PASS] in " << __t << " seconds." << std::endl;
                        ++__pass;
This page took 0.026847 seconds and 4 git commands to generate.