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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
// Copyright 2008 Google Inc.
// Author: Lincoln Smith
//
// 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.
//
// Unit tests for the class VCDiffCodeTableReader, found in decodetable.h.
#include <config.h>
#include "decodetable.h"
#include <stdint.h> // int32_t
#include <vector>
#include "addrcache.h"
#include "codetable.h"
#include "testing.h"
#include "varint_bigendian.h"
namespace open_vcdiff {
namespace {
class DecodeTableTest : public testing::Test {
protected:
DecodeTableTest()
: instructions_and_sizes_(instruction_buffer_size),
found_size_(0),
found_mode_(0) {
instructions_and_sizes_ptr_ = &instructions_and_sizes_[0];
reader_.Init(&instructions_and_sizes_ptr_,
instructions_and_sizes_ptr_ + instruction_buffer_size);
}
static void AddExerciseOpcode(unsigned char inst1,
unsigned char mode1,
unsigned char size1,
unsigned char inst2,
unsigned char mode2,
unsigned char size2,
int opcode) {
g_exercise_code_table_->inst1[opcode] = inst1;
g_exercise_code_table_->mode1[opcode] = mode1;
g_exercise_code_table_->size1[opcode] = (inst1 == VCD_NOOP) ? 0 : size1;
g_exercise_code_table_->inst2[opcode] = inst2;
g_exercise_code_table_->mode2[opcode] = mode2;
g_exercise_code_table_->size2[opcode] = (inst2 == VCD_NOOP) ? 0 : size2;
}
static void SetUpTestCase() {
g_exercise_code_table_ = new VCDiffCodeTableData;
int opcode = 0;
for (unsigned char inst_mode1 = 0;
inst_mode1 <= VCD_LAST_INSTRUCTION_TYPE + kLastExerciseMode;
++inst_mode1) {
unsigned char inst1 = inst_mode1;
unsigned char mode1 = 0;
if (inst_mode1 > VCD_COPY) {
inst1 = VCD_COPY;
mode1 = inst_mode1 - VCD_COPY;
}
for (unsigned char inst_mode2 = 0;
inst_mode2 <= VCD_LAST_INSTRUCTION_TYPE + kLastExerciseMode;
++inst_mode2) {
unsigned char inst2 = inst_mode2;
unsigned char mode2 = 0;
if (inst_mode2 > VCD_COPY) {
inst2 = VCD_COPY;
mode2 = inst_mode2 - VCD_COPY;
}
AddExerciseOpcode(inst1, mode1, 0, inst2, mode2, 0, opcode++);
AddExerciseOpcode(inst1, mode1, 0, inst2, mode2, 255, opcode++);
AddExerciseOpcode(inst1, mode1, 255, inst2, mode2, 0, opcode++);
AddExerciseOpcode(inst1, mode1, 255, inst2, mode2, 255, opcode++);
}
}
CHECK_EQ(VCDiffCodeTableData::kCodeTableSize, opcode);
EXPECT_TRUE(VCDiffCodeTableData::kDefaultCodeTableData.Validate());
EXPECT_TRUE(g_exercise_code_table_->Validate(kLastExerciseMode));
}
static void TearDownTestCase() {
delete g_exercise_code_table_;
}
void VerifyInstModeSize(unsigned char inst,
unsigned char mode,
unsigned char size,
unsigned char opcode) {
if (inst == VCD_NOOP) return; // GetNextInstruction skips NOOPs
int32_t found_size = 0;
unsigned char found_mode = 0;
unsigned char found_inst = reader_.GetNextInstruction(&found_size,
&found_mode);
EXPECT_EQ(inst, found_inst);
EXPECT_EQ(mode, found_mode);
if (size == 0) {
EXPECT_EQ(1000 + opcode, found_size);
} else {
EXPECT_EQ(size, found_size);
}
}
void VerifyInstModeSize1(unsigned char inst,
unsigned char mode,
unsigned char size,
unsigned char opcode) {
if (inst == VCD_NOOP) size = 0;
EXPECT_EQ(g_exercise_code_table_->inst1[opcode], inst);
EXPECT_EQ(g_exercise_code_table_->mode1[opcode], mode);
EXPECT_EQ(g_exercise_code_table_->size1[opcode], size);
VerifyInstModeSize(inst, mode, size, opcode);
}
void VerifyInstModeSize2(unsigned char inst,
unsigned char mode,
unsigned char size,
unsigned char opcode) {
if (inst == VCD_NOOP) size = 0;
EXPECT_EQ(g_exercise_code_table_->inst2[opcode], inst);
EXPECT_EQ(g_exercise_code_table_->mode2[opcode], mode);
EXPECT_EQ(g_exercise_code_table_->size2[opcode], size);
VerifyInstModeSize(inst, mode, size, opcode);
}
// This value is designed so that the total number of inst values and modes
// will equal 8 (VCD_NOOP, VCD_ADD, VCD_RUN, VCD_COPY modes 0 - 4).
// Eight combinations of inst and mode, times two possible size values,
// squared (because there are two instructions per opcode), makes
// exactly 256 possible instruction combinations, which fits kCodeTableSize
// (the number of opcodes in the table.)
static const int kLastExerciseMode = 4;
// The buffer size (in bytes) needed to store kCodeTableSize opcodes plus
// up to kCodeTableSize VarintBE-encoded size values.
static const int instruction_buffer_size;
// A code table that exercises as many combinations as possible:
// 2 instructions, each is a NOOP, ADD, RUN, or one of 5 copy modes
// (== 8 total combinations of inst and mode), and each has
// size == 0 or 255 (2 possibilities.)
static VCDiffCodeTableData* g_exercise_code_table_;
VCDiffCodeTableReader reader_;
// A buffer to which instructions and sizes will be added manually
// in order to exercise VCDiffCodeTableReader.
std::vector<char> instructions_and_sizes_;
// The buffer pointer used by the VCDiffCodeTableReader.
const char* instructions_and_sizes_ptr_;
// The size and mode returned by GetNextInstruction().
int32_t found_size_;
unsigned char found_mode_;
};
VCDiffCodeTableData* DecodeTableTest::g_exercise_code_table_ = NULL;
const int DecodeTableTest::instruction_buffer_size =
VCDiffCodeTableData::kCodeTableSize *
(1 + (VarintBE<VCDAddress>::kMaxBytes));
TEST_F(DecodeTableTest, ReadAdd) {
instructions_and_sizes_[0] = 1;
VarintBE<VCDAddress>::Encode(257, &instructions_and_sizes_[1]);
unsigned char found_inst = reader_.GetNextInstruction(&found_size_,
&found_mode_);
EXPECT_EQ(VCD_ADD, found_inst);
EXPECT_EQ(257, found_size_);
EXPECT_EQ(0, found_mode_);
}
TEST_F(DecodeTableTest, ReadRun) {
instructions_and_sizes_[0] = 0;
VarintBE<VCDAddress>::Encode(111, &instructions_and_sizes_[1]);
unsigned char found_inst = reader_.GetNextInstruction(&found_size_,
&found_mode_);
EXPECT_EQ(VCD_RUN, found_inst);
EXPECT_EQ(111, found_size_);
EXPECT_EQ(0, found_mode_);
}
TEST_F(DecodeTableTest, ReadCopy) {
instructions_and_sizes_[0] = 58;
instructions_and_sizes_[1] = 0;
unsigned char found_inst = reader_.GetNextInstruction(&found_size_,
&found_mode_);
EXPECT_EQ(VCD_COPY, found_inst);
EXPECT_EQ(10, found_size_);
EXPECT_EQ(2, found_mode_);
}
TEST_F(DecodeTableTest, ReadAddCopy) {
instructions_and_sizes_[0] = 175;
instructions_and_sizes_[1] = 0;
unsigned char found_inst = reader_.GetNextInstruction(&found_size_,
&found_mode_);
EXPECT_EQ(VCD_ADD, found_inst);
EXPECT_EQ(1, found_size_);
EXPECT_EQ(0, found_mode_);
found_inst = reader_.GetNextInstruction(&found_size_, &found_mode_);
EXPECT_EQ(VCD_COPY, found_inst);
EXPECT_EQ(4, found_size_);
EXPECT_EQ(1, found_mode_);
}
TEST_F(DecodeTableTest, ReadCopyAdd) {
instructions_and_sizes_[0] = 255;
instructions_and_sizes_[1] = 0;
unsigned char found_inst = reader_.GetNextInstruction(&found_size_,
&found_mode_);
EXPECT_EQ(VCD_COPY, found_inst);
EXPECT_EQ(4, found_size_);
EXPECT_EQ(8, found_mode_);
found_mode_ = 0;
found_inst = reader_.GetNextInstruction(&found_size_, &found_mode_);
EXPECT_EQ(VCD_ADD, found_inst);
EXPECT_EQ(1, found_size_);
EXPECT_EQ(0, found_mode_);
}
TEST_F(DecodeTableTest, UnGetAdd) {
instructions_and_sizes_[0] = 1;
instructions_and_sizes_[1] = 255;
VarintBE<VCDAddress>::Encode(257, &instructions_and_sizes_[1]);
unsigned char found_inst = reader_.GetNextInstruction(&found_size_,
&found_mode_);
EXPECT_EQ(VCD_ADD, found_inst);
EXPECT_EQ(257, found_size_);
EXPECT_EQ(0, found_mode_);
reader_.UnGetInstruction();
found_size_ = 0;
found_inst = reader_.GetNextInstruction(&found_size_, &found_mode_);
EXPECT_EQ(VCD_ADD, found_inst);
EXPECT_EQ(257, found_size_);
EXPECT_EQ(0, found_mode_);
}
TEST_F(DecodeTableTest, UnGetCopy) {
instructions_and_sizes_[0] = 58;
instructions_and_sizes_[1] = 0;
instructions_and_sizes_[2] = 255;
unsigned char found_inst = reader_.GetNextInstruction(&found_size_,
&found_mode_);
EXPECT_EQ(VCD_COPY, found_inst);
EXPECT_EQ(10, found_size_);
EXPECT_EQ(2, found_mode_);
reader_.UnGetInstruction();
found_size_ = 0;
found_mode_ = 0;
found_inst = reader_.GetNextInstruction(&found_size_, &found_mode_);
EXPECT_EQ(VCD_COPY, found_inst);
EXPECT_EQ(10, found_size_);
EXPECT_EQ(2, found_mode_);
}
TEST_F(DecodeTableTest, UnGetCopyAdd) {
instructions_and_sizes_[0] = 255;
instructions_and_sizes_[1] = 0;
unsigned char found_inst = reader_.GetNextInstruction(&found_size_,
&found_mode_);
EXPECT_EQ(VCD_COPY, found_inst);
EXPECT_EQ(4, found_size_);
EXPECT_EQ(8, found_mode_);
reader_.UnGetInstruction();
found_mode_ = 0;
found_inst = reader_.GetNextInstruction(&found_size_, &found_mode_);
EXPECT_EQ(VCD_COPY, found_inst);
EXPECT_EQ(4, found_size_);
EXPECT_EQ(8, found_mode_);
found_mode_ = 0;
found_inst = reader_.GetNextInstruction(&found_size_, &found_mode_);
EXPECT_EQ(VCD_ADD, found_inst);
EXPECT_EQ(1, found_size_);
EXPECT_EQ(0, found_mode_);
}
TEST_F(DecodeTableTest, UnGetTwice) {
instructions_and_sizes_[0] = 255;
instructions_and_sizes_[1] = 0;
unsigned char found_inst = reader_.GetNextInstruction(&found_size_,
&found_mode_);
EXPECT_EQ(VCD_COPY, found_inst);
EXPECT_EQ(4, found_size_);
EXPECT_EQ(8, found_mode_);
reader_.UnGetInstruction();
reader_.UnGetInstruction();
found_mode_ = 0;
found_inst = reader_.GetNextInstruction(&found_size_, &found_mode_);
EXPECT_EQ(VCD_COPY, found_inst);
EXPECT_EQ(4, found_size_);
EXPECT_EQ(8, found_mode_);
found_mode_ = 0;
found_inst = reader_.GetNextInstruction(&found_size_, &found_mode_);
EXPECT_EQ(VCD_ADD, found_inst);
EXPECT_EQ(1, found_size_);
EXPECT_EQ(0, found_mode_);
}
TEST_F(DecodeTableTest, UnGetBeforeGet) {
instructions_and_sizes_[0] = 255;
instructions_and_sizes_[1] = 0;
reader_.UnGetInstruction();
unsigned char found_inst = reader_.GetNextInstruction(&found_size_,
&found_mode_);
EXPECT_EQ(VCD_COPY, found_inst);
EXPECT_EQ(4, found_size_);
EXPECT_EQ(8, found_mode_);
}
TEST_F(DecodeTableTest, UnGetAddCopy) {
instructions_and_sizes_[0] = 175;
instructions_and_sizes_[1] = 0;
unsigned char found_inst = reader_.GetNextInstruction(&found_size_,
&found_mode_);
EXPECT_EQ(VCD_ADD, found_inst);
EXPECT_EQ(1, found_size_);
EXPECT_EQ(0, found_mode_);
reader_.UnGetInstruction();
found_inst = reader_.GetNextInstruction(&found_size_, &found_mode_);
EXPECT_EQ(VCD_ADD, found_inst);
EXPECT_EQ(1, found_size_);
EXPECT_EQ(0, found_mode_);
found_inst = reader_.GetNextInstruction(&found_size_, &found_mode_);
EXPECT_EQ(VCD_COPY, found_inst);
EXPECT_EQ(4, found_size_);
EXPECT_EQ(1, found_mode_);
}
TEST_F(DecodeTableTest, ReReadIncomplete) {
instructions_and_sizes_[0] = 175; // Add(1) + Copy1(4)
instructions_and_sizes_[1] = 1; // Add(0)
instructions_and_sizes_[2] = 111; // with size 111
instructions_and_sizes_[3] = 255; // Copy8(4) + Add(1)
reader_.Init(&instructions_and_sizes_ptr_,
instructions_and_sizes_ptr_ + 0); // 0 bytes available
EXPECT_EQ(VCD_INSTRUCTION_END_OF_DATA,
reader_.GetNextInstruction(&found_size_, &found_mode_));
EXPECT_EQ(&instructions_and_sizes_[0], instructions_and_sizes_ptr_);
reader_.Init(&instructions_and_sizes_ptr_,
instructions_and_sizes_ptr_ + 1); // 1 more byte available
EXPECT_EQ(VCD_ADD, reader_.GetNextInstruction(&found_size_, &found_mode_));
EXPECT_EQ(1, found_size_);
EXPECT_EQ(0, found_mode_);
EXPECT_EQ(VCD_COPY, reader_.GetNextInstruction(&found_size_, &found_mode_));
EXPECT_EQ(4, found_size_);
EXPECT_EQ(1, found_mode_);
EXPECT_EQ(VCD_INSTRUCTION_END_OF_DATA,
reader_.GetNextInstruction(&found_size_, &found_mode_));
EXPECT_EQ(&instructions_and_sizes_[1], instructions_and_sizes_ptr_);
reader_.Init(&instructions_and_sizes_ptr_,
instructions_and_sizes_ptr_ + 1); // 1 more byte available
// The opcode is available, but the separately encoded size is not
EXPECT_EQ(VCD_INSTRUCTION_END_OF_DATA,
reader_.GetNextInstruction(&found_size_, &found_mode_));
EXPECT_EQ(&instructions_and_sizes_[1], instructions_and_sizes_ptr_);
reader_.Init(&instructions_and_sizes_ptr_,
instructions_and_sizes_ptr_ + 2); // 2 more bytes available
EXPECT_EQ(VCD_ADD, reader_.GetNextInstruction(&found_size_, &found_mode_));
EXPECT_EQ(111, found_size_);
EXPECT_EQ(0, found_mode_);
EXPECT_EQ(VCD_INSTRUCTION_END_OF_DATA,
reader_.GetNextInstruction(&found_size_, &found_mode_));
EXPECT_EQ(&instructions_and_sizes_[3], instructions_and_sizes_ptr_);
reader_.Init(&instructions_and_sizes_ptr_,
instructions_and_sizes_ptr_ + 1); // 1 more byte available
EXPECT_EQ(VCD_COPY, reader_.GetNextInstruction(&found_size_, &found_mode_));
EXPECT_EQ(4, found_size_);
EXPECT_EQ(8, found_mode_);
EXPECT_EQ(VCD_ADD, reader_.GetNextInstruction(&found_size_, &found_mode_));
EXPECT_EQ(1, found_size_);
EXPECT_EQ(0, found_mode_);
EXPECT_EQ(VCD_INSTRUCTION_END_OF_DATA,
reader_.GetNextInstruction(&found_size_, &found_mode_));
EXPECT_EQ(&instructions_and_sizes_[4], instructions_and_sizes_ptr_);
}
TEST_F(DecodeTableTest, ExerciseCodeTableReader) {
char* instruction_ptr = &instructions_and_sizes_[0];
for (int opcode = 0; opcode < VCDiffCodeTableData::kCodeTableSize; ++opcode) {
*instruction_ptr = opcode;
++instruction_ptr;
if ((g_exercise_code_table_->inst1[opcode] != VCD_NOOP) &&
(g_exercise_code_table_->size1[opcode] == 0)) {
// A separately-encoded size value
int encoded_size = VarintBE<VCDAddress>::Encode(1000 + opcode,
instruction_ptr);
EXPECT_LT(0, encoded_size);
instruction_ptr += encoded_size;
}
if ((g_exercise_code_table_->inst2[opcode] != VCD_NOOP) &&
(g_exercise_code_table_->size2[opcode] == 0)) {
int encoded_size = VarintBE<VCDAddress>::Encode(1000 + opcode,
instruction_ptr);
EXPECT_LT(0, encoded_size);
instruction_ptr += encoded_size;
}
}
EXPECT_TRUE(reader_.UseCodeTable(*g_exercise_code_table_, kLastExerciseMode));
int opcode = 0;
// This loop has the same bounds as the one in SetUpTestCase.
// Iterate over the instruction types and make sure that the opcodes,
// interpreted in order, return exactly those instruction types.
for (unsigned char inst_mode1 = 0;
inst_mode1 <= VCD_LAST_INSTRUCTION_TYPE + kLastExerciseMode;
++inst_mode1) {
unsigned char inst1 = inst_mode1;
unsigned char mode1 = 0;
if (inst_mode1 > VCD_COPY) {
inst1 = VCD_COPY;
mode1 = inst_mode1 - VCD_COPY;
}
for (unsigned char inst_mode2 = 0;
inst_mode2 <= VCD_LAST_INSTRUCTION_TYPE + kLastExerciseMode;
++inst_mode2) {
unsigned char inst2 = inst_mode2;
unsigned char mode2 = 0;
if (inst_mode2 > VCD_COPY) {
inst2 = VCD_COPY;
mode2 = inst_mode2 - VCD_COPY;
}
VerifyInstModeSize1(inst1, mode1, 0, opcode);
VerifyInstModeSize2(inst2, mode2, 0, opcode);
++opcode;
VerifyInstModeSize1(inst1, mode1, 0, opcode);
VerifyInstModeSize2(inst2, mode2, 255, opcode);
++opcode;
VerifyInstModeSize1(inst1, mode1, 255, opcode);
VerifyInstModeSize2(inst2, mode2, 0, opcode);
++opcode;
VerifyInstModeSize1(inst1, mode1, 255, opcode);
VerifyInstModeSize2(inst2, mode2, 255, opcode);
++opcode;
}
}
CHECK_EQ(VCDiffCodeTableData::kCodeTableSize, opcode);
}
} // unnamed namespace
} // namespace open_vcdiff
|