mirror of
https://github.com/romixlab/qmsgpack.git
synced 2025-07-29 18:07:16 +02:00
Nil packer and unittests pack/unpack for nil
This commit is contained in:
@ -15,7 +15,10 @@ bool MsgPackPrivate::compatibilityMode = false;
|
||||
quint8 *MsgPackPrivate::pack(const QVariant &v, quint8 *p, bool wr)
|
||||
{
|
||||
QMetaType::Type t = (QMetaType::Type)v.type();
|
||||
if (t == QMetaType::Int)
|
||||
|
||||
if (v.isNull())
|
||||
p = pack_nil(p, wr);
|
||||
else if (t == QMetaType::Int)
|
||||
p = pack_int(v.toInt(), p, wr);
|
||||
else if (t == QMetaType::UInt)
|
||||
p = pack_uint(v.toUInt(), p, wr);
|
||||
@ -47,6 +50,13 @@ quint8 *MsgPackPrivate::pack(const QVariant &v, quint8 *p, bool wr)
|
||||
return p;
|
||||
}
|
||||
|
||||
quint8 *MsgPackPrivate::pack_nil(quint8 *p, bool wr)
|
||||
{
|
||||
if (wr)
|
||||
*p = 0xc0;
|
||||
return p + 1;
|
||||
}
|
||||
|
||||
quint8 *MsgPackPrivate::pack_int(qint32 i, quint8 *p, bool wr)
|
||||
{
|
||||
if (i >= -32 && i <= 127) {
|
||||
|
@ -23,6 +23,7 @@ extern bool compatibilityMode;
|
||||
|
||||
quint8 * pack(const QVariant &v, quint8 *p, bool wr);
|
||||
|
||||
quint8 * pack_nil(quint8 *p, bool wr);
|
||||
quint8 * pack_int(qint32 i, quint8 *p, bool wr);
|
||||
quint8 * pack_uint(quint32 i, quint8 *p, bool wr);
|
||||
quint8 * pack_longlong(qint64 i, quint8 *p, bool wr);
|
||||
|
@ -9,6 +9,7 @@ class PackTest : public QObject
|
||||
Q_OBJECT
|
||||
|
||||
private Q_SLOTS:
|
||||
void test_nil();
|
||||
void test_bool();
|
||||
void test_fixint();
|
||||
void test_integer8();
|
||||
@ -21,6 +22,14 @@ private Q_SLOTS:
|
||||
void test_array();
|
||||
};
|
||||
|
||||
void PackTest::test_nil()
|
||||
{
|
||||
QByteArray arr = MsgPack::pack(QVariant());
|
||||
quint8 *p = (quint8 *)arr.data();
|
||||
QVERIFY(arr.size() == 1);
|
||||
QVERIFY(p[0] == 0xc0);
|
||||
}
|
||||
|
||||
void PackTest::test_bool()
|
||||
{
|
||||
QByteArray arr = MsgPack::pack(false);
|
||||
|
@ -9,6 +9,7 @@ class UnpackTest : public QObject
|
||||
Q_OBJECT
|
||||
|
||||
private Q_SLOTS:
|
||||
void test_nil();
|
||||
void test_booleans();
|
||||
void test_integers();
|
||||
void test_floats();
|
||||
@ -17,6 +18,13 @@ private Q_SLOTS:
|
||||
void test_array();
|
||||
};
|
||||
|
||||
void UnpackTest::test_nil()
|
||||
{
|
||||
QByteArray pack = MsgPack::pack(QVariantList() << true << QVariant());
|
||||
QVariantList u = MsgPack::unpack(pack).toList();
|
||||
QVERIFY(u[0] == true);
|
||||
QVERIFY(u[1].isNull());
|
||||
}
|
||||
|
||||
void UnpackTest::test_booleans()
|
||||
{
|
||||
|
Reference in New Issue
Block a user