1

我正在尝试使用 QtTest 设置一些单元测试,并且我想使用 QFETCH。

我正在测试以下功能:

static std::vector<bool> FrameHandler::getBitsFromFrame(unsigned char* data, unsigned char length);

它只是将 char 数组转换为位向量。

所以,我这样设置我的测试类:

#include <QtTest/QtTest>
#include <vector>
#include "FrameHandler.h"

class BusTester : public QObject {
    Q_OBJECT

private slots:

    void bytesToVector_data() {
        QTest::addColumn<unsigned char[]>("Tested");
        QTest::addColumn<unsigned char>("Length");
        QTest::addColumn< std::vector<bool> >("Reference");

        // Test for one byte
        std::vector<bool> oneByte(8, false);
        oneByte[0] = true;
        oneByte[1] = true;
        oneByte[3] = true;
        oneByte[4] = true;

        unsigned char oneByteInput[1]{
            0b11011000
        };

        QTest::newRow("One byte") << oneByteInput << 1 << oneByte;
    }

    void bytesToVector() {
        QFETCH(unsigned char[], tested);
        QFETCH(unsigned char, length);
        QFETCH(std::vector<bool>, reference);

        QCOMPARE(FrameHandler::getBitsFromFrame(tested, length), reference);
    }
};

QTEST_MAIN(BusTester)
#include "bustester.moc"

当我这样做时,编译器会说:

expected unqualified-id before ‘[’ token
         QFETCH(unsigned char[], tested);

并且 :

On line `QTest::addColumn<unsigned char[]>("Tested");`, Type is not registered, please use the Q_DECLARE_METATYPE macro to make it known to Qt's meta-object system

我认为这两个错误是相关的,所以我Q_DECLARE_METATYPE(unsigned char[]);在类声明之前添加了,但后来我得到了这个:

qmetatype.h'*' 标记之前的预期 '>' (第 1695 行)

是否可以向unsigned char[]Qt 的 QMetaType 系统声明?谢谢

4

1 回答 1

2

Q_DECLARE_METATYPE(T) 类型 T 必须是可构造的、可复制的和可破坏的。数组不符合此规则,但您可以创建包装器。

struct Arr
{
    unsigned char arr[SIZE];
};
Q_DECLARE_METATYPE( Arr );

或者

typedef std::array<unsigned char, SIZE> TArr;
Q_DECLARE_METATYPE( TArr );

但是有一个困难 - SIZE,你必须 - 声明它

于 2017-08-11T14:35:22.050 回答