github.com/undoio/delve@v1.9.0/pkg/proc/proc_unexported_test.go (about)

     1  package proc
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestAlignAddr(t *testing.T) {
     8  	c := func(align, in, tgt int64) {
     9  		out := alignAddr(in, align)
    10  		if out != tgt {
    11  			t.Errorf("alignAddr(%x, %x) = %x, expected %x", in, align, out, tgt)
    12  		}
    13  	}
    14  
    15  	for i := int64(0); i <= 0xf; i++ {
    16  		c(1, i, i)
    17  		c(1, i+0x10000, i+0x10000)
    18  	}
    19  
    20  	for _, example := range []struct{ align, in, tgt int64 }{
    21  		{2, 0, 0},
    22  		{2, 1, 2},
    23  		{2, 2, 2},
    24  		{2, 3, 4},
    25  		{2, 4, 4},
    26  		{2, 5, 6},
    27  		{2, 6, 6},
    28  		{2, 7, 8},
    29  		{2, 8, 8},
    30  		{2, 9, 0xa},
    31  		{2, 0xa, 0xa},
    32  		{2, 0xb, 0xc},
    33  		{2, 0xc, 0xc},
    34  		{2, 0xd, 0xe},
    35  		{2, 0xe, 0xe},
    36  		{2, 0xf, 0x10},
    37  
    38  		{4, 0, 0},
    39  		{4, 1, 4},
    40  		{4, 2, 4},
    41  		{4, 3, 4},
    42  		{4, 4, 4},
    43  		{4, 5, 8},
    44  		{4, 6, 8},
    45  		{4, 7, 8},
    46  		{4, 8, 8},
    47  		{4, 9, 0xc},
    48  		{4, 0xa, 0xc},
    49  		{4, 0xb, 0xc},
    50  		{4, 0xc, 0xc},
    51  		{4, 0xd, 0x10},
    52  		{4, 0xe, 0x10},
    53  		{4, 0xf, 0x10},
    54  
    55  		{8, 0, 0},
    56  		{8, 1, 8},
    57  		{8, 2, 8},
    58  		{8, 3, 8},
    59  		{8, 4, 8},
    60  		{8, 5, 8},
    61  		{8, 6, 8},
    62  		{8, 7, 8},
    63  		{8, 8, 8},
    64  		{8, 9, 0x10},
    65  		{8, 0xa, 0x10},
    66  		{8, 0xb, 0x10},
    67  		{8, 0xc, 0x10},
    68  		{8, 0xd, 0x10},
    69  		{8, 0xe, 0x10},
    70  		{8, 0xf, 0x10},
    71  	} {
    72  		c(example.align, example.in, example.tgt)
    73  		c(example.align, example.in+0x10000, example.tgt+0x10000)
    74  	}
    75  }
    76  
    77  func TestConvertInt(t *testing.T) {
    78  	var testCases = []struct {
    79  		in     uint64
    80  		signed bool
    81  		size   int64
    82  		tgt    uint64
    83  	}{
    84  		{1, false, 1, 1},
    85  		{uint64(0xf0), true, 1, 0xfffffffffffffff0},
    86  		{uint64(0xf0), false, 1, 0xf0},
    87  		{uint64(0x70), true, 1, 0x70},
    88  		{uint64(0x90f0), true, 2, 0xffffffffffff90f0},
    89  		{uint64(0x90f0), false, 2, 0x90f0},
    90  	}
    91  	for _, tc := range testCases {
    92  		out := convertInt(tc.in, tc.signed, tc.size)
    93  		t.Logf("in=%#016x signed=%v size=%d -> %#016x\n", tc.in, tc.signed, tc.size, out)
    94  		if out != tc.tgt {
    95  			t.Errorf("expected=%#016x got=%#016x\n", tc.tgt, out)
    96  		}
    97  	}
    98  }