3

I have a template function for serializing data to a file and another to retrieve it back using the ID returned by the fist function:

template<typename T> int serialize(const T &value); //returns id
template<typename T> T deserialize(int id);

Now I want to create a unit test for these with number of types using QTest framework, the relevant incomplete private slots:

void serialization_data()
{
    QTest::addColumn<???>("data");
    QTest::addColumn<int>("id"); //id is assigned sequentially starting from 0

    QTest::newRow("row1") << QByteArray("Array") << 0;
    QTest::newRow("row2") << QString("String") << 1;
    // etc. 
}

void serialization()
{
    QFETCH(???, data);
    QFETCH(int, id);

    QVERIFY(serialize(data) == id);
    QVERIFY(deserialize<???>(id) == data);
}

My question basically is what should be the substitue of ???.

Spelling it out

Obvious solution for completeness sake. I am specifically trying to find a solution that would not require typing it all out.

QVariant

One workaround is using QVariant. The problems with that are many. First it is rather messy, even when using Qt types because things like QVector<qint64> require registering them to the meta system, registering their streaming operators AND registering their comparison operators... Furthermore it adds serialization of the QVariant itself to the mix and serializing custom types in QVariant apparently involves some further data written than the types themselves. It certainly is not just what the docs say it should be for QVariant - I guess it might have to do with the meta system and its special streaming methods that are invoked when serializing custom types in QVariant.

void* and dynamic_cast?

Another option I thought of was using typeid's result type_info along with void*. The ??? would be replaced by void* in the _data and another column containing the const type_info* would be provided so that the test method could convert it to the proper type and dereference it. But that is unfortunately not possible it seems.

Are there any other options?

Community
  • 1
  • 1
Resurrection
  • 3,916
  • 2
  • 34
  • 56

0 Answers0