BOOST_CHECK_EQUAL_COLLECTIONS在Google测试中

问题描述:

我一直在试图在Google C++ Testing Framework/gtest中找到断言,这相当于在Boost Test Library中找到的BOOST_CHECK_EQUAL_COLLECTIONS断言。BOOST_CHECK_EQUAL_COLLECTIONS在Google测试中

但是;没有成功。所以我的问题是双重的:

  1. gtest是否有一个等效的断言?
  2. 如果不是:如何在gtest中声明容器内容?

EDIT(略作修改答案):

#include <iostream> 

template<typename LeftIter, typename RightIter> 
::testing::AssertionResult CheckEqualCollections(LeftIter left_begin, 
               LeftIter left_end, 
               RightIter right_begin) 
{ 
    std::stringstream message; 
    std::size_t index(0); 
    bool equal(true); 

    for(;left_begin != left_end; left_begin++, right_begin++) { 
     if (*left_begin != *right_begin) { 
      equal = false; 
      message << "\n Mismatch in position " << index << ": " << *left_begin << " != " << *right_begin; 
     } 
     ++index; 
    } 
    if (message.str().size()) { 
     message << "\n"; 
    } 
    return equal ? ::testing::AssertionSuccess() : 
        ::testing::AssertionFailure() << message.str(); 
} 
+0

我想你需要看看姊妹项目:Google Mock。 – 2013-03-16 11:22:04

我不知道一个完全等同GTEST断言。但是,下面的功能应该提供类似的功能:

template<typename LeftIter, typename RightIter> 
::testing::AssertionResult CheckEqualCollections(LeftIter left_begin, 
               LeftIter left_end, 
               RightIter right_begin) { 
    bool equal(true); 
    std::string message; 
    std::size_t index(0); 
    while (left_begin != left_end) { 
    if (*left_begin++ != *right_begin++) { 
     equal = false; 
     message += "\n\tMismatch at index " + std::to_string(index); 
    } 
    ++index; 
    } 
    if (message.size()) 
    message += "\n\t"; 
    return equal ? ::testing::AssertionSuccess() : 
       ::testing::AssertionFailure() << message; 
} 

using a function that returns an AssertionResult的章节介绍如何使用此全部细节,但它会是这样的:

EXPECT_TRUE(CheckEqualCollections(collection1.begin(), 
            collection1.end(), 
            collection2.begin())); 

亚历克斯指出,GTEST有一个名为谷歌模拟一个姊妹项目,它具有优良的设施比较两个容器:

EXPECT_THAT(actual, ContainerEq(expected)); 
// ElementsAre accepts up to ten parameters. 
EXPECT_THAT(actual, ElementsAre(a, b, c)); 
EXPECT_THAT(actual, ElementsAreArray(array)); 

您将在获取更多210。