private static void AssertGetBytes <TInstruction>(Opcode opcode, params object[] args) where TInstruction : IInstruction, new() { var instruction = new TInstruction(); var expectedBytes = InstructionByteBuilder.Create().Opcode(opcode, args).AsSpan(); Assert.True(expectedBytes.SequenceEqual(instruction.GetBytes())); }
private static void AssertCreatedInstruction <TInstruction>(Opcode opcode, params object[] args) where TInstruction : IInstruction { var bytes = InstructionByteBuilder.Create() .Opcode(opcode, args) .ToArray(); AssertBytesDecodedAs <TInstruction>(bytes); }
public void GetBytes_LoadStringInstruction() { var instruction = new LoadStringInstruction("helloWorld"); var expectedBytes = InstructionByteBuilder.Create() .Opcode(Opcode.Load, LoadKind.String, "helloWorld") .AsSpan(); Assert.True(expectedBytes.SequenceEqual(instruction.GetBytes())); }
public void GetBytes_LoadLibraryInstruction() { var instruction = new LoadLibraryInstruction("MyLib"); var expectedBytes = InstructionByteBuilder.Create() .Opcode(Opcode.Load, LoadKind.Library, "MyLib") .AsSpan(); Assert.True(expectedBytes.SequenceEqual(instruction.GetBytes())); }
public void GetBytes_CompareInstruction(CompareKind compareKind) { var instruction = new CompareInstruction(compareKind); var expectedBytes = InstructionByteBuilder.Create() .Opcode(Opcode.Compare, compareKind) .AsSpan(); Assert.True(expectedBytes.SequenceEqual(instruction.GetBytes())); }
public void GetBytes_BranchInstruction(BranchKind branchKind) { var instruction = new BranchInstruction(branchKind, 42); var expectedBytes = InstructionByteBuilder.Create() .Opcode(Opcode.Branch, branchKind, 42) .AsSpan(); Assert.True(expectedBytes.SequenceEqual(instruction.GetBytes())); }
public void GetBytes_LoadConstantInstruction(LoadKind loadKind, object value) { var instruction = new LoadConstantInstruction(loadKind, value); var expectedBytes = InstructionByteBuilder.Create() .Opcode(Opcode.Load, loadKind, value) .AsSpan(); Assert.True(expectedBytes.SequenceEqual(instruction.GetBytes())); }
public void Decode_CompareInstruction_FromBytesArray(CompareKind compareKind) { var bytes = InstructionByteBuilder.Create() .Opcode(Opcode.Compare, compareKind) .ToArray(); var compareInstruction = AssertBytesDecodedAs <CompareInstruction>(bytes); Assert.Equal(compareKind, compareInstruction.CompareKind); }
public void Decode_BranchInstruction_FromByteArray(BranchKind branchKind) { var bytes = InstructionByteBuilder.Create() .Opcode(Opcode.Branch, branchKind, 42) .ToArray(); var branchInstruction = AssertBytesDecodedAs <BranchInstruction>(bytes); Assert.Equal(branchKind, branchInstruction.BranchKind); }
public void GetBytes_CallInstruction() { const string methodCall = "void HelloWorld()"; var instruction = new CallInstruction(methodCall); var expectedBytes = InstructionByteBuilder.Create() .Opcode(Opcode.Call, methodCall) .AsSpan(); Assert.True(expectedBytes.SequenceEqual(instruction.GetBytes())); }
public void GetBytes_LoadPlaceInstruction(LoadKind loadKind) { var instruction = new LoadPlaceInstruction(loadKind, (byte)0x03); var expectedBytes = InstructionByteBuilder.Create() .Opcode(Opcode.Load, loadKind, (byte)0x03) .AsSpan(); var actualBytes = instruction.GetBytes(); Assert.True(expectedBytes.SequenceEqual(actualBytes)); }
public void Decode_LoadLibraryInstruction_FromByteArray() { var bytes = InstructionByteBuilder.Create() .Opcode(Opcode.Load, LoadKind.Library, "MyLibraryCore") .ToArray(); var loadInstruction = AssertBytesDecodedAs <LoadLibraryInstruction>(bytes); Assert.Equal(LoadKind.Library, loadInstruction.LoadKind); Assert.Equal("MyLibraryCore", loadInstruction.LibraryName); }
public void Decode_LoadConstantInstruction_FromByteArray(LoadKind loadKind, object expectedValue) { var bytes = InstructionByteBuilder.Create() .Opcode(Opcode.Load, loadKind, expectedValue) .ToArray(); var loadInstruction = AssertBytesDecodedAs <LoadConstantInstruction>(bytes); Assert.Equal(loadKind, loadInstruction.LoadKind); Assert.Equal(expectedValue, loadInstruction.Value); }
public void Decode_CallInstruction_FromByteArray() { const string methodCall = "void HelloWorld()"; var bytes = InstructionByteBuilder.Create() .Opcode(Opcode.Call, methodCall) .ToArray(); var callInstruction = AssertBytesDecodedAs <CallInstruction>(bytes); Assert.Equal(methodCall, callInstruction.MethodName); }
public void GetBytes_StoreInstruction(StoreKind storeKind) { var instruction = new StoreInstruction(storeKind, (byte)0x03); var expectedBytes = InstructionByteBuilder.Create() .Opcode(Opcode.Store, storeKind, (byte)0x03) .AsSpan(); var actualBytes = instruction.GetBytes(); Assert.True(expectedBytes.SequenceEqual(actualBytes)); }
public void Decode_LoadString_FromByteArray() { const string value = "helloWorld"; var bytes = InstructionByteBuilder.Create() .Opcode(Opcode.Load, LoadKind.String, value) .ToArray(); var loadInstruction = AssertBytesDecodedAs <LoadStringInstruction>(bytes); Assert.Equal(LoadKind.String, loadInstruction.LoadKind); Assert.Equal(value, loadInstruction.Value); }
public void Decode_StoreInstructionFromByteArray_RegistryIndexAndStoreTypeAreSet() { const StoreKind expectedStoreType = StoreKind.Field; const byte expectedIndex = 0x05; var bytes = InstructionByteBuilder.Create() .Opcode(Opcode.Store, expectedStoreType, expectedIndex) .ToArray(); var storeInstruction = AssertBytesDecodedAs <StoreInstruction>(bytes); Assert.Equal(expectedIndex, storeInstruction.Index); Assert.Equal(expectedStoreType, storeInstruction.StoreKind); }
public void Decode_LoadPlaceInstruction_FromByteArray_RegistryIndexAndLoadTypeAreSet() { const LoadKind expectedLoadType = LoadKind.StaticField; const byte expectedIndex = 0x07; var bytes = InstructionByteBuilder.Create() .Opcode(Opcode.Load, expectedLoadType, expectedIndex) .ToArray(); var loadInstruction = AssertBytesDecodedAs <LoadPlaceInstruction>(bytes); Assert.Equal(expectedIndex, loadInstruction.Index); Assert.Equal(expectedLoadType, loadInstruction.LoadKind); }
public void DecodeStream_StreamWithMultipleOpcode_DecodesInstructions() { var bytesStream = InstructionByteBuilder.Create() .Opcode(Opcode.Load, LoadKind.Integer, 42) .Opcode(Opcode.Load, LoadKind.Integer, 69) .Opcode(Opcode.Add) .Opcode(Opcode.Return) .AsStream(); var instructions = InstructionDecoder.DecodeStream(bytesStream).ToList(); Assert.Equal(4, instructions.Count); Assert.Equal("add", instructions.ElementAt(2).ToString()); Assert.Equal("ret", instructions.ElementAt(3).ToString()); }
public void Disassemble() { var bytesStream = InstructionByteBuilder.Create() .Opcode(Opcode.Load, LoadKind.Integer, 42) .Opcode(Opcode.Load, LoadKind.Integer, 69) .Opcode(Opcode.Add) .Opcode(Opcode.Return) .AsStream(); var disassembler = new Disassembler(); var disassembled = disassembler.Disassemble(bytesStream).ToArray(); Assert.Equal(4, disassembled.Length); Assert.Equal("add", disassembled[2]); Assert.Equal("ret", disassembled[3]); }
public void Assemble_ToExpectedFormat() { var expectedBytes = InstructionByteBuilder.Create() .Opcode(Opcode.Load, LoadKind.Integer, 42) .Opcode(Opcode.Load, LoadKind.Integer, 69) .Opcode(Opcode.Add) .Opcode(Opcode.Return) .ToArray(); var instructions = new List <IInstruction> { new LoadConstantInstruction(LoadKind.Integer, 42), new LoadConstantInstruction(LoadKind.Integer, 69), new AddInstruction(), new ReturnInstruction() }; var assembler = new Assembler(); using var outputStream = new MemoryStream(); assembler.Assemble(outputStream, instructions); Assert.Equal(expectedBytes, outputStream.ToArray()); }
public void Decode_NotSupportedOpcode_ThrowOpcodeReadException(Opcode opcode) { var bytes = InstructionByteBuilder.Create().Opcode(opcode).ToArray(); Assert.Throws <OpcodeDecodeException>(() => InstructionDecoder.Decode(bytes)); }