github.com/undoio/delve@v1.9.0/pkg/dwarf/util/util_test.go (about)

     1  package util
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  )
     7  
     8  func TestDecodeULEB128(t *testing.T) {
     9  	var leb128 = bytes.NewBuffer([]byte{0xE5, 0x8E, 0x26})
    10  
    11  	n, c := DecodeULEB128(leb128)
    12  	if n != 624485 {
    13  		t.Fatal("Number was not decoded properly, got: ", n, c)
    14  	}
    15  
    16  	if c != 3 {
    17  		t.Fatal("Count not returned correctly")
    18  	}
    19  }
    20  
    21  func TestDecodeSLEB128(t *testing.T) {
    22  	sleb128 := bytes.NewBuffer([]byte{0x9b, 0xf1, 0x59})
    23  
    24  	n, c := DecodeSLEB128(sleb128)
    25  	if n != -624485 {
    26  		t.Fatal("Number was not decoded properly, got: ", n, c)
    27  	}
    28  }
    29  
    30  func TestEncodeULEB128(t *testing.T) {
    31  	tc := []uint64{0x00, 0x7f, 0x80, 0x8f, 0xffff, 0xfffffff7}
    32  	for i := range tc {
    33  		var buf bytes.Buffer
    34  		EncodeULEB128(&buf, tc[i])
    35  		enc := append([]byte{}, buf.Bytes()...)
    36  		buf.Write([]byte{0x1, 0x2, 0x3})
    37  		out, c := DecodeULEB128(&buf)
    38  		t.Logf("input %x output %x encoded %x", tc[i], out, enc)
    39  		if c != uint32(len(enc)) {
    40  			t.Errorf("wrong encode")
    41  		}
    42  		if out != tc[i] {
    43  			t.Errorf("wrong encode")
    44  		}
    45  	}
    46  }
    47  
    48  func TestEncodeSLEB128(t *testing.T) {
    49  	tc := []int64{2, -2, 127, -127, 128, -128, 129, -129}
    50  	for i := range tc {
    51  		var buf bytes.Buffer
    52  		EncodeSLEB128(&buf, tc[i])
    53  		enc := append([]byte{}, buf.Bytes()...)
    54  		buf.Write([]byte{0x1, 0x2, 0x3})
    55  		out, c := DecodeSLEB128(&buf)
    56  		t.Logf("input %x output %x encoded %x", tc[i], out, enc)
    57  		if c != uint32(len(enc)) {
    58  			t.Errorf("wrong encode")
    59  		}
    60  		if out != tc[i] {
    61  			t.Errorf("wrong encode")
    62  		}
    63  	}
    64  }
    65  
    66  func TestParseString(t *testing.T) {
    67  	bstr := bytes.NewBuffer([]byte{'h', 'i', 0x0, 0xFF, 0xCC})
    68  	str, _ := ParseString(bstr)
    69  
    70  	if str != "hi" {
    71  		t.Fatalf("String was not parsed correctly %#v", str)
    72  	}
    73  }