I'm using the Catch unit testing framework, and I'd like to compare a vector of doubles. This other answer suggests using Approx to compare floating point/double values, but this doesn't work for a vector of them. Is there any convenient way of accomplishing this?
EDIT: An Example
With the following code:
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
TEST_CASE("Compare Vectors", "[vector]") {
std::vector<int> vec1 = {0, 1, 2, 3};
std::vector<int> vec2 = {0, 1, 2, 4};
REQUIRE(vec1 == vec2);
}
The test fails with following report:
-------------------------------------------------------------------------------
Compare Vectors
-------------------------------------------------------------------------------
test/UnitTests/test_Example/example.cc:4
...............................................................................
test/UnitTests/test_Example/example.cc:7: FAILED:
REQUIRE( vec1 == vec2 )
with expansion:
{ 0, 1, 2, 3 } == { 0, 1, 2, 4 }
===============================================================================
test cases: 1 | 1 failed
assertions: 1 | 1 failed
But if I change the code as follows, I would want the test to pass, but obviously it doesn't.
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
TEST_CASE("Compare Vectors", "[vector]") {
std::vector<double> vec1 = {0, 1, 2, 3};
std::vector<double> vec2 = {0, 1, 2, 3.000001};
REQUIRE(vec1 == vec2);
}
I could loop through the elements and compare them one-by-one, but in the event of a discrepancy, it will be more difficult to determine the source of the error.