Compare containers with GoogleTest
Asked Answered
H

2

15

I'm trying to get a working googletest test that compares two vectors. For this I'm using google mock with its matchers but I get a C3861 error saying "ContainerEq identifier not found" and also C2512 saying "testing::AssertionResult has not a proper default constructor available". Why?

TEST(MyTestSuite, MyTest)
{
    std::vector<int> test1;
    std::vector<int> test2;

    ...

    EXPECT_THAT(test1, ContainerEq(test2));
}
Hive answered 9/9, 2012 at 15:42 Comment(1)
This question has a recent correct answer by @phetdam #phetdamDiaconate
R
36

You're just missing gtest's testing namespace qualifier:

EXPECT_THAT(test1, ::testing::ContainerEq(test2));
Rivalee answered 9/9, 2012 at 15:56 Comment(0)
I
1

Since std::vector does define operator==, why not just use EXPECT_EQ? Ex.

#include <vector>

#include <gtest/gtest.h>

namespace {

TEST(MyTestSuite, MyTest)
{
  std::vector<double> a = {1, 2};
  std::vector<double> b = {1, 2};
  EXPECT_EQ(a, b);
}

}  // namespace

This works just fine. Although I mostly use C++17, std::vector definitely predates C++11.

For any of your own custom Container types, define your own operator==.

Internationalize answered 5/8, 2022 at 4:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.