|
1 | 1 |
|
2 | 2 | #include "$CLASSNAME$.cc"
|
3 | 3 |
|
| 4 | +#include <algorithm> |
| 5 | +#include <cmath> |
4 | 6 | #include <cstdlib>
|
5 | 7 | #include <fstream>
|
6 | 8 | #include <iostream>
|
|
12 | 14 | using namespace std;
|
13 | 15 |
|
14 | 16 |
|
| 17 | +const static double __EPSILON = 1e-9; |
15 | 18 | static double __time = 0.0;
|
16 | 19 |
|
17 | 20 | static void __timer_start()
|
@@ -99,6 +102,38 @@ std::istream& operator >> (std::istream& in, std::vector<T>& vec)
|
99 | 102 | return in;
|
100 | 103 | }
|
101 | 104 |
|
| 105 | +template <class T> |
| 106 | +bool __equals(const T& actual, const T& expected) |
| 107 | +{ |
| 108 | + return actual == expected; |
| 109 | +} |
| 110 | + |
| 111 | +bool __equals(double actual, double expected) |
| 112 | +{ |
| 113 | + if (std::abs(actual - expected) < __EPSILON) |
| 114 | + { |
| 115 | + return true; |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + double minimum = std::min(expected * (1.0 - __EPSILON), expected * (1.0 + __EPSILON)); |
| 120 | + double maximum = std::max(expected * (1.0 - __EPSILON), expected * (1.0 + __EPSILON)); |
| 121 | + return actual > minimum && actual < maximum; |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +bool __equals(const std::vector<double>& actual, const std::vector<double>& expected) |
| 126 | +{ |
| 127 | + if (actual.size() != expected.size()) |
| 128 | + return false; |
| 129 | + |
| 130 | + for (size_t i = 0; i < actual.size(); ++i) |
| 131 | + if (!__equals(actual[i], expected[i])) |
| 132 | + return false; |
| 133 | + |
| 134 | + return true; |
| 135 | +} |
| 136 | + |
102 | 137 |
|
103 | 138 | int main(int argc, char* argv[])
|
104 | 139 | {
|
@@ -127,7 +162,7 @@ int main(int argc, char* argv[])
|
127 | 162 |
|
128 | 163 | double __t = __timer_stop();
|
129 | 164 |
|
130 |
| - if (__actual == __expected) |
| 165 | + if (__equals(__actual, __expected)) |
131 | 166 | {
|
132 | 167 | std::cout << "[PASS] in " << __t << " seconds." << std::endl;
|
133 | 168 | ++__pass;
|
|
0 commit comments