1// Copyright 2008 Google Inc.
2// Author: Lincoln Smith
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//      http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16// Unit tests for the class VCDiffInstructionMap, found in instruction_map.h.
17
18#include <config.h>
19#include "instruction_map.h"
20#include "codetable.h"
21#include "testing.h"
22#include "vcdiff_defs.h"
23
24namespace open_vcdiff {
25namespace {
26
27class InstructionMapTest : public testing::Test {
28 protected:
29  static void SetUpTestCase();
30  static void TearDownTestCase();
31
32  static void AddExerciseOpcode(unsigned char inst1,
33                                unsigned char mode1,
34                                unsigned char size1,
35                                unsigned char inst2,
36                                unsigned char mode2,
37                                unsigned char size2,
38                                int opcode);
39
40  void VerifyExerciseFirstInstruction(unsigned char expected_opcode,
41                                      unsigned char inst,
42                                      unsigned char size,
43                                      unsigned char mode);
44
45  void VerifyExerciseSecondInstruction(unsigned char expected_opcode,
46                                       unsigned char inst1,
47                                       unsigned char size1,
48                                       unsigned char mode1,
49                                       unsigned char inst2,
50                                       unsigned char size2,
51                                       unsigned char mode2);
52
53  // This value is designed so that the total number of inst values and modes
54  // will equal 8 (VCD_NOOP, VCD_ADD, VCD_RUN, VCD_COPY modes 0 - 4).
55  // Eight combinations of inst and mode, times two possible size values,
56  // squared (because there are two instructions per opcode), makes
57  // exactly 256 possible instruction combinations, which fits kCodeTableSize
58  // (the number of opcodes in the table.)
59  static const int kLastExerciseMode = 4;
60
61  // A code table that exercises as many combinations as possible:
62  // 2 instructions, each is a NOOP, ADD, RUN, or one of 5 copy modes
63  // (== 8 total combinations of inst and mode), and each has
64  // size == 0 or 255 (2 possibilities.)
65  static VCDiffCodeTableData* g_exercise_code_table_;
66
67  // The instruction map corresponding to kDefaultCodeTableData.
68  static const VCDiffInstructionMap* default_map;
69
70  // The instruction map corresponding to g_exercise_code_table_.
71  static const VCDiffInstructionMap* exercise_map;
72
73  size_t out_index;
74};
75
76VCDiffCodeTableData* InstructionMapTest::g_exercise_code_table_ = NULL;
77const VCDiffInstructionMap* InstructionMapTest::default_map = NULL;
78const VCDiffInstructionMap* InstructionMapTest::exercise_map = NULL;
79
80void InstructionMapTest::SetUpTestCase() {
81  g_exercise_code_table_ = new VCDiffCodeTableData;
82  int opcode = 0;
83  for (unsigned char inst_mode1 = 0;
84       inst_mode1 <= VCD_LAST_INSTRUCTION_TYPE + kLastExerciseMode;
85       ++inst_mode1) {
86    unsigned char inst1 = inst_mode1;
87    unsigned char mode1 = 0;
88    if (inst_mode1 > VCD_COPY) {
89      inst1 = VCD_COPY;
90      mode1 = inst_mode1 - VCD_COPY;
91    }
92    for (unsigned char inst_mode2 = 0;
93         inst_mode2 <= VCD_LAST_INSTRUCTION_TYPE + kLastExerciseMode;
94         ++inst_mode2) {
95      unsigned char inst2 = inst_mode2;
96      unsigned char mode2 = 0;
97      if (inst_mode2 > VCD_COPY) {
98        inst2 = VCD_COPY;
99        mode2 = inst_mode2 - VCD_COPY;
100      }
101      AddExerciseOpcode(inst1, mode1, 0, inst2, mode2, 0, opcode++);
102      AddExerciseOpcode(inst1, mode1, 0, inst2, mode2, 255, opcode++);
103      AddExerciseOpcode(inst1, mode1, 255, inst2, mode2, 0, opcode++);
104      AddExerciseOpcode(inst1, mode1, 255, inst2, mode2, 255, opcode++);
105    }
106  }
107  // This is a CHECK rather than an EXPECT because it validates only
108  // the logic of the test, not of the code being tested.
109  CHECK_EQ(VCDiffCodeTableData::kCodeTableSize, opcode);
110
111  EXPECT_TRUE(VCDiffCodeTableData::kDefaultCodeTableData.Validate());
112  EXPECT_TRUE(g_exercise_code_table_->Validate(kLastExerciseMode));
113  default_map = VCDiffInstructionMap::GetDefaultInstructionMap();
114  exercise_map = new VCDiffInstructionMap(*g_exercise_code_table_,
115                                          kLastExerciseMode);
116}
117
118void InstructionMapTest::TearDownTestCase() {
119  delete exercise_map;
120  delete g_exercise_code_table_;
121}
122
123void InstructionMapTest::AddExerciseOpcode(unsigned char inst1,
124                                           unsigned char mode1,
125                                           unsigned char size1,
126                                           unsigned char inst2,
127                                           unsigned char mode2,
128                                           unsigned char size2,
129                                           int opcode) {
130  g_exercise_code_table_->inst1[opcode] = inst1;
131  g_exercise_code_table_->mode1[opcode] = mode1;
132  g_exercise_code_table_->size1[opcode] = (inst1 == VCD_NOOP) ? 0 : size1;
133  g_exercise_code_table_->inst2[opcode] = inst2;
134  g_exercise_code_table_->mode2[opcode] = mode2;
135  g_exercise_code_table_->size2[opcode] = (inst2 == VCD_NOOP) ? 0 : size2;
136}
137
138void InstructionMapTest::VerifyExerciseFirstInstruction(
139    unsigned char expected_opcode,
140    unsigned char inst,
141    unsigned char size,
142    unsigned char mode) {
143  int found_opcode = exercise_map->LookupFirstOpcode(inst, size, mode);
144  if (g_exercise_code_table_->inst1[found_opcode] == VCD_NOOP) {
145    // The opcode is backwards: (VCD_NOOP, [instruction])
146    EXPECT_GE(expected_opcode, found_opcode);
147    EXPECT_EQ(inst, g_exercise_code_table_->inst2[found_opcode]);
148    EXPECT_EQ(size, g_exercise_code_table_->size2[found_opcode]);
149    EXPECT_EQ(mode, g_exercise_code_table_->mode2[found_opcode]);
150    EXPECT_EQ(VCD_NOOP, g_exercise_code_table_->inst1[found_opcode]);
151    EXPECT_EQ(0, g_exercise_code_table_->size1[found_opcode]);
152    EXPECT_EQ(0, g_exercise_code_table_->mode1[found_opcode]);
153  } else {
154    EXPECT_EQ(expected_opcode, found_opcode);
155    EXPECT_EQ(inst, g_exercise_code_table_->inst1[found_opcode]);
156    EXPECT_EQ(size, g_exercise_code_table_->size1[found_opcode]);
157    EXPECT_EQ(mode, g_exercise_code_table_->mode1[found_opcode]);
158    EXPECT_EQ(VCD_NOOP, g_exercise_code_table_->inst2[found_opcode]);
159    EXPECT_EQ(0, g_exercise_code_table_->size2[found_opcode]);
160    EXPECT_EQ(0, g_exercise_code_table_->mode2[found_opcode]);
161  }
162}
163
164void InstructionMapTest::VerifyExerciseSecondInstruction(
165    unsigned char expected_opcode,
166    unsigned char inst1,
167    unsigned char size1,
168    unsigned char mode1,
169    unsigned char inst2,
170    unsigned char size2,
171    unsigned char mode2) {
172  int first_opcode = exercise_map->LookupFirstOpcode(inst1, size1, mode1);
173  EXPECT_NE(kNoOpcode, first_opcode);
174  EXPECT_EQ(expected_opcode,
175            exercise_map->LookupSecondOpcode(first_opcode,
176                                             inst2,
177                                             size2,
178                                             mode2));
179}
180
181TEST_F(InstructionMapTest, DefaultMapLookupFirstNoop) {
182  EXPECT_EQ(kNoOpcode, default_map->LookupFirstOpcode(VCD_NOOP, 0, 0));
183  EXPECT_EQ(kNoOpcode, default_map->LookupFirstOpcode(VCD_NOOP, 0, 255));
184  EXPECT_EQ(kNoOpcode, default_map->LookupFirstOpcode(VCD_NOOP, 255, 0));
185  EXPECT_EQ(kNoOpcode, default_map->LookupFirstOpcode(VCD_NOOP, 255, 255));
186}
187
188TEST_F(InstructionMapTest, DefaultMapLookupFirstAdd) {
189  EXPECT_EQ(2, default_map->LookupFirstOpcode(VCD_ADD, 1, 0));
190  EXPECT_EQ(3, default_map->LookupFirstOpcode(VCD_ADD, 2, 0));
191  EXPECT_EQ(4, default_map->LookupFirstOpcode(VCD_ADD, 3, 0));
192  EXPECT_EQ(5, default_map->LookupFirstOpcode(VCD_ADD, 4, 0));
193  EXPECT_EQ(6, default_map->LookupFirstOpcode(VCD_ADD, 5, 0));
194  EXPECT_EQ(7, default_map->LookupFirstOpcode(VCD_ADD, 6, 0));
195  EXPECT_EQ(8, default_map->LookupFirstOpcode(VCD_ADD, 7, 0));
196  EXPECT_EQ(9, default_map->LookupFirstOpcode(VCD_ADD, 8, 0));
197  EXPECT_EQ(10, default_map->LookupFirstOpcode(VCD_ADD, 9, 0));
198  EXPECT_EQ(11, default_map->LookupFirstOpcode(VCD_ADD, 10, 0));
199  EXPECT_EQ(12, default_map->LookupFirstOpcode(VCD_ADD, 11, 0));
200  EXPECT_EQ(13, default_map->LookupFirstOpcode(VCD_ADD, 12, 0));
201  EXPECT_EQ(14, default_map->LookupFirstOpcode(VCD_ADD, 13, 0));
202  EXPECT_EQ(15, default_map->LookupFirstOpcode(VCD_ADD, 14, 0));
203  EXPECT_EQ(16, default_map->LookupFirstOpcode(VCD_ADD, 15, 0));
204  EXPECT_EQ(17, default_map->LookupFirstOpcode(VCD_ADD, 16, 0));
205  EXPECT_EQ(18, default_map->LookupFirstOpcode(VCD_ADD, 17, 0));
206  EXPECT_EQ(kNoOpcode, default_map->LookupFirstOpcode(VCD_ADD, 100, 0));
207  EXPECT_EQ(kNoOpcode, default_map->LookupFirstOpcode(VCD_ADD, 255, 0));
208  EXPECT_EQ(1, default_map->LookupFirstOpcode(VCD_ADD, 0, 0));
209  // Value of "mode" should not matter
210  EXPECT_EQ(2, default_map->LookupFirstOpcode(VCD_ADD, 1, 2));
211  EXPECT_EQ(2, default_map->LookupFirstOpcode(VCD_ADD, 1, 255));
212}
213
214TEST_F(InstructionMapTest, DefaultMapLookupFirstRun) {
215  EXPECT_EQ(0, default_map->LookupFirstOpcode(VCD_RUN, 0, 0));
216  EXPECT_EQ(kNoOpcode, default_map->LookupFirstOpcode(VCD_RUN, 1, 0));
217  EXPECT_EQ(kNoOpcode, default_map->LookupFirstOpcode(VCD_RUN, 255, 0));
218  // Value of "mode" should not matter
219  EXPECT_EQ(0, default_map->LookupFirstOpcode(VCD_RUN, 0, 2));
220}
221
222TEST_F(InstructionMapTest, DefaultMapLookupFirstCopyMode0) {
223  EXPECT_EQ(19, default_map->LookupFirstOpcode(VCD_COPY, 0, 0));
224  EXPECT_EQ(20, default_map->LookupFirstOpcode(VCD_COPY, 4, 0));
225  EXPECT_EQ(21, default_map->LookupFirstOpcode(VCD_COPY, 5, 0));
226  EXPECT_EQ(22, default_map->LookupFirstOpcode(VCD_COPY, 6, 0));
227  EXPECT_EQ(23, default_map->LookupFirstOpcode(VCD_COPY, 7, 0));
228  EXPECT_EQ(24, default_map->LookupFirstOpcode(VCD_COPY, 8, 0));
229  EXPECT_EQ(25, default_map->LookupFirstOpcode(VCD_COPY, 9, 0));
230  EXPECT_EQ(26, default_map->LookupFirstOpcode(VCD_COPY, 10, 0));
231  EXPECT_EQ(27, default_map->LookupFirstOpcode(VCD_COPY, 11, 0));
232  EXPECT_EQ(28, default_map->LookupFirstOpcode(VCD_COPY, 12, 0));
233  EXPECT_EQ(29, default_map->LookupFirstOpcode(VCD_COPY, 13, 0));
234  EXPECT_EQ(30, default_map->LookupFirstOpcode(VCD_COPY, 14, 0));
235  EXPECT_EQ(31, default_map->LookupFirstOpcode(VCD_COPY, 15, 0));
236  EXPECT_EQ(32, default_map->LookupFirstOpcode(VCD_COPY, 16, 0));
237  EXPECT_EQ(33, default_map->LookupFirstOpcode(VCD_COPY, 17, 0));
238  EXPECT_EQ(34, default_map->LookupFirstOpcode(VCD_COPY, 18, 0));
239}
240
241TEST_F(InstructionMapTest, DefaultMapLookupFirstCopyMode1) {
242  EXPECT_EQ(35, default_map->LookupFirstOpcode(VCD_COPY, 0, 1));
243  EXPECT_EQ(36, default_map->LookupFirstOpcode(VCD_COPY, 4, 1));
244  EXPECT_EQ(37, default_map->LookupFirstOpcode(VCD_COPY, 5, 1));
245  EXPECT_EQ(38, default_map->LookupFirstOpcode(VCD_COPY, 6, 1));
246  EXPECT_EQ(39, default_map->LookupFirstOpcode(VCD_COPY, 7, 1));
247  EXPECT_EQ(40, default_map->LookupFirstOpcode(VCD_COPY, 8, 1));
248  EXPECT_EQ(41, default_map->LookupFirstOpcode(VCD_COPY, 9, 1));
249  EXPECT_EQ(42, default_map->LookupFirstOpcode(VCD_COPY, 10, 1));
250  EXPECT_EQ(43, default_map->LookupFirstOpcode(VCD_COPY, 11, 1));
251  EXPECT_EQ(44, default_map->LookupFirstOpcode(VCD_COPY, 12, 1));
252  EXPECT_EQ(45, default_map->LookupFirstOpcode(VCD_COPY, 13, 1));
253  EXPECT_EQ(46, default_map->LookupFirstOpcode(VCD_COPY, 14, 1));
254  EXPECT_EQ(47, default_map->LookupFirstOpcode(VCD_COPY, 15, 1));
255  EXPECT_EQ(48, default_map->LookupFirstOpcode(VCD_COPY, 16, 1));
256  EXPECT_EQ(49, default_map->LookupFirstOpcode(VCD_COPY, 17, 1));
257  EXPECT_EQ(50, default_map->LookupFirstOpcode(VCD_COPY, 18, 1));
258}
259
260TEST_F(InstructionMapTest, DefaultMapLookupFirstCopyMode2) {
261  EXPECT_EQ(51, default_map->LookupFirstOpcode(VCD_COPY, 0, 2));
262  EXPECT_EQ(52, default_map->LookupFirstOpcode(VCD_COPY, 4, 2));
263  EXPECT_EQ(53, default_map->LookupFirstOpcode(VCD_COPY, 5, 2));
264  EXPECT_EQ(54, default_map->LookupFirstOpcode(VCD_COPY, 6, 2));
265  EXPECT_EQ(55, default_map->LookupFirstOpcode(VCD_COPY, 7, 2));
266  EXPECT_EQ(56, default_map->LookupFirstOpcode(VCD_COPY, 8, 2));
267  EXPECT_EQ(57, default_map->LookupFirstOpcode(VCD_COPY, 9, 2));
268  EXPECT_EQ(58, default_map->LookupFirstOpcode(VCD_COPY, 10, 2));
269  EXPECT_EQ(59, default_map->LookupFirstOpcode(VCD_COPY, 11, 2));
270  EXPECT_EQ(60, default_map->LookupFirstOpcode(VCD_COPY, 12, 2));
271  EXPECT_EQ(61, default_map->LookupFirstOpcode(VCD_COPY, 13, 2));
272  EXPECT_EQ(62, default_map->LookupFirstOpcode(VCD_COPY, 14, 2));
273  EXPECT_EQ(63, default_map->LookupFirstOpcode(VCD_COPY, 15, 2));
274  EXPECT_EQ(64, default_map->LookupFirstOpcode(VCD_COPY, 16, 2));
275  EXPECT_EQ(65, default_map->LookupFirstOpcode(VCD_COPY, 17, 2));
276  EXPECT_EQ(66, default_map->LookupFirstOpcode(VCD_COPY, 18, 2));
277}
278
279TEST_F(InstructionMapTest, DefaultMapLookupFirstCopyMode3) {
280  EXPECT_EQ(67, default_map->LookupFirstOpcode(VCD_COPY, 0, 3));
281  EXPECT_EQ(68, default_map->LookupFirstOpcode(VCD_COPY, 4, 3));
282  EXPECT_EQ(69, default_map->LookupFirstOpcode(VCD_COPY, 5, 3));
283  EXPECT_EQ(70, default_map->LookupFirstOpcode(VCD_COPY, 6, 3));
284  EXPECT_EQ(71, default_map->LookupFirstOpcode(VCD_COPY, 7, 3));
285  EXPECT_EQ(72, default_map->LookupFirstOpcode(VCD_COPY, 8, 3));
286  EXPECT_EQ(73, default_map->LookupFirstOpcode(VCD_COPY, 9, 3));
287  EXPECT_EQ(74, default_map->LookupFirstOpcode(VCD_COPY, 10, 3));
288  EXPECT_EQ(75, default_map->LookupFirstOpcode(VCD_COPY, 11, 3));
289  EXPECT_EQ(76, default_map->LookupFirstOpcode(VCD_COPY, 12, 3));
290  EXPECT_EQ(77, default_map->LookupFirstOpcode(VCD_COPY, 13, 3));
291  EXPECT_EQ(78, default_map->LookupFirstOpcode(VCD_COPY, 14, 3));
292  EXPECT_EQ(79, default_map->LookupFirstOpcode(VCD_COPY, 15, 3));
293  EXPECT_EQ(80, default_map->LookupFirstOpcode(VCD_COPY, 16, 3));
294  EXPECT_EQ(81, default_map->LookupFirstOpcode(VCD_COPY, 17, 3));
295  EXPECT_EQ(82, default_map->LookupFirstOpcode(VCD_COPY, 18, 3));
296}
297
298TEST_F(InstructionMapTest, DefaultMapLookupFirstCopyMode4) {
299  EXPECT_EQ(83, default_map->LookupFirstOpcode(VCD_COPY, 0, 4));
300  EXPECT_EQ(84, default_map->LookupFirstOpcode(VCD_COPY, 4, 4));
301  EXPECT_EQ(85, default_map->LookupFirstOpcode(VCD_COPY, 5, 4));
302  EXPECT_EQ(86, default_map->LookupFirstOpcode(VCD_COPY, 6, 4));
303  EXPECT_EQ(87, default_map->LookupFirstOpcode(VCD_COPY, 7, 4));
304  EXPECT_EQ(88, default_map->LookupFirstOpcode(VCD_COPY, 8, 4));
305  EXPECT_EQ(89, default_map->LookupFirstOpcode(VCD_COPY, 9, 4));
306  EXPECT_EQ(90, default_map->LookupFirstOpcode(VCD_COPY, 10, 4));
307  EXPECT_EQ(91, default_map->LookupFirstOpcode(VCD_COPY, 11, 4));
308  EXPECT_EQ(92, default_map->LookupFirstOpcode(VCD_COPY, 12, 4));
309  EXPECT_EQ(93, default_map->LookupFirstOpcode(VCD_COPY, 13, 4));
310  EXPECT_EQ(94, default_map->LookupFirstOpcode(VCD_COPY, 14, 4));
311  EXPECT_EQ(95, default_map->LookupFirstOpcode(VCD_COPY, 15, 4));
312  EXPECT_EQ(96, default_map->LookupFirstOpcode(VCD_COPY, 16, 4));
313  EXPECT_EQ(97, default_map->LookupFirstOpcode(VCD_COPY, 17, 4));
314  EXPECT_EQ(98, default_map->LookupFirstOpcode(VCD_COPY, 18, 4));
315}
316
317TEST_F(InstructionMapTest, DefaultMapLookupFirstCopyMode5) {
318  EXPECT_EQ(99, default_map->LookupFirstOpcode(VCD_COPY, 0, 5));
319  EXPECT_EQ(100, default_map->LookupFirstOpcode(VCD_COPY, 4, 5));
320  EXPECT_EQ(101, default_map->LookupFirstOpcode(VCD_COPY, 5, 5));
321  EXPECT_EQ(102, default_map->LookupFirstOpcode(VCD_COPY, 6, 5));
322  EXPECT_EQ(103, default_map->LookupFirstOpcode(VCD_COPY, 7, 5));
323  EXPECT_EQ(104, default_map->LookupFirstOpcode(VCD_COPY, 8, 5));
324  EXPECT_EQ(105, default_map->LookupFirstOpcode(VCD_COPY, 9, 5));
325  EXPECT_EQ(106, default_map->LookupFirstOpcode(VCD_COPY, 10, 5));
326  EXPECT_EQ(107, default_map->LookupFirstOpcode(VCD_COPY, 11, 5));
327  EXPECT_EQ(108, default_map->LookupFirstOpcode(VCD_COPY, 12, 5));
328  EXPECT_EQ(109, default_map->LookupFirstOpcode(VCD_COPY, 13, 5));
329  EXPECT_EQ(110, default_map->LookupFirstOpcode(VCD_COPY, 14, 5));
330  EXPECT_EQ(111, default_map->LookupFirstOpcode(VCD_COPY, 15, 5));
331  EXPECT_EQ(112, default_map->LookupFirstOpcode(VCD_COPY, 16, 5));
332  EXPECT_EQ(113, default_map->LookupFirstOpcode(VCD_COPY, 17, 5));
333  EXPECT_EQ(114, default_map->LookupFirstOpcode(VCD_COPY, 18, 5));
334}
335
336TEST_F(InstructionMapTest, DefaultMapLookupFirstCopyMode6) {
337  EXPECT_EQ(115, default_map->LookupFirstOpcode(VCD_COPY, 0, 6));
338  EXPECT_EQ(116, default_map->LookupFirstOpcode(VCD_COPY, 4, 6));
339  EXPECT_EQ(117, default_map->LookupFirstOpcode(VCD_COPY, 5, 6));
340  EXPECT_EQ(118, default_map->LookupFirstOpcode(VCD_COPY, 6, 6));
341  EXPECT_EQ(119, default_map->LookupFirstOpcode(VCD_COPY, 7, 6));
342  EXPECT_EQ(120, default_map->LookupFirstOpcode(VCD_COPY, 8, 6));
343  EXPECT_EQ(121, default_map->LookupFirstOpcode(VCD_COPY, 9, 6));
344  EXPECT_EQ(122, default_map->LookupFirstOpcode(VCD_COPY, 10, 6));
345  EXPECT_EQ(123, default_map->LookupFirstOpcode(VCD_COPY, 11, 6));
346  EXPECT_EQ(124, default_map->LookupFirstOpcode(VCD_COPY, 12, 6));
347  EXPECT_EQ(125, default_map->LookupFirstOpcode(VCD_COPY, 13, 6));
348  EXPECT_EQ(126, default_map->LookupFirstOpcode(VCD_COPY, 14, 6));
349  EXPECT_EQ(127, default_map->LookupFirstOpcode(VCD_COPY, 15, 6));
350  EXPECT_EQ(128, default_map->LookupFirstOpcode(VCD_COPY, 16, 6));
351  EXPECT_EQ(129, default_map->LookupFirstOpcode(VCD_COPY, 17, 6));
352  EXPECT_EQ(130, default_map->LookupFirstOpcode(VCD_COPY, 18, 6));
353}
354
355TEST_F(InstructionMapTest, DefaultMapLookupFirstCopyMode7) {
356  EXPECT_EQ(131, default_map->LookupFirstOpcode(VCD_COPY, 0, 7));
357  EXPECT_EQ(132, default_map->LookupFirstOpcode(VCD_COPY, 4, 7));
358  EXPECT_EQ(133, default_map->LookupFirstOpcode(VCD_COPY, 5, 7));
359  EXPECT_EQ(134, default_map->LookupFirstOpcode(VCD_COPY, 6, 7));
360  EXPECT_EQ(135, default_map->LookupFirstOpcode(VCD_COPY, 7, 7));
361  EXPECT_EQ(136, default_map->LookupFirstOpcode(VCD_COPY, 8, 7));
362  EXPECT_EQ(137, default_map->LookupFirstOpcode(VCD_COPY, 9, 7));
363  EXPECT_EQ(138, default_map->LookupFirstOpcode(VCD_COPY, 10, 7));
364  EXPECT_EQ(139, default_map->LookupFirstOpcode(VCD_COPY, 11, 7));
365  EXPECT_EQ(140, default_map->LookupFirstOpcode(VCD_COPY, 12, 7));
366  EXPECT_EQ(141, default_map->LookupFirstOpcode(VCD_COPY, 13, 7));
367  EXPECT_EQ(142, default_map->LookupFirstOpcode(VCD_COPY, 14, 7));
368  EXPECT_EQ(143, default_map->LookupFirstOpcode(VCD_COPY, 15, 7));
369  EXPECT_EQ(144, default_map->LookupFirstOpcode(VCD_COPY, 16, 7));
370  EXPECT_EQ(145, default_map->LookupFirstOpcode(VCD_COPY, 17, 7));
371  EXPECT_EQ(146, default_map->LookupFirstOpcode(VCD_COPY, 18, 7));
372}
373
374TEST_F(InstructionMapTest, DefaultMapLookupFirstCopyMode8) {
375  EXPECT_EQ(147, default_map->LookupFirstOpcode(VCD_COPY, 0, 8));
376  EXPECT_EQ(148, default_map->LookupFirstOpcode(VCD_COPY, 4, 8));
377  EXPECT_EQ(149, default_map->LookupFirstOpcode(VCD_COPY, 5, 8));
378  EXPECT_EQ(150, default_map->LookupFirstOpcode(VCD_COPY, 6, 8));
379  EXPECT_EQ(151, default_map->LookupFirstOpcode(VCD_COPY, 7, 8));
380  EXPECT_EQ(152, default_map->LookupFirstOpcode(VCD_COPY, 8, 8));
381  EXPECT_EQ(153, default_map->LookupFirstOpcode(VCD_COPY, 9, 8));
382  EXPECT_EQ(154, default_map->LookupFirstOpcode(VCD_COPY, 10, 8));
383  EXPECT_EQ(155, default_map->LookupFirstOpcode(VCD_COPY, 11, 8));
384  EXPECT_EQ(156, default_map->LookupFirstOpcode(VCD_COPY, 12, 8));
385  EXPECT_EQ(157, default_map->LookupFirstOpcode(VCD_COPY, 13, 8));
386  EXPECT_EQ(158, default_map->LookupFirstOpcode(VCD_COPY, 14, 8));
387  EXPECT_EQ(159, default_map->LookupFirstOpcode(VCD_COPY, 15, 8));
388  EXPECT_EQ(160, default_map->LookupFirstOpcode(VCD_COPY, 16, 8));
389  EXPECT_EQ(161, default_map->LookupFirstOpcode(VCD_COPY, 17, 8));
390  EXPECT_EQ(162, default_map->LookupFirstOpcode(VCD_COPY, 18, 8));
391}
392
393TEST_F(InstructionMapTest, DefaultMapLookupFirstCopyInvalid) {
394  EXPECT_EQ(kNoOpcode, default_map->LookupFirstOpcode(VCD_COPY, 3, 0));
395  EXPECT_EQ(kNoOpcode, default_map->LookupFirstOpcode(VCD_COPY, 3, 3));
396  EXPECT_EQ(kNoOpcode, default_map->LookupFirstOpcode(VCD_COPY, 255, 0));
397}
398
399TEST_F(InstructionMapTest, DefaultMapLookupSecondNoop) {
400  // The second opcode table does not store entries for NOOP instructions.
401  // Just make sure that a NOOP does not crash the lookup code.
402  EXPECT_EQ(kNoOpcode, default_map->LookupSecondOpcode(20, VCD_NOOP, 0, 0));
403  EXPECT_EQ(kNoOpcode, default_map->LookupSecondOpcode(20, VCD_NOOP, 0, 255));
404  EXPECT_EQ(kNoOpcode, default_map->LookupSecondOpcode(20, VCD_NOOP, 255, 0));
405  EXPECT_EQ(kNoOpcode, default_map->LookupSecondOpcode(20, VCD_NOOP, 255, 255));
406}
407
408TEST_F(InstructionMapTest, DefaultMapLookupSecondAdd) {
409  EXPECT_EQ(247, default_map->LookupSecondOpcode(20, VCD_ADD, 1, 0));
410  EXPECT_EQ(248, default_map->LookupSecondOpcode(36, VCD_ADD, 1, 0));
411  EXPECT_EQ(249, default_map->LookupSecondOpcode(52, VCD_ADD, 1, 0));
412  EXPECT_EQ(250, default_map->LookupSecondOpcode(68, VCD_ADD, 1, 0));
413  EXPECT_EQ(251, default_map->LookupSecondOpcode(84, VCD_ADD, 1, 0));
414  EXPECT_EQ(252, default_map->LookupSecondOpcode(100, VCD_ADD, 1, 0));
415  EXPECT_EQ(253, default_map->LookupSecondOpcode(116, VCD_ADD, 1, 0));
416  EXPECT_EQ(254, default_map->LookupSecondOpcode(132, VCD_ADD, 1, 0));
417  EXPECT_EQ(255, default_map->LookupSecondOpcode(148, VCD_ADD, 1, 0));
418  // Value of "mode" should not matter
419  EXPECT_EQ(247, default_map->LookupSecondOpcode(20, VCD_ADD, 1, 2));
420  EXPECT_EQ(247, default_map->LookupSecondOpcode(20, VCD_ADD, 1, 255));
421  // Only valid 2nd ADD opcode has size 1
422  EXPECT_EQ(kNoOpcode, default_map->LookupSecondOpcode(20, VCD_ADD, 0, 0));
423  EXPECT_EQ(kNoOpcode, default_map->LookupSecondOpcode(20, VCD_ADD, 0, 255));
424  EXPECT_EQ(kNoOpcode, default_map->LookupSecondOpcode(20, VCD_ADD, 255, 0));
425  EXPECT_EQ(kNoOpcode, default_map->LookupSecondOpcode(0, VCD_ADD, 1, 0));
426  EXPECT_EQ(kNoOpcode, default_map->LookupSecondOpcode(1, VCD_ADD, 1, 0));
427  EXPECT_EQ(kNoOpcode, default_map->LookupSecondOpcode(247, VCD_ADD, 1, 0));
428  EXPECT_EQ(kNoOpcode, default_map->LookupSecondOpcode(255, VCD_ADD, 1, 0));
429}
430
431TEST_F(InstructionMapTest, DefaultMapLookupSecondRun) {
432  EXPECT_EQ(kNoOpcode, default_map->LookupSecondOpcode(0, VCD_RUN, 0, 0));
433  EXPECT_EQ(kNoOpcode, default_map->LookupSecondOpcode(20, VCD_RUN, 0, 0));
434  EXPECT_EQ(kNoOpcode, default_map->LookupSecondOpcode(20, VCD_RUN, 0, 255));
435  EXPECT_EQ(kNoOpcode, default_map->LookupSecondOpcode(20, VCD_RUN, 255, 0));
436  EXPECT_EQ(kNoOpcode, default_map->LookupSecondOpcode(20, VCD_RUN, 255, 255));
437  EXPECT_EQ(kNoOpcode, default_map->LookupSecondOpcode(255, VCD_RUN, 0, 0));
438}
439
440TEST_F(InstructionMapTest, DefaultMapLookupSecondCopyMode0) {
441  EXPECT_EQ(163, default_map->LookupSecondOpcode(2, VCD_COPY, 4, 0));
442  EXPECT_EQ(164, default_map->LookupSecondOpcode(2, VCD_COPY, 5, 0));
443  EXPECT_EQ(165, default_map->LookupSecondOpcode(2, VCD_COPY, 6, 0));
444  EXPECT_EQ(166, default_map->LookupSecondOpcode(3, VCD_COPY, 4, 0));
445  EXPECT_EQ(167, default_map->LookupSecondOpcode(3, VCD_COPY, 5, 0));
446  EXPECT_EQ(168, default_map->LookupSecondOpcode(3, VCD_COPY, 6, 0));
447  EXPECT_EQ(169, default_map->LookupSecondOpcode(4, VCD_COPY, 4, 0));
448  EXPECT_EQ(170, default_map->LookupSecondOpcode(4, VCD_COPY, 5, 0));
449  EXPECT_EQ(171, default_map->LookupSecondOpcode(4, VCD_COPY, 6, 0));
450  EXPECT_EQ(172, default_map->LookupSecondOpcode(5, VCD_COPY, 4, 0));
451  EXPECT_EQ(173, default_map->LookupSecondOpcode(5, VCD_COPY, 5, 0));
452  EXPECT_EQ(174, default_map->LookupSecondOpcode(5, VCD_COPY, 6, 0));
453}
454
455TEST_F(InstructionMapTest, DefaultMapLookupSecondCopyMode1) {
456  EXPECT_EQ(175, default_map->LookupSecondOpcode(2, VCD_COPY, 4, 1));
457  EXPECT_EQ(176, default_map->LookupSecondOpcode(2, VCD_COPY, 5, 1));
458  EXPECT_EQ(177, default_map->LookupSecondOpcode(2, VCD_COPY, 6, 1));
459  EXPECT_EQ(178, default_map->LookupSecondOpcode(3, VCD_COPY, 4, 1));
460  EXPECT_EQ(179, default_map->LookupSecondOpcode(3, VCD_COPY, 5, 1));
461  EXPECT_EQ(180, default_map->LookupSecondOpcode(3, VCD_COPY, 6, 1));
462  EXPECT_EQ(181, default_map->LookupSecondOpcode(4, VCD_COPY, 4, 1));
463  EXPECT_EQ(182, default_map->LookupSecondOpcode(4, VCD_COPY, 5, 1));
464  EXPECT_EQ(183, default_map->LookupSecondOpcode(4, VCD_COPY, 6, 1));
465  EXPECT_EQ(184, default_map->LookupSecondOpcode(5, VCD_COPY, 4, 1));
466  EXPECT_EQ(185, default_map->LookupSecondOpcode(5, VCD_COPY, 5, 1));
467  EXPECT_EQ(186, default_map->LookupSecondOpcode(5, VCD_COPY, 6, 1));
468}
469
470TEST_F(InstructionMapTest, DefaultMapLookupSecondCopyMode2) {
471  EXPECT_EQ(187, default_map->LookupSecondOpcode(2, VCD_COPY, 4, 2));
472  EXPECT_EQ(188, default_map->LookupSecondOpcode(2, VCD_COPY, 5, 2));
473  EXPECT_EQ(189, default_map->LookupSecondOpcode(2, VCD_COPY, 6, 2));
474  EXPECT_EQ(190, default_map->LookupSecondOpcode(3, VCD_COPY, 4, 2));
475  EXPECT_EQ(191, default_map->LookupSecondOpcode(3, VCD_COPY, 5, 2));
476  EXPECT_EQ(192, default_map->LookupSecondOpcode(3, VCD_COPY, 6, 2));
477  EXPECT_EQ(193, default_map->LookupSecondOpcode(4, VCD_COPY, 4, 2));
478  EXPECT_EQ(194, default_map->LookupSecondOpcode(4, VCD_COPY, 5, 2));
479  EXPECT_EQ(195, default_map->LookupSecondOpcode(4, VCD_COPY, 6, 2));
480  EXPECT_EQ(196, default_map->LookupSecondOpcode(5, VCD_COPY, 4, 2));
481  EXPECT_EQ(197, default_map->LookupSecondOpcode(5, VCD_COPY, 5, 2));
482  EXPECT_EQ(198, default_map->LookupSecondOpcode(5, VCD_COPY, 6, 2));
483}
484
485TEST_F(InstructionMapTest, DefaultMapLookupSecondCopyMode3) {
486  EXPECT_EQ(199, default_map->LookupSecondOpcode(2, VCD_COPY, 4, 3));
487  EXPECT_EQ(200, default_map->LookupSecondOpcode(2, VCD_COPY, 5, 3));
488  EXPECT_EQ(201, default_map->LookupSecondOpcode(2, VCD_COPY, 6, 3));
489  EXPECT_EQ(202, default_map->LookupSecondOpcode(3, VCD_COPY, 4, 3));
490  EXPECT_EQ(203, default_map->LookupSecondOpcode(3, VCD_COPY, 5, 3));
491  EXPECT_EQ(204, default_map->LookupSecondOpcode(3, VCD_COPY, 6, 3));
492  EXPECT_EQ(205, default_map->LookupSecondOpcode(4, VCD_COPY, 4, 3));
493  EXPECT_EQ(206, default_map->LookupSecondOpcode(4, VCD_COPY, 5, 3));
494  EXPECT_EQ(207, default_map->LookupSecondOpcode(4, VCD_COPY, 6, 3));
495  EXPECT_EQ(208, default_map->LookupSecondOpcode(5, VCD_COPY, 4, 3));
496  EXPECT_EQ(209, default_map->LookupSecondOpcode(5, VCD_COPY, 5, 3));
497  EXPECT_EQ(210, default_map->LookupSecondOpcode(5, VCD_COPY, 6, 3));
498}
499
500TEST_F(InstructionMapTest, DefaultMapLookupSecondCopyMode4) {
501  EXPECT_EQ(211, default_map->LookupSecondOpcode(2, VCD_COPY, 4, 4));
502  EXPECT_EQ(212, default_map->LookupSecondOpcode(2, VCD_COPY, 5, 4));
503  EXPECT_EQ(213, default_map->LookupSecondOpcode(2, VCD_COPY, 6, 4));
504  EXPECT_EQ(214, default_map->LookupSecondOpcode(3, VCD_COPY, 4, 4));
505  EXPECT_EQ(215, default_map->LookupSecondOpcode(3, VCD_COPY, 5, 4));
506  EXPECT_EQ(216, default_map->LookupSecondOpcode(3, VCD_COPY, 6, 4));
507  EXPECT_EQ(217, default_map->LookupSecondOpcode(4, VCD_COPY, 4, 4));
508  EXPECT_EQ(218, default_map->LookupSecondOpcode(4, VCD_COPY, 5, 4));
509  EXPECT_EQ(219, default_map->LookupSecondOpcode(4, VCD_COPY, 6, 4));
510  EXPECT_EQ(220, default_map->LookupSecondOpcode(5, VCD_COPY, 4, 4));
511  EXPECT_EQ(221, default_map->LookupSecondOpcode(5, VCD_COPY, 5, 4));
512  EXPECT_EQ(222, default_map->LookupSecondOpcode(5, VCD_COPY, 6, 4));
513}
514
515TEST_F(InstructionMapTest, DefaultMapLookupSecondCopyMode5) {
516  EXPECT_EQ(223, default_map->LookupSecondOpcode(2, VCD_COPY, 4, 5));
517  EXPECT_EQ(224, default_map->LookupSecondOpcode(2, VCD_COPY, 5, 5));
518  EXPECT_EQ(225, default_map->LookupSecondOpcode(2, VCD_COPY, 6, 5));
519  EXPECT_EQ(226, default_map->LookupSecondOpcode(3, VCD_COPY, 4, 5));
520  EXPECT_EQ(227, default_map->LookupSecondOpcode(3, VCD_COPY, 5, 5));
521  EXPECT_EQ(228, default_map->LookupSecondOpcode(3, VCD_COPY, 6, 5));
522  EXPECT_EQ(229, default_map->LookupSecondOpcode(4, VCD_COPY, 4, 5));
523  EXPECT_EQ(230, default_map->LookupSecondOpcode(4, VCD_COPY, 5, 5));
524  EXPECT_EQ(231, default_map->LookupSecondOpcode(4, VCD_COPY, 6, 5));
525  EXPECT_EQ(232, default_map->LookupSecondOpcode(5, VCD_COPY, 4, 5));
526  EXPECT_EQ(233, default_map->LookupSecondOpcode(5, VCD_COPY, 5, 5));
527  EXPECT_EQ(234, default_map->LookupSecondOpcode(5, VCD_COPY, 6, 5));
528}
529
530TEST_F(InstructionMapTest, DefaultMapLookupSecondCopyMode6) {
531  EXPECT_EQ(235, default_map->LookupSecondOpcode(2, VCD_COPY, 4, 6));
532  EXPECT_EQ(236, default_map->LookupSecondOpcode(3, VCD_COPY, 4, 6));
533  EXPECT_EQ(237, default_map->LookupSecondOpcode(4, VCD_COPY, 4, 6));
534  EXPECT_EQ(238, default_map->LookupSecondOpcode(5, VCD_COPY, 4, 6));
535  EXPECT_EQ(239, default_map->LookupSecondOpcode(2, VCD_COPY, 4, 7));
536}
537
538TEST_F(InstructionMapTest, DefaultMapLookupSecondCopyMode7) {
539  EXPECT_EQ(240, default_map->LookupSecondOpcode(3, VCD_COPY, 4, 7));
540  EXPECT_EQ(241, default_map->LookupSecondOpcode(4, VCD_COPY, 4, 7));
541  EXPECT_EQ(242, default_map->LookupSecondOpcode(5, VCD_COPY, 4, 7));
542}
543
544TEST_F(InstructionMapTest, DefaultMapLookupSecondCopyMode8) {
545  EXPECT_EQ(243, default_map->LookupSecondOpcode(2, VCD_COPY, 4, 8));
546  EXPECT_EQ(244, default_map->LookupSecondOpcode(3, VCD_COPY, 4, 8));
547  EXPECT_EQ(245, default_map->LookupSecondOpcode(4, VCD_COPY, 4, 8));
548  EXPECT_EQ(246, default_map->LookupSecondOpcode(5, VCD_COPY, 4, 8));
549}
550
551TEST_F(InstructionMapTest, DefaultMapLookupSecondCopyInvalid) {
552  EXPECT_EQ(kNoOpcode, default_map->LookupSecondOpcode(2, VCD_COPY, 0, 0));
553  EXPECT_EQ(kNoOpcode, default_map->LookupSecondOpcode(2, VCD_COPY, 255, 0));
554  EXPECT_EQ(kNoOpcode, default_map->LookupSecondOpcode(2, VCD_COPY, 255, 255));
555  EXPECT_EQ(kNoOpcode, default_map->LookupSecondOpcode(0, VCD_COPY, 4, 0));
556  EXPECT_EQ(kNoOpcode, default_map->LookupSecondOpcode(255, VCD_COPY, 4, 0));
557}
558
559TEST_F(InstructionMapTest, ExerciseTableLookup) {
560  int opcode = 0;
561  // This loop has the same bounds as the one in SetUpTestCase.
562  // Look up each instruction type and make sure it returns
563  // the proper opcode.
564  for (unsigned char inst_mode1 = 0;
565       inst_mode1 <= VCD_LAST_INSTRUCTION_TYPE + kLastExerciseMode;
566       ++inst_mode1) {
567    unsigned char inst1 = inst_mode1;
568    unsigned char mode1 = 0;
569    if (inst_mode1 > VCD_COPY) {
570      inst1 = VCD_COPY;
571      mode1 = inst_mode1 - VCD_COPY;
572    }
573    for (unsigned char inst_mode2 = 0;
574         inst_mode2 <= VCD_LAST_INSTRUCTION_TYPE + kLastExerciseMode;
575         ++inst_mode2) {
576      unsigned char inst2 = inst_mode2;
577      unsigned char mode2 = 0;
578      if (inst_mode2 > VCD_COPY) {
579        inst2 = VCD_COPY;
580        mode2 = inst_mode2 - VCD_COPY;
581      }
582      if (inst2 == VCD_NOOP) {
583        VerifyExerciseFirstInstruction(opcode, inst1, 0, mode1);
584        VerifyExerciseFirstInstruction(opcode + 2,
585                                       inst1,
586                                       ((inst1 == VCD_NOOP) ? 0 : 255),
587                                       mode1);
588      } else if (inst1 != VCD_NOOP) {
589        VerifyExerciseSecondInstruction(opcode,
590                                        inst1,
591                                        0,
592                                        mode1,
593                                        inst2,
594                                        0,
595                                        mode2);
596        VerifyExerciseSecondInstruction(opcode + 1,
597                                        inst1,
598                                        0,
599                                        mode1,
600                                        inst2,
601                                        255,
602                                        mode2);
603        VerifyExerciseSecondInstruction(opcode + 2,
604                                        inst1,
605                                        255,
606                                        mode1,
607                                        inst2,
608                                        0,
609                                        mode2);
610        VerifyExerciseSecondInstruction(opcode + 3,
611                                        inst1,
612                                        255,
613                                        mode1,
614                                        inst2,
615                                        255,
616                                        mode2);
617      }
618      opcode += 4;
619    }
620  }
621  // This is a CHECK rather than an EXPECT because it validates only
622  // the logic of the test, not of the code being tested.
623  CHECK_EQ(VCDiffCodeTableData::kCodeTableSize, opcode);
624}
625
626}  // unnamed namespace
627}  // namespace open_vcdiff
628