1 /* 2 * Copyright (C) 2016 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 17 package com.android.ahat; 18 19 import com.android.ahat.heapdump.DiffFields; 20 import com.android.ahat.heapdump.DiffedFieldValue; 21 import com.android.ahat.heapdump.FieldValue; 22 import com.android.ahat.heapdump.Type; 23 import com.android.ahat.heapdump.Value; 24 import java.util.ArrayList; 25 import java.util.List; 26 import org.junit.Test; 27 28 import static org.junit.Assert.assertEquals; 29 import static org.junit.Assert.assertNull; 30 31 public class DiffFieldsTest { 32 // Give more convenient abstract names for different types. 33 private static final Type t0 = Type.OBJECT; 34 private static final Type t1 = Type.BOOLEAN; 35 private static final Type t2 = Type.CHAR; 36 private static final Type t3 = Type.FLOAT; 37 private static final Type t4 = Type.DOUBLE; 38 private static final Type t5 = Type.BYTE; 39 private static final Type t6 = Type.SHORT; 40 private static final Type t7 = Type.INT; 41 private static final Type t8 = Type.LONG; 42 43 @Test normalMatchedDiffedFieldValues()44 public void normalMatchedDiffedFieldValues() { 45 FieldValue normal1 = new FieldValue("name", t0, Value.pack(1)); 46 FieldValue normal2 = new FieldValue("name", t0, Value.pack(2)); 47 48 DiffedFieldValue x = DiffedFieldValue.matched(normal1, normal2); 49 assertEquals("name", x.name); 50 assertEquals(t0, x.type); 51 assertEquals(Value.pack(1), x.current); 52 assertEquals(Value.pack(2), x.baseline); 53 assertEquals(DiffedFieldValue.Status.MATCHED, x.status); 54 } 55 56 @Test nulledMatchedDiffedFieldValues()57 public void nulledMatchedDiffedFieldValues() { 58 FieldValue normal = new FieldValue("name", t0, Value.pack(1)); 59 FieldValue nulled = new FieldValue("name", t0, null); 60 61 DiffedFieldValue x = DiffedFieldValue.matched(normal, nulled); 62 assertEquals("name", x.name); 63 assertEquals(t0, x.type); 64 assertEquals(Value.pack(1), x.current); 65 assertNull(x.baseline); 66 assertEquals(DiffedFieldValue.Status.MATCHED, x.status); 67 68 DiffedFieldValue y = DiffedFieldValue.matched(nulled, normal); 69 assertEquals("name", y.name); 70 assertEquals(t0, y.type); 71 assertNull(y.current); 72 assertEquals(Value.pack(1), y.baseline); 73 assertEquals(DiffedFieldValue.Status.MATCHED, y.status); 74 } 75 76 @Test normalAddedDiffedFieldValues()77 public void normalAddedDiffedFieldValues() { 78 FieldValue normal = new FieldValue("name", t0, Value.pack(1)); 79 80 DiffedFieldValue x = DiffedFieldValue.added(normal); 81 assertEquals("name", x.name); 82 assertEquals(t0, x.type); 83 assertEquals(Value.pack(1), x.current); 84 assertEquals(DiffedFieldValue.Status.ADDED, x.status); 85 } 86 87 @Test nulledAddedDiffedFieldValues()88 public void nulledAddedDiffedFieldValues() { 89 FieldValue nulled = new FieldValue("name", t0, null); 90 91 DiffedFieldValue x = DiffedFieldValue.added(nulled); 92 assertEquals("name", x.name); 93 assertEquals(t0, x.type); 94 assertNull(x.current); 95 assertEquals(DiffedFieldValue.Status.ADDED, x.status); 96 } 97 98 @Test normalDeletedDiffedFieldValues()99 public void normalDeletedDiffedFieldValues() { 100 FieldValue normal = new FieldValue("name", t0, Value.pack(1)); 101 102 DiffedFieldValue x = DiffedFieldValue.deleted(normal); 103 assertEquals("name", x.name); 104 assertEquals(t0, x.type); 105 assertEquals(Value.pack(1), x.baseline); 106 assertEquals(DiffedFieldValue.Status.DELETED, x.status); 107 } 108 109 @Test nulledDeletedDiffedFieldValues()110 public void nulledDeletedDiffedFieldValues() { 111 FieldValue nulled = new FieldValue("name", t0, null); 112 113 DiffedFieldValue x = DiffedFieldValue.deleted(nulled); 114 assertEquals("name", x.name); 115 assertEquals(t0, x.type); 116 assertNull(x.baseline); 117 assertEquals(DiffedFieldValue.Status.DELETED, x.status); 118 } 119 120 @Test basicDiff()121 public void basicDiff() { 122 List<FieldValue> a = new ArrayList<FieldValue>(); 123 a.add(new FieldValue("n0", t0, null)); 124 a.add(new FieldValue("n2", t2, null)); 125 a.add(new FieldValue("n3", t3, null)); 126 a.add(new FieldValue("n4", t4, null)); 127 a.add(new FieldValue("n5", t5, null)); 128 a.add(new FieldValue("n6", t6, null)); 129 130 List<FieldValue> b = new ArrayList<FieldValue>(); 131 b.add(new FieldValue("n0", t0, null)); 132 b.add(new FieldValue("n1", t1, null)); 133 b.add(new FieldValue("n2", t2, null)); 134 b.add(new FieldValue("n3", t3, null)); 135 b.add(new FieldValue("n5", t5, null)); 136 b.add(new FieldValue("n6", t6, null)); 137 b.add(new FieldValue("n7", t7, null)); 138 139 // Note: The expected result makes assumptions about the implementation of 140 // field diff to match the order of the returned fields. If the 141 // implementation changes, this test may need to be generalized to accept 142 // the new implementation. 143 List<DiffedFieldValue> expected = new ArrayList<DiffedFieldValue>(); 144 expected.add(DiffedFieldValue.matched(a.get(0), b.get(0))); 145 expected.add(DiffedFieldValue.deleted(b.get(1))); 146 expected.add(DiffedFieldValue.matched(a.get(1), b.get(2))); 147 expected.add(DiffedFieldValue.matched(a.get(2), b.get(3))); 148 expected.add(DiffedFieldValue.added(a.get(3))); 149 expected.add(DiffedFieldValue.matched(a.get(4), b.get(4))); 150 expected.add(DiffedFieldValue.matched(a.get(5), b.get(5))); 151 expected.add(DiffedFieldValue.deleted(b.get(6))); 152 153 List<DiffedFieldValue> diffed = DiffFields.diff(a, b); 154 assertEquals(expected, diffed); 155 } 156 157 @Test reorderedDiff()158 public void reorderedDiff() { 159 List<FieldValue> a = new ArrayList<FieldValue>(); 160 a.add(new FieldValue("n0", t0, null)); 161 a.add(new FieldValue("n1", t1, null)); 162 a.add(new FieldValue("n2", t2, null)); 163 a.add(new FieldValue("n3", t3, null)); 164 a.add(new FieldValue("n4", t4, null)); 165 a.add(new FieldValue("n5", t5, null)); 166 a.add(new FieldValue("n6", t6, null)); 167 168 List<FieldValue> b = new ArrayList<FieldValue>(); 169 b.add(new FieldValue("n4", t4, null)); 170 b.add(new FieldValue("n1", t1, null)); 171 b.add(new FieldValue("n3", t3, null)); 172 b.add(new FieldValue("n0", t0, null)); 173 b.add(new FieldValue("n5", t5, null)); 174 b.add(new FieldValue("n2", t2, null)); 175 b.add(new FieldValue("n6", t6, null)); 176 177 // Note: The expected result makes assumptions about the implementation of 178 // field diff to match the order of the returned fields. If the 179 // implementation changes, this test may need to be generalized to accept 180 // the new implementation. 181 List<DiffedFieldValue> expected = new ArrayList<DiffedFieldValue>(); 182 expected.add(DiffedFieldValue.matched(a.get(0), b.get(3))); 183 expected.add(DiffedFieldValue.matched(a.get(1), b.get(1))); 184 expected.add(DiffedFieldValue.matched(a.get(2), b.get(5))); 185 expected.add(DiffedFieldValue.matched(a.get(3), b.get(2))); 186 expected.add(DiffedFieldValue.matched(a.get(4), b.get(0))); 187 expected.add(DiffedFieldValue.matched(a.get(5), b.get(4))); 188 expected.add(DiffedFieldValue.matched(a.get(6), b.get(6))); 189 190 List<DiffedFieldValue> diffed = DiffFields.diff(a, b); 191 assertEquals(expected, diffed); 192 } 193 } 194