mirror of
https://github.com/romixlab/qmsgpack.git
synced 2025-08-04 04:34:27 +02:00
Work on QTime, QDate, QDateTime, QSize, QPoint, QRect
unpack_upto_<integer> added for user packer / unpacker functions
This commit is contained in:
@@ -8,7 +8,7 @@ if (Qt5Core_FOUND)
|
||||
set(TEST_LIBRARIES ${Qt5Test_LIBRARIES})
|
||||
endif ()
|
||||
|
||||
set(TEST_SUBDIRS pack unpack mixed)
|
||||
set(TEST_SUBDIRS pack unpack mixed qttypes ext)
|
||||
|
||||
foreach(subdir ${TEST_SUBDIRS})
|
||||
add_subdirectory(${subdir})
|
||||
|
24
tests/ext/CMakeLists.txt
Normal file
24
tests/ext/CMakeLists.txt
Normal file
@@ -0,0 +1,24 @@
|
||||
set(QT_USE_QTTEST TRUE)
|
||||
|
||||
if (NOT Qt5Core_FOUND)
|
||||
include( ${QT_USE_FILE} )
|
||||
endif()
|
||||
|
||||
include(AddFileDependencies)
|
||||
|
||||
include_directories(../../src ${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
||||
set(UNIT_TESTS ext_test)
|
||||
|
||||
foreach(test ${UNIT_TESTS})
|
||||
message(status "Building ${test}")
|
||||
add_executable(${test} ${test}.cpp)
|
||||
|
||||
target_link_libraries(${test}
|
||||
${QT_LIBRARIES}
|
||||
${TEST_LIBRARIES}
|
||||
qmsgpack
|
||||
)
|
||||
|
||||
add_test(${test} ${test})
|
||||
endforeach()
|
149
tests/ext/ext_test.cpp
Normal file
149
tests/ext/ext_test.cpp
Normal file
@@ -0,0 +1,149 @@
|
||||
#include <QString>
|
||||
#include <QtTest>
|
||||
#include <QDebug>
|
||||
#include <msgpack.h>
|
||||
#include <limits>
|
||||
#include <msgpack_ext.h>
|
||||
|
||||
class ExtText : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
private Q_SLOTS:
|
||||
void test_upto_quint();
|
||||
void test_upto_qint();
|
||||
void test_upto_qint_to_quint();
|
||||
void test_fail();
|
||||
};
|
||||
|
||||
void ExtText::test_upto_quint()
|
||||
{
|
||||
QByteArray packed;
|
||||
bool success;
|
||||
quint8 u8;
|
||||
quint16 u16;
|
||||
quint32 u32;
|
||||
quint64 u64;
|
||||
|
||||
packed = MsgPack::pack(0);
|
||||
MsgPack::Ext::unpack_upto_quint8(&u8, (quint8 *)packed.data(), &success);
|
||||
QVERIFY(u8 == 0);
|
||||
QVERIFY(success);
|
||||
MsgPack::Ext::unpack_upto_quint16(&u16, (quint8 *)packed.data(), &success);
|
||||
QVERIFY(u16 == 0);
|
||||
QVERIFY(success);
|
||||
MsgPack::Ext::unpack_upto_quint32(&u32, (quint8 *)packed.data(), &success);
|
||||
QVERIFY(u32 == 0);
|
||||
QVERIFY(success);
|
||||
MsgPack::Ext::unpack_upto_quint64(&u64, (quint8 *)packed.data(), &success);
|
||||
QVERIFY(u64 == 0);
|
||||
QVERIFY(success);
|
||||
|
||||
packed = MsgPack::pack(std::numeric_limits<quint8>::max());
|
||||
MsgPack::Ext::unpack_upto_quint8(&u8, (quint8 *)packed.data(), &success);
|
||||
QVERIFY(u8 == std::numeric_limits<quint8>::max());
|
||||
QVERIFY(success);
|
||||
|
||||
packed = MsgPack::pack(std::numeric_limits<quint16>::max());
|
||||
MsgPack::Ext::unpack_upto_quint16(&u16, (quint8 *)packed.data(), &success);
|
||||
QVERIFY(u16 == std::numeric_limits<quint16>::max());
|
||||
QVERIFY(success);
|
||||
|
||||
packed = MsgPack::pack(std::numeric_limits<quint32>::max());
|
||||
MsgPack::Ext::unpack_upto_quint32(&u32, (quint8 *)packed.data(), &success);
|
||||
QVERIFY(u32 == std::numeric_limits<quint32>::max());
|
||||
QVERIFY(success);
|
||||
|
||||
packed = MsgPack::pack(std::numeric_limits<quint64>::max());
|
||||
MsgPack::Ext::unpack_upto_quint64(&u64, (quint8 *)packed.data(), &success);
|
||||
QVERIFY(u64 == std::numeric_limits<quint64>::max());
|
||||
QVERIFY(success);
|
||||
}
|
||||
|
||||
void ExtText::test_upto_qint()
|
||||
{
|
||||
QByteArray packed;
|
||||
bool success;
|
||||
qint8 i8;
|
||||
qint16 i16;
|
||||
qint32 i32;
|
||||
qint64 i64;
|
||||
|
||||
packed = MsgPack::pack(-32);
|
||||
MsgPack::Ext::unpack_upto_qint8(&i8, (quint8 *)packed.data(), &success);
|
||||
QVERIFY(i8 == -32);
|
||||
QVERIFY(success);
|
||||
MsgPack::Ext::unpack_upto_qint16(&i16, (quint8 *)packed.data(), &success);
|
||||
QVERIFY(i16 == -32);
|
||||
QVERIFY(success);
|
||||
MsgPack::Ext::unpack_upto_qint32(&i32, (quint8 *)packed.data(), &success);
|
||||
QVERIFY(i32 == -32);
|
||||
QVERIFY(success);
|
||||
MsgPack::Ext::unpack_upto_qint64(&i64, (quint8 *)packed.data(), &success);
|
||||
QVERIFY(i64 == -32);
|
||||
QVERIFY(success);
|
||||
|
||||
packed = MsgPack::pack(std::numeric_limits<quint8>::min() - 1);
|
||||
MsgPack::Ext::unpack_upto_qint8(&i8, (quint8 *)packed.data(), &success);
|
||||
QVERIFY(i8 == std::numeric_limits<quint8>::min() - 1);
|
||||
QVERIFY(success);
|
||||
|
||||
packed = MsgPack::pack(std::numeric_limits<quint16>::min() - 1);
|
||||
MsgPack::Ext::unpack_upto_qint16(&i16, (quint8 *)packed.data(), &success);
|
||||
QVERIFY(i16 == std::numeric_limits<quint16>::min() - 1);
|
||||
QVERIFY(success);
|
||||
|
||||
packed = MsgPack::pack(std::numeric_limits<quint32>::min() - 1);
|
||||
MsgPack::Ext::unpack_upto_qint32(&i32, (quint8 *)packed.data(), &success);
|
||||
QVERIFY(i32 == std::numeric_limits<quint32>::min() - 1);
|
||||
QVERIFY(success);
|
||||
|
||||
packed = MsgPack::pack(std::numeric_limits<quint64>::min() - 1);
|
||||
MsgPack::Ext::unpack_upto_qint64(&i64, (quint8 *)packed.data(), &success);
|
||||
QVERIFY(i64 == std::numeric_limits<quint64>::min() - 1);
|
||||
QVERIFY(success);
|
||||
}
|
||||
|
||||
void ExtText::test_upto_qint_to_quint()
|
||||
{
|
||||
QByteArray packed;
|
||||
bool success;
|
||||
qint8 i8;
|
||||
qint16 i16;
|
||||
qint32 i32;
|
||||
qint64 i64;
|
||||
|
||||
packed = MsgPack::pack(std::numeric_limits<qint8>::max());
|
||||
MsgPack::Ext::unpack_upto_qint8(&i8, (quint8 *)packed.data(), &success);
|
||||
QVERIFY(i8 == std::numeric_limits<qint8>::max());
|
||||
QVERIFY(success);
|
||||
|
||||
packed = MsgPack::pack(std::numeric_limits<qint16>::max());
|
||||
MsgPack::Ext::unpack_upto_qint16(&i16, (quint8 *)packed.data(), &success);
|
||||
QVERIFY(i16 == std::numeric_limits<qint16>::max());
|
||||
QVERIFY(success);
|
||||
|
||||
packed = MsgPack::pack(std::numeric_limits<qint32>::max());
|
||||
MsgPack::Ext::unpack_upto_qint32(&i32, (quint8 *)packed.data(), &success);
|
||||
QVERIFY(i32 == std::numeric_limits<qint32>::max());
|
||||
QVERIFY(success);
|
||||
|
||||
packed = MsgPack::pack(std::numeric_limits<qint64>::max());
|
||||
MsgPack::Ext::unpack_upto_qint64(&i64, (quint8 *)packed.data(), &success);
|
||||
QVERIFY(i64 == std::numeric_limits<qint64>::max());
|
||||
QVERIFY(success);
|
||||
}
|
||||
|
||||
void ExtText::test_fail()
|
||||
{
|
||||
quint8 p[] = {0xd9};
|
||||
quint8 u8;
|
||||
bool ok;
|
||||
quint8 *p2 = MsgPack::Ext::unpack_upto_quint8(&u8, p, &ok);
|
||||
QVERIFY(!ok);
|
||||
QVERIFY(p2 - p == 0);
|
||||
}
|
||||
|
||||
QTEST_APPLESS_MAIN(ExtText)
|
||||
|
||||
#include "ext_test.moc"
|
24
tests/qttypes/CMakeLists.txt
Normal file
24
tests/qttypes/CMakeLists.txt
Normal file
@@ -0,0 +1,24 @@
|
||||
set(QT_USE_QTTEST TRUE)
|
||||
|
||||
if (NOT Qt5Core_FOUND)
|
||||
include( ${QT_USE_FILE} )
|
||||
endif()
|
||||
|
||||
include(AddFileDependencies)
|
||||
|
||||
include_directories(../../src ${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
||||
set(UNIT_TESTS qttypes_test)
|
||||
|
||||
foreach(test ${UNIT_TESTS})
|
||||
message(status "Building ${test}")
|
||||
add_executable(${test} ${test}.cpp)
|
||||
|
||||
target_link_libraries(${test}
|
||||
${QT_LIBRARIES}
|
||||
${TEST_LIBRARIES}
|
||||
qmsgpack
|
||||
)
|
||||
|
||||
add_test(${test} ${test})
|
||||
endforeach()
|
124
tests/qttypes/qttypes_test.cpp
Normal file
124
tests/qttypes/qttypes_test.cpp
Normal file
@@ -0,0 +1,124 @@
|
||||
#include <QString>
|
||||
#include <QtTest>
|
||||
#include <QDebug>
|
||||
#include <msgpack.h>
|
||||
#include <limits>
|
||||
|
||||
class QtTypesTest : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
private Q_SLOTS:
|
||||
void test_qtime();
|
||||
void test_qdate();
|
||||
void test_qpoint();
|
||||
void test_qsize();
|
||||
void test_qrect();
|
||||
};
|
||||
|
||||
void QtTypesTest::test_qtime()
|
||||
{
|
||||
MsgPack::registerType(QMetaType::QTime, 0x77);
|
||||
QTime t(0, 0, 0, 0);
|
||||
QByteArray packed = MsgPack::pack(t);
|
||||
QTime t2 = MsgPack::unpack(packed).toTime();
|
||||
QVERIFY(t == t2);
|
||||
QVERIFY(packed.size() == 4);
|
||||
|
||||
t = QTime(12, 01, 01, 0);
|
||||
packed = MsgPack::pack(t);
|
||||
t2 = MsgPack::unpack(packed).toTime();
|
||||
QVERIFY(t == t2);
|
||||
QVERIFY(packed.size() == 4);
|
||||
|
||||
t = QTime(12, 59, 59, 0);
|
||||
packed = MsgPack::pack(t);
|
||||
t2 = MsgPack::unpack(packed).toTime();
|
||||
QVERIFY(t == t2);
|
||||
QVERIFY(packed.size() == 4);
|
||||
|
||||
t = QTime(12, 34, 56, 999);
|
||||
packed = MsgPack::pack(t);
|
||||
t2 = MsgPack::unpack(packed).toTime();
|
||||
QVERIFY(t == t2);
|
||||
QVERIFY(packed.size() == 6);
|
||||
}
|
||||
|
||||
void QtTypesTest::test_qdate()
|
||||
{
|
||||
MsgPack::registerType(QMetaType::QDate, 0x78);
|
||||
QDate d;
|
||||
QByteArray packed = MsgPack::pack(d);
|
||||
QDate d2 = MsgPack::unpack(packed).toDate();
|
||||
QVERIFY(d == d2);
|
||||
QVERIFY(packed.size() == 6);
|
||||
|
||||
d = QDate(1234, 12, 1);
|
||||
packed = MsgPack::pack(d);
|
||||
d2 = MsgPack::unpack(packed).toDate();
|
||||
QVERIFY(d == d2);
|
||||
|
||||
d = QDate(9999, 1, 31);
|
||||
packed = MsgPack::pack(d);
|
||||
d2 = MsgPack::unpack(packed).toDate();
|
||||
QVERIFY(d == d2);
|
||||
}
|
||||
|
||||
void QtTypesTest::test_qpoint()
|
||||
{
|
||||
MsgPack::registerType(QMetaType::QPoint, 0x79);
|
||||
QPoint pt(1, 2);
|
||||
QByteArray packed = MsgPack::pack(pt);
|
||||
QVERIFY(packed.size() == 4);
|
||||
QPoint pt2 = MsgPack::unpack(packed).toPoint();
|
||||
QVERIFY(pt == pt2);
|
||||
|
||||
pt = QPoint(1234, 5678);
|
||||
packed = MsgPack::pack(pt);
|
||||
QVERIFY(packed.size() == 9);
|
||||
pt2 = MsgPack::unpack(packed).toPoint();
|
||||
QVERIFY(pt == pt2);
|
||||
|
||||
pt = QPoint(std::numeric_limits<qint32>::max(), std::numeric_limits<qint32>::max());
|
||||
packed = MsgPack::pack(pt);
|
||||
QVERIFY(packed.size() == 13);
|
||||
pt2 = MsgPack::unpack(packed).toPoint();
|
||||
QVERIFY(pt == pt2);
|
||||
}
|
||||
|
||||
void QtTypesTest::test_qsize()
|
||||
{
|
||||
MsgPack::registerType(QMetaType::QSize, 80);
|
||||
QSize sz(1, 2);
|
||||
QByteArray packed = MsgPack::pack(sz);
|
||||
QVERIFY(packed.size() == 4);
|
||||
QSize sz2 = MsgPack::unpack(packed).toSize();
|
||||
QVERIFY(sz == sz2);
|
||||
|
||||
sz = QSize(1234, 5678);
|
||||
packed = MsgPack::pack(sz);
|
||||
QVERIFY(packed.size() == 9);
|
||||
sz2 = MsgPack::unpack(packed).toSize();
|
||||
QVERIFY(sz == sz2);
|
||||
}
|
||||
|
||||
void QtTypesTest::test_qrect()
|
||||
{
|
||||
MsgPack::registerType(QMetaType::QRect, 81);
|
||||
QRect r(1, 2, 3, 4);
|
||||
QByteArray packed = MsgPack::pack(r);
|
||||
QVERIFY(packed.size() == 6);
|
||||
QRect r2 = MsgPack::unpack(packed).toRect();
|
||||
QVERIFY(r == r2);
|
||||
|
||||
qint32 max = std::numeric_limits<qint32>::max();
|
||||
r = QRect(0, 0, max, max);
|
||||
packed = MsgPack::pack(r);
|
||||
QVERIFY(packed.size() == 15);
|
||||
r2 = MsgPack::unpack(packed).toRect();
|
||||
QVERIFY(r == r2);
|
||||
}
|
||||
|
||||
QTEST_APPLESS_MAIN(QtTypesTest)
|
||||
|
||||
#include "qttypes_test.moc"
|
Reference in New Issue
Block a user