user1761982
user1761982

Reputation: 143

How to write gtest unittest for my own classes in c++

I have defined my own class DoubleMatrix in C++. How can I write gtest unittests for it with different error messages, e.g. dimension mismatch or number of mismatches?

I need to realize smth like this code

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

but it should be calling like this:

DoubleMatrix a, b;
EXPECT_EQ(a, b) 

or

DoubleMatrix a, b;
double epsilon = 0.0001;
EXPECT_NEAR(a, b, epsilon)

Upvotes: 0

Views: 970

Answers (1)

abnvp
abnvp

Reputation: 1027

You can define custom predicates to do the same.

You can check https://github.com/google/googletest/blob/master/googletest/docs/advanced.md for details. (check Predicate Assertions for Better Error Messages section in the link)

For example, you can have a function:

bool foo(DoubleMatrix a, DoubleMatrix b) {
// do the comparison and return true or false }

Use this via EXPECT_PRED2(foo, a, b);

Upvotes: 1

Related Questions