github.com/cnboonhan/delve@v0.0.0-20230908061759-363f2388c2fb/pkg/dwarf/godwarf/tree_test.go (about) 1 package godwarf 2 3 import ( 4 "testing" 5 ) 6 7 func makeRanges(v ...uint64) [][2]uint64 { 8 r := make([][2]uint64, 0, len(v)/2) 9 for i := 0; i < len(v); i += 2 { 10 r = append(r, [2]uint64{v[i], v[i+1]}) 11 } 12 return r 13 } 14 15 func assertRanges(t *testing.T, out, tgt [][2]uint64) { 16 if len(out) != len(tgt) { 17 t.Errorf("\nexpected:\t%v\ngot:\t\t%v", tgt, out) 18 } 19 for i := range out { 20 if out[i] != tgt[i] { 21 t.Errorf("\nexpected:\t%v\ngot:\t\t%v", tgt, out) 22 break 23 } 24 } 25 } 26 27 func TestNormalizeRanges(t *testing.T) { 28 mr := makeRanges 29 //assertRanges(t, normalizeRanges(mr(105, 103, 90, 95, 25, 20, 20, 23)), mr(20, 23, 90, 95)) 30 assertRanges(t, normalizeRanges(mr(10, 12, 12, 15)), mr(10, 15)) 31 assertRanges(t, normalizeRanges(mr(12, 15, 10, 12)), mr(10, 15)) 32 assertRanges(t, normalizeRanges(mr(4910012, 4910013, 4910013, 4910098, 4910124, 4910127)), mr(4910012, 4910098, 4910124, 4910127)) 33 } 34 35 func TestRangeContains(t *testing.T) { 36 mr := func(start, end uint64) [2]uint64 { 37 return [2]uint64{start, end} 38 } 39 tcs := []struct { 40 a, b [2]uint64 41 tgt bool 42 }{ 43 {mr(1, 10), mr(1, 11), false}, 44 {mr(1, 10), mr(1, 1), true}, 45 {mr(1, 10), mr(10, 11), false}, 46 {mr(1, 10), mr(1, 10), true}, 47 {mr(1, 10), mr(2, 5), true}, 48 } 49 50 for _, tc := range tcs { 51 if rangeContains(tc.a, tc.b) != tc.tgt { 52 if tc.tgt { 53 t.Errorf("range %v does not contain %v (but should)", tc.a, tc.b) 54 } else { 55 t.Errorf("range %v does contain %v (but shouldn't)", tc.a, tc.b) 56 } 57 } 58 } 59 } 60 61 func TestRangesContains(t *testing.T) { 62 mr := makeRanges 63 tcs := []struct { 64 rngs1, rngs2 [][2]uint64 65 tgt bool 66 }{ 67 {mr(1, 10), mr(1, 11), false}, 68 {mr(1, 10), mr(1, 1), true}, 69 {mr(1, 10), mr(10, 11), false}, 70 {mr(1, 10), mr(1, 10), true}, 71 {mr(1, 10), mr(2, 5), true}, 72 73 {mr(1, 10, 20, 30), mr(1, 11), false}, 74 {mr(1, 10, 20, 30), mr(1, 1, 20, 22), true}, 75 {mr(1, 10, 20, 30), mr(30, 31), false}, 76 {mr(1, 10, 20, 30), mr(15, 17), false}, 77 {mr(1, 10, 20, 30), mr(1, 5, 6, 9, 21, 24), true}, 78 {mr(1, 10, 20, 30), mr(0, 1), false}, 79 } 80 81 for _, tc := range tcs { 82 if rangesContains(tc.rngs1, tc.rngs2) != tc.tgt { 83 if tc.tgt { 84 t.Errorf("ranges %v does not contain %v (but should)", tc.rngs1, tc.rngs2) 85 } else { 86 t.Errorf("ranges %v does contain %v (but shouldn't)", tc.rngs1, tc.rngs2) 87 } 88 } 89 } 90 } 91 92 func TestContainsPC(t *testing.T) { 93 mr := makeRanges 94 95 tcs := []struct { 96 rngs [][2]uint64 97 pc uint64 98 tgt bool 99 }{ 100 {mr(1, 10), 1, true}, 101 {mr(1, 10), 5, true}, 102 {mr(1, 10), 10, false}, 103 {mr(1, 10, 20, 30), 15, false}, 104 {mr(1, 10, 20, 30), 20, true}, 105 {mr(1, 10, 20, 30), 30, false}, 106 {mr(1, 10, 20, 30), 31, false}, 107 } 108 109 for _, tc := range tcs { 110 n := &Tree{Ranges: tc.rngs} 111 if n.ContainsPC(tc.pc) != tc.tgt { 112 if tc.tgt { 113 t.Errorf("ranges %v does not contain %d (but should)", tc.rngs, tc.pc) 114 } else { 115 t.Errorf("ranges %v does contain %d (but shouldn't)", tc.rngs, tc.pc) 116 } 117 } 118 } 119 }