VCDiffCodeTableData implements Cloneable { protected boolean Validate(short max_mode) { final int kNumberOfTypesAndModes = VCD_LAST_INSTRUCTION_TYPE + max_mode + 1; boolean[] hasOpcodeForTypeAndMode = new boolean[VCD_LAST_INSTRUCTION_TYPE + VCDiffAddressCache.VCD_MAX_MODES]; boolean no_errors_found = true; for (int i = 0; i < kNumberOfTypesAndModes; ++i) { hasOpcodeForTypeAndMode[i] = false; } for (int i = 0; i < kCodeTableSize; ++i) { no_errors_found = ValidateOpcode(i, (short)(inst1[i] & 0xff), (short)(size1[i] & 0xff), (short)(mode1[i] & 0xff), max_mode, "first") && no_errors_found; no_errors_found = ValidateOpcode(i, (short)(inst2[i] & 0xff), (short)(size2[i] & 0xff), (short)(mode2[i] & 0xff), max_mode, "second") && no_errors_found; if (size1[i] == 0 && inst2[i] == VCD_NOOP && ((inst1[i] & 0xff) + (mode1[i] & 0xff)) < kNumberOfTypesAndModes) { hasOpcodeForTypeAndMode[(inst1[i] & 0xff) + (mode1[i] & 0xff)] = true; } } for (int i = 0; i < kNumberOfTypesAndModes; ++i) { if (i == VCD_NOOP) continue; if (!hasOpcodeForTypeAndMode[i]) { if (i >= VCD_COPY) { LOGGER.warn("VCDiff: Bad code table; there is no opcode for inst COPY, size 0, mode {}", (i - VCD_COPY)); } else { LOGGER.warn("VCDiff: Bad code table; there is no opcode for inst {}, size -, mode 0", VCDiffInstructionName(i)); } no_errors_found = false; } } return no_errors_found; } VCDiffCodeTableData();  VCDiffCodeTableData(byte[] bytes);  VCDiffCodeTableData(byte[] inst1, byte[] inst2, byte[] size1, byte[] size2, byte[] mode1, byte[] mode2); VCDiffCodeTableData clone(); static String VCDiffInstructionName(int inst); byte[] getBytes(); static final int kCodeTableSize; static final byte VCD_NOOP; static final byte VCD_ADD; static final byte VCD_RUN; static final byte VCD_COPY; static final byte VCD_INSTRUCTION_END_OF_DATA; static final VCDiffCodeTableData kDefaultCodeTableData; static final int SERIALIZED_BYTE_SIZE; final byte inst1; final byte inst2; final byte size1; final byte size2; final byte mode1; final byte mode2; }
@Test public void MissingCopyMode() { VerifyInstruction(131, VCDiffCodeTableData.VCD_COPY, (byte) 0, (byte) 7); code_table_data_.size1[131] = (byte) 0xFF; Assert.assertFalse(code_table_data_.Validate()); }
@Test public void MissingAdd() { VerifyInstruction(1, VCDiffCodeTableData.VCD_ADD, (byte) 0, (byte) 0); code_table_data_.size1[1] = (byte) 0xFF; Assert.assertFalse(code_table_data_.Validate()); }
@Test public void MissingRun() { VerifyInstruction(0, VCDiffCodeTableData.VCD_RUN, (byte) 0, (byte) 0); code_table_data_.size1[0] = (byte) 0xFF; Assert.assertFalse(code_table_data_.Validate()); }
@Test public void BadOpcode() { VerifyInstruction(0, VCDiffCodeTableData.VCD_RUN, (byte) 0, (byte) 0); code_table_data_.inst1[0] = VCDiffCodeTableData.VCD_LAST_INSTRUCTION_TYPE + 1; Assert.assertFalse(code_table_data_.Validate()); code_table_data_.inst1[0] = (byte) 0xFF; Assert.assertFalse(code_table_data_.Validate()); }
@Test public void BadMode() { VerifyInstruction(131, VCDiffCodeTableData.VCD_COPY, (byte) 0, (byte) 7); code_table_data_.mode1[131] = (byte) (VCDiffAddressCache.DefaultLastMode() + 1); Assert.assertFalse(code_table_data_.Validate()); code_table_data_.mode1[131] = (byte) 0xFF; Assert.assertFalse(code_table_data_.Validate()); }
@Test public void AddWithNonzeroMode() { VerifyInstruction(1, VCDiffCodeTableData.VCD_ADD, (byte) 0, (byte) 0); code_table_data_.mode1[1] = 1; Assert.assertFalse(code_table_data_.Validate()); }
@Test public void RunWithNonzeroMode() { VerifyInstruction(0, VCDiffCodeTableData.VCD_RUN, (byte) 0, (byte) 0); code_table_data_.mode1[0] = 1; Assert.assertFalse(code_table_data_.Validate()); }
@Test public void NoOpWithNonzeroMode() { VerifyInstruction(20, VCDiffCodeTableData.VCD_COPY, (byte) 4, (byte) 0); code_table_data_.inst1[20] = VCDiffCodeTableData.VCD_NOOP; code_table_data_.mode1[20] = 0; code_table_data_.size1[20] = 0; Assert.assertTrue(code_table_data_.Validate()); code_table_data_.mode1[20] = 1; Assert.assertFalse(code_table_data_.Validate()); }
@Test public void NoOpWithNonzeroSize() { VerifyInstruction(20, VCDiffCodeTableData.VCD_COPY, (byte) 4, (byte) 0); code_table_data_.inst1[20] = VCDiffCodeTableData.VCD_NOOP; code_table_data_.mode1[20] = 0; code_table_data_.size1[20] = 0; Assert.assertTrue(code_table_data_.Validate()); code_table_data_.size1[20] = 1; Assert.assertFalse(code_table_data_.Validate()); }
@Test public void BadSecondOpcode() { VerifyInstruction(20, VCDiffCodeTableData.VCD_COPY, (byte) 4, (byte) 0); code_table_data_.inst2[20] = VCDiffCodeTableData.VCD_LAST_INSTRUCTION_TYPE + 1; Assert.assertFalse(code_table_data_.Validate()); code_table_data_.inst2[20] = (byte) 0xFF; Assert.assertFalse(code_table_data_.Validate()); }
@Test public void BadSecondMode() { VerifyInstruction(20, VCDiffCodeTableData.VCD_COPY, (byte) 4, (byte) 0); code_table_data_.inst2[20] = VCDiffCodeTableData.VCD_COPY; Assert.assertTrue(code_table_data_.Validate()); code_table_data_.mode2[20] = (byte) (VCDiffAddressCache.DefaultLastMode() + 1); Assert.assertFalse(code_table_data_.Validate()); code_table_data_.mode2[20] = (byte) 0xFF; Assert.assertFalse(code_table_data_.Validate()); }
@Test public void AddSecondWithNonzeroMode() { VerifyInstruction(20, VCDiffCodeTableData.VCD_COPY, (byte) 4, (byte) 0); code_table_data_.inst2[20] = VCDiffCodeTableData.VCD_ADD; Assert.assertTrue(code_table_data_.Validate()); code_table_data_.mode2[20] = 1; Assert.assertFalse(code_table_data_.Validate()); }
@Test public void RunSecondWithNonzeroMode() { VerifyInstruction(20, VCDiffCodeTableData.VCD_COPY, (byte) 4, (byte) 0); code_table_data_.inst2[20] = VCDiffCodeTableData.VCD_RUN; Assert.assertTrue(code_table_data_.Validate()); code_table_data_.mode2[20] = 1; Assert.assertFalse(code_table_data_.Validate()); }
@Test public void SecondNoOpWithNonzeroMode() { VerifyInstruction(20, VCDiffCodeTableData.VCD_COPY, (byte) 4, (byte) 0); Assert.assertEquals(VCDiffCodeTableData.VCD_NOOP, code_table_data_.inst2[20]); code_table_data_.mode2[20] = 1; Assert.assertFalse(code_table_data_.Validate()); }
@Test public void SecondNoOpWithNonzeroSize() { VerifyInstruction(20, VCDiffCodeTableData.VCD_COPY, (byte) 4, (byte) 0); Assert.assertEquals(VCDiffCodeTableData.VCD_NOOP, code_table_data_.inst2[20]); code_table_data_.size2[20] = 1; Assert.assertFalse(code_table_data_.Validate()); }
@Test public void ValidateExerciseCodeTable() { final short kLastExerciseMode = 4; VCDiffCodeTableData g_exercise_code_table_ = new VCDiffCodeTableData(); int opcode = 0; for (byte inst_mode1 = 0; inst_mode1 <= VCDiffCodeTableData.VCD_LAST_INSTRUCTION_TYPE + kLastExerciseMode; ++inst_mode1) { byte inst1 = inst_mode1; byte mode1 = 0; if (inst_mode1 > VCDiffCodeTableData.VCD_COPY) { inst1 = VCDiffCodeTableData.VCD_COPY; mode1 = (byte) (inst_mode1 - VCDiffCodeTableData.VCD_COPY); } for (byte inst_mode2 = 0; inst_mode2 <= VCDiffCodeTableData.VCD_LAST_INSTRUCTION_TYPE + kLastExerciseMode; ++inst_mode2) { byte inst2 = inst_mode2; byte mode2 = 0; if (inst_mode2 > VCDiffCodeTableData.VCD_COPY) { inst2 = VCDiffCodeTableData.VCD_COPY; mode2 = (byte) (inst_mode2 - VCDiffCodeTableData.VCD_COPY); } AddExerciseOpcode(g_exercise_code_table_, inst1, mode1, (byte) 0, inst2, mode2, (byte) 0, opcode++); AddExerciseOpcode(g_exercise_code_table_, inst1, mode1, (byte) 0, inst2, mode2, (byte) 255, opcode++); AddExerciseOpcode(g_exercise_code_table_, inst1, mode1, (byte) 255, inst2, mode2, (byte) 0, opcode++); AddExerciseOpcode(g_exercise_code_table_, inst1, mode1, (byte) 255, inst2, mode2, (byte) 255, opcode++); } } Assert.assertEquals(VCDiffCodeTableData.kCodeTableSize, opcode); Assert.assertTrue(VCDiffCodeTableData.kDefaultCodeTableData.Validate()); Assert.assertTrue(g_exercise_code_table_.Validate(kLastExerciseMode)); }