mirror of
https://github.com/romixlab/qmsgpack.git
synced 2025-07-30 02:17:15 +02:00
@ -19,7 +19,10 @@ QReadWriteLock MsgPackPrivate::packers_lock;
|
||||
quint8 *MsgPackPrivate::pack(const QVariant &v, quint8 *p, bool wr, QVector<QByteArray> &user_data)
|
||||
{
|
||||
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);
|
||||
@ -49,6 +52,13 @@ quint8 *MsgPackPrivate::pack(const QVariant &v, quint8 *p, bool wr, QVector<QByt
|
||||
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) {
|
||||
|
@ -25,6 +25,7 @@ extern bool compatibilityMode;
|
||||
|
||||
quint8 * pack(const QVariant &v, quint8 *p, bool wr, QVector<QByteArray> &user_data);
|
||||
|
||||
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);
|
||||
|
@ -67,7 +67,7 @@ quint8 *MsgPackPrivate::unpack_type(QVariant &v, quint8 *p)
|
||||
quint8 * MsgPackPrivate::unpack_nil(QVariant &v, quint8 *p)
|
||||
{
|
||||
Q_UNUSED(p)
|
||||
Q_UNUSED(v)
|
||||
v = QVariant();
|
||||
return p + 1;
|
||||
}
|
||||
|
||||
|
@ -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