I want to check if the elements in two containers of different types are the same. However, when I use the ContainerEq matcher of the GoogleTest library it gives me an error that it can not initialize the vector from the array:
TEST(TestExample, Example) {
std::array<int, 4> ar = {1,2,3,4};
std::vector<int> v(ar.begin(), ar.end());
EXPECT_THAT(ar, ::testing::ContainerEq(v));
}
Error message:
error: invalid initialization of reference of type 'testing::internal::ContainerEqMatcher<std::vector<int> >::StlContainerReference' {aka 'const std::vector<int>&'} from expression of type 'const type' {aka 'const std::array<int, 4>'}
What options do I have to check for equality in different containers except for switching to EXPECT_TRUE and using std::equal?
>Solution :
You can compare elements of different containers using ::testing::ElementsAreArray(v):
TEST(TestExample, Example) {
std::array<int, 4> ar = {1,2,3,4};
std::vector<int> v(ar.begin(), ar.end());
EXPECT_THAT(ar, ::testing::ElementsAreArray(v));
}