@@ -9,6 +9,109 @@ import (
9
9
"testing"
10
10
)
11
11
12
+ func validateAddrRanges (t * testing.T , a * AddrRanges , want ... AddrRange ) {
13
+ ranges := a .Ranges ()
14
+ if len (ranges ) != len (want ) {
15
+ t .Errorf ("want %v, got %v" , want , ranges )
16
+ t .Fatal ("different lengths" )
17
+ }
18
+ gotTotalBytes := uintptr (0 )
19
+ wantTotalBytes := uintptr (0 )
20
+ for i := range ranges {
21
+ gotTotalBytes += ranges [i ].Size ()
22
+ wantTotalBytes += want [i ].Size ()
23
+ if ranges [i ].Base () >= ranges [i ].Limit () {
24
+ t .Error ("empty range found" )
25
+ }
26
+ // Ensure this is equivalent to what we want.
27
+ if ! ranges [i ].Equals (want [i ]) {
28
+ t .Errorf ("range %d: got [0x%x, 0x%x), want [0x%x, 0x%x)" , i ,
29
+ ranges [i ].Base (), ranges [i ].Limit (),
30
+ want [i ].Base (), want [i ].Limit (),
31
+ )
32
+ }
33
+ if i != 0 {
34
+ // Ensure the ranges are sorted.
35
+ if ranges [i - 1 ].Base () >= ranges [i ].Base () {
36
+ t .Errorf ("ranges %d and %d are out of sorted order" , i - 1 , i )
37
+ }
38
+ // Check for a failure to coalesce.
39
+ if ranges [i - 1 ].Limit () == ranges [i ].Base () {
40
+ t .Errorf ("ranges %d and %d should have coalesced" , i - 1 , i )
41
+ }
42
+ // Check if any ranges overlap. Because the ranges are sorted
43
+ // by base, it's sufficient to just check neighbors.
44
+ if ranges [i - 1 ].Limit () > ranges [i ].Base () {
45
+ t .Errorf ("ranges %d and %d overlap" , i - 1 , i )
46
+ }
47
+ }
48
+ }
49
+ if wantTotalBytes != gotTotalBytes {
50
+ t .Errorf ("expected %d total bytes, got %d" , wantTotalBytes , gotTotalBytes )
51
+ }
52
+ if b := a .TotalBytes (); b != gotTotalBytes {
53
+ t .Errorf ("inconsistent total bytes: want %d, got %d" , gotTotalBytes , b )
54
+ }
55
+ if t .Failed () {
56
+ t .Errorf ("addrRanges: %v" , ranges )
57
+ t .Fatal ("detected bad addrRanges" )
58
+ }
59
+ }
60
+
61
+ func TestAddrRangesAdd (t * testing.T ) {
62
+ a := NewAddrRanges ()
63
+
64
+ // First range.
65
+ a .Add (MakeAddrRange (512 , 1024 ))
66
+ validateAddrRanges (t , & a ,
67
+ MakeAddrRange (512 , 1024 ),
68
+ )
69
+
70
+ // Coalesce up.
71
+ a .Add (MakeAddrRange (1024 , 2048 ))
72
+ validateAddrRanges (t , & a ,
73
+ MakeAddrRange (512 , 2048 ),
74
+ )
75
+
76
+ // Add new independent range.
77
+ a .Add (MakeAddrRange (4096 , 8192 ))
78
+ validateAddrRanges (t , & a ,
79
+ MakeAddrRange (512 , 2048 ),
80
+ MakeAddrRange (4096 , 8192 ),
81
+ )
82
+
83
+ // Coalesce down.
84
+ a .Add (MakeAddrRange (3776 , 4096 ))
85
+ validateAddrRanges (t , & a ,
86
+ MakeAddrRange (512 , 2048 ),
87
+ MakeAddrRange (3776 , 8192 ),
88
+ )
89
+
90
+ // Coalesce up and down.
91
+ a .Add (MakeAddrRange (2048 , 3776 ))
92
+ validateAddrRanges (t , & a ,
93
+ MakeAddrRange (512 , 8192 ),
94
+ )
95
+
96
+ // Push a bunch of independent ranges to the end to try and force growth.
97
+ expectedRanges := []AddrRange {MakeAddrRange (512 , 8192 )}
98
+ for i := uintptr (0 ); i < 64 ; i ++ {
99
+ dRange := MakeAddrRange (8192 + (i + 1 )* 2048 , 8192 + (i + 1 )* 2048 + 10 )
100
+ a .Add (dRange )
101
+ expectedRanges = append (expectedRanges , dRange )
102
+ validateAddrRanges (t , & a , expectedRanges ... )
103
+ }
104
+
105
+ // Push a bunch of independent ranges to the beginning to try and force growth.
106
+ var bottomRanges []AddrRange
107
+ for i := uintptr (0 ); i < 63 ; i ++ {
108
+ dRange := MakeAddrRange (8 + i * 8 , 8 + i * 8 + 4 )
109
+ a .Add (dRange )
110
+ bottomRanges = append (bottomRanges , dRange )
111
+ validateAddrRanges (t , & a , append (bottomRanges , expectedRanges ... )... )
112
+ }
113
+ }
114
+
12
115
func TestAddrRangesFindSucc (t * testing.T ) {
13
116
var large []AddrRange
14
117
for i := 0 ; i < 100 ; i ++ {
0 commit comments