blob: 59b1c90e10abfb06b0f4f7ac5879f44debddb229 [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
19import com.android.tools.perflib.heap.Instance;
20import java.io.IOException;
21import static org.junit.Assert.assertEquals;
22import static org.junit.Assert.assertNotNull;
23import static org.junit.Assert.assertNull;
24import org.junit.Test;
25
26public class InstanceUtilsTest {
27 @Test
Richard Uhler77ff54b2015-08-31 10:22:56 -070028 public void asStringBasic() throws IOException {
Richard Uhler35244722015-09-10 16:45:54 -070029 TestDump dump = TestDump.getTestDump();
30 Instance str = (Instance)dump.getDumpedThing("basicString");
31 assertEquals("hello, world", InstanceUtils.asString(str));
32 }
33
34 @Test
Richard Uhlerc7f77122016-02-11 08:48:24 -080035 public void asStringCharArray() throws IOException {
36 TestDump dump = TestDump.getTestDump();
37 Instance str = (Instance)dump.getDumpedThing("charArray");
38 assertEquals("char thing", InstanceUtils.asString(str));
39 }
40
41 @Test
Richard Uhler77ff54b2015-08-31 10:22:56 -070042 public void asStringTruncated() throws IOException {
43 TestDump dump = TestDump.getTestDump();
44 Instance str = (Instance)dump.getDumpedThing("basicString");
45 assertEquals("hello", InstanceUtils.asString(str, 5));
46 }
47
48 @Test
Richard Uhlerc7f77122016-02-11 08:48:24 -080049 public void asStringCharArrayTruncated() throws IOException {
50 TestDump dump = TestDump.getTestDump();
51 Instance str = (Instance)dump.getDumpedThing("charArray");
52 assertEquals("char ", InstanceUtils.asString(str, 5));
53 }
54
55 @Test
Richard Uhler77ff54b2015-08-31 10:22:56 -070056 public void asStringExactMax() throws IOException {
57 TestDump dump = TestDump.getTestDump();
58 Instance str = (Instance)dump.getDumpedThing("basicString");
59 assertEquals("hello, world", InstanceUtils.asString(str, 12));
60 }
61
62 @Test
Richard Uhlerc7f77122016-02-11 08:48:24 -080063 public void asStringCharArrayExactMax() throws IOException {
64 TestDump dump = TestDump.getTestDump();
65 Instance str = (Instance)dump.getDumpedThing("charArray");
66 assertEquals("char thing", InstanceUtils.asString(str, 10));
67 }
68
69 @Test
Richard Uhler77ff54b2015-08-31 10:22:56 -070070 public void asStringNotTruncated() throws IOException {
71 TestDump dump = TestDump.getTestDump();
72 Instance str = (Instance)dump.getDumpedThing("basicString");
73 assertEquals("hello, world", InstanceUtils.asString(str, 50));
74 }
75
76 @Test
Richard Uhlerc7f77122016-02-11 08:48:24 -080077 public void asStringCharArrayNotTruncated() throws IOException {
78 TestDump dump = TestDump.getTestDump();
79 Instance str = (Instance)dump.getDumpedThing("charArray");
80 assertEquals("char thing", InstanceUtils.asString(str, 50));
81 }
82
83 @Test
Richard Uhler77ff54b2015-08-31 10:22:56 -070084 public void asStringNegativeMax() throws IOException {
85 TestDump dump = TestDump.getTestDump();
86 Instance str = (Instance)dump.getDumpedThing("basicString");
87 assertEquals("hello, world", InstanceUtils.asString(str, -3));
88 }
89
90 @Test
Richard Uhlerc7f77122016-02-11 08:48:24 -080091 public void asStringCharArrayNegativeMax() throws IOException {
92 TestDump dump = TestDump.getTestDump();
93 Instance str = (Instance)dump.getDumpedThing("charArray");
94 assertEquals("char thing", InstanceUtils.asString(str, -3));
95 }
96
97 @Test
Richard Uhler77ff54b2015-08-31 10:22:56 -070098 public void asStringNull() throws IOException {
Richard Uhler35244722015-09-10 16:45:54 -070099 TestDump dump = TestDump.getTestDump();
100 Instance obj = (Instance)dump.getDumpedThing("nullString");
101 assertNull(InstanceUtils.asString(obj));
102 }
103
104 @Test
Richard Uhler77ff54b2015-08-31 10:22:56 -0700105 public void asStringNotString() throws IOException {
Richard Uhler35244722015-09-10 16:45:54 -0700106 TestDump dump = TestDump.getTestDump();
107 Instance obj = (Instance)dump.getDumpedThing("anObject");
108 assertNotNull(obj);
109 assertNull(InstanceUtils.asString(obj));
110 }
Richard Uhlerb3577302015-10-29 13:02:42 -0700111
112 @Test
113 public void basicReference() throws IOException {
114 TestDump dump = TestDump.getTestDump();
115
116 Instance pref = (Instance)dump.getDumpedThing("aPhantomReference");
117 Instance wref = (Instance)dump.getDumpedThing("aWeakReference");
118 Instance referent = (Instance)dump.getDumpedThing("anObject");
119 assertNotNull(pref);
120 assertNotNull(wref);
121 assertNotNull(referent);
122 assertEquals(referent, InstanceUtils.getReferent(pref));
123 assertEquals(referent, InstanceUtils.getReferent(wref));
124 assertNull(InstanceUtils.getReferent(referent));
125 }
Richard Uhler35244722015-09-10 16:45:54 -0700126}