- Use absolute file paths by using $$PWD variable.
- Adds unit testing project + unit-test for serialization.
- Adds ADS_IMPORT define possibility for static code include.

Core
- Fixes usage of QBuffer.. missed the QBuffer::open() call..
This commit is contained in:
mfreiholz 2016-04-01 11:49:16 +02:00
parent 5ab1751f9b
commit 158dac5309
11 changed files with 123 additions and 43 deletions

View File

@ -1,24 +1,24 @@
SOURCES += \ SOURCES += \
src/API.cpp \ $$PWD/src/API.cpp \
src/ContainerWidget.cpp \ $$PWD/src/ContainerWidget.cpp \
src/SectionWidget.cpp \ $$PWD/src/SectionWidget.cpp \
src/SectionContent.cpp \ $$PWD/src/SectionContent.cpp \
src/SectionTitleWidget.cpp \ $$PWD/src/SectionTitleWidget.cpp \
src/SectionContentWidget.cpp \ $$PWD/src/SectionContentWidget.cpp \
src/DropOverlay.cpp \ $$PWD/src/DropOverlay.cpp \
src/FloatingWidget.cpp \ $$PWD/src/FloatingWidget.cpp \
src/Internal.cpp \ $$PWD/src/Internal.cpp \
src/Serialization.cpp $$PWD/src/Serialization.cpp
HEADERS += \ HEADERS += \
include/ads/API.h \ $$PWD/include/ads/API.h \
include/ads/ContainerWidget.h \ $$PWD/include/ads/ContainerWidget.h \
include/ads/SectionWidget.h \ $$PWD/include/ads/SectionWidget.h \
include/ads/SectionContent.h \ $$PWD/include/ads/SectionContent.h \
include/ads/SectionTitleWidget.h \ $$PWD/include/ads/SectionTitleWidget.h \
include/ads/SectionContentWidget.h \ $$PWD/include/ads/SectionContentWidget.h \
include/ads/DropOverlay.h \ $$PWD/include/ads/DropOverlay.h \
include/ads/FloatingWidget.h \ $$PWD/include/ads/FloatingWidget.h \
include/ads/Internal.h \ $$PWD/include/ads/Internal.h \
include/ads/Serialization.h $$PWD/include/ads/Serialization.h

View File

@ -4,6 +4,7 @@ CONFIG += adsBuildShared
QT += core gui QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
greaterThan(QT_MAJOR_VERSION, 4): DEFINES += ADS_NAMESPACE_ENABLED
TEMPLATE = lib TEMPLATE = lib
@ -18,8 +19,6 @@ adsBuildShared {
INCLUDEPATH += $$PWD/src INCLUDEPATH += $$PWD/src
INCLUDEPATH += $$PWD/include INCLUDEPATH += $$PWD/include
greaterThan(QT_MAJOR_VERSION, 4): DEFINES += ADS_NAMESPACE_ENABLED
windows { windows {
# MinGW # MinGW
*-g++* { *-g++* {

View File

@ -7,7 +7,9 @@ class QSplitter;
// DLL Export API // DLL Export API
#ifdef _WIN32 #ifdef _WIN32
#ifdef ADS_EXPORT #if defined(ADS_IMPORT)
#define ADS_EXPORT_API
#elif defined(ADS_EXPORT)
#define ADS_EXPORT_API __declspec(dllexport) #define ADS_EXPORT_API __declspec(dllexport)
#else #else
#define ADS_EXPORT_API __declspec(dllimport) #define ADS_EXPORT_API __declspec(dllimport)

View File

@ -129,9 +129,10 @@ class InMemoryWriter
{ {
public: public:
InMemoryWriter(); InMemoryWriter();
bool write(OffsetsHeaderEntry::Type type, const QByteArray& data); bool write(qint32 entryType, const QByteArray& data);
bool write(const SectionIndexData& data); bool write(const SectionIndexData& data);
QByteArray toByteArray() const; QByteArray toByteArray() const;
qint32 offsetsCount() const { return _offsetsHeader.entriesCount; }
private: private:
QBuffer _contentBuffer; QBuffer _contentBuffer;
@ -146,12 +147,11 @@ class InMemoryReader
public: public:
InMemoryReader(const QByteArray& data); InMemoryReader(const QByteArray& data);
bool initReadHeader(); bool initReadHeader();
bool read(OffsetsHeaderEntry::Type type, QByteArray &data); bool read(qint32 entryType, QByteArray &data);
qint32 offsetsCount() const { return _offsetsHeader.entriesCount; }
private: private:
QByteArray _data; QByteArray _data;
QBuffer _buff;
OffsetsHeader _offsetsHeader; OffsetsHeader _offsetsHeader;
}; };

View File

@ -276,12 +276,13 @@ QDataStream& operator>>(QDataStream& in, SectionIndexData& data)
InMemoryWriter::InMemoryWriter() InMemoryWriter::InMemoryWriter()
{ {
_contentBuffer.open(QIODevice::ReadWrite);
} }
bool InMemoryWriter::write(OffsetsHeaderEntry::Type type, const QByteArray& data) bool InMemoryWriter::write(qint32 entryType, const QByteArray& data)
{ {
OffsetsHeaderEntry entry; OffsetsHeaderEntry entry;
entry.type = type; entry.type = entryType;
entry.offset = _contentBuffer.pos(); // Relative offset! entry.offset = _contentBuffer.pos(); // Relative offset!
entry.contentSize = data.size(); entry.contentSize = data.size();
@ -314,9 +315,7 @@ bool InMemoryWriter::write(const SectionIndexData& data)
QByteArray InMemoryWriter::toByteArray() const QByteArray InMemoryWriter::toByteArray() const
{ {
QByteArray data; QByteArray data;
QBuffer buff(&data); QDataStream out(&data, QIODevice::ReadWrite);
QDataStream out(&buff);
out.setVersion(QDataStream::Qt_4_5); out.setVersion(QDataStream::Qt_4_5);
// Basic format header. // Basic format header.
@ -332,13 +331,13 @@ QByteArray InMemoryWriter::toByteArray() const
// - Convert relative- to absolute-offsets // - Convert relative- to absolute-offsets
// - Seek back to begin-pos and write OffsetsHeader again. // - Seek back to begin-pos and write OffsetsHeader again.
// Use a copy of OffsetsHeader to keep the _offsetsHeader relative. // Use a copy of OffsetsHeader to keep the _offsetsHeader relative.
const qint64 posOffsetHeaders = buff.pos(); const qint64 posOffsetHeaders = out.device()->pos();
OffsetsHeader offsetsHeader = _offsetsHeader; OffsetsHeader offsetsHeader = _offsetsHeader;
out << offsetsHeader; out << offsetsHeader;
// Now we know the size of the entire header. // Now we know the size of the entire header.
// We can update the relative- to absolute-offsets now. // We can update the relative- to absolute-offsets now.
const qint64 allHeaderSize = buff.pos(); const qint64 allHeaderSize = out.device()->pos();
for (int i = 0; i < offsetsHeader.entriesCount; ++i) for (int i = 0; i < offsetsHeader.entriesCount; ++i)
{ {
offsetsHeader.entries[i].offset += allHeaderSize; offsetsHeader.entries[i].offset += allHeaderSize;
@ -346,11 +345,11 @@ QByteArray InMemoryWriter::toByteArray() const
// Seek back and write again with absolute offsets. // Seek back and write again with absolute offsets.
// TODO Thats not nice, but it works... // TODO Thats not nice, but it works...
buff.seek(posOffsetHeaders); out.device()->seek(posOffsetHeaders);
out << offsetsHeader; out << offsetsHeader;
// Write contents. // Write contents.
buff.write(_contentBuffer.data()); out.writeRawData(_contentBuffer.data().constData(), _contentBuffer.size());
return data; return data;
} }
@ -358,13 +357,13 @@ QByteArray InMemoryWriter::toByteArray() const
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
InMemoryReader::InMemoryReader(const QByteArray& data) : InMemoryReader::InMemoryReader(const QByteArray& data) :
_data(data), _buff(&_data) _data(data)
{ {
} }
bool InMemoryReader::initReadHeader() bool InMemoryReader::initReadHeader()
{ {
QDataStream in(&_buff); QDataStream in(_data);
in.setVersion(QDataStream::Qt_4_5); in.setVersion(QDataStream::Qt_4_5);
// Basic format header. // Basic format header.
@ -388,13 +387,13 @@ bool InMemoryReader::initReadHeader()
return !in.atEnd(); return !in.atEnd();
} }
bool InMemoryReader::read(OffsetsHeaderEntry::Type type, QByteArray& data) bool InMemoryReader::read(qint32 entryType, QByteArray& data)
{ {
// Find offset for "type". // Find offset for "type".
int index = -1; int index = -1;
for (int i = 0; i < _offsetsHeader.entriesCount; ++i) for (int i = 0; i < _offsetsHeader.entriesCount; ++i)
{ {
if (_offsetsHeader.entries.at(index).type == type) if (_offsetsHeader.entries.at(i).type == entryType)
{ {
index = i; index = i;
break; break;
@ -406,8 +405,16 @@ bool InMemoryReader::read(OffsetsHeaderEntry::Type type, QByteArray& data)
return false; return false;
const OffsetsHeaderEntry& entry = _offsetsHeader.entries.at(index); const OffsetsHeaderEntry& entry = _offsetsHeader.entries.at(index);
_buff.seek(entry.offset);
data.append(_buff.read(entry.contentSize)); QDataStream in(_data);
in.setVersion(QDataStream::Qt_4_5);
in.device()->seek(entry.offset);
char* buff = new char[entry.contentSize];
in.readRawData(buff, entry.contentSize);
data.append(buff, entry.contentSize);
delete[] buff;
return true; return true;
} }

View File

@ -0,0 +1,7 @@
HEADERS += \
$$PWD/src/TestCore.h
SOURCES += \
$$PWD/src/main.cpp \
$$PWD/src/TestCore.cpp

View File

@ -0,0 +1,14 @@
TARGET = AdvancedDockingSystemUnitTests
QT += core gui testlib
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
greaterThan(QT_MAJOR_VERSION, 4): DEFINES += ADS_NAMESPACE_ENABLED
DEFINES += ADS_IMPORT
INCLUDEPATH += $$PWD/src
INCLUDEPATH += $$PWD/../AdvancedDockingSystem/include
DEPENDPATH += $$PWD/../AdvancedDockingSystem/include
include(AdvancedDockingSystemUnitTests.pri)
include(../AdvancedDockingSystem/AdvancedDockingSystem.pri)

View File

@ -0,0 +1,35 @@
#include "TestCore.h"
#include "ads/API.h"
#include "ads/Serialization.h"
void TestCore::serialization()
{
QList<QByteArray> datas;
datas.append(QByteArray("Custom Data Here!!!"));
datas.append(QByteArray("Even More..."));
datas.append(QByteArray("lalalaalalalalalalal").toBase64());
// Write some data.
ADS_NS_SER::InMemoryWriter writer;
for (int i = 0; i < datas.count(); ++i)
{
QVERIFY(writer.write(i + 1, datas.at(i)));
}
QVERIFY(writer.offsetsCount() == datas.count());
const QByteArray writtenData = writer.toByteArray();
QVERIFY(writtenData.size() > 0);
// Read and validate written data.
ADS_NS_SER::InMemoryReader reader(writtenData);
QVERIFY(reader.initReadHeader());
QVERIFY(reader.offsetsCount() == datas.count());
for (int i = 0; i < datas.count(); ++i)
{
QByteArray readData;
QVERIFY(reader.read(i + 1, readData));
QVERIFY(readData == datas.at(i));
}
}
QTEST_MAIN(TestCore)

View File

@ -0,0 +1,14 @@
#ifndef TEST_CORE_H
#define TEST_CORE_H
#include <QtTest/QtTest>
class TestCore : public QObject
{
Q_OBJECT
private slots:
void serialization();
};
#endif

View File

@ -0,0 +1 @@
#include <QtTest/QtTest>

View File

@ -2,4 +2,5 @@ TEMPLATE = subdirs
SUBDIRS = \ SUBDIRS = \
AdvancedDockingSystem \ AdvancedDockingSystem \
AdvancedDockingSystemDemo AdvancedDockingSystemDemo \
AdvancedDockingSystemUnitTests