1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
#include "lz.h"
#include <library/cpp/testing/unittest/registar.h>
#include <library/cpp/resource/resource.h>
#include <util/stream/file.h>
#include <util/generic/vector.h>
#include <util/system/tempfile.h>
#include <util/generic/singleton.h>
#define LDATA "./ldata"
#define LDATA_RANDOM "./ldata.random"
static const TString data = "aa aaa aa aaa aa aaa bb bbb bb bbb bb bbb";
namespace {
/**
* Produces well-formed random crap
**/
TString RandomString(size_t size) {
TString entropy(NResource::Find("/random.data"));
TString result;
size_t seed = 1;
size_t j = 0;
for (size_t i = 0; i < size; ++i) {
seed *= 3;
char sym;
do {
sym = char((seed ^ i) % 256);
if (!sym) {
seed += 1;
}
} while (!sym);
Y_ASSERT(sym);
j = (j + 1) % entropy.size();
result += char(sym + entropy[j]);
}
return result;
}
TVector<TString> InitRandomData() {
static const TVector<size_t> sizes = {
0,
1,
127,
2017,
32767,
};
TVector<TString> result;
for (auto size : sizes) {
result.push_back(RandomString(size));
}
result.push_back(NResource::Find("/request.data"));
return result;
}
TString TestFileName(const TString& d, size_t bufferSize) {
return LDATA_RANDOM + TString(".") + ToString(d.size()) + TString(".") + ToString(bufferSize);
}
struct TRandomData: public TVector<TString> {
inline TRandomData() {
InitRandomData().swap(*this);
}
};
}
static const TVector<size_t> bufferSizes = {
127,
1024,
32768,
};
namespace {
template <TLzqCompress::EVersion Ver, int Level, TLzqCompress::EMode Mode>
struct TLzqCompressX: public TLzqCompress {
inline TLzqCompressX(IOutputStream* out, size_t bufLen)
: TLzqCompress(out, bufLen, Ver, Level, Mode)
{
}
};
}
template <class C>
static inline void TestGoodDataCompress() {
TFixedBufferFileOutput o(LDATA);
C c(&o, 1024);
TString d = data;
for (size_t i = 0; i < 10; ++i) {
c.Write(d.data(), d.size());
c << Endl;
d = d + d;
}
c.Finish();
o.Finish();
}
template <class C>
static inline void TestIncompressibleDataCompress(const TString& d, size_t bufferSize) {
TString testFileName = TestFileName(d, bufferSize);
TFixedBufferFileOutput o(testFileName);
C c(&o, bufferSize);
c.Write(d.data(), d.size());
c.Finish();
o.Finish();
}
template <class C>
static inline void TestCompress() {
TestGoodDataCompress<C>();
for (auto bufferSize : bufferSizes) {
for (auto rd : *Singleton<TRandomData>()) {
TestIncompressibleDataCompress<C>(rd, bufferSize);
}
}
}
template <class D>
static inline void TestGoodDataDecompress() {
TTempFile tmpFile(LDATA);
{
TFileInput i1(LDATA);
D ld(&i1);
TString d = data;
for (size_t i2 = 0; i2 < 10; ++i2) {
UNIT_ASSERT_EQUAL(ld.ReadLine(), d);
d = d + d;
}
}
}
template <class D>
static inline void TestIncompressibleDataDecompress(const TString& d, size_t bufferSize) {
TString testFileName = TestFileName(d, bufferSize);
TTempFile tmpFile(testFileName);
{
TFileInput i(testFileName);
D ld(&i);
UNIT_ASSERT_EQUAL(ld.ReadAll(), d);
}
}
template <class D>
static inline void TestDecompress() {
TestGoodDataDecompress<D>();
for (auto bufferSize : bufferSizes) {
for (auto rd : *Singleton<TRandomData>()) {
TestIncompressibleDataDecompress<D>(rd, bufferSize);
}
}
}
class TMixedDecompress: public IInputStream {
public:
TMixedDecompress(IInputStream* input)
: Slave_(OpenLzDecompressor(input).Release())
{
}
private:
size_t DoRead(void* buf, size_t len) override {
return Slave_->Read(buf, len);
}
private:
THolder<IInputStream> Slave_;
};
template <class C>
static inline void TestMixedDecompress() {
TestCompress<C>();
TestDecompress<TMixedDecompress>();
}
template <class D, class C>
static inline void TestDecompressError() {
TestCompress<C>();
UNIT_ASSERT_EXCEPTION(TestDecompress<D>(), TDecompressorError);
}
Y_UNIT_TEST_SUITE(TLzTest) {
Y_UNIT_TEST(TestLzo) {
TestCompress<TLzoCompress>();
TestDecompress<TLzoDecompress>();
}
Y_UNIT_TEST(TestLzf) {
TestCompress<TLzfCompress>();
TestDecompress<TLzfDecompress>();
}
Y_UNIT_TEST(TestLzq) {
TestCompress<TLzqCompress>();
TestDecompress<TLzqDecompress>();
}
Y_UNIT_TEST(TestLzq151_1) {
TestCompress<TLzqCompressX<TLzqCompress::V_1_51, 1, TLzqCompress::M_0>>();
TestDecompress<TLzqDecompress>();
}
Y_UNIT_TEST(TestLzq151_2) {
TestCompress<TLzqCompressX<TLzqCompress::V_1_51, 2, TLzqCompress::M_100000>>();
TestDecompress<TLzqDecompress>();
}
Y_UNIT_TEST(TestLzq151_3) {
TestCompress<TLzqCompressX<TLzqCompress::V_1_51, 3, TLzqCompress::M_1000000>>();
TestDecompress<TLzqDecompress>();
}
Y_UNIT_TEST(TestLzq140_1) {
TestCompress<TLzqCompressX<TLzqCompress::V_1_40, 1, TLzqCompress::M_0>>();
TestDecompress<TLzqDecompress>();
}
Y_UNIT_TEST(TestLzq140_2) {
TestCompress<TLzqCompressX<TLzqCompress::V_1_40, 2, TLzqCompress::M_100000>>();
TestDecompress<TLzqDecompress>();
}
Y_UNIT_TEST(TestLzq140_3) {
TestCompress<TLzqCompressX<TLzqCompress::V_1_40, 3, TLzqCompress::M_1000000>>();
TestDecompress<TLzqDecompress>();
}
Y_UNIT_TEST(TestLz4) {
TestCompress<TLz4Compress>();
TestDecompress<TLz4Decompress>();
}
Y_UNIT_TEST(TestSnappy) {
TestCompress<TSnappyCompress>();
TestDecompress<TSnappyDecompress>();
}
Y_UNIT_TEST(TestGeneric) {
TestMixedDecompress<TLzoCompress>();
TestMixedDecompress<TLzfCompress>();
TestMixedDecompress<TLzqCompress>();
TestMixedDecompress<TLz4Compress>();
TestMixedDecompress<TSnappyCompress>();
}
Y_UNIT_TEST(TestDecompressorError) {
TestDecompressError<TLzoDecompress, TLzfCompress>();
TestDecompressError<TLzfDecompress, TLzqCompress>();
TestDecompressError<TLzqDecompress, TLz4Compress>();
TestDecompressError<TLz4Decompress, TSnappyCompress>();
TestDecompressError<TSnappyDecompress, TBufferedOutput>();
TestDecompressError<TMixedDecompress, TBufferedOutput>();
}
Y_UNIT_TEST(TestFactory) {
TStringStream ss;
{
TLz4Compress c(&ss);
c.Write("123456789", 9);
c.Finish();
}
TAutoPtr<IInputStream> is(OpenOwnedLzDecompressor(new TStringInput(ss.Str())));
UNIT_ASSERT_EQUAL(is->ReadAll(), "123456789");
}
Y_UNIT_TEST(TestYQ609) {
auto data = NResource::Find("/yq_609.data");
TMemoryInput input(data.Data(), data.Size());
TLz4Decompress d(&input);
UNIT_ASSERT_EXCEPTION(d.ReadAll(), TDecompressorError);
}
}
|