github.com/undoio/delve@v1.9.0/pkg/dwarf/op/op_test.go (about)

     1  package op
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  )
     7  
     8  func assertExprResult(t *testing.T, expected int64, instructions []byte) {
     9  	t.Helper()
    10  	actual, _, err := ExecuteStackProgram(DwarfRegisters{}, instructions, 8, nil)
    11  	if err != nil {
    12  		t.Error(err)
    13  	}
    14  	if actual != expected {
    15  		buf := new(strings.Builder)
    16  		PrettyPrint(buf, instructions)
    17  		t.Errorf("actual %d != expected %d (in %s)", actual, expected, buf.String())
    18  	}
    19  }
    20  
    21  func TestExecuteStackProgram(t *testing.T) {
    22  	assertExprResult(t, 56, []byte{byte(DW_OP_consts), 0x1c, byte(DW_OP_consts), 0x1c, byte(DW_OP_plus)})
    23  }
    24  
    25  func TestSignExtension(t *testing.T) {
    26  	var tgt uint64 = 0xffffffffffffff88
    27  	assertExprResult(t, int64(tgt), []byte{byte(DW_OP_const1s), 0x88})
    28  	tgt = 0xffffffffffff8888
    29  	assertExprResult(t, int64(tgt), []byte{byte(DW_OP_const2s), 0x88, 0x88})
    30  }
    31  
    32  func TestStackOps(t *testing.T) {
    33  	assertExprResult(t, 1, []byte{byte(DW_OP_lit1), byte(DW_OP_lit2), byte(DW_OP_drop)})
    34  	assertExprResult(t, 0, []byte{byte(DW_OP_lit1), byte(DW_OP_lit0), byte(DW_OP_pick), 0})
    35  	assertExprResult(t, 1, []byte{byte(DW_OP_lit1), byte(DW_OP_lit0), byte(DW_OP_pick), 1})
    36  }
    37  
    38  func TestBra(t *testing.T) {
    39  	assertExprResult(t, 32, []byte{
    40  		byte(DW_OP_lit1),
    41  		byte(DW_OP_lit5),
    42  
    43  		byte(DW_OP_dup),
    44  		byte(DW_OP_lit0),
    45  		byte(DW_OP_eq),
    46  		byte(DW_OP_bra), 9, 0x0,
    47  
    48  		byte(DW_OP_swap),
    49  		byte(DW_OP_dup),
    50  		byte(DW_OP_plus),
    51  		byte(DW_OP_swap),
    52  		byte(DW_OP_lit1),
    53  		byte(DW_OP_minus),
    54  		byte(DW_OP_skip), 0xf1, 0xff,
    55  
    56  		byte(DW_OP_drop),
    57  	})
    58  }