c++qtqttest

Declare unsigned char[] with Q_DECLARE_METATYPE


I'm trying to set up some unit tests with QtTest and i'd like to use QFETCH.

I am testing the following function :

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

It simply converts a array of char into a vector of bits.

So, I set up my test class like this :

#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"

When I do this, the compiler says :

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

And also :

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

I thought the two errors were linked, so i added Q_DECLARE_METATYPE(unsigned char[]); before the class declaration, but then I get this :

In qmetatype.h, expected '>' before '*' token (line 1695)

Is it possible to declare unsigned char[] to Qt's QMetaType system ? Thanks


Solution

  • Q_DECLARE_METATYPE(T) Type T must be constructable, copiable and destructable. Array doesn match this rules, but you can create wrapper around.

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

    or

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

    But there is one difficulties - SIZE, You have-to declare it