1/*
2 * Copyright (C) 2007 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 android.core;
18
19import android.test.PerformanceTestBase;
20import android.test.PerformanceTestCase;
21
22import java.util.Collection;
23import java.util.Set;
24import java.util.SortedMap;
25import java.util.TreeMap;
26
27/**
28 * Implements basic performance test functionality for java.util.TreeMap
29 */
30
31public class TreeMapPerformanceTest extends PerformanceTestBase {
32    public static final int ITERATIONS = 1000;
33    public static TreeMap<String, Integer> sMap;
34    public static String[] sKeys;
35
36    @Override
37    @SuppressWarnings("unchecked")
38    protected void setUp() throws Exception {
39        super.setUp();
40        sMap = new TreeMap();
41        sKeys = new String[ITERATIONS];
42        for (int i = ITERATIONS - 1; i >= 0; i--) {
43            sKeys[i] = Integer.toString(i, 16);
44            sMap.put(sKeys[i], i);
45        }
46    }
47
48    @Override
49    public int startPerformance(PerformanceTestCase.Intermediates intermediates) {
50        intermediates.setInternalIterations(ITERATIONS);
51        return 0;
52    }
53
54    @SuppressWarnings("unchecked")
55    public void testTreeMapPut() {
56        TreeMap map = new TreeMap();
57        for (int i = ITERATIONS - 1; i >= 0; i--) {
58            map.put(i, i);
59            map.put(i, i);
60            map.put(i, i);
61            map.put(i, i);
62            map.put(i, i);
63            map.put(i, i);
64            map.put(i, i);
65            map.put(i, i);
66            map.put(i, i);
67            map.put(i, i);
68        }
69    }
70
71    public void testTreeMapGet() {
72        int value;
73        TreeMap<String, Integer> map = sMap;
74        String[] keys = sKeys;
75        for (int i = ITERATIONS - 1; i >= 0; i--) {
76            value = map.get(keys[i]);
77            value = map.get(keys[i]);
78            value = map.get(keys[i]);
79            value = map.get(keys[i]);
80            value = map.get(keys[i]);
81            value = map.get(keys[i]);
82            value = map.get(keys[i]);
83            value = map.get(keys[i]);
84            value = map.get(keys[i]);
85            value = map.get(keys[i]);
86        }
87    }
88
89    public void testTreeMapFirstKey() {
90        String key;
91        TreeMap<String, Integer> map = sMap;
92        for (int i = ITERATIONS - 1; i >= 0; i--) {
93            key = map.firstKey();
94            key = map.firstKey();
95            key = map.firstKey();
96            key = map.firstKey();
97            key = map.firstKey();
98            key = map.firstKey();
99            key = map.firstKey();
100            key = map.firstKey();
101            key = map.firstKey();
102            key = map.firstKey();
103        }
104    }
105
106    public void testTreeMapKeySet() {
107        Set keyset;
108        TreeMap<String, Integer> map = sMap;
109        for (int i = ITERATIONS - 1; i >= 0; i--) {
110            keyset = map.keySet();
111            keyset = map.keySet();
112            keyset = map.keySet();
113            keyset = map.keySet();
114            keyset = map.keySet();
115            keyset = map.keySet();
116            keyset = map.keySet();
117            keyset = map.keySet();
118            keyset = map.keySet();
119            keyset = map.keySet();
120        }
121    }
122
123    public void testTreeMapEntrySet() {
124        Set keyset;
125        TreeMap<String, Integer> map = sMap;
126        for (int i = ITERATIONS - 1; i >= 0; i--) {
127            keyset = map.entrySet();
128            keyset = map.entrySet();
129            keyset = map.entrySet();
130            keyset = map.entrySet();
131            keyset = map.entrySet();
132            keyset = map.entrySet();
133            keyset = map.entrySet();
134            keyset = map.entrySet();
135            keyset = map.entrySet();
136            keyset = map.entrySet();
137        }
138    }
139
140    public void testTreeMapValues() {
141        Collection collection;
142        TreeMap<String, Integer> map = sMap;
143        for (int i = ITERATIONS - 1; i >= 0; i--) {
144            collection = map.values();
145            collection = map.values();
146            collection = map.values();
147            collection = map.values();
148            collection = map.values();
149            collection = map.values();
150            collection = map.values();
151            collection = map.values();
152            collection = map.values();
153            collection = map.values();
154        }
155    }
156
157    public void testTreeMapSize() {
158        int len;
159        TreeMap<String, Integer> map = sMap;
160        for (int i = ITERATIONS - 1; i >= 0; i--) {
161            len = map.size();
162            len = map.size();
163            len = map.size();
164            len = map.size();
165            len = map.size();
166            len = map.size();
167            len = map.size();
168            len = map.size();
169            len = map.size();
170            len = map.size();
171        }
172    }
173
174    public void testTreeMapContainsKey() {
175        boolean flag;
176        String key = sKeys[525];
177        TreeMap<String, Integer> map = sMap;
178        for (int i = ITERATIONS - 1; i >= 0; i--) {
179            flag = map.containsKey(key);
180            flag = map.containsKey(key);
181            flag = map.containsKey(key);
182            flag = map.containsKey(key);
183            flag = map.containsKey(key);
184            flag = map.containsKey(key);
185            flag = map.containsKey(key);
186            flag = map.containsKey(key);
187            flag = map.containsKey(key);
188            flag = map.containsKey(key);
189        }
190    }
191
192    public void testTreeMapContainsValue() {
193        boolean flag;
194        TreeMap<String, Integer> map = sMap;
195        for (int i = ITERATIONS - 1; i >= 0; i--) {
196            flag = map.containsValue(i);
197            flag = map.containsValue(i);
198            flag = map.containsValue(i);
199            flag = map.containsValue(i);
200            flag = map.containsValue(i);
201            flag = map.containsValue(i);
202            flag = map.containsValue(i);
203            flag = map.containsValue(i);
204            flag = map.containsValue(i);
205            flag = map.containsValue(i);
206        }
207    }
208
209    public void testTreeMapHeadMap() {
210        SortedMap map;
211        String str = sKeys[100];
212        TreeMap<String, Integer> tMap = sMap;
213        for (int i = ITERATIONS - 1; i >= 0; i--) {
214            map = tMap.headMap(str);
215            map = tMap.headMap(str);
216            map = tMap.headMap(str);
217            map = tMap.headMap(str);
218            map = tMap.headMap(str);
219            map = tMap.headMap(str);
220            map = tMap.headMap(str);
221            map = tMap.headMap(str);
222            map = tMap.headMap(str);
223            map = tMap.headMap(str);
224        }
225    }
226
227    public void testTreeMapSubMap() {
228        String str1 = sKeys[400];
229        String str2 = sKeys[500];
230        SortedMap map;
231        TreeMap<String, Integer> tMap = sMap;
232        for (int i = ITERATIONS - 1; i >= 0; i--) {
233            map = tMap.subMap(str1, str2);
234            map = tMap.subMap(str1, str2);
235            map = tMap.subMap(str1, str2);
236            map = tMap.subMap(str1, str2);
237            map = tMap.subMap(str1, str2);
238            map = tMap.subMap(str1, str2);
239            map = tMap.subMap(str1, str2);
240            map = tMap.subMap(str1, str2);
241            map = tMap.subMap(str1, str2);
242            map = tMap.subMap(str1, str2);
243        }
244    }
245
246    public void testTreeMapTailMap() {
247        String str = sKeys[900];
248        TreeMap<String, Integer> tMap = sMap;
249        SortedMap map;
250        for (int i = ITERATIONS - 1; i >= 0; i--) {
251            map = tMap.tailMap(str);
252            map = tMap.tailMap(str);
253            map = tMap.tailMap(str);
254            map = tMap.tailMap(str);
255            map = tMap.tailMap(str);
256            map = tMap.tailMap(str);
257            map = tMap.tailMap(str);
258            map = tMap.tailMap(str);
259            map = tMap.tailMap(str);
260            map = tMap.tailMap(str);
261        }
262    }
263
264    @SuppressWarnings("unchecked")
265    public void testTreeMapRemove() {
266        TreeMap<String, Integer> tMap = new TreeMap(sMap);
267        String[] keys = sKeys;
268        for (int i = ITERATIONS - 1; i >= 0; i--) {
269            tMap.remove(keys[i]);
270            tMap.remove(keys[i]);
271            tMap.remove(keys[i]);
272            tMap.remove(keys[i]);
273            tMap.remove(keys[i]);
274            tMap.remove(keys[i]);
275            tMap.remove(keys[i]);
276            tMap.remove(keys[i]);
277            tMap.remove(keys[i]);
278            tMap.remove(keys[i]);
279        }
280    }
281}
282