blob: ec77e70da1e33dbe2978a1352743800bb0e6d4d0 [file] [log] [blame]
Richard Uhler35244722015-09-10 16:45:54 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.ahat;
18
Richard Uhler69286492016-09-20 10:41:47 +010019import com.android.tools.perflib.heap.ArrayInstance;
20import com.android.tools.perflib.heap.ClassObj;
Richard Uhler35244722015-09-10 16:45:54 -070021import com.android.tools.perflib.heap.Instance;
22import java.io.IOException;
Richard Uhler69286492016-09-20 10:41:47 +010023import java.util.List;
Richard Uhler35244722015-09-10 16:45:54 -070024import static org.junit.Assert.assertEquals;
Richard Uhler69286492016-09-20 10:41:47 +010025import static org.junit.Assert.assertFalse;
Richard Uhler35244722015-09-10 16:45:54 -070026import static org.junit.Assert.assertNotNull;
27import static org.junit.Assert.assertNull;
Richard Uhler69286492016-09-20 10:41:47 +010028import static org.junit.Assert.assertTrue;
Richard Uhler35244722015-09-10 16:45:54 -070029import org.junit.Test;
30
31public class InstanceUtilsTest {
32 @Test
Richard Uhler77ff54b2015-08-31 10:22:56 -070033 public void asStringBasic() throws IOException {
Richard Uhler35244722015-09-10 16:45:54 -070034 TestDump dump = TestDump.getTestDump();
35 Instance str = (Instance)dump.getDumpedThing("basicString");
36 assertEquals("hello, world", InstanceUtils.asString(str));
37 }
38
39 @Test
Richard Uhlerc7f77122016-02-11 08:48:24 -080040 public void asStringCharArray() throws IOException {
41 TestDump dump = TestDump.getTestDump();
42 Instance str = (Instance)dump.getDumpedThing("charArray");
43 assertEquals("char thing", InstanceUtils.asString(str));
44 }
45
46 @Test
Richard Uhler77ff54b2015-08-31 10:22:56 -070047 public void asStringTruncated() throws IOException {
48 TestDump dump = TestDump.getTestDump();
49 Instance str = (Instance)dump.getDumpedThing("basicString");
50 assertEquals("hello", InstanceUtils.asString(str, 5));
51 }
52
53 @Test
Richard Uhlerc7f77122016-02-11 08:48:24 -080054 public void asStringCharArrayTruncated() throws IOException {
55 TestDump dump = TestDump.getTestDump();
56 Instance str = (Instance)dump.getDumpedThing("charArray");
57 assertEquals("char ", InstanceUtils.asString(str, 5));
58 }
59
60 @Test
Richard Uhler77ff54b2015-08-31 10:22:56 -070061 public void asStringExactMax() throws IOException {
62 TestDump dump = TestDump.getTestDump();
63 Instance str = (Instance)dump.getDumpedThing("basicString");
64 assertEquals("hello, world", InstanceUtils.asString(str, 12));
65 }
66
67 @Test
Richard Uhlerc7f77122016-02-11 08:48:24 -080068 public void asStringCharArrayExactMax() throws IOException {
69 TestDump dump = TestDump.getTestDump();
70 Instance str = (Instance)dump.getDumpedThing("charArray");
71 assertEquals("char thing", InstanceUtils.asString(str, 10));
72 }
73
74 @Test
Richard Uhler77ff54b2015-08-31 10:22:56 -070075 public void asStringNotTruncated() throws IOException {
76 TestDump dump = TestDump.getTestDump();
77 Instance str = (Instance)dump.getDumpedThing("basicString");
78 assertEquals("hello, world", InstanceUtils.asString(str, 50));
79 }
80
81 @Test
Richard Uhlerc7f77122016-02-11 08:48:24 -080082 public void asStringCharArrayNotTruncated() throws IOException {
83 TestDump dump = TestDump.getTestDump();
84 Instance str = (Instance)dump.getDumpedThing("charArray");
85 assertEquals("char thing", InstanceUtils.asString(str, 50));
86 }
87
88 @Test
Richard Uhler77ff54b2015-08-31 10:22:56 -070089 public void asStringNegativeMax() throws IOException {
90 TestDump dump = TestDump.getTestDump();
91 Instance str = (Instance)dump.getDumpedThing("basicString");
92 assertEquals("hello, world", InstanceUtils.asString(str, -3));
93 }
94
95 @Test
Richard Uhlerc7f77122016-02-11 08:48:24 -080096 public void asStringCharArrayNegativeMax() throws IOException {
97 TestDump dump = TestDump.getTestDump();
98 Instance str = (Instance)dump.getDumpedThing("charArray");
99 assertEquals("char thing", InstanceUtils.asString(str, -3));
100 }
101
102 @Test
Richard Uhler77ff54b2015-08-31 10:22:56 -0700103 public void asStringNull() throws IOException {
Richard Uhler35244722015-09-10 16:45:54 -0700104 TestDump dump = TestDump.getTestDump();
105 Instance obj = (Instance)dump.getDumpedThing("nullString");
106 assertNull(InstanceUtils.asString(obj));
107 }
108
109 @Test
Richard Uhler77ff54b2015-08-31 10:22:56 -0700110 public void asStringNotString() throws IOException {
Richard Uhler35244722015-09-10 16:45:54 -0700111 TestDump dump = TestDump.getTestDump();
112 Instance obj = (Instance)dump.getDumpedThing("anObject");
113 assertNotNull(obj);
114 assertNull(InstanceUtils.asString(obj));
115 }
Richard Uhlerb3577302015-10-29 13:02:42 -0700116
117 @Test
118 public void basicReference() throws IOException {
119 TestDump dump = TestDump.getTestDump();
120
121 Instance pref = (Instance)dump.getDumpedThing("aPhantomReference");
122 Instance wref = (Instance)dump.getDumpedThing("aWeakReference");
123 Instance referent = (Instance)dump.getDumpedThing("anObject");
124 assertNotNull(pref);
125 assertNotNull(wref);
126 assertNotNull(referent);
127 assertEquals(referent, InstanceUtils.getReferent(pref));
128 assertEquals(referent, InstanceUtils.getReferent(wref));
129 assertNull(InstanceUtils.getReferent(referent));
130 }
Richard Uhler69286492016-09-20 10:41:47 +0100131
132 @Test
133 public void gcRootPath() throws IOException {
134 TestDump dump = TestDump.getTestDump();
135
136 ClassObj main = dump.getAhatSnapshot().findClass("Main");
137 ArrayInstance gcPathArray = (ArrayInstance)dump.getDumpedThing("gcPathArray");
138 Object[] values = gcPathArray.getValues();
139 Instance base = (Instance)values[2];
140 Instance left = InstanceUtils.getRefField(base, "left");
141 Instance right = InstanceUtils.getRefField(base, "right");
142 Instance target = InstanceUtils.getRefField(left, "right");
143
144 List<InstanceUtils.PathElement> path = InstanceUtils.getPathFromGcRoot(target);
145 assertEquals(6, path.size());
146
147 assertEquals(main, path.get(0).instance);
148 assertEquals(".stuff", path.get(0).field);
149 assertTrue(path.get(0).isDominator);
150
151 assertEquals(".gcPathArray", path.get(1).field);
152 assertTrue(path.get(1).isDominator);
153
154 assertEquals(gcPathArray, path.get(2).instance);
155 assertEquals("[2]", path.get(2).field);
156 assertTrue(path.get(2).isDominator);
157
158 assertEquals(base, path.get(3).instance);
159 assertTrue(path.get(3).isDominator);
160
161 // There are two possible paths. Either it can go through the 'left' node,
162 // or the 'right' node.
163 if (path.get(3).field.equals(".left")) {
164 assertEquals(".left", path.get(3).field);
165
166 assertEquals(left, path.get(4).instance);
167 assertEquals(".right", path.get(4).field);
168 assertFalse(path.get(4).isDominator);
169
170 } else {
171 assertEquals(".right", path.get(3).field);
172
173 assertEquals(right, path.get(4).instance);
174 assertEquals(".left", path.get(4).field);
175 assertFalse(path.get(4).isDominator);
176 }
177
178 assertEquals(target, path.get(5).instance);
179 assertEquals("", path.get(5).field);
180 assertTrue(path.get(5).isDominator);
181 }
Richard Uhler35244722015-09-10 16:45:54 -0700182}