MsgPackStream missing dev() added. Some little improvements.

This commit is contained in:
Isaikin Roman
2015-08-28 21:12:09 +03:00
parent 5be342038a
commit 4da435044d
26 changed files with 205 additions and 371 deletions

3
.qmake.conf Normal file
View File

@ -0,0 +1,3 @@
TOP_SRCDIR=$$PWD
TOP_BUILDDIR=$$shadowed($$PWD)

View File

@ -80,14 +80,14 @@ set(MSGPACK_QT_LIB_VERSION_STRING "${QMSGPACK_MAJOR}.${QMSGPACK_MINOR}.${QMSGPAC
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
add_subdirectory(src)
if (BUILD_TESTS)
#if (MSGPACK_BUILD_TESTS)
enable_testing()
add_subdirectory(tests)
endif ()
#endif ()
install(EXPORT qmsgpack-export DESTINATION ${CMAKECONFIG_INSTALL_DIR} FILE MsgPackQtTargets.cmake)
file(RELATIVE_PATH relInstallDir ${CMAKE_INSTALL_PREFIX}/$CMAKECONFIG_INSTALL_DIR} ${CMAKE_INSTALL_PREFIX})
add_custom_target(uninstall
"${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake")
"${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake")

View File

@ -1,4 +1,4 @@
TEMPLATE = subdirs
SUBDIRS += \
src
src

View File

@ -1,5 +1,5 @@
set(qmsgpack_srcs msgpack.cpp msgpack_common.cpp msgpackstream.cpp private/pack_p.cpp private/unpack_p.cpp private/qt_types_p.cpp)
set(qmsgpack_headers msgpack.h msgpackstream.h msgpack_common.h msgpack_export.h endianhelper.h)
set(qmsgpack_srcs msgpack.cpp msgpackcommon.cpp msgpackstream.cpp private/pack_p.cpp private/unpack_p.cpp private/qt_types_p.cpp)
set(qmsgpack_headers msgpack.h msgpackstream.h msgpackcommon.h msgpack_export.h endianhelper.h)
add_library(qmsgpack SHARED ${qmsgpack_srcs} ${qmsgpack_headers})
@ -14,8 +14,8 @@ if (Qt5Gui_FOUND)
endif ()
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/msgpack_common.h.in"
"${CMAKE_CURRENT_SOURCE_DIR}/msgpack_common.h"
"${CMAKE_CURRENT_SOURCE_DIR}/msgpackcommon.h.in"
"${CMAKE_CURRENT_SOURCE_DIR}/msgpackcommon.h"
IMMEDIATE @ONLY)
if (NOT android)
@ -31,4 +31,4 @@ install(TARGETS qmsgpack EXPORT qmsgpack-export
LIBRARY DESTINATION ${LIB_INSTALL_DIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin
ARCHIVE DESTINATION ${LIB_INSTALL_DIR}
PUBLIC_HEADER DESTINATION ${INCLUDE_INSTALL_DIR}/qmsgpack)
PUBLIC_HEADER DESTINATION ${INCLUDE_INSTALL_DIR}/qmsgpack)

View File

@ -1,41 +0,0 @@
#-------------------------------------------------
#
# Project created by QtCreator 2014-09-07T17:43:05
#
#-------------------------------------------------
QT += core
QT += gui
TARGET = qmsgpack
CONFIG -= app_bundle
TEMPLATE = lib
DEFINES += MSGPACK_MAKE_LIB
DESTDIR = $$_PRO_FILE_PWD_/../bin
INSTALLS += target
QMAKE_CXXFLAGS += -fPIC
CONFIG += debug_and_release
CONFIG(debug, debug|release) {
TARGET = $$join(TARGET,,,d)
}
SOURCES += msgpack.cpp \
msgpack_common.cpp \
private/pack_p.cpp \
private/unpack_p.cpp \
private/qt_types_p.cpp \
stream.cpp
HEADERS += \
msgpack.h \
private/pack_p.h \
private/unpack_p.h \
private/sysdep.h \
msgpack_common.h \
msgpack_export.h \
private/qt_types_p.h \
stream.h

View File

@ -1,8 +1,8 @@
#include "msgpack.h"
#include "msgpack_common.h"
#include "private/unpack_p.h"
#include "private/pack_p.h"
#include "private/qt_types_p.h"
#include <QVector>
QVariant MsgPack::unpack(const QByteArray &data)

View File

@ -1,8 +1,8 @@
#ifndef MSGPACK_H
#define MSGPACK_H
#include "msgpack_common.h"
#include "msgpack_export.h"
#include "msgpackcommon.h"
#include <QByteArray>
#include <QVariantList>

View File

@ -1,4 +1,4 @@
#include "msgpack_common.h"
#include "msgpackcommon.h"
QString MsgPack::version()
{

View File

@ -1,5 +1,5 @@
#ifndef MSGPACK_COMMON_H
#define MSGPACK_COMMON_H
#ifndef COMMON_H
#define COMMON_H
#include <QVariant>
#include <QtGlobal>
@ -36,8 +36,8 @@ const quint8 FIXARRAY = 0x90;
const quint8 FIXSTR = 0xa0;
const quint8 NIL = 0xc0;
const quint8 NEVER_USED = 0xc1;
const quint8 MFALSE = 0xc2;
const quint8 MTRUE = 0xc3;
const quint8 FALSE = 0xc2;
const quint8 TRUE = 0xc3;
const quint8 BIN8 = 0xc4;
const quint8 BIN16 = 0xc5;
const quint8 BIN32 = 0xc6;
@ -69,4 +69,4 @@ const quint8 MAP32 = 0xdf;
const quint8 NEGATIVE_FIXINT = 0xe0;
}
}
#endif // MSGPACK_COMMON_H
#endif // COMMON_H

View File

@ -1,5 +1,5 @@
#ifndef MSGPACK_COMMON_H
#define MSGPACK_COMMON_H
#ifndef COMMON_H
#define COMMON_H
#include <QVariant>
#include <QtGlobal>
@ -36,8 +36,8 @@ const quint8 FIXARRAY = 0x90;
const quint8 FIXSTR = 0xa0;
const quint8 NIL = 0xc0;
const quint8 NEVER_USED = 0xc1;
const quint8 MFALSE = 0xc2;
const quint8 MTRUE = 0xc3;
const quint8 FALSE = 0xc2;
const quint8 TRUE = 0xc3;
const quint8 BIN8 = 0xc4;
const quint8 BIN16 = 0xc5;
const quint8 BIN32 = 0xc6;
@ -69,4 +69,4 @@ const quint8 MAP32 = 0xdf;
const quint8 NEGATIVE_FIXINT = 0xe0;
}
}
#endif // MSGPACK_COMMON_H
#endif // COMMON_H

View File

@ -1,5 +1,6 @@
#include "msgpackstream.h"
#include "private/pack_p.h"
#include <QBuffer>
#include <QDebug>
@ -27,7 +28,7 @@ MsgPackStream::MsgPackStream() :
{ }
MsgPackStream::MsgPackStream(QIODevice *d) :
dev(d), owndev(false), q_status(Ok)
dev(d), owndev(false)
{ }
MsgPackStream::MsgPackStream(QByteArray *a, QIODevice::OpenMode mode) :
@ -61,6 +62,11 @@ void MsgPackStream::setDevice(QIODevice *d)
owndev = false;
}
QIODevice *MsgPackStream::device() const
{
return dev;
}
bool MsgPackStream::atEnd() const
{
return dev ? dev->atEnd() : true;
@ -89,10 +95,10 @@ MsgPackStream &MsgPackStream::operator>>(bool &b)
b = false;
setStatus(ReadPastEnd);
} else {
if (p[0] != MsgPack::FirstByte::MTRUE ||
p[0] != MsgPack::FirstByte::MFALSE)
if (p[0] != MsgPack::FirstByte::TRUE ||
p[0] != MsgPack::FirstByte::FALSE)
setStatus(ReadCorruptData);
b = (p[0] == MsgPack::FirstByte::MTRUE);
b = (p[0] == MsgPack::FirstByte::TRUE);
}
return *this;
}
@ -319,23 +325,11 @@ bool MsgPackStream::readBytes(char *data, uint len)
return dev->read(data, len) == len;
}
bool MsgPackStream::readNil()
{
CHECK_STREAM_PRECOND(false);
quint8 b;
if (dev->read((char *)&b, 1) != 1 ||
b != MsgPack::FirstByte::NIL) {
setStatus(ReadCorruptData);
return false;
}
return true;
}
MsgPackStream &MsgPackStream::operator<<(bool b)
{
CHECK_STREAM_WRITE_PRECOND(*this);
quint8 m = b == true ?
MsgPack::FirstByte::MTRUE : MsgPack::FirstByte::MFALSE;
MsgPack::FirstByte::TRUE : MsgPack::FirstByte::FALSE;
if (dev->write((char *)&m, 1) != 1)
setStatus(WriteFailed);
return *this;
@ -446,21 +440,8 @@ MsgPackStream &MsgPackStream::operator<<(QByteArray array)
bool MsgPackStream::writeBytes(const char *data, uint len)
{
CHECK_STREAM_WRITE_PRECOND(false);
if (dev->write(data, len) != len) {
if (dev->write(data, len) != len)
setStatus(WriteFailed);
return false;
}
return true;
}
bool MsgPackStream::writeNil()
{
CHECK_STREAM_WRITE_PRECOND(false);
quint8 b = MsgPack::FirstByte::NIL;
if (dev->write((char *)&b, 1) != 1) {
setStatus(WriteFailed);
return false;
}
return true;
}
@ -501,7 +482,7 @@ bool MsgPackStream::unpack_longlong(qint64 &i64)
} else if (p[0] == MsgPack::FirstByte::UINT64) {
quint64 u64;
u64 = _msgpack_load64(quint64, p + 1);
if (u64 > std::numeric_limits<qint64>::max()) {
if (u64 > (quint64)std::numeric_limits<qint64>::max()) {
setStatus(ReadCorruptData);
return false;
}

View File

@ -1,15 +1,14 @@
#ifndef STREAM_H
#define STREAM_H
#include "msgpack_export.h"
#include "msgpack_common.h"
#include "msgpackcommon.h"
#include "endianhelper.h"
#include <QIODevice>
#include <limits>
class MSGPACK_EXPORT MsgPackStream
#include <QIODevice>
class MsgPackStream
{
public:
MsgPackStream();
@ -41,7 +40,6 @@ public:
MsgPackStream &operator>>(QString &str);
MsgPackStream &operator>>(QByteArray &array);
bool readBytes(char *data, uint len);
bool readNil();
MsgPackStream &operator<<(bool b);
MsgPackStream &operator<<(quint32 u32);
@ -54,7 +52,6 @@ public:
MsgPackStream &operator<<(const char *str);
MsgPackStream &operator<<(QByteArray array);
bool writeBytes(const char *data, uint len);
bool writeNil();
private:
QIODevice *dev;
@ -82,12 +79,10 @@ MsgPackStream& operator<<(MsgPackStream& s, const QList<T> &list)
_msgpack_store32(p + 1, len);
s.writeBytes((const char *)p, 5);
}
if (s.status() != MsgPackStream::Ok)
return s;
for (int i = 0; i < list.size(); ++i)
s << list[i];
return s;
}
@ -95,7 +90,6 @@ template <typename T>
MsgPackStream& operator>>(MsgPackStream& s, QList<T> &list)
{
list.clear();
quint8 p[5];
quint32 len;
s.readBytes((char *)p, 1);
@ -108,7 +102,6 @@ MsgPackStream& operator>>(MsgPackStream& s, QList<T> &list)
s.readBytes((char *)p + 1, 4);
len = _msgpack_load32(quint32, p + 1);
}
for (quint32 i = 0; i < len; ++i) {
T t;
s >> t;
@ -116,133 +109,6 @@ MsgPackStream& operator>>(MsgPackStream& s, QList<T> &list)
if (s.atEnd())
break;
}
return s;
}
template <class Key, class T>
MsgPackStream& operator<<(MsgPackStream &s, const QHash<Key, T> &hash)
{
quint32 len = 0;
QHashIterator<Key, T> it(hash);
while (it.hasNext()) {
it.next();
len++;
}
quint8 p[5];
if (len <= 15) {
p[0] = MsgPack::FirstByte::FIXMAP | len;
s.writeBytes((const char *)p, 1);
} else if (len <= std::numeric_limits<quint16>::max()) {
p[0] = MsgPack::FirstByte::MAP16;
_msgpack_store16(p + 1, len);
s.writeBytes((const char *)p, 3);
} else {
p[0] = MsgPack::FirstByte::MAP32;
_msgpack_store32(p + 1, len);
s.writeBytes((const char *)p, 5);
}
it.toFront();
while (it.hasNext()) {
it.next();
s << it.key() << it.value();
}
return s;
}
template <class Key, class T>
MsgPackStream& operator>>(MsgPackStream& s, QHash<Key, T> &hash)
{
hash.clear();
quint8 p[5];
quint32 len;
s.readBytes((char *)p, 1);
if (p[0] >= 0x80 && p[0] <= 0x8f) {
len = p[0] & 0xf;
} else if (p[0] == MsgPack::FirstByte::MAP16) {
s.readBytes((char *)p + 1, 2);
len = _msgpack_load16(quint16, p + 1);
} else if (p[0] == MsgPack::FirstByte::MAP32) {
s.readBytes((char *)p + 1, 4);
len = _msgpack_load32(quint32, p + 1);
}
for (quint32 i = 0; i < len; ++i) {
Key key;
T t;
s >> key >> t;
hash.insert(key, t);
if (s.atEnd())
break;
}
return s;
}
template <class Key, class T>
MsgPackStream& operator<<(MsgPackStream &s, const QMap<Key, T> &map)
{
quint32 len = 0;
QMapIterator<Key, T> it(map);
while (it.hasNext()) {
it.next();
len++;
}
quint8 p[5];
if (len <= 15) {
p[0] = MsgPack::FirstByte::FIXMAP | len;
s.writeBytes((const char *)p, 1);
} else if (len <= std::numeric_limits<quint16>::max()) {
p[0] = MsgPack::FirstByte::MAP16;
_msgpack_store16(p + 1, len);
s.writeBytes((const char *)p, 3);
} else {
p[0] = MsgPack::FirstByte::MAP32;
_msgpack_store32(p + 1, len);
s.writeBytes((const char *)p, 5);
}
it.toFront();
while (it.hasNext()) {
it.next();
s << it.key() << it.value();
}
return s;
}
template <class Key, class T>
MsgPackStream& operator>>(MsgPackStream& s, QMap<Key, T> &map)
{
map.clear();
quint8 p[5];
quint32 len;
s.readBytes((char *)p, 1);
if (p[0] >= 0x80 && p[0] <= 0x8f) {
len = p[0] & 0xf;
} else if (p[0] == MsgPack::FirstByte::MAP16) {
s.readBytes((char *)p + 1, 2);
len = _msgpack_load16(quint16, p + 1);
} else if (p[0] == MsgPack::FirstByte::MAP32) {
s.readBytes((char *)p + 1, 4);
len = _msgpack_load32(quint32, p + 1);
}
for (quint32 i = 0; i < len; ++i) {
Key key;
T t;
s >> key >> t;
map.insert(key, t);
if (s.atEnd())
break;
}
return s;
}

View File

@ -1,14 +1,13 @@
#include "pack_p.h"
#include "../endianhelper.h"
#include <limits>
#include <QByteArray>
#include <QDebug>
#include <QMapIterator>
#include <QString>
#include <QStringList>
#include <limits>
#include <QReadLocker>
#include <QWriteLocker>
@ -19,10 +18,7 @@ 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 (v.isNull())
p = pack_nil(p, wr);
else if (t == QMetaType::Int)
if (t == QMetaType::Int)
p = pack_int(v.toInt(), p, wr);
else if (t == QMetaType::UInt)
p = pack_uint(v.toUInt(), p, wr);
@ -46,19 +42,21 @@ quint8 *MsgPackPrivate::pack(const QVariant &v, quint8 *p, bool wr, QVector<QByt
p = pack_bin(v.toByteArray(), p, wr);
else if (t == QMetaType::QVariantMap)
p = pack_map(v.toMap(), p, wr, user_data);
else
p = pack_user(v, p, wr, user_data);
else {
if (t == QMetaType::User)
t = (QMetaType::Type)v.userType();
QReadLocker locker(&packers_lock);
bool has_packer = user_packers.contains(t);
locker.unlock();
if (has_packer)
p = pack_user(v, p, wr, user_data);
else
qWarning() << "MsgPack::pack can't pack type:" << t;
}
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) {
@ -329,18 +327,9 @@ bool MsgPackPrivate::register_packer(QMetaType::Type q_type, qint8 msgpack_type,
quint8 *MsgPackPrivate::pack_user(const QVariant &v, quint8 *p, bool wr, QVector<QByteArray> &user_data)
{
QMetaType::Type t;
if (v.type() == QVariant::UserType)
t = (QMetaType::Type)v.userType();
else
t = (QMetaType::Type)v.type();
QMetaType::Type t = (QMetaType::Type)v.type() == QMetaType::User ?
(QMetaType::Type)v.userType() : (QMetaType::Type)v.type();
QReadLocker locker(&packers_lock);
bool has_packer = user_packers.contains(t);
if (!has_packer) {
qWarning() << "MsgPack::pack can't pack type:" << t;
return p;
}
packer_t pt = user_packers[t];
locker.unlock();

View File

@ -1,7 +1,7 @@
#ifndef PACK_P_H
#define PACK_P_H
#include "../msgpack_common.h"
#include "../msgpackcommon.h"
#include <QHash>
#include <QMetaType>
@ -25,7 +25,6 @@ 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);

View File

@ -1,8 +1,9 @@
#include "qt_types_p.h"
#include "pack_p.h"
#include "unpack_p.h"
#include "msgpackstream.h"
#include "endianhelper.h"
#include "../msgpackstream.h"
#include "../endianhelper.h"
#include <QDebug>
#ifdef QT_GUI_LIB
@ -69,7 +70,7 @@ QVariant MsgPackPrivate::unpack_qcolor(const QByteArray &data)
// Date and Time
void MsgPackPrivate::pack_qtime_raw(const QTime &time, quint8 *p)
{
quint8 hm = 0, ms = 0;
quint8 hm, ms;
hm = (quint8)time.hour() << 4;
hm |= (quint8)time.minute() >> 2;
ms = ((quint8)time.minute() << 6) & 0xc0; // 11000000
@ -169,10 +170,10 @@ QVariant MsgPackPrivate::unpack_qdatetime(const QByteArray &data)
// Points and Vectors
QByteArray MsgPackPrivate::pack_qpoint(const QVariant &variant)
{
QPoint point = variant.toPoint();
QByteArray packed;
MsgPackStream stream(&packed, QIODevice::WriteOnly);
stream << point.x() << point.y();
QPoint pt = variant.toPoint();
stream << pt.x() << pt.y();
return packed;
}
@ -181,15 +182,16 @@ QVariant MsgPackPrivate::unpack_qpoint(const QByteArray &data)
MsgPackStream stream(data);
qint32 x, y;
stream >> x >> y;
qDebug() << "unpack qpoint stream" << (stream.status() == MsgPackStream::Ok);
return QPoint(x, y);
}
QByteArray MsgPackPrivate::pack_qsize(const QVariant &variant)
{
QSize size = variant.toSize();
QByteArray packed;
MsgPackStream stream(&packed, QIODevice::WriteOnly);
stream << size.width() << size.height();
QSize sz = variant.toSize();
stream << sz.width() << sz.height();
return packed;
}

View File

@ -4,7 +4,6 @@
#include <QByteArray>
#include <QDebug>
#include <QMap>
#include <QReadLocker>
#include <QWriteLocker>
@ -67,7 +66,7 @@ quint8 *MsgPackPrivate::unpack_type(QVariant &v, quint8 *p)
quint8 * MsgPackPrivate::unpack_nil(QVariant &v, quint8 *p)
{
Q_UNUSED(p)
v = QVariant();
Q_UNUSED(v)
return p + 1;
}

View File

@ -1,7 +1,7 @@
#ifndef MSGPACK_P_H
#define MSGPACK_P_H
#include "../msgpack_common.h"
#include "../msgpackcommon.h"
#include <QHash>
#include <QVariant>

View File

@ -1,44 +1,52 @@
TEMPLATE = lib
CONFIG += shared
QMAKE_CXXFLAGS += -std=c++11
TARGET = qmsgpack
DEFINES += MSGPACK_MAKE_LIB
VERSION = 0.1.0
QT += core
QT -= gui
TARGET = qmsgpack
CONFIG -= app_bundle
TEMPLATE = lib
DEFINES += MSGPACK_MAKE_LIB
DESTDIR = $$TOP_SRCDIR/bin
QMAKE_CXXFLAGS += -fPIC
CONFIG += debug_and_release
CONFIG(debug, debug|release) {
TARGET = $$join(TARGET,,,d)
}
SOURCES += msgpack.cpp \
msgpackcommon.cpp \
private/pack_p.cpp \
private/unpack_p.cpp \
private/qt_types_p.cpp \
msgpackstream.cpp
HEADERS += \
msgpack_common.h \
msgpackstream.h \
msgpack_export.h \
msgpack.h \
endianhelper.h \
private/pack_p.h \
private/unpack_p.h \
endianhelper.h \
msgpackcommon.h \
msgpack_export.h \
private/qt_types_p.h \
private/pack_p.h
SOURCES += \
msgpackstream.cpp \
msgpack_common.cpp \
msgpack.cpp \
private/qt_types_p.cpp \
private/unpack_p.cpp \
private/pack_p.cpp
release: DESTDIR = ../build/release
debug: DESTDIR = ../build/debug
OBJECTS_DIR = $$DESTDIR/obj
MOC_DIR = $$DESTDIR/moc
msgpackstream.h
HEADERS_INSTALL = \
msgpack_common.h \
msgpackstream.h \
msgpack_export.h \
msgpack.h \
endianhelper.h
endianhelper.h \
msgpackcommon.h \
msgpack_export.h \
msgpackstream.h \
STREAM_HEADERS_INSTALL = \
stream/location.h
unix {
header_files.files = $$HEADERS_INSTALL
header_files.path = /usr/include/qmsgpack
stream_header_files.files = $$STREAM_HEADERS_INSTALL
stream_header_files.path = /usr/include/qmsgpack/stream
target.path = /usr/lib
INSTALLS += header_files target
INSTALLS += header_files stream_header_files target
}

24
src/stream/location.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef QMSGPACK_STREAM_LOCATION_H
#define QMSGPACK_STREAM_LOCATION_H
#include "../msgpackstream.h"
#include <QGeoCoordinate>
MsgPackStream& operator>>(MsgPackStream& s, QGeoCoordinate &coordinate)
{
double x;
s >> x;
coordinate.setLatitude(x);
s >> x;
coordinate.setLongitude(x);
return s;
}
MsgPackStream &operator<<(MsgPackStream& s, const QGeoCoordinate &coordinate)
{
s << coordinate.latitude() << coordinate.longitude();
return s;
}
#endif // QMSGPACK_STREAM_LOCATION_H

View File

@ -8,7 +8,7 @@ if (Qt5Core_FOUND)
set(TEST_LIBRARIES ${Qt5Test_LIBRARIES})
endif ()
set(TEST_SUBDIRS pack unpack mixed qttypes stream)
set(TEST_SUBDIRS pack unpack mixed stream)
foreach(subdir ${TEST_SUBDIRS})
add_subdirectory(${subdir})

24
tests/big/CMakeLists.txt Normal file
View 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 big)
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()

39
tests/big/big.cpp Normal file
View File

@ -0,0 +1,39 @@
#include <QString>
#include <QtTest>
#include <QDebug>
#include <msgpack.h>
#include <limits>
#include <msgpack_ext.h>
class Big : public QObject
{
Q_OBJECT
private Q_SLOTS:
void test_big_list();
};
void Big::test_big_list()
{
QVariantList list;
for (int i = 0; i < 10000; ++i)
list << i;
QByteArray packed;
QVariantList unpacked;
QBENCHMARK {
packed = MsgPack::pack(list);
unpacked = MsgPack::unpack(packed).toList();
}
bool listOk = true;
for (int i = 0; i < unpacked.size(); ++i) {
if (unpacked[i].toInt() != i) {
listOk = false;
}
}
QVERIFY(listOk);
}
QTEST_APPLESS_MAIN(Big)
#include "big.moc"

View File

@ -9,7 +9,6 @@ class PackTest : public QObject
Q_OBJECT
private Q_SLOTS:
void test_nil();
void test_bool();
void test_fixint();
void test_integer8();
@ -22,14 +21,6 @@ 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);
@ -225,7 +216,7 @@ void PackTest::test_float()
void PackTest::test_str()
{
QString str = QString("msgpack rocks");
QString str = QStringLiteral("msgpack rocks");
QByteArray arr = MsgPack::pack(str);
QVERIFY(arr.size() == 14);
quint8 *p = (quint8 *)arr.data();

View File

@ -74,11 +74,11 @@ void QtTypesTest::test_qpoint()
QPoint pt2 = MsgPack::unpack(packed).toPoint();
QVERIFY(pt == pt2);
pt = QPoint(1, 2);
packed = MsgPack::pack(pt);
QVERIFY(packed.size() == 4);
pt2 = MsgPack::unpack(packed).toPoint();
QVERIFY(pt == pt2);
pt = QPoint(1, 2);
packed = MsgPack::pack(pt);
QVERIFY(packed.size() == 4);
pt2 = MsgPack::unpack(packed).toPoint();
QVERIFY(pt == pt2);
pt = QPoint(1234, 5678);
packed = MsgPack::pack(pt);
@ -141,4 +141,4 @@ void QtTypesTest::test_qrect()
QTEST_APPLESS_MAIN(QtTypesTest)
#include "qttypes_test.moc"
#include "qttypes_test.moc"

View File

@ -1,5 +1,6 @@
#include <QString>
#include <QtTest>
#include <QDebug>
#include <msgpackstream.h>
#include <msgpack.h>
#include <limits>
@ -17,7 +18,6 @@ private Q_SLOTS:
void test_double();
void test_bin();
void test_array();
void test_map();
};
void StreamTest::test_unpack_integers()
@ -142,7 +142,7 @@ void StreamTest::test_pack_integers()
void StreamTest::test_unpack_string()
{
QString str = QString("msgpack rocks");
QString str = QStringLiteral("msgpack rocks");
QByteArray packed = MsgPack::pack(str);
QString str2;
@ -367,47 +367,5 @@ void StreamTest::test_array()
}
}
void StreamTest::test_map()
{
QMap<QString, int> map, map2;
QByteArray ba;
map.insert("m0", 0);
{
MsgPackStream stream(&ba, QIODevice::WriteOnly);
stream << map;
MsgPackStream stream2(ba);
stream2 >> map2;
}
QVERIFY(ba.length() == 5);
quint8 *p = (quint8 *)ba.data();
QVERIFY(p[0] == 0x80 | 1);
QVERIFY(map == map2);
for (int i = 1; i < 16; ++i)
map.insert(QString("m%1").QString::arg(i), i);
{
MsgPackStream stream(&ba, QIODevice::WriteOnly);
stream << map;
MsgPackStream stream2(ba);
stream2 >> map2;
}
p = (quint8 *)ba.data();
QVERIFY(p[0] == 0xde);
QVERIFY(map == map2);
for (int i = 16; i < 65536; ++i)
map.insert(QString("m%1").QString::arg(i), i);
{
MsgPackStream stream(&ba, QIODevice::WriteOnly);
stream << map;
MsgPackStream stream2(ba);
stream2 >> map2;
}
p = (quint8 *)ba.data();
QVERIFY(p[0] == 0xdf);
QVERIFY(map == map2);
}
QTEST_APPLESS_MAIN(StreamTest)
#include "stream_test.moc"

View File

@ -9,7 +9,6 @@ class UnpackTest : public QObject
Q_OBJECT
private Q_SLOTS:
void test_nil();
void test_booleans();
void test_integers();
void test_floats();
@ -18,13 +17,6 @@ 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()
{