Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
teo-tsirpanis committed Oct 12, 2022
1 parent 66edae9 commit 3549412
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ public void Branch_LongInstruction_LongDistance()

AssertEx.Equal(new byte[]
{
0x13, 0x30, 0x08, 0x00, 0x0A, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,// header
0x13, 0x30, 0x08, 0x00, 0x0A, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // header
(byte)ILOpCode.Br, 0x04, 0x01, 0x00, 0x00,
(byte)ILOpCode.Call, 0x01, 0x00, 0x00, 0x06,
(byte)ILOpCode.Call, 0x01, 0x00, 0x00, 0x06,
Expand Down Expand Up @@ -419,6 +419,50 @@ public void Branch_LongInstruction_LongDistance()
}, builder.ToArray());
}

[Fact]
public void Switch()
{
var code = new BlobBuilder();
var il = new InstructionEncoder(code, new ControlFlowBuilder());

var lStart = il.DefineLabel();
var l1 = il.DefineLabel();
var l2 = il.DefineLabel();
var l3 = il.DefineLabel();

il.OpCode(ILOpCode.Nop);
il.MarkLabel(lStart);
var switchEncoder = il.Switch(4);
switchEncoder.Branch(l1);
switchEncoder.Branch(l2);
switchEncoder.Branch(l3);
switchEncoder.Branch(lStart);

il.MarkLabel(l1);
il.OpCode(ILOpCode.Nop);
il.MarkLabel(l2);
il.OpCode(ILOpCode.Nop);
il.MarkLabel(l3);
il.OpCode(ILOpCode.Nop);

var builder = new BlobBuilder();
new MethodBodyStreamEncoder(builder).AddMethodBody(il);

AssertEx.Equal(new byte[]
{
0x66, // header
(byte)ILOpCode.Nop,
(byte)ILOpCode.Switch, 0x04, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00,
0x02, 0x00, 0x00, 0x00,
0xeb, 0xff, 0xff, 0xff,
(byte)ILOpCode.Nop,
(byte)ILOpCode.Nop,
(byte)ILOpCode.Nop
}, builder.ToArray());
}

[Fact]
public void Clear()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,28 @@ public void Branch()
}, builder.ToArray());
}

[Fact]
public void Switch()
{
var builder = new BlobBuilder();
var il = new InstructionEncoder(builder, new ControlFlowBuilder());
var l = il.DefineLabel();
var switchEncoder = il.Switch(4);
switchEncoder.Branch(l);
switchEncoder.Branch(l);
switchEncoder.Branch(l);
switchEncoder.Branch(l);

AssertEx.Equal(new byte[]
{
(byte)ILOpCode.Switch, 0x04, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff
}, builder.ToArray());
}

[Fact]
public void BranchAndLabel_Errors()
{
Expand Down

0 comments on commit 3549412

Please sign in to comment.