Debian Codegraph @

internal/graph/graph_test.go

raw symbols SCIP
1 // Copyright 2016 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package graphscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/
16
17 import (
18 "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/"
19 "testingscip-go gomod github.com/golang/go/src go1.24.0 testing/"
20
21 "github.com/google/pprof/profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/"
22 )
23
24 func edgeDebugStringscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/edgeDebugString().(edge *Edgescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Edge#) string {
25 debug := ""
26 debug += fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("\t\tSrc: %p\n", edge.Srcscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Edge#Src.)
27 debug += fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("\t\tDest: %p\n", edge.Destscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Edge#Dest.)
28 debug += fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("\t\tWeight: %d\n", edge.Weightscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Edge#Weight.)
29 debug += fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("\t\tResidual: %t\n", edge.Residualscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Edge#Residual.)
30 debug += fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("\t\tInline: %t\n", edge.Inlinescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Edge#Inline.)
31 return debug
32 }
33
34 func edgeMapsDebugStringscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/edgeMapsDebugString().(in, out EdgeMapscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/EdgeMap#) string {
35 debug := ""
36 debug += "In Edges:\n"
37 for parent, edge := range in {
38 debug += fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("\tParent: %p\n", parent)
39 debug += edgeDebugStringscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/edgeDebugString().(edge)
40 }
41 debug += "Out Edges:\n"
42 for child, edge := range out {
43 debug += fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("\tChild: %p\n", child)
44 debug += edgeDebugStringscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/edgeDebugString().(edge)
45 }
46 return debug
47 }
48
49 func graphDebugStringscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/graphDebugString().(graph *Graphscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Graph#) string {
50 debug := ""
51 for i, node := range graph.Nodesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Graph#Nodes. {
52 debug += fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("Node %d: %p\n", i, node)
53 }
54
55 for i, node := range graph.Nodesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Graph#Nodes. {
56 debug += "\n"
57 debug += fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("=== Node %d: %p ===\n", i, node)
58 debug += edgeMapsDebugStringscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/edgeMapsDebugString().(node.Inscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#In., node.Outscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#Out.)
59 }
60 return debug
61 }
62
63 func expectedNodesDebugStringscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNodesDebugString().(expected []expectedNodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#) string {
64 debug := ""
65 for i, node := range expected {
66 debug += fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("Node %d: %p\n", i, node.nodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#node.)
67 }
68
69 for i, node := range expected {
70 debug += "\n"
71 debug += fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("=== Node %d: %p ===\n", i, node.nodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#node.)
72 debug += edgeMapsDebugStringscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/edgeMapsDebugString().(node.inscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#in., node.outscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#out.)
73 }
74 return debug
75 }
76
77 // edgeMapsEqual checks if all the edges in this equal all the edges in that.
78 func edgeMapsEqualscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/edgeMapsEqual().(this, that EdgeMapscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/EdgeMap#) bool {
79 if len(this) != len(that) {
80 return false
81 }
82 for node, thisEdge := range this {
83 if *thisEdge != *that[node] {
84 return false
85 }
86 }
87 return true
88 }
89
90 // nodesEqual checks if node is equal to expected.
91 func nodesEqualscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/nodesEqual().(node *Nodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#, expected expectedNodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#) bool {
92 return node == expected.nodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#node. && edgeMapsEqualscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/edgeMapsEqual().(node.Inscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#In., expected.inscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#in.) &&
93 edgeMapsEqualscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/edgeMapsEqual().(node.Outscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#Out., expected.outscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#out.)
94 }
95
96 // graphsEqual checks if graph is equivalent to the graph templated by expected.
97 func graphsEqualscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/graphsEqual().(graph *Graphscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Graph#, expected []expectedNodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#) bool {
98 if len(graph.Nodesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Graph#Nodes.) != len(expected) {
99 return false
100 }
101 expectedSet := make(map[*Nodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#]expectedNodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#)
102 for i := range expected {
103 expectedSet[expected[i].nodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#node.] = expected[i]
104 }
105
106 for _, node := range graph.Nodesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Graph#Nodes. {
107 expectedNode, found := expectedSet[node]
108 if !found || !nodesEqualscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/nodesEqual().(node, expectedNode) {
109 return false
110 }
111 }
112 return true
113 }
114
115 type expectedNodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode# struct {
116 nodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#node. *Nodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#
117 inscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#in., outscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#out. EdgeMapscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/EdgeMap#
118 }
119
120 type trimTreeTestcasescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase# struct {
121 initialscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase#initial. *Graphscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Graph#
122 expectedscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase#expected. []expectedNodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#
123 keepscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase#keep. NodePtrSetscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/NodePtrSet#
124 }
125
126 // makeExpectedEdgeResidual makes the edge from parent to child residual.
127 func makeExpectedEdgeResidualscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/makeExpectedEdgeResidual().(parent, child expectedNodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#) {
128 parent.outscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#out.[child.nodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#node.].Residualscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Edge#Residual. = true
129 child.inscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#in.[parent.nodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#node.].Residualscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Edge#Residual. = true
130 }
131
132 func makeEdgeInlinescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/makeEdgeInline().(edgeMap EdgeMapscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/EdgeMap#, node *Nodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#) {
133 edgeMap[node].Inlinescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Edge#Inline. = true
134 }
135
136 func setEdgeWeightscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/setEdgeWeight().(edgeMap EdgeMapscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/EdgeMap#, node *Nodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#, weight int64) {
137 edgeMap[node].Weightscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Edge#Weight. = weight
138 }
139
140 // createEdges creates directed edges from the parent to each of the children.
141 func createEdgesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createEdges().(parent *Nodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#, children ...*Nodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#) {
142 for _, child := range children {
143 edge := &Edgescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Edge#{
144 Srcscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Edge#Src.: parent,
145 Destscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Edge#Dest.: child,
146 }
147 parent.Outscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#Out.[child] = edge
148 child.Inscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#In.[parent] = edge
149 }
150 }
151
152 // createEmptyNode creates a node without any edges.
153 func createEmptyNodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createEmptyNode().() *Nodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node# {
154 return &Nodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#{
155 Inscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#In.: make(EdgeMapscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/EdgeMap#),
156 Outscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#Out.: make(EdgeMapscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/EdgeMap#),
157 }
158 }
159
160 // createExpectedNodes creates a slice of expectedNodes from nodes.
161 func createExpectedNodesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createExpectedNodes().(nodes ...*Nodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#) ([]expectedNodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#, NodePtrSetscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/NodePtrSet#) {
162 expected := make([]expectedNodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#, len(nodes))
163 keep := make(NodePtrSetscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/NodePtrSet#, len(nodes))
164
165 for i, node := range nodes {
166 expected[i] = expectedNodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#{
167 nodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#node.: node,
168 inscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#in.: make(EdgeMapscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/EdgeMap#),
169 outscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#out.: make(EdgeMapscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/EdgeMap#),
170 }
171 keep[node] = true
172 }
173
174 return expected, keep
175 }
176
177 // createExpectedEdges creates directed edges from the parent to each of the
178 // children.
179 func createExpectedEdgesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createExpectedEdges().(parent expectedNodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#, children ...expectedNodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#) {
180 for _, child := range children {
181 edge := &Edgescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Edge#{
182 Srcscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Edge#Src.: parent.nodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#node.,
183 Destscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Edge#Dest.: child.nodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#node.,
184 }
185 parent.outscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#out.[child.nodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#node.] = edge
186 child.inscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#in.[parent.nodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#node.] = edge
187 }
188 }
189
190 // createTestCase1 creates a test case that initially looks like:
191 //
192 // 0
193 // |(5)
194 // 1
195 // (3)/ \(4)
196 // 2 3.
197 //
198 // After keeping 0, 2, and 3, it expects the graph:
199 //
200 // 0
201 // (3)/ \(4)
202 // 2 3.
203 func createTestCase1scip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createTestCase1().() trimTreeTestcasescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase# {
204 // Create initial graph
205 graph := &Graphscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Graph#{make(Nodesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Nodes#, 4)}
206 nodes := graph.Nodesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Graph#Nodes.
207 for i := range nodes {
208 nodes[i] = createEmptyNodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createEmptyNode().()
209 }
210 createEdgesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createEdges().(nodes[0], nodes[1])
211 createEdgesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createEdges().(nodes[1], nodes[2], nodes[3])
212 makeEdgeInlinescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/makeEdgeInline().(nodes[0].Outscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#Out., nodes[1])
213 makeEdgeInlinescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/makeEdgeInline().(nodes[1].Outscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#Out., nodes[2])
214 setEdgeWeightscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/setEdgeWeight().(nodes[0].Outscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#Out., nodes[1], 5)
215 setEdgeWeightscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/setEdgeWeight().(nodes[1].Outscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#Out., nodes[2], 3)
216 setEdgeWeightscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/setEdgeWeight().(nodes[1].Outscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#Out., nodes[3], 4)
217
218 // Create expected graph
219 expected, keep := createExpectedNodesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createExpectedNodes().(nodes[0], nodes[2], nodes[3])
220 createExpectedEdgesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createExpectedEdges().(expected[0], expected[1], expected[2])
221 makeEdgeInlinescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/makeEdgeInline().(expected[0].outscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#out., expected[1].nodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#node.)
222 makeExpectedEdgeResidualscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/makeExpectedEdgeResidual().(expected[0], expected[1])
223 makeExpectedEdgeResidualscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/makeExpectedEdgeResidual().(expected[0], expected[2])
224 setEdgeWeightscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/setEdgeWeight().(expected[0].outscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#out., expected[1].nodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#node., 3)
225 setEdgeWeightscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/setEdgeWeight().(expected[0].outscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#out., expected[2].nodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#node., 4)
226 return trimTreeTestcasescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase#{
227 initialscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase#initial.: graph,
228 expectedscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase#expected.: expected,
229 keepscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase#keep.: keep,
230 }
231 }
232
233 // createTestCase2 creates a test case that initially looks like:
234 //
235 // 3
236 // | (12)
237 // 1
238 // | (8)
239 // 2
240 // | (15)
241 // 0
242 // | (10)
243 // 4.
244 //
245 // After keeping 3 and 4, it expects the graph:
246 //
247 // 3
248 // | (10)
249 // 4.
250 func createTestCase2scip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createTestCase2().() trimTreeTestcasescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase# {
251 // Create initial graph
252 graph := &Graphscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Graph#{make(Nodesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Nodes#, 5)}
253 nodes := graph.Nodesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Graph#Nodes.
254 for i := range nodes {
255 nodes[i] = createEmptyNodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createEmptyNode().()
256 }
257 createEdgesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createEdges().(nodes[3], nodes[1])
258 createEdgesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createEdges().(nodes[1], nodes[2])
259 createEdgesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createEdges().(nodes[2], nodes[0])
260 createEdgesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createEdges().(nodes[0], nodes[4])
261 setEdgeWeightscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/setEdgeWeight().(nodes[3].Outscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#Out., nodes[1], 12)
262 setEdgeWeightscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/setEdgeWeight().(nodes[1].Outscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#Out., nodes[2], 8)
263 setEdgeWeightscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/setEdgeWeight().(nodes[2].Outscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#Out., nodes[0], 15)
264 setEdgeWeightscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/setEdgeWeight().(nodes[0].Outscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#Out., nodes[4], 10)
265
266 // Create expected graph
267 expected, keep := createExpectedNodesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createExpectedNodes().(nodes[3], nodes[4])
268 createExpectedEdgesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createExpectedEdges().(expected[0], expected[1])
269 makeExpectedEdgeResidualscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/makeExpectedEdgeResidual().(expected[0], expected[1])
270 setEdgeWeightscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/setEdgeWeight().(expected[0].outscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#out., expected[1].nodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNode#node., 10)
271 return trimTreeTestcasescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase#{
272 initialscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase#initial.: graph,
273 expectedscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase#expected.: expected,
274 keepscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase#keep.: keep,
275 }
276 }
277
278 // createTestCase3 creates an initially empty graph and expects an empty graph
279 // after trimming.
280 func createTestCase3scip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createTestCase3().() trimTreeTestcasescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase# {
281 graph := &Graphscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Graph#{make(Nodesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Nodes#, 0)}
282 expected, keep := createExpectedNodesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createExpectedNodes().()
283 return trimTreeTestcasescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase#{
284 initialscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase#initial.: graph,
285 expectedscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase#expected.: expected,
286 keepscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase#keep.: keep,
287 }
288 }
289
290 // createTestCase4 creates a test case that initially looks like:
291 //
292 // 0.
293 //
294 // After keeping 0, it expects the graph:
295 //
296 // 0.
297 func createTestCase4scip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createTestCase4().() trimTreeTestcasescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase# {
298 graph := &Graphscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Graph#{make(Nodesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Nodes#, 1)}
299 nodes := graph.Nodesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Graph#Nodes.
300 for i := range nodes {
301 nodes[i] = createEmptyNodescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createEmptyNode().()
302 }
303 expected, keep := createExpectedNodesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createExpectedNodes().(nodes[0])
304 return trimTreeTestcasescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase#{
305 initialscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase#initial.: graph,
306 expectedscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase#expected.: expected,
307 keepscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase#keep.: keep,
308 }
309 }
310
311 func createTrimTreeTestCasesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createTrimTreeTestCases().() []trimTreeTestcasescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase# {
312 caseGenerators := []func() trimTreeTestcasescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase#{
313 createTestCase1scip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createTestCase1().,
314 createTestCase2scip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createTestCase2().,
315 createTestCase3scip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createTestCase3().,
316 createTestCase4scip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createTestCase4().,
317 }
318 cases := make([]trimTreeTestcasescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase#, len(caseGenerators))
319 for i, gen := range caseGenerators {
320 cases[i] = gen()
321 }
322 return cases
323 }
324
325 func TestTrimTreescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/TestTrimTree().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) {
326 tests := createTrimTreeTestCasesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/createTrimTreeTestCases().()
327 for _, test := range tests {
328 graph := test.initialscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase#initial.
329 graph.TrimTreescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Graph#TrimTree().(test.keepscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase#keep.)
330 if !graphsEqualscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/graphsEqual().(graph, test.expectedscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase#expected.) {
331 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("Graphs do not match.\nExpected: %s\nFound: %s\n",
332 expectedNodesDebugStringscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/expectedNodesDebugString().(test.expectedscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/trimTreeTestcase#expected.),
333 graphDebugStringscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/graphDebugString().(graph))
334 }
335 }
336 }
337
338 func nodeTestProfilescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/nodeTestProfile().() *profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile# {
339 mappings := []*profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#{
340 {
341 IDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#ID.: 1,
342 Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#File.: "symbolized_binary",
343 },
344 {
345 IDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#ID.: 2,
346 Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#File.: "unsymbolized_library_1",
347 },
348 {
349 IDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#ID.: 3,
350 Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#File.: "unsymbolized_library_2",
351 },
352 }
353 functions := []*profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Functionscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Function#{
354 {IDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Function#ID.: 1, Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Function#Name.: "symname"},
355 {IDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Function#ID.: 2},
356 }
357 locations := []*profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#{
358 {
359 IDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#ID.: 1,
360 Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Mapping.: mappings[0],
361 Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line.: []profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Line#{
362 {Functionscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Line#Function.: functions[0]},
363 },
364 },
365 {
366 IDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#ID.: 2,
367 Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Mapping.: mappings[1],
368 Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line.: []profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Line#{
369 {Functionscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Line#Function.: functions[1]},
370 },
371 },
372 {
373 IDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#ID.: 3,
374 Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Mapping.: mappings[2],
375 },
376 }
377 return &profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#{
378 PeriodTypescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#PeriodType.: &profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.ValueTypescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/ValueType#{Typescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/ValueType#Type.: "cpu", Unitscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/ValueType#Unit.: "milliseconds"},
379 SampleTypescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#SampleType.: []*profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.ValueTypescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/ValueType#{
380 {Typescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/ValueType#Type.: "type", Unitscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/ValueType#Unit.: "unit"},
381 },
382 Samplescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Sample.: []*profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Samplescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Sample#{
383 {
384 Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Sample#Location.: []*profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#{locations[0]},
385 Valuescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Sample#Value.: []int64{1},
386 },
387 {
388 Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Sample#Location.: []*profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#{locations[1]},
389 Valuescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Sample#Value.: []int64{1},
390 },
391 {
392 Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Sample#Location.: []*profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#{locations[2]},
393 Valuescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Sample#Value.: []int64{1},
394 },
395 },
396 Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Location.: locations,
397 Functionscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Function.: functions,
398 Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Mapping.: mappings,
399 }
400 }
401
402 // TestCreateNodes checks that nodes are properly created for a simple profile.
403 func TestCreateNodesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/TestCreateNodes().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) {
404 testProfile := nodeTestProfilescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/nodeTestProfile().()
405 wantNodeSet := NodeSetscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/NodeSet#{
406 {Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/NodeInfo#Name.: "symname"}: true,
407 {Objfilescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/NodeInfo#Objfile.: "unsymbolized_library_1"}: true,
408 {Objfilescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/NodeInfo#Objfile.: "unsymbolized_library_2"}: true,
409 }
410
411 nodes, _ := CreateNodesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/CreateNodes().(testProfile, &Optionsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Options#{})
412 if len(nodes) != len(wantNodeSet) {
413 t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("got %d nodes, want %d", len(nodes), len(wantNodeSet))
414 }
415 for _, node := range nodes {
416 if !wantNodeSet[node.Infoscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#Info.] {
417 t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("unexpected node %v", node.Infoscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/Node#Info.)
418 }
419 }
420 }
421
422 func TestShortenFunctionNamescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/TestShortenFunctionName().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) {
423 type testCase struct {
424 name string
425 want string
426 }
427 testcases := []testCase{
428 {
429 "root",
430 "root",
431 },
432 {
433 "syscall.Syscall",
434 "syscall.Syscall",
435 },
436 {
437 "net/http.(*conn).serve",
438 "http.(*conn).serve",
439 },
440 {
441 "github.com/blahBlah/foo.Foo",
442 "foo.Foo",
443 },
444 {
445 "github.com/BlahBlah/foo.Foo",
446 "foo.Foo",
447 },
448 {
449 "github.com/BlahBlah/foo.Foo[...]",
450 "foo.Foo[...]",
451 },
452 {
453 "github.com/blah-blah/foo_bar.(*FooBar).Foo",
454 "foo_bar.(*FooBar).Foo",
455 },
456 {
457 "encoding/json.(*structEncoder).(encoding/json.encode)-fm",
458 "json.(*structEncoder).(encoding/json.encode)-fm",
459 },
460 {
461 "github.com/blah/blah/vendor/gopkg.in/redis.v3.(*baseClient).(github.com/blah/blah/vendor/gopkg.in/redis.v3.process)-fm",
462 "redis.v3.(*baseClient).(github.com/blah/blah/vendor/gopkg.in/redis.v3.process)-fm",
463 },
464 {
465 "github.com/foo/bar/v4.(*Foo).Bar",
466 "bar.(*Foo).Bar",
467 },
468 {
469 "github.com/foo/bar/v4/baz.Foo.Bar",
470 "baz.Foo.Bar",
471 },
472 {
473 "github.com/foo/bar/v123.(*Foo).Bar",
474 "bar.(*Foo).Bar",
475 },
476 {
477 "github.com/foobar/v0.(*Foo).Bar",
478 "v0.(*Foo).Bar",
479 },
480 {
481 "github.com/foobar/v1.(*Foo).Bar",
482 "v1.(*Foo).Bar",
483 },
484 {
485 "example.org/v2xyz.Foo",
486 "v2xyz.Foo",
487 },
488 {
489 "github.com/foo/bar/v4/v4.(*Foo).Bar",
490 "v4.(*Foo).Bar",
491 },
492 {
493 "github.com/foo/bar/v4/foo/bar/v4.(*Foo).Bar",
494 "v4.(*Foo).Bar",
495 },
496 {
497 "java.util.concurrent.ThreadPoolExecutor$Worker.run",
498 "ThreadPoolExecutor$Worker.run",
499 },
500 {
501 "java.bar.foo.FooBar.run(java.lang.Runnable)",
502 "FooBar.run",
503 },
504 {
505 "(anonymous namespace)::Bar::Foo",
506 "Bar::Foo",
507 },
508 {
509 "(anonymous namespace)::foo",
510 "foo",
511 },
512 {
513 "cpp::namespace::Class::method()::$_100::operator()",
514 "Class::method",
515 },
516 {
517 "foo_bar::Foo::bar",
518 "Foo::bar",
519 },
520 {
521 "cpp::namespace::Class::method<float, long, int>()",
522 "Class::method<float, long, int>",
523 },
524 {
525 "foo",
526 "foo",
527 },
528 {
529 "foo/xyz",
530 "foo/xyz",
531 },
532 {
533 "com.google.perftools.gwp.benchmark.FloatBench.lambda$run$0",
534 "FloatBench.lambda$run$0",
535 },
536 {
537 "java.bar.foo.FooBar.run$0",
538 "FooBar.run$0",
539 },
540 }
541 for _, tc := range testcases {
542 name := ShortenFunctionNamescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/graph`/ShortenFunctionName().(tc.name)
543 if got, want := name, tc.want; got != want {
544 t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("ShortenFunctionName(%q) = %q, want %q", tc.name, got, want)
545 }
546 }
547 }