-
Notifications
You must be signed in to change notification settings - Fork 1
/
rle-test.cc
361 lines (317 loc) · 9.04 KB
/
rle-test.cc
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright 2012 Cloudera Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <boost/utility.hpp>
#include <gtest/gtest.h>
#include <math.h>
#include "util/rle-encoding.h"
#include "util/bit-stream-utils.h"
#include "util/bit-stream-utils.inline.h"
using namespace std;
namespace impala {
TEST(BitArray, TestBool) {
const int len = 8;
uint8_t buffer[len];
BitWriter writer(buffer, len);
// Write alternating 0's and 1's
for (int i = 0; i < 8; ++i) {
bool result = writer.PutValue(i % 2, 1);
EXPECT_TRUE(result);
}
EXPECT_EQ((int)buffer[0], BOOST_BINARY(1 0 1 0 1 0 1 0));
// Write 00110011
for (int i = 0; i < 8; ++i) {
bool result = false;
switch (i) {
case 0:
case 1:
case 4:
case 5:
result = writer.PutValue(false, 1);
break;
default:
result = writer.PutValue(true, 1);
break;
}
EXPECT_TRUE(result);
}
// Validate the exact bit value
EXPECT_EQ((int)buffer[0], BOOST_BINARY(1 0 1 0 1 0 1 0));
EXPECT_EQ((int)buffer[1], BOOST_BINARY(1 1 0 0 1 1 0 0));
// Use the reader and validate
BitReader reader(buffer, len);
for (int i = 0; i < 8; ++i) {
bool val;
bool result = reader.GetValue(1, &val);
EXPECT_TRUE(result);
EXPECT_EQ(val, i % 2);
}
for (int i = 0; i < 8; ++i) {
bool val;
bool result = reader.GetValue(1, &val);
EXPECT_TRUE(result);
switch (i) {
case 0:
case 1:
case 4:
case 5:
EXPECT_EQ(val, false);
break;
default:
EXPECT_EQ(val, true);
break;
}
}
}
// Writes 'num_vals' values with width 'bit_width' and reads them back.
void TestBitArrayValues(int bit_width, int num_vals) {
const int len = BitUtil::Ceil(bit_width * num_vals, 8);
const uint64_t mod = bit_width == 64? 1 : 1LL << bit_width;
uint8_t buffer[len];
BitWriter writer(buffer, len);
for (int i = 0; i < num_vals; ++i) {
bool result = writer.PutValue(i % mod, bit_width);
EXPECT_TRUE(result);
}
EXPECT_EQ(writer.bytes_written(), len);
BitReader reader(buffer, len);
for (int i = 0; i < num_vals; ++i) {
int64_t val;
bool result = reader.GetValue(bit_width, &val);
EXPECT_TRUE(result);
EXPECT_EQ(val, i % mod);
}
EXPECT_EQ(reader.bytes_left(), 0);
}
TEST(BitArray, TestValues) {
for (int width = 1; width <= 64; ++width) {
TestBitArrayValues(width, 1);
TestBitArrayValues(width, 2);
// Don't write too many values
TestBitArrayValues(width, (width < 12) ? (1 << width) : 4096);
TestBitArrayValues(width, 1024);
}
}
// Test some mixed values
TEST(BitArray, TestMixed) {
const int len = 1024;
uint8_t buffer[len];
bool parity = true;
BitWriter writer(buffer, len);
for (int i = 0; i < len; ++i) {
bool result;
if (i % 2 == 0) {
result = writer.PutValue(parity, 1);
parity = !parity;
} else {
result = writer.PutValue(i, 10);
}
EXPECT_TRUE(result);
}
parity = true;
BitReader reader(buffer, len);
for (int i = 0; i < len; ++i) {
bool result;
if (i % 2 == 0) {
bool val;
result = reader.GetValue(1, &val);
EXPECT_EQ(val, parity);
parity = !parity;
} else {
int val;
result = reader.GetValue(10, &val);
EXPECT_EQ(val, i);
}
EXPECT_TRUE(result);
}
}
// Validates encoding of values by encoding and decoding them. If
// expected_encoding != NULL, also validates that the encoded buffer is
// exactly 'expected_encoding'.
// if expected_len is not -1, it will validate the encoded size is correct.
void ValidateRle(const vector<int>& values, int bit_width,
uint8_t* expected_encoding, int expected_len) {
const int len = 64 * 1024;
uint8_t buffer[len];
EXPECT_LE(expected_len, len);
RleEncoder encoder(buffer, len, bit_width);
for (int i = 0; i < values.size(); ++i) {
bool result = encoder.Put(values[i]);
EXPECT_TRUE(result);
}
int encoded_len = encoder.Flush();
if (expected_len != -1) {
EXPECT_EQ(encoded_len, expected_len);
}
if (expected_encoding != NULL) {
EXPECT_TRUE(memcmp(buffer, expected_encoding, expected_len) == 0);
}
// Verify read
RleDecoder decoder(buffer, len, bit_width);
for (int i = 0; i < values.size(); ++i) {
uint64_t val;
bool result = decoder.Get(&val);
EXPECT_TRUE(result);
EXPECT_EQ(values[i], val);
}
}
TEST(Rle, SpecificSequences) {
const int len = 1024;
uint8_t expected_buffer[len];
vector<int> values;
// Test 50 0' followed by 50 1's
values.resize(100);
for (int i = 0; i < 50; ++i) {
values[i] = 0;
}
for (int i = 50; i < 100; ++i) {
values[i] = 1;
}
// expected_buffer valid for bit width <= 1 byte
expected_buffer[0] = (50 << 1);
expected_buffer[1] = 0;
expected_buffer[2] = (50 << 1);
expected_buffer[3] = 1;
for (int width = 1; width <= 8; ++width) {
ValidateRle(values, width, expected_buffer, 4);
}
for (int width = 9; width <= 64; ++width) {
ValidateRle(values, width, NULL, 2 * (1 + BitUtil::Ceil(width, 8)));
}
// Test 100 0's and 1's alternating
for (int i = 0; i < 100; ++i) {
values[i] = i % 2;
}
int num_groups = BitUtil::Ceil(100, 8);
expected_buffer[0] = (num_groups << 1) | 1;
for (int i = 0; i < 100/8; ++i) {
expected_buffer[i + 1] = BOOST_BINARY(1 0 1 0 1 0 1 0);
}
// Values for the last 4 0 and 1's
expected_buffer[1 + 100/8] = BOOST_BINARY(0 0 0 0 1 0 1 0);
// num_groups and expected_buffer only valid for bit width = 1
ValidateRle(values, 1, expected_buffer, 1 + num_groups);
for (int width = 2; width <= 64; ++width) {
ValidateRle(values, width, NULL, 1 + BitUtil::Ceil(width * 100, 8));
}
}
// ValidateRle on 'num_vals' values with width 'bit_width'. If 'value' != -1, that value
// is used, otherwise alternating values are used.
void TestRleValues(int bit_width, int num_vals, int value = -1) {
const uint64_t mod = (bit_width == 64) ? 1 : 1LL << bit_width;
vector<int> values;
for (int v = 0; v < num_vals; ++v) {
values.push_back((value != -1) ? value : (v % mod));
}
ValidateRle(values, bit_width, NULL, -1);
}
TEST(Rle, TestValues) {
for (int width = 1; width <= 64; ++width) {
TestRleValues(width, 1);
TestRleValues(width, 1024);
TestRleValues(width, 1024, 0);
TestRleValues(width, 1024, 1);
}
}
// Test that writes out a repeated group and then a literal
// group but flush before finishing.
TEST(BitRle, Flush) {
vector<int> values;
for (int i = 0; i < 16; ++i) values.push_back(1);
values.push_back(0);
ValidateRle(values, 1, NULL, -1);
values.push_back(1);
ValidateRle(values, 1, NULL, -1);
values.push_back(1);
ValidateRle(values, 1, NULL, -1);
values.push_back(1);
ValidateRle(values, 1, NULL, -1);
}
// Test some random sequences.
TEST(BitRle, Random) {
int iters = 0;
while (iters < 1000) {
srand(iters++);
if (iters % 10000 == 0) LOG(ERROR) << "Seed: " << iters;
vector<int> values;
bool parity = 0;
for (int i = 0; i < 1000; ++i) {
int group_size = rand() % 20 + 1;
if (group_size > 16) {
group_size = 1;
}
for (int i = 0; i < group_size; ++i) {
values.push_back(parity);
}
parity = !parity;
}
ValidateRle(values, (iters % 64) + 1, NULL, -1);
}
}
// Test a sequence of 1 0's, 2 1's, 3 0's. etc
// e.g. 011000111100000
TEST(BitRle, RepeatedPattern) {
vector<int> values;
const int min_run = 1;
const int max_run = 32;
for (int i = min_run; i <= max_run; ++i) {
int v = i % 2;
for (int j = 0; j < i; ++j) {
values.push_back(v);
}
}
// And go back down again
for (int i = max_run; i >= min_run; --i) {
int v = i % 2;
for (int j = 0; j < i; ++j) {
values.push_back(v);
}
}
ValidateRle(values, 1, NULL, -1);
}
TEST(BitRle, Overflow) {
// TODO: test overflow
return;
const int len = 16;
uint8_t buffer[len];
int num_added = 0;
bool parity = true;
RleEncoder encoder(buffer, len, 1);
// Insert alternating true/false until there is no space left
while (true) {
bool result = encoder.Put(parity);
parity = !parity;
if (!result) break;
++num_added;
}
int bytes_written = encoder.Flush();
EXPECT_LE(bytes_written, len);
EXPECT_GT(num_added, 0);
RleDecoder decoder(buffer, bytes_written, 1);
parity = true;
for (int i = 0; i < num_added; ++i) {
uint8_t v;
bool result = decoder.Get(&v);
EXPECT_TRUE(result);
EXPECT_EQ(v, parity);
parity = !parity;
}
}
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}