LukesDiner
LukesDiner

Reputation: 155

How do I check the contents in a vector using google-test?

std::vector<uint8_t> vector1(10);
std::vector<uint8_t> vector2(10);

std::fill(vector1.begin(), vector1.end(), 2);
std::fill(vector2.begin(), vector2.end(), 2);

EXPECT_EQ(vector1, vector2);

Does the EXPECT_EQ() above check that the contents in vector1 and vector2 are equal? If not, how do I check that that the contents in vector1 and vector2 are equal using a googletest EXPECT_* function?

The documentation for googletest explains how to test the contents of C strings and c++ string objects, but not how to check the contents of a c++ vector.

Upvotes: 1

Views: 4650

Answers (2)

Alex
Alex

Reputation: 671

The googletest does explain how to compare containers (such as a vector) in the matchers section.

The matcher ContainerEq(container) would probably do what you're after. So something similar to EXPECT_THAT(firstVector, testing::ContainerEq(secondVector)); might do the trick.

Upvotes: 0

Yksisarvinen
Yksisarvinen

Reputation: 22269

EXPECT_EQ uses operator == to compare objects, and operator == for std::vector (quote from cppreference):

Checks if the contents of lhs and rhs are equal, that is, they have the same number of elements and each element in lhs compares equal with the element in rhs at the same position.

So the answer is yes, it will compare the contents of the vectors.

Upvotes: 8

Related Questions