github.com/neilgarb/delve@v1.9.2-nobreaks/service/api/prettyprint_test.go (about) 1 package api 2 3 import ( 4 "fmt" 5 "math" 6 "strings" 7 "testing" 8 ) 9 10 func TestPrettyExamineMemory(t *testing.T) { 11 // Test whether always use the last addr's len to format when the lens of two adjacent address are different 12 addr := uintptr(0xffff) 13 memArea := []byte("abcdefghijklmnopqrstuvwxyz") 14 format := byte('o') 15 16 display := []string{ 17 "0x0ffff: 0141 0142 0143 0144 0145 0146 0147 0150 ", 18 "0x10007: 0151 0152 0153 0154 0155 0156 0157 0160 ", 19 "0x1000f: 0161 0162 0163 0164 0165 0166 0167 0170 ", 20 "0x10017: 0171 0172"} 21 res := strings.Split(strings.TrimSpace(PrettyExamineMemory(addr, memArea, true, format, 1)), "\n") 22 23 if len(display) != len(res) { 24 t.Fatalf("wrong lines return, expected %d but got %d", len(display), len(res)) 25 } 26 27 for i := 0; i < len(display); i++ { 28 if display[i] != res[i] { 29 errInfo := fmt.Sprintf("wrong display return at line %d\n", i+1) 30 errInfo += fmt.Sprintf("expected:\n %q\n", display[i]) 31 errInfo += fmt.Sprintf("but got:\n %q\n", res[i]) 32 t.Fatal(errInfo) 33 } 34 } 35 } 36 37 func Test_byteArrayToUInt64(t *testing.T) { 38 tests := []struct { 39 name string 40 args []byte 41 want uint64 42 }{ 43 {"case-nil", nil, 0}, 44 {"case-empty", []byte{}, 0}, 45 {"case-1", []byte{0x1}, 1}, 46 {"case-2", []byte{0x12}, 18}, 47 {"case-3", []byte{0x1, 0x2}, 513}, 48 {"case-4", []byte{0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x2}, 144397766876004609}, 49 {"case-5", []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, math.MaxUint64}, 50 } 51 for _, tt := range tests { 52 t.Run(tt.name, func(t *testing.T) { 53 got := byteArrayToUInt64(tt.args, true) 54 if got != tt.want { 55 t.Errorf("byteArrayToUInt64() got = %v, want %v", got, tt.want) 56 } 57 }) 58 } 59 }