~ubuntu-branches/debian/sid/calligraplan/sid

« back to all changes in this revision

Viewing changes to src/libs/store/tests/TestKoLZF.cpp

  • Committer: Package Import Robot
  • Author(s): Pino Toscano
  • Date: 2018-02-01 18:20:19 UTC
  • Revision ID: package-import@ubuntu.com-20180201182019-1qo7qaim5wejm5k9
Tags: upstream-3.1.0
ImportĀ upstreamĀ versionĀ 3.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE project
 
2
 * Copyright 2015 Friedrich W. H. Kossebau <kossebau@kde.org>
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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.
 
18
 */
 
19
 
 
20
#include "TestKoLZF.h"
 
21
 
 
22
#include <KoLZF.h>
 
23
 
 
24
#include <QTest>
 
25
 
 
26
void TestKoLZF::testArrayCompressionEmpty_data()
 
27
{
 
28
    QTest::addColumn<char>("canary");
 
29
    QTest::newRow("00") << '\0';
 
30
    QTest::newRow("FF") << '\xFF';
 
31
}
 
32
 
 
33
void TestKoLZF::testArrayCompressionEmpty()
 
34
{
 
35
    QFETCH(char, canary);
 
36
 
 
37
    const char inputData[] = "test";
 
38
    // use one more byte and see if it stays untouched
 
39
    char outputdata[1];
 
40
    outputdata[0] = canary;
 
41
 
 
42
    const int compressedDataLength = KoLZF::compress(inputData, 0, outputdata, 0);
 
43
 
 
44
    QCOMPARE(compressedDataLength, 0);
 
45
    QCOMPARE(outputdata[0], canary);
 
46
}
 
47
 
 
48
void TestKoLZF::testArrayCompressionNullPointerInput()
 
49
{
 
50
    char outputdata[4];
 
51
 
 
52
    const int compressedDataLength = KoLZF::compress(0, 4, outputdata, 4);
 
53
 
 
54
    QCOMPARE(compressedDataLength, 0);
 
55
}
 
56
 
 
57
void TestKoLZF::testArrayCompressionNullPointerOutput()
 
58
{
 
59
    const char inputData[] = "test";
 
60
 
 
61
    const int compressedDataLength = KoLZF::compress(inputData, 4, 0, 4);
 
62
 
 
63
    QCOMPARE(compressedDataLength, 0);
 
64
}
 
65
 
 
66
void TestKoLZF::testArrayDecompressionEmpty_data()
 
67
{
 
68
    QTest::addColumn<char>("canary");
 
69
    QTest::newRow("00") << '\0';
 
70
    QTest::newRow("FF") << '\xFF';
 
71
}
 
72
 
 
73
void TestKoLZF::testArrayDecompressionEmpty()
 
74
{
 
75
    QFETCH(char, canary);
 
76
 
 
77
    const char inputData[] = "test";
 
78
    char outputdata[1];
 
79
    outputdata[0] = canary;
 
80
 
 
81
    const int uncompressedDataLength = KoLZF::decompress(inputData, 0, outputdata, 0);
 
82
 
 
83
    QCOMPARE(uncompressedDataLength, 0);
 
84
    QCOMPARE(outputdata[0], canary);
 
85
}
 
86
 
 
87
void TestKoLZF::testArrayDecompressionNullPointerInput()
 
88
{
 
89
    char outputdata[4];
 
90
 
 
91
    const int uncompressedDataLength = KoLZF::decompress(0, 0, outputdata, 4);
 
92
 
 
93
    QCOMPARE(uncompressedDataLength, 0);
 
94
}
 
95
 
 
96
void TestKoLZF::testArrayDecompressionNullPointerOutput()
 
97
{
 
98
    const char inputData[] = "test";
 
99
 
 
100
    const int uncompressedDataLength = KoLZF::decompress(inputData, 4, 0, 4);
 
101
 
 
102
    QCOMPARE(uncompressedDataLength, 0);
 
103
}
 
104
 
 
105
Q_DECLARE_METATYPE(char*)
 
106
 
 
107
void TestKoLZF::testArrayRoundtripDifferentSizes_data()
 
108
{
 
109
    QTest::addColumn<char*>("data");
 
110
    QTest::addColumn<int>("size");
 
111
    QTest::addColumn<char>("canary");
 
112
 
 
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"};
 
117
 
 
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];
 
122
                if (f == 0) {
 
123
                    memset(data, 't', i);
 
124
                } else {
 
125
                    for (int b = 0; b < i; ++b) {
 
126
                        data[b] = b % 256;
 
127
                    }
 
128
                }
 
129
 
 
130
                QByteArray ba = QByteArray::number(i)+'-'+QByteArray(1, canary[c]).toHex()+'-'+QByteArray(fillMethodName[f]);
 
131
                QTest::newRow(ba.constData())
 
132
                    << data << i << canary[c];
 
133
            }
 
134
        }
 
135
    }
 
136
}
 
137
 
 
138
void TestKoLZF::testArrayRoundtripDifferentSizes()
 
139
{
 
140
    QFETCH(char*, data);
 
141
    QFETCH(int, size);
 
142
    QFETCH(char, canary);
 
143
 
 
144
    char * compressedData = new char[size+1];
 
145
    compressedData[size] = canary;
 
146
 
 
147
    // try
 
148
    const int compressedDataLength = KoLZF::compress(data, size, compressedData, size);
 
149
 
 
150
    QCOMPARE(compressedData[size], canary);
 
151
 
 
152
    const bool compressed = (compressedDataLength != 0);
 
153
    // done with testing if not compressed
 
154
    if (!compressed) {
 
155
        delete [] data;
 
156
        delete [] compressedData;
 
157
        return;
 
158
    }
 
159
 
 
160
    // now try uncompressing
 
161
    char * uncompressedData = new char[size+1];
 
162
    uncompressedData[size] = canary;
 
163
 
 
164
    const int uncompressedDataLength =
 
165
        KoLZF::decompress(compressedData, compressedDataLength, uncompressedData, size);
 
166
 
 
167
    QVERIFY(uncompressedDataLength != 0);
 
168
    QCOMPARE(uncompressedDataLength, size);
 
169
    QCOMPARE(memcmp(uncompressedData, data, size), 0 );
 
170
    QCOMPARE(uncompressedData[size], canary);
 
171
 
 
172
    delete [] data;
 
173
    delete [] compressedData;
 
174
    delete [] uncompressedData;
 
175
}
 
176
 
 
177
 
 
178
void TestKoLZF::testByteArrayCompressionEmpty()
 
179
{
 
180
    const QByteArray empty;
 
181
 
 
182
    // try
 
183
    const QByteArray compressed = KoLZF::compress(empty);
 
184
 
 
185
    // only metadata
 
186
    QCOMPARE(compressed.size(), 5);
 
187
    // size is 0
 
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));
 
192
    // uncompressed
 
193
    QCOMPARE(compressed.at(4), static_cast<char>(0));
 
194
}
 
195
 
 
196
void TestKoLZF::testByteArrayDecompressionEmpty()
 
197
{
 
198
    const char emptyCompressedRaw[5] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
 
199
    const QByteArray emptyCompressed = QByteArray::fromRawData(emptyCompressedRaw, 5);
 
200
 
 
201
    QByteArray uncompressed;
 
202
 
 
203
    // try
 
204
    KoLZF::decompress(emptyCompressed, uncompressed);
 
205
 
 
206
    // empty
 
207
    QCOMPARE(uncompressed.size(), 0);
 
208
}
 
209
 
 
210
void TestKoLZF::testByteArrayRoundtripDifferentSizes_data()
 
211
{
 
212
    QTest::addColumn<QByteArray>("data");
 
213
 
 
214
    static const int fillMethodCount = 2;
 
215
    static const char * const fillMethodName[fillMethodCount] = {"uni",  "series"};
 
216
 
 
217
    for(int i = 0; i < 512; ++i) {
 
218
        for (int f = 0; f < 2; ++f) {
 
219
            QByteArray data;
 
220
 
 
221
            if (f == 0) {
 
222
                data = QByteArray(i, 't');
 
223
            } else {
 
224
                data.resize(i);
 
225
                for (int b = 0; b < i; ++b) {
 
226
                    data[b] = b % 256;
 
227
                }
 
228
            }
 
229
            QByteArray ba = QByteArray::number(i)+'-'+fillMethodName[f];
 
230
            QTest::newRow(ba.constData()) << data;
 
231
        }
 
232
    }
 
233
}
 
234
 
 
235
void TestKoLZF::testByteArrayRoundtripDifferentSizes()
 
236
{
 
237
    QFETCH(QByteArray, data);
 
238
 
 
239
    // try
 
240
    const QByteArray compressed = KoLZF::compress(data);
 
241
    QByteArray uncompressed;
 
242
    KoLZF::decompress(compressed, uncompressed);
 
243
 
 
244
    QCOMPARE(uncompressed, data);
 
245
}
 
246
 
 
247
 
 
248
QTEST_GUILESS_MAIN(TestKoLZF)