Debian Codegraph @

src/libs/store/tests/TestKoLZF.cpp

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