0

Boost Test LibraryにあるBOOST_CHECK_EQUAL_COLLECTIONSアサーションと同等のアサーションをGoogle C++ Testing Framework/gtestで見つけようとしています。

でも; 成功せずに。だから私の質問は2つあります:

  1. gtestには同等のアサーションがありますか?
  2. そうでない場合: gtestで container-content をアサートするにはどうすればよいでしょうか?

編集(わずかに修正された回答):

#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();
}
4

2 に答える 2

2

Alex が指摘したように、gtest には Google Mock と呼ばれる姉妹プロジェクトがあり、2 つのコンテナーを比較するための優れた機能があります。

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

詳細については、Google Mock の wikiを参照してください。

于 2013-03-18T15:59:17.517 に答える
0

まったく同等の 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;
}

を返す関数の使用AssertionResultに関するセクションでは、これを使用する方法について詳しく説明していますが、次のようになります。

EXPECT_TRUE(CheckEqualCollections(collection1.begin(),
                                  collection1.end(),
                                  collection2.begin()));
于 2013-03-16T15:37:53.970 に答える