Here is the class I used as a base for creating a couple of tests for std::equal():
class EqualTest : public ::testing::Test
{
protected:
EqualTest()
{
vi_.reserve(10);
vc_.reserve(10);
for(int i = 0; i < 10; ++i)
vi_.push_back(i * i);
std::copy(vi_.begin(), vi_.end(), std::back_inserter(vc_));
}
std::vector<int> vi_;
std::vector<int> vc_;
};
In the first test I show the normal usage, we simply pass to equal() the beginning/end iterators for the first sequence, and just the beginning of the second one. Notice that before using equal() I also made a test on the vector sizes, to ensure we don't risk a crash while comparing:
TEST_F(EqualTest, Same)
{
ASSERT_GE(vi_.size(), vc_.size());
EXPECT_TRUE(std::equal(vi_.begin(), vi_.end(), vc_.begin()));
}
The second test is more interesting, since we use a custom comparison function (actually, a lambda function) to force std::equal() to behave how we want:
TEST_F(EqualTest, Lambda)
{
const int step = 3;
std::for_each(vi_.begin(), vi_.end(), [step](int& i){ i += step; });
ASSERT_GE(vi_.size(), vc_.size());
EXPECT_TRUE(std::equal(vi_.begin(), vi_.end(), vc_.begin(),
[step](int i, int j) { return i == j + step; } ));
}
No comments:
Post a Comment