-
Notifications
You must be signed in to change notification settings - Fork 3
/
DominatorTreeTest.java
166 lines (141 loc) · 5.96 KB
/
DominatorTreeTest.java
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
/*******************************************************************************
* Copyright (c) 2008, 2023 SAP AG, IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* SAP AG - initial API and implementation
*******************************************************************************/
package org.eclipse.mat.tests.snapshot;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.eclipse.mat.SnapshotException;
import org.eclipse.mat.query.IResultTable;
import org.eclipse.mat.query.IResultTree;
import org.eclipse.mat.snapshot.ISnapshot;
import org.eclipse.mat.snapshot.model.IClass;
import org.eclipse.mat.snapshot.query.SnapshotQuery;
import org.eclipse.mat.tests.TestSnapshots;
import org.eclipse.mat.util.VoidProgressListener;
import org.junit.Test;
public class DominatorTreeTest
{
@Test
public void testDomTreeSunJdk6_32() throws SnapshotException
{
testWith(TestSnapshots.getSnapshot(TestSnapshots.SUN_JDK6_32BIT, false), 224);
}
@Test
public void testDomTreeSunJdk5_64() throws SnapshotException
{
testWith(TestSnapshots.getSnapshot(TestSnapshots.SUN_JDK5_64BIT, false), 376);
}
@Test
public void testDomTreeIBMJdk6_32_System() throws SnapshotException
{
testWith(TestSnapshots.getSnapshot(TestSnapshots.IBM_JDK6_32BIT_SYSTEM, false), 256);
}
@Test
public void testDomTreeIBMJdk6_32_Heap() throws SnapshotException
{
testWith(TestSnapshots.getSnapshot(TestSnapshots.IBM_JDK6_32BIT_HEAP, false), 256);
}
private void testWith(ISnapshot snapshot, long size) throws SnapshotException
{
Collection<IClass> rClasses = snapshot.getClassesByName(
"org.eclipse.mat.tests.CreateSampleDump$DominatorTestData$R", false);
assertNotNull(rClasses);
assertFalse(rClasses.isEmpty());
IClass rClass = rClasses.iterator().next();
int rId = rClass.getObjectIds()[0];
int[] dominated = snapshot.getImmediateDominatedIds(rId);
int[] retainedSetR = snapshot.getRetainedSet(new int[] { rId }, new VoidProgressListener());
assert dominated.length == 8 : "R should be immediate dominator of 8 objects";
assertEquals("R has an unexpected retained size", size, snapshot.getRetainedHeapSize(rId));
assert retainedSetR.length == 13 : "R should retain 13 objects";
Map<String, Set<String>> children = new HashMap<String, Set<String>>();
Set<String> set = new HashSet<String>();
set.add("$A");
set.add("$B");
set.add("$C");
set.add("$D");
set.add("$E");
set.add("$H");
set.add("$I");
set.add("$K");
children.put("$R", set);
set = new HashSet<String>();
set.add("$F");
set.add("$G");
children.put("$C", set);
set = new HashSet<String>();
set.add("$L");
children.put("$D", set);
set = new HashSet<String>();
set.add("$J");
children.put("$G", set);
Map<String, String> parent = new HashMap<String, String>();
parent.put("$A", "$R");
parent.put("$B", "$R");
parent.put("$C", "$R");
parent.put("$D", "$R");
parent.put("$E", "$R");
parent.put("$H", "$R");
parent.put("$I", "$R");
parent.put("$K", "$R");
parent.put("$F", "$C");
parent.put("$G", "$C");
parent.put("$L", "$D");
parent.put("$J", "$G");
// check R
for (int id : retainedSetR)
{
String name = name(id, snapshot);
int dominatorId = snapshot.getImmediateDominatorId(id);
if (!"$R".equals(name))
{
assert parent.get(name).equals(name(dominatorId, snapshot)) : "Wrong parent of " + name;
}
int[] dominatedIds = snapshot.getImmediateDominatedIds(id);
Set<String> childrenSet = children.get(name);
for (int child : dominatedIds)
{
assert childrenSet.contains(name(child, snapshot)) : "Unknown child of " + name + " -> "
+ name(child, snapshot);
}
}
}
@Test
public void testImmDomQuerySunJdk6_32() throws SnapshotException
{
ISnapshot snapshot = TestSnapshots.getSnapshot(TestSnapshots.SUN_JDK6_32BIT, false);
SnapshotQuery query = SnapshotQuery.parse("immediate_dominators char[]", snapshot);
IResultTable t = (IResultTable) query.execute(new VoidProgressListener());
assertThat("Immediate dominators char[]", t.getRowCount(), equalTo(18));
}
@Test
public void testShowDomTreeQuerySunJdk6_32() throws SnapshotException
{
ISnapshot snapshot = TestSnapshots.getSnapshot(TestSnapshots.SUN_JDK6_32BIT, false);
SnapshotQuery query = SnapshotQuery.parse("show_dominator_tree char[]", snapshot);
IResultTree t = (IResultTree) query.execute(new VoidProgressListener());
assertThat("show dominator tree char[]", t.getElements().size(), equalTo(1341));
}
private String name(int id, ISnapshot snapshot) throws UnsupportedOperationException, SnapshotException
{
String nodeClass = snapshot.getClassOf(id).getName();
return nodeClass.substring(nodeClass.length() - 2);
}
}