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 17 package android.core; 18 19 import android.test.PerformanceTestBase; 20 import android.test.PerformanceTestCase; 21 import java.util.LinkedList; 22 import java.util.ListIterator; 23 24 /** 25 * This class contains performance tests for methods in java.util.LinkedList 26 * 27 */ 28 @SuppressWarnings("unchecked") 29 public class LinkedListTest extends PerformanceTestBase { 30 public static final int ITERATIONS = 1000; 31 LinkedList<Integer> mLinkedList; 32 33 @Override 34 protected void setUp() throws Exception { 35 super.setUp(); 36 mLinkedList = new LinkedList(); 37 for (int i = ITERATIONS - 1; i >= 0; i--) { 38 mLinkedList.add(i); 39 } 40 } 41 42 @Override 43 public int startPerformance(PerformanceTestCase.Intermediates intermediates) { 44 intermediates.setInternalIterations(ITERATIONS); 45 return 0; 46 } 47 48 public void testLinkedListAdd() { 49 LinkedList<Integer> list = new LinkedList(); 50 for (int i = ITERATIONS - 1; i >= 0; i--) { 51 list.add(i); 52 list.add(i); 53 list.add(i); 54 list.add(i); 55 list.add(i); 56 list.add(i); 57 list.add(i); 58 list.add(i); 59 list.add(i); 60 list.add(i); 61 } 62 } 63 64 public void testLinkedListAdd1() { 65 LinkedList<Integer> list = new LinkedList(); 66 for (int i = ITERATIONS - 1; i >= 0; i--) { 67 list.add(0, i); 68 list.add(0, i); 69 list.add(0, i); 70 list.add(0, i); 71 list.add(0, i); 72 list.add(0, i); 73 list.add(0, i); 74 list.add(0, i); 75 list.add(0, i); 76 list.add(0, i); 77 } 78 } 79 80 public void testLinkedListToArray() { 81 Object array; 82 LinkedList<Integer> list = mLinkedList; 83 for (int i = ITERATIONS - 1; i >= 0; i--) { 84 array = list.toArray(); 85 array = list.toArray(); 86 array = list.toArray(); 87 array = list.toArray(); 88 array = list.toArray(); 89 array = list.toArray(); 90 array = list.toArray(); 91 array = list.toArray(); 92 array = list.toArray(); 93 array = list.toArray(); 94 } 95 } 96 97 public void testLinkedListSize() { 98 LinkedList<Integer> list = mLinkedList; 99 int len; 100 for (int i = ITERATIONS - 1; i >= 0; i--) { 101 len = list.size(); 102 len = list.size(); 103 len = list.size(); 104 len = list.size(); 105 len = list.size(); 106 len = list.size(); 107 len = list.size(); 108 len = list.size(); 109 len = list.size(); 110 len = list.size(); 111 } 112 } 113 114 public void testLinkedListGet() { 115 int element; 116 LinkedList<Integer> list = mLinkedList; 117 for (int i = ITERATIONS - 1; i >= 0; i--) { 118 element = list.get(i); 119 element = list.get(i); 120 element = list.get(i); 121 element = list.get(i); 122 element = list.get(i); 123 element = list.get(i); 124 element = list.get(i); 125 element = list.get(i); 126 element = list.get(i); 127 element = list.get(i); 128 } 129 } 130 131 public void testLinkedListContains() { 132 boolean flag; 133 LinkedList<Integer> list = mLinkedList; 134 for (int i = ITERATIONS - 1; i >= 0; i--) { 135 flag = list.contains(i); 136 flag = list.contains(i); 137 flag = list.contains(i); 138 flag = list.contains(i); 139 flag = list.contains(i); 140 flag = list.contains(i); 141 flag = list.contains(i); 142 flag = list.contains(i); 143 flag = list.contains(i); 144 flag = list.contains(i); 145 } 146 } 147 148 public void testLinkedListToArray1() { 149 Integer[] rArray = new Integer[100]; 150 Integer[] array; 151 LinkedList<Integer> list = mLinkedList; 152 for (int i = ITERATIONS - 1; i >= 0; i--) { 153 array = list.toArray(rArray); 154 array = list.toArray(rArray); 155 array = list.toArray(rArray); 156 array = list.toArray(rArray); 157 array = list.toArray(rArray); 158 array = list.toArray(rArray); 159 array = list.toArray(rArray); 160 array = list.toArray(rArray); 161 array = list.toArray(rArray); 162 array = list.toArray(rArray); 163 } 164 } 165 166 public void testLinkedListSet() { 167 LinkedList<Integer> list = mLinkedList; 168 int value1 = 500, value2 = 0; 169 for (int i = ITERATIONS - 1; i >= 0; i--) { 170 list.set(value1, value2); 171 list.set(value1, value2); 172 list.set(value1, value2); 173 list.set(value1, value2); 174 list.set(value1, value2); 175 list.set(value1, value2); 176 list.set(value1, value2); 177 list.set(value1, value2); 178 list.set(value1, value2); 179 list.set(value1, value2); 180 } 181 } 182 183 public void testLinkedListIndexOf() { 184 int index; 185 LinkedList<Integer> list = mLinkedList; 186 for (int i = ITERATIONS - 1; i >= 0; i--) { 187 index = list.indexOf(0); 188 index = list.indexOf(0); 189 index = list.indexOf(0); 190 index = list.indexOf(0); 191 index = list.indexOf(0); 192 index = list.indexOf(0); 193 index = list.indexOf(0); 194 index = list.indexOf(0); 195 index = list.indexOf(0); 196 index = list.indexOf(0); 197 198 } 199 } 200 201 public void testLinkedListLastIndexOf() { 202 int index; 203 LinkedList<Integer> list = mLinkedList; 204 for (int i = ITERATIONS - 1; i >= 0; i--) { 205 index = list.lastIndexOf(0); 206 index = list.lastIndexOf(0); 207 index = list.lastIndexOf(0); 208 index = list.lastIndexOf(0); 209 index = list.lastIndexOf(0); 210 index = list.lastIndexOf(0); 211 index = list.lastIndexOf(0); 212 index = list.lastIndexOf(0); 213 index = list.lastIndexOf(0); 214 index = list.lastIndexOf(0); 215 } 216 } 217 218 public void testLinkedListRemove() { 219 int index; 220 LinkedList<Integer> list = new LinkedList(mLinkedList); 221 for (int i = 10; i > 0; i--) { 222 index = list.remove(); 223 index = list.remove(); 224 index = list.remove(); 225 index = list.remove(); 226 index = list.remove(); 227 index = list.remove(); 228 index = list.remove(); 229 index = list.remove(); 230 index = list.remove(); 231 index = list.remove(); 232 } 233 } 234 235 public void testLinkedListRemove1() { 236 int index; 237 LinkedList<Integer> list = new LinkedList(mLinkedList); 238 for (int i = 10; i > 0; i--) { 239 index = list.remove(0); 240 index = list.remove(0); 241 index = list.remove(0); 242 index = list.remove(0); 243 index = list.remove(0); 244 index = list.remove(0); 245 index = list.remove(0); 246 index = list.remove(0); 247 index = list.remove(0); 248 index = list.remove(0); 249 } 250 } 251 252 public void testLinkedListRemoveFirst() { 253 int index; 254 LinkedList<Integer> list = new LinkedList(mLinkedList); 255 for (int i = 10; i > 0; i--) { 256 index = list.removeFirst(); 257 index = list.removeFirst(); 258 index = list.removeFirst(); 259 index = list.removeFirst(); 260 index = list.removeFirst(); 261 index = list.removeFirst(); 262 index = list.removeFirst(); 263 index = list.removeFirst(); 264 index = list.removeFirst(); 265 index = list.removeFirst(); 266 } 267 } 268 269 public void testLinkedListRemoveLast() { 270 int index; 271 LinkedList<Integer> list = new LinkedList(mLinkedList); 272 for (int i = 10; i > 0; i--) { 273 index = list.removeLast(); 274 index = list.removeLast(); 275 index = list.removeLast(); 276 index = list.removeLast(); 277 index = list.removeLast(); 278 index = list.removeLast(); 279 index = list.removeLast(); 280 index = list.removeLast(); 281 index = list.removeLast(); 282 index = list.removeLast(); 283 } 284 } 285 286 public void testLinkedListAddAll() { 287 LinkedList<Integer> mList = mLinkedList; 288 boolean flag; 289 LinkedList<Integer> list = new LinkedList(); 290 for (int i = 10; i > 0; i--) { 291 flag = list.addAll(mList); 292 flag = list.addAll(mList); 293 flag = list.addAll(mList); 294 flag = list.addAll(mList); 295 flag = list.addAll(mList); 296 flag = list.addAll(mList); 297 flag = list.addAll(mList); 298 flag = list.addAll(mList); 299 flag = list.addAll(mList); 300 flag = list.addAll(mList); 301 } 302 } 303 304 public void testLinkedListRemove2() { 305 LinkedList<String> list; 306 String s = new String("a"); 307 list = new LinkedList(); 308 for (int j = 1000; j > 0; j--) { 309 list.add("a"); 310 list.add("b"); 311 } 312 boolean flag; 313 for (int i = 10; i > 0; i--) { 314 flag = list.remove(s); 315 flag = list.remove(s); 316 flag = list.remove(s); 317 flag = list.remove(s); 318 flag = list.remove(s); 319 flag = list.remove(s); 320 flag = list.remove(s); 321 flag = list.remove(s); 322 flag = list.remove(s); 323 flag = list.remove(s); 324 } 325 } 326 327 public void testLinkedListAddAll1() { 328 LinkedList<Integer> mList = new LinkedList(); 329 int pos = 0; 330 boolean flag; 331 LinkedList<Integer> list = mLinkedList; 332 for (int i = 0; i < 10; i++) { 333 flag = mList.addAll(pos, list); 334 flag = mList.addAll(pos, list); 335 flag = mList.addAll(pos, list); 336 flag = mList.addAll(pos, list); 337 flag = mList.addAll(pos, list); 338 flag = mList.addAll(pos, list); 339 flag = mList.addAll(pos, list); 340 flag = mList.addAll(pos, list); 341 flag = mList.addAll(pos, list); 342 flag = mList.addAll(pos, list); 343 } 344 } 345 346 public void testLinkedListClone() { 347 Object rObj; 348 LinkedList<Integer> list = mLinkedList; 349 for (int i = 100; i > 0; i--) { 350 rObj = list.clone(); 351 rObj = list.clone(); 352 rObj = list.clone(); 353 rObj = list.clone(); 354 rObj = list.clone(); 355 rObj = list.clone(); 356 rObj = list.clone(); 357 rObj = list.clone(); 358 rObj = list.clone(); 359 rObj = list.clone(); 360 } 361 } 362 363 public void testLinkedListHashcode() { 364 int element; 365 LinkedList<Integer> list = mLinkedList; 366 for (int i = ITERATIONS - 1; i >= 0; i--) { 367 element = list.hashCode(); 368 element = list.hashCode(); 369 element = list.hashCode(); 370 element = list.hashCode(); 371 element = list.hashCode(); 372 element = list.hashCode(); 373 element = list.hashCode(); 374 element = list.hashCode(); 375 element = list.hashCode(); 376 element = list.hashCode(); 377 } 378 } 379 380 public void testLinkedListElement() { 381 int element; 382 LinkedList<Integer> list = mLinkedList; 383 for (int i = ITERATIONS - 1; i >= 0; i--) { 384 element = list.element(); 385 element = list.element(); 386 element = list.element(); 387 element = list.element(); 388 element = list.element(); 389 element = list.element(); 390 element = list.element(); 391 element = list.element(); 392 element = list.element(); 393 element = list.element(); 394 } 395 } 396 397 public void testLinkedListToString() { 398 String str; 399 LinkedList<Integer> list = mLinkedList; 400 for (int i = ITERATIONS - 1; i >= 0; i--) { 401 str = list.toString(); 402 str = list.toString(); 403 str = list.toString(); 404 str = list.toString(); 405 str = list.toString(); 406 str = list.toString(); 407 str = list.toString(); 408 str = list.toString(); 409 str = list.toString(); 410 str = list.toString(); 411 } 412 } 413 414 public void testLinkedListIsEmpty() { 415 boolean flag; 416 LinkedList<Integer> list = mLinkedList; 417 for (int i = ITERATIONS - 1; i >= 0; i--) { 418 flag = list.isEmpty(); 419 flag = list.isEmpty(); 420 flag = list.isEmpty(); 421 flag = list.isEmpty(); 422 flag = list.isEmpty(); 423 flag = list.isEmpty(); 424 flag = list.isEmpty(); 425 flag = list.isEmpty(); 426 flag = list.isEmpty(); 427 flag = list.isEmpty(); 428 } 429 } 430 431 public void testLinkedListOffer() { 432 LinkedList<Integer> list = new LinkedList(); 433 for (int i = ITERATIONS - 1; i >= 0; i--) { 434 list.offer(i); 435 list.offer(i); 436 list.offer(i); 437 list.offer(i); 438 list.offer(i); 439 list.offer(i); 440 list.offer(i); 441 list.offer(i); 442 list.offer(i); 443 list.offer(i); 444 } 445 } 446 447 public void testLinkedListPeek() { 448 int element; 449 LinkedList<Integer> list = mLinkedList; 450 for (int i = ITERATIONS - 1; i >= 0; i--) { 451 element = list.peek(); 452 element = list.peek(); 453 element = list.peek(); 454 element = list.peek(); 455 element = list.peek(); 456 element = list.peek(); 457 element = list.peek(); 458 element = list.peek(); 459 element = list.peek(); 460 element = list.peek(); 461 } 462 } 463 464 public void testLinkedListPoll() { 465 int element; 466 LinkedList<Integer> list = new LinkedList(mLinkedList); 467 for (int i = 10; i > 0; i--) { 468 element = list.poll(); 469 element = list.poll(); 470 element = list.poll(); 471 element = list.poll(); 472 element = list.poll(); 473 element = list.poll(); 474 element = list.poll(); 475 element = list.poll(); 476 element = list.poll(); 477 element = list.poll(); 478 } 479 } 480 481 public void testLinkedListAddLast() { 482 LinkedList<Integer> list = new LinkedList(); 483 for (int i = ITERATIONS - 1; i >= 0; i--) { 484 list.addLast(i); 485 list.addLast(i); 486 list.addLast(i); 487 list.addLast(i); 488 list.addLast(i); 489 list.addLast(i); 490 list.addLast(i); 491 list.addLast(i); 492 list.addLast(i); 493 list.addLast(i); 494 } 495 } 496 497 public void testLinkedListAddFirst() { 498 LinkedList<Integer> list = new LinkedList(); 499 for (int i = ITERATIONS - 1; i >= 0; i--) { 500 list.addFirst(i); 501 list.addFirst(i); 502 list.addFirst(i); 503 list.addFirst(i); 504 list.addFirst(i); 505 list.addFirst(i); 506 list.addFirst(i); 507 list.addFirst(i); 508 list.addFirst(i); 509 list.addFirst(i); 510 } 511 } 512 513 public void testLinkedListIterator() { 514 ListIterator iterator; 515 LinkedList<Integer> list = mLinkedList; 516 for (int i = ITERATIONS - 1; i >= 0; i--) { 517 iterator = list.listIterator(); 518 iterator = list.listIterator(); 519 iterator = list.listIterator(); 520 iterator = list.listIterator(); 521 iterator = list.listIterator(); 522 iterator = list.listIterator(); 523 iterator = list.listIterator(); 524 iterator = list.listIterator(); 525 iterator = list.listIterator(); 526 iterator = list.listIterator(); 527 } 528 } 529 } 530