3

CPPUnit 是否有任何功能可以让我进行printf-style 断言?例如:

CPPUNIT_ASSERT("Actual size: %d", p->GetSize(), p->GetSize() == 0);

我知道这不是有效的 CPPUNIT_ASSERT - 我只是以此为例。

我发现CPPUNIT_ASSERT_MESSAGE(message,condition)which 需要一个字符串,然后是要评估的条件,但没有运气将值放入断言中。

4

4 回答 4

6

你应该能够做这样的事情:

#define CPPUNIT_ASSERT_STREAM(MSG, CONDITION) \
    do { \
        std::ostringstream oss; \
        CPPUNIT_ASSERT_MESSAGE(\
            static_cast<std::ostringstream &>(oss << MSG).str(), \
            CONDITION); \
    } while (0)

CPPUNIT_ASSERT_STREAM("Actual size: " << p->GetSize(), p->GetSize() == 0);

上述宏也可以与Boost.format结合使用:

CPPUNIT_ASSERT_STREAM(boost::format("Actual size: %d") % p->GetSize(),
                      p->GetSize() == 0);
于 2013-08-19T17:13:05.660 回答
0

请使用CPPUNIT_ASSERT_EQUAL,它取实际值和预期值。这几乎消除了格式化字符串的需要,因为实际值和预期值将打印在失败消息中。

您还可以复制此宏的实现及其调用的函数,并为其他类型的比较添加其他宏。我尝试的另一个想法是使用运算符重载从表达式中捕获值。这个 (ab) 使用运算符重载来捕获表达式,但它似乎有点太 hackish。我将它包含在内是为了让您了解可能的情况,但不建议使用它:

#include <iostream>
#include <sstream>

class ExpressionPrinter
{
public:
  std::ostringstream result;

  template<typename T> ExpressionPrinter& operator<<(const T& other)
  {
    result << other;
    return *this;
  }

  template<typename T> ExpressionPrinter& operator<(const T& other)
  {
    result << " < ";
    result << other;
    return *this;
  }

  template<typename T> ExpressionPrinter& operator<=(const T& other)
  {
    result << " <= ";
    result << other;
    return *this;
  }

  template<typename T> ExpressionPrinter& operator>(const T& other)
  {
    result << " > ";
    result << other;
    return *this;
  }

  template<typename T> ExpressionPrinter& operator>=(const T& other)
  {
    result << " >= ";
    result << other;
    return *this;
  }

  template<typename T> ExpressionPrinter& operator==(const T& other)
  {
    result << " == ";
    result << other;
    return *this;
  }

  template<typename T> ExpressionPrinter& operator!=(const T& other)
  {
    result << " != ";
    result << other;
    return *this;
  }
};

#define ASSERT(X) doAssert((X), (ExpressionPrinter() << X));

void doAssert(bool result, const ExpressionPrinter& message)
{
  std::cout << "Result: " << result << ", Expression: " << message.result.str() << std::endl;
}

int main()
{
  int i = 1, j = 2;

  ASSERT(i < j);

  return 0;
}
于 2013-08-19T17:00:36.180 回答
0

我使用自定义断言打印所有需要的信息:

#ifdef NDEBUG
#define ASSERT(v, msg)
#else
#define ASSERT(v, msg) \
if (v) {} else { \
  std::cerr << __FILE__ << ":" << __LINE__ << " assertion failed: " \
            << #v << " = " << (v) << "\n" << msg << std::endl; \
  abort(); \
}
#endif

要使用:

#include <iostream>
...
ASSERT( p->GetSize() == 0, p->GetSize() );

或者

ASSERT( p->GetSize() == 0, "Actual size: " << p->GetSize() << " Actual size * 2: " << p->GetSize()*2 );
于 2013-08-19T17:01:26.270 回答
-1
#include <iostream>
...
ASSERT( p->GetSize() == 0, p->GetSize() );
于 2013-08-19T18:49:41.263 回答