forked from google/pprof
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_test.go
167 lines (162 loc) · 3.47 KB
/
merge_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2018 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package profile
import (
"testing"
)
func TestMapMapping(t *testing.T) {
pm := &profileMerger{
p: &Profile{},
mappings: make(map[mappingKey]*Mapping),
mappingsByID: make(map[uint64]mapInfo),
}
for _, tc := range []struct {
desc string
m1 Mapping
m2 Mapping
wantMerged bool
}{
{
desc: "same file name",
m1: Mapping{
ID: 1,
File: "test-file-1",
},
m2: Mapping{
ID: 2,
File: "test-file-1",
},
wantMerged: true,
},
{
desc: "same build ID",
m1: Mapping{
ID: 3,
BuildID: "test-build-id-1",
},
m2: Mapping{
ID: 4,
BuildID: "test-build-id-1",
},
wantMerged: true,
},
{
desc: "same fake mapping",
m1: Mapping{
ID: 5,
},
m2: Mapping{
ID: 6,
},
wantMerged: true,
},
{
desc: "different start",
m1: Mapping{
ID: 7,
Start: 0x1000,
Limit: 0x2000,
BuildID: "test-build-id-2",
},
m2: Mapping{
ID: 8,
Start: 0x3000,
Limit: 0x4000,
BuildID: "test-build-id-2",
},
wantMerged: true,
},
{
desc: "different file name",
m1: Mapping{
ID: 9,
File: "test-file-2",
},
m2: Mapping{
ID: 10,
File: "test-file-3",
},
},
{
desc: "different build id",
m1: Mapping{
ID: 11,
BuildID: "test-build-id-3",
},
m2: Mapping{
ID: 12,
BuildID: "test-build-id-4",
},
},
{
desc: "different size",
m1: Mapping{
ID: 13,
Start: 0x1000,
Limit: 0x3000,
BuildID: "test-build-id-5",
},
m2: Mapping{
ID: 14,
Start: 0x1000,
Limit: 0x5000,
BuildID: "test-build-id-5",
},
},
{
desc: "different offset",
m1: Mapping{
ID: 15,
Offset: 1,
BuildID: "test-build-id-6",
},
m2: Mapping{
ID: 16,
Offset: 2,
BuildID: "test-build-id-6",
},
},
} {
t.Run(tc.desc, func(t *testing.T) {
info1 := pm.mapMapping(&tc.m1)
info2 := pm.mapMapping(&tc.m2)
gotM1, gotM2 := *info1.m, *info2.m
wantM1 := tc.m1
wantM1.ID = gotM1.ID
if gotM1 != wantM1 {
t.Errorf("first mapping got %v, want %v", gotM1, wantM1)
}
if tc.wantMerged {
if gotM1 != gotM2 {
t.Errorf("first mapping got %v, second mapping got %v, want equal", gotM1, gotM2)
}
if info1.offset != 0 {
t.Errorf("first mapping info got offset %d, want 0", info1.offset)
}
if wantOffset := int64(tc.m1.Start) - int64(tc.m2.Start); wantOffset != info2.offset {
t.Errorf("second mapping info got offset %d, want %d", info2.offset, wantOffset)
}
} else {
if gotM1.ID == gotM2.ID {
t.Errorf("first mapping got %v, second mapping got %v, want different IDs", gotM1, gotM2)
}
wantM2 := tc.m2
wantM2.ID = gotM2.ID
if gotM2 != wantM2 {
t.Errorf("second mapping got %v, want %v", gotM2, wantM2)
}
}
})
}
}