<QTest> 的宏:QCOMPARE(actual, expected)

目录

官方手册:<QTest> 的宏:QCOMPARE(actual, expected)

========================================

官方手册:<QTest> 的宏:QCOMPARE(actual, expected)

QTest Namespace

The QTest namespace contains all the functions and declarations that are related to Qt Test. More...

Header:
#include <QTest>
qmake:
QT += testlib

QCOMPARE(actual, expected)

The QCOMPARE() macro compares an actual value to an expected value using the equality operator. If actual and expected match, execution continues. If not, a failure is recorded in the test log and the test function returns without attempting any later checks.
Always respect QCOMPARE() parameter semantics. The first parameter passed to it should always be the actual value produced by the code-under-test, while the second parameter should always be the expected value. When the values don't match, QCOMPARE() prints them with the labels Actual and Expected. If the parameter order is swapped, debugging a failing test can be confusing.
When comparing floating-point types (float, double, and qfloat16), qFuzzyCompare() is used for finite values. Infinities match if they have the same sign, and any NaN as actual value matches with any NaN as expected value (even though NaN != NaN, even when they're identical). This means that expecting 0 can fail when the actual value may be affected by rounding errors. One solution to this is to offset both actual and expected values by adding some suitable constant (such as 1).
QCOMPARE() tries to output the contents of the values if the comparison fails, so it is visible from the test log why the comparison failed.
Example:

  QCOMPARE(QString("hello").toUpper(), QString("HELLO"));

Note: This macro can only be used in a test function that is invoked by the test framework.
For your own classes, you can use QTest::toString() to format values for outputting into the test log.
Example:

  namespace QTest {
      template <> char *toString<MyType>(const MyType &t)
      {
          char *repr = new char[t.reprSize()];
          t.writeRepr(repr);
          return repr;
      }
  }

The return from toString() must be a new char []. That is, it shall be released with delete[] (rather than free() or plain delete) once the calling code is done with it.
See also QVERIFY(), QTRY_COMPARE(), QTest::toString(), and QEXPECT_FAIL().

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值