1
/* This file is part of the KDE project
2
* Copyright 2015 Friedrich W. H. Kossebau <kossebau@kde.org>
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Library General Public
6
* License as published by the Free Software Foundation; either
7
* version 2 of the License, or (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Library General Public License for more details.
14
* You should have received a copy of the GNU Library General Public License
15
* along with this library; see the file COPYING.LIB. If not, write to
16
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
* Boston, MA 02110-1301, USA.
20
#include "TestKoLZF.h"
26
void TestKoLZF::testArrayCompressionEmpty_data()
28
QTest::addColumn<char>("canary");
29
QTest::newRow("00") << '\0';
30
QTest::newRow("FF") << '\xFF';
33
void TestKoLZF::testArrayCompressionEmpty()
37
const char inputData[] = "test";
38
// use one more byte and see if it stays untouched
40
outputdata[0] = canary;
42
const int compressedDataLength = KoLZF::compress(inputData, 0, outputdata, 0);
44
QCOMPARE(compressedDataLength, 0);
45
QCOMPARE(outputdata[0], canary);
48
void TestKoLZF::testArrayCompressionNullPointerInput()
52
const int compressedDataLength = KoLZF::compress(0, 4, outputdata, 4);
54
QCOMPARE(compressedDataLength, 0);
57
void TestKoLZF::testArrayCompressionNullPointerOutput()
59
const char inputData[] = "test";
61
const int compressedDataLength = KoLZF::compress(inputData, 4, 0, 4);
63
QCOMPARE(compressedDataLength, 0);
66
void TestKoLZF::testArrayDecompressionEmpty_data()
68
QTest::addColumn<char>("canary");
69
QTest::newRow("00") << '\0';
70
QTest::newRow("FF") << '\xFF';
73
void TestKoLZF::testArrayDecompressionEmpty()
77
const char inputData[] = "test";
79
outputdata[0] = canary;
81
const int uncompressedDataLength = KoLZF::decompress(inputData, 0, outputdata, 0);
83
QCOMPARE(uncompressedDataLength, 0);
84
QCOMPARE(outputdata[0], canary);
87
void TestKoLZF::testArrayDecompressionNullPointerInput()
91
const int uncompressedDataLength = KoLZF::decompress(0, 0, outputdata, 4);
93
QCOMPARE(uncompressedDataLength, 0);
96
void TestKoLZF::testArrayDecompressionNullPointerOutput()
98
const char inputData[] = "test";
100
const int uncompressedDataLength = KoLZF::decompress(inputData, 4, 0, 4);
102
QCOMPARE(uncompressedDataLength, 0);
105
Q_DECLARE_METATYPE(char*)
107
void TestKoLZF::testArrayRoundtripDifferentSizes_data()
109
QTest::addColumn<char*>("data");
110
QTest::addColumn<int>("size");
111
QTest::addColumn<char>("canary");
113
static const char canary[] = {'\0', '\xFF'};
114
static const int canaryCount = sizeof( canary ) / sizeof( canary[0] );
115
static const int fillMethodCount = 2;
116
static const char * const fillMethodName[fillMethodCount] = {"uni", "series"};
118
for(int c = 0; c < canaryCount; ++c) {
119
for(int i = 1; i < 512; ++i) {
120
for (int f = 0; f < 2; ++f) {
121
char *data = new char[i];
123
memset(data, 't', i);
125
for (int b = 0; b < i; ++b) {
130
QByteArray ba = QByteArray::number(i)+'-'+QByteArray(1, canary[c]).toHex()+'-'+QByteArray(fillMethodName[f]);
131
QTest::newRow(ba.constData())
132
<< data << i << canary[c];
138
void TestKoLZF::testArrayRoundtripDifferentSizes()
142
QFETCH(char, canary);
144
char * compressedData = new char[size+1];
145
compressedData[size] = canary;
148
const int compressedDataLength = KoLZF::compress(data, size, compressedData, size);
150
QCOMPARE(compressedData[size], canary);
152
const bool compressed = (compressedDataLength != 0);
153
// done with testing if not compressed
156
delete [] compressedData;
160
// now try uncompressing
161
char * uncompressedData = new char[size+1];
162
uncompressedData[size] = canary;
164
const int uncompressedDataLength =
165
KoLZF::decompress(compressedData, compressedDataLength, uncompressedData, size);
167
QVERIFY(uncompressedDataLength != 0);
168
QCOMPARE(uncompressedDataLength, size);
169
QCOMPARE(memcmp(uncompressedData, data, size), 0 );
170
QCOMPARE(uncompressedData[size], canary);
173
delete [] compressedData;
174
delete [] uncompressedData;
178
void TestKoLZF::testByteArrayCompressionEmpty()
180
const QByteArray empty;
183
const QByteArray compressed = KoLZF::compress(empty);
186
QCOMPARE(compressed.size(), 5);
188
QCOMPARE(compressed.at(0), static_cast<char>(0));
189
QCOMPARE(compressed.at(1), static_cast<char>(0));
190
QCOMPARE(compressed.at(2), static_cast<char>(0));
191
QCOMPARE(compressed.at(3), static_cast<char>(0));
193
QCOMPARE(compressed.at(4), static_cast<char>(0));
196
void TestKoLZF::testByteArrayDecompressionEmpty()
198
const char emptyCompressedRaw[5] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
199
const QByteArray emptyCompressed = QByteArray::fromRawData(emptyCompressedRaw, 5);
201
QByteArray uncompressed;
204
KoLZF::decompress(emptyCompressed, uncompressed);
207
QCOMPARE(uncompressed.size(), 0);
210
void TestKoLZF::testByteArrayRoundtripDifferentSizes_data()
212
QTest::addColumn<QByteArray>("data");
214
static const int fillMethodCount = 2;
215
static const char * const fillMethodName[fillMethodCount] = {"uni", "series"};
217
for(int i = 0; i < 512; ++i) {
218
for (int f = 0; f < 2; ++f) {
222
data = QByteArray(i, 't');
225
for (int b = 0; b < i; ++b) {
229
QByteArray ba = QByteArray::number(i)+'-'+fillMethodName[f];
230
QTest::newRow(ba.constData()) << data;
235
void TestKoLZF::testByteArrayRoundtripDifferentSizes()
237
QFETCH(QByteArray, data);
240
const QByteArray compressed = KoLZF::compress(data);
241
QByteArray uncompressed;
242
KoLZF::decompress(compressed, uncompressed);
244
QCOMPARE(uncompressed, data);
248
QTEST_GUILESS_MAIN(TestKoLZF)