1/*
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.ClassObj;
20import com.android.tools.perflib.heap.Heap;
21import java.util.ArrayList;
22import java.util.Collections;
23import java.util.List;
24import static org.junit.Assert.assertEquals;
25import org.junit.Test;
26
27public class SortTest {
28  @Test
29  public void objectsInfo() {
30    Heap heapA = new Heap(0xA, "A");
31    Heap heapB = new Heap(0xB, "B");
32    ClassObj classA = new ClassObj(0x1A, null, "classA", 0);
33    ClassObj classB = new ClassObj(0x1B, null, "classB", 0);
34    ClassObj classC = new ClassObj(0x1C, null, "classC", 0);
35    Site.ObjectsInfo infoA = new Site.ObjectsInfo(heapA, classA, 4, 14);
36    Site.ObjectsInfo infoB = new Site.ObjectsInfo(heapB, classB, 2, 15);
37    Site.ObjectsInfo infoC = new Site.ObjectsInfo(heapA, classC, 3, 13);
38    Site.ObjectsInfo infoD = new Site.ObjectsInfo(heapB, classA, 5, 12);
39    Site.ObjectsInfo infoE = new Site.ObjectsInfo(heapA, classB, 1, 11);
40    List<Site.ObjectsInfo> list = new ArrayList<Site.ObjectsInfo>();
41    list.add(infoA);
42    list.add(infoB);
43    list.add(infoC);
44    list.add(infoD);
45    list.add(infoE);
46
47    // Sort by size.
48    Collections.sort(list, new Sort.ObjectsInfoBySize());
49    assertEquals(infoB, list.get(0));
50    assertEquals(infoA, list.get(1));
51    assertEquals(infoC, list.get(2));
52    assertEquals(infoD, list.get(3));
53    assertEquals(infoE, list.get(4));
54
55    // Sort by class name.
56    Collections.sort(list, new Sort.ObjectsInfoByClassName());
57    assertEquals(classA, list.get(0).classObj);
58    assertEquals(classA, list.get(1).classObj);
59    assertEquals(classB, list.get(2).classObj);
60    assertEquals(classB, list.get(3).classObj);
61    assertEquals(classC, list.get(4).classObj);
62
63    // Sort by heap name.
64    Collections.sort(list, new Sort.ObjectsInfoByHeapName());
65    assertEquals(heapA, list.get(0).heap);
66    assertEquals(heapA, list.get(1).heap);
67    assertEquals(heapA, list.get(2).heap);
68    assertEquals(heapB, list.get(3).heap);
69    assertEquals(heapB, list.get(4).heap);
70
71    // Sort first by class name, then by size.
72    Collections.sort(list, new Sort.WithPriority<Site.ObjectsInfo>(
73          new Sort.ObjectsInfoByClassName(),
74          new Sort.ObjectsInfoBySize()));
75    assertEquals(infoA, list.get(0));
76    assertEquals(infoD, list.get(1));
77    assertEquals(infoB, list.get(2));
78    assertEquals(infoE, list.get(3));
79    assertEquals(infoC, list.get(4));
80  }
81}
82