Google test compare vector C++

Asked

Viewed 40 times

0

I’m building a project and trying to use google test to compare the result of some functions. The problem is that my functions return double vectors and I can only compare double or whole in google test.

i try to make a:

EXPECT_EQ(resposta, FUNC_lib::litopar(ZZIN,AMASS,MULTPL,frac,dens,checawv));

In which answer is a double vector and Func_lib returns a double vector as well. But it automatically generates the following error:

a nonstatic member reference must be relative to a specific object.

1 answer

2


As far as I know, Googletest does not have a single function/macro to compare vectors. But in documentation of them has an example of how to do this:

ASSERT_EQ(x.size(), y.size()) << "Vectors x and y are of unequal length";

for (int i = 0; i < x.size(); ++i) {
   EXPECT_EQ(x[i], y[i]) << "Vectors x and y differ at index " << i;
}

I think you have to do something like this.

P.S.: As in your case you are comparing real numbers (floating point), it is best to use one of the macros EXPECT_DOUBLE_EQ or EXPECT_NEAR, documented here. For example, the code can look like this

ASSERT_EQ(x.size(), y.size()) << "Vectors x and y are of unequal length";

for (int i = 0; i < x.size(); ++i) {
   EXPECT_DOUBLE_EQ(x[i], y[i]) << "Vectors x and y differ at index " << i;
   // ou então o seguinte (para usar uma tolerância de comparação de 1e-4):
   // EXPECT_NEAR(x[i], y[i], 1.e-4) << "Vectors x and y differ at index " << i;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.