0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-06-20 16:18:37 +03:00

unbreak tests

This commit is contained in:
bird_egop
2025-04-14 23:08:52 +03:00
parent 685eeda03d
commit 9117830ff1
41 changed files with 3820 additions and 736 deletions

View File

@ -1,4 +1,5 @@
using X86Disassembler.X86;
using X86Disassembler.X86.Operands;
namespace X86DisassemblerTests.InstructionTests;
@ -16,16 +17,32 @@ public class CmpInstructionHandlerTests
// Arrange
// CMP AL, 0x03 (3C 03)
byte[] codeBuffer = new byte[] { 0x3C, 0x03 };
var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length);
var disassembler = new Disassembler(codeBuffer, 0);
// Act
var instruction = decoder.DecodeInstruction();
var instructions = disassembler.Disassemble();
// Assert
Assert.Single(instructions);
var instruction = instructions[0];
Assert.NotNull(instruction);
Assert.Equal("cmp", instruction.Mnemonic);
// The handler should produce "al, 0xXX" as the operands
Assert.Equal("al, 0x03", instruction.Operands);
Assert.Equal(InstructionType.Cmp, instruction.Type);
// Check that we have two operands
Assert.Equal(2, instruction.StructuredOperands.Count);
// Check the first operand (AL)
var alOperand = instruction.StructuredOperands[0];
Assert.IsType<RegisterOperand>(alOperand);
var registerOperand = (RegisterOperand)alOperand;
Assert.Equal(RegisterIndex.A, registerOperand.Register);
Assert.Equal(8, registerOperand.Size); // Validate that it's an 8-bit register (AL)
// Check the second operand (immediate value)
var immOperand = instruction.StructuredOperands[1];
Assert.IsType<ImmediateOperand>(immOperand);
var immediateOperand = (ImmediateOperand)immOperand;
Assert.Equal(0x03U, immediateOperand.Value);
}
/// <summary>
@ -37,56 +54,110 @@ public class CmpInstructionHandlerTests
// Arrange
// CMP AL, 0xFF (3C FF)
byte[] codeBuffer = new byte[] { 0x3C, 0xFF };
var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length);
var disassembler = new Disassembler(codeBuffer, 0);
// Act
var instruction = decoder.DecodeInstruction();
var instructions = disassembler.Disassemble();
// Assert
Assert.Single(instructions);
var instruction = instructions[0];
Assert.NotNull(instruction);
Assert.Equal("cmp", instruction.Mnemonic);
Assert.Equal("al, 0xFF", instruction.Operands);
Assert.Equal(InstructionType.Cmp, instruction.Type);
// Check that we have two operands
Assert.Equal(2, instruction.StructuredOperands.Count);
// Check the first operand (AL)
var alOperand = instruction.StructuredOperands[0];
Assert.IsType<RegisterOperand>(alOperand);
var registerOperand = (RegisterOperand)alOperand;
Assert.Equal(RegisterIndex.A, registerOperand.Register);
Assert.Equal(8, registerOperand.Size); // Validate that it's an 8-bit register (AL)
// Check the second operand (immediate value)
var immOperand = instruction.StructuredOperands[1];
Assert.IsType<ImmediateOperand>(immOperand);
var immediateOperand = (ImmediateOperand)immOperand;
Assert.Equal(0xFFU, immediateOperand.Value);
}
/// <summary>
/// Tests the CmpRm32R32Handler for decoding CMP r/m32, r32 instructions with register operands
/// Tests the CmpRm32R32Handler for decoding CMP r32, r32 instructions
/// </summary>
[Fact]
public void CmpRm32R32Handler_DecodesCmpRm32R32_WithRegisterOperands()
public void CmpRm32R32Handler_DecodesCmpR32R32_Correctly()
{
// Arrange
// CMP ECX, EAX (39 C1) - ModR/M byte C1 = 11 000 001 (mod=3, reg=0, rm=1)
// mod=3 means direct register addressing, reg=0 is EAX, rm=1 is ECX
byte[] codeBuffer = new byte[] { 0x39, 0xC1 };
var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length);
var disassembler = new Disassembler(codeBuffer, 0);
// Act
var instruction = decoder.DecodeInstruction();
var instructions = disassembler.Disassemble();
// Assert
Assert.Single(instructions);
var instruction = instructions[0];
Assert.NotNull(instruction);
Assert.Equal("cmp", instruction.Mnemonic);
Assert.Equal("ecx, eax", instruction.Operands);
Assert.Equal(InstructionType.Cmp, instruction.Type);
// Check that we have two operands
Assert.Equal(2, instruction.StructuredOperands.Count);
// Check the first operand (ECX)
var ecxOperand = instruction.StructuredOperands[0];
Assert.IsType<RegisterOperand>(ecxOperand);
var ecxRegisterOperand = (RegisterOperand)ecxOperand;
Assert.Equal(RegisterIndex.C, ecxRegisterOperand.Register);
Assert.Equal(32, ecxRegisterOperand.Size); // Validate that it's a 32-bit register (ECX)
// Check the second operand (EAX)
var eaxOperand = instruction.StructuredOperands[1];
Assert.IsType<RegisterOperand>(eaxOperand);
var eaxRegisterOperand = (RegisterOperand)eaxOperand;
Assert.Equal(RegisterIndex.A, eaxRegisterOperand.Register);
Assert.Equal(32, eaxRegisterOperand.Size); // Validate that it's a 32-bit register (EAX)
}
/// <summary>
/// Tests the CmpRm32R32Handler for decoding CMP r/m32, r32 instructions with memory operands
/// Tests the CmpRm32R32Handler for decoding CMP m32, r32 instructions
/// </summary>
[Fact]
public void CmpRm32R32Handler_DecodesCmpRm32R32_WithMemoryOperands()
public void CmpRm32R32Handler_DecodesCmpM32R32_Correctly()
{
// Arrange
// CMP [EBX+0x10], EDX (39 53 10) - ModR/M byte 53 = 01 010 011 (mod=1, reg=2, rm=3)
// mod=1 means memory addressing with 8-bit displacement, reg=2 is EDX, rm=3 is EBX
byte[] codeBuffer = new byte[] { 0x39, 0x53, 0x10 };
var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length);
var disassembler = new Disassembler(codeBuffer, 0);
// Act
var instruction = decoder.DecodeInstruction();
var instructions = disassembler.Disassemble();
// Assert
Assert.Single(instructions);
var instruction = instructions[0];
Assert.NotNull(instruction);
Assert.Equal("cmp", instruction.Mnemonic);
Assert.Equal("[ebx+0x10], edx", instruction.Operands);
Assert.Equal(InstructionType.Cmp, instruction.Type);
// Check that we have two operands
Assert.Equal(2, instruction.StructuredOperands.Count);
// Check the first operand ([EBX+0x10])
var memOperand = instruction.StructuredOperands[0];
Assert.IsType<DisplacementMemoryOperand>(memOperand);
var memoryOperand = (DisplacementMemoryOperand)memOperand;
Assert.Equal(RegisterIndex.B, memoryOperand.BaseRegister);
Assert.Equal(0x10, memoryOperand.Displacement);
Assert.Equal(32, memoryOperand.Size); // Memory size is 32 bits (DWORD)
// Check the second operand (EDX)
var edxOperand = instruction.StructuredOperands[1];
Assert.IsType<RegisterOperand>(edxOperand);
var edxRegisterOperand = (RegisterOperand)edxOperand;
Assert.Equal(RegisterIndex.D, edxRegisterOperand.Register);
Assert.Equal(32, edxRegisterOperand.Size); // Validate that it's a 32-bit register (EDX)
}
}