1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // https://developers.google.com/protocol-buffers/ 4 // 5 // Redistribution and use in source and binary forms, with or without 6 // modification, are permitted provided that the following conditions are 7 // met: 8 // 9 // * Redistributions of source code must retain the above copyright 10 // notice, this list of conditions and the following disclaimer. 11 // * Redistributions in binary form must reproduce the above 12 // copyright notice, this list of conditions and the following disclaimer 13 // in the documentation and/or other materials provided with the 14 // distribution. 15 // * Neither the name of Google Inc. nor the names of its 16 // contributors may be used to endorse or promote products derived from 17 // this software without specific prior written permission. 18 // 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31 package com.google.protobuf; 32 33 import static java.util.Arrays.asList; 34 35 import com.google.protobuf.Internal.IntList; 36 import java.util.Collections; 37 import java.util.ConcurrentModificationException; 38 import java.util.Iterator; 39 import junit.framework.TestCase; 40 41 /** 42 * Tests for {@link IntArrayList}. 43 * 44 * @author dweis@google.com (Daniel Weis) 45 */ 46 public class IntArrayListTest extends TestCase { 47 48 private static final IntArrayList UNARY_LIST = newImmutableIntArrayList(1); 49 private static final IntArrayList TERTIARY_LIST = newImmutableIntArrayList(1, 2, 3); 50 51 private IntArrayList list; 52 53 @Override setUp()54 protected void setUp() throws Exception { 55 list = new IntArrayList(); 56 } 57 testEmptyListReturnsSameInstance()58 public void testEmptyListReturnsSameInstance() { 59 assertSame(IntArrayList.emptyList(), IntArrayList.emptyList()); 60 } 61 testEmptyListIsImmutable()62 public void testEmptyListIsImmutable() { 63 assertImmutable(IntArrayList.emptyList()); 64 } 65 testMakeImmutable()66 public void testMakeImmutable() { 67 list.addInt(3); 68 list.addInt(4); 69 list.addInt(5); 70 list.addInt(7); 71 list.makeImmutable(); 72 assertImmutable(list); 73 } 74 testModificationWithIteration()75 public void testModificationWithIteration() { 76 list.addAll(asList(1, 2, 3, 4)); 77 Iterator<Integer> iterator = list.iterator(); 78 assertEquals(4, list.size()); 79 assertEquals(1, (int) list.get(0)); 80 assertEquals(1, (int) iterator.next()); 81 list.set(0, 1); 82 assertEquals(2, (int) iterator.next()); 83 84 list.remove(0); 85 try { 86 iterator.next(); 87 fail(); 88 } catch (ConcurrentModificationException e) { 89 // expected 90 } 91 92 iterator = list.iterator(); 93 list.add(0, 0); 94 try { 95 iterator.next(); 96 fail(); 97 } catch (ConcurrentModificationException e) { 98 // expected 99 } 100 } 101 testGet()102 public void testGet() { 103 assertEquals(1, (int) TERTIARY_LIST.get(0)); 104 assertEquals(2, (int) TERTIARY_LIST.get(1)); 105 assertEquals(3, (int) TERTIARY_LIST.get(2)); 106 107 try { 108 TERTIARY_LIST.get(-1); 109 fail(); 110 } catch (IndexOutOfBoundsException e) { 111 // expected 112 } 113 114 try { 115 TERTIARY_LIST.get(3); 116 fail(); 117 } catch (IndexOutOfBoundsException e) { 118 // expected 119 } 120 } 121 testGetInt()122 public void testGetInt() { 123 assertEquals(1, TERTIARY_LIST.getInt(0)); 124 assertEquals(2, TERTIARY_LIST.getInt(1)); 125 assertEquals(3, TERTIARY_LIST.getInt(2)); 126 127 try { 128 TERTIARY_LIST.get(-1); 129 fail(); 130 } catch (IndexOutOfBoundsException e) { 131 // expected 132 } 133 134 try { 135 TERTIARY_LIST.get(3); 136 fail(); 137 } catch (IndexOutOfBoundsException e) { 138 // expected 139 } 140 } 141 testIndexOf_nullElement()142 public void testIndexOf_nullElement() { 143 assertEquals(-1, TERTIARY_LIST.indexOf(null)); 144 } 145 testIndexOf_incompatibleElementType()146 public void testIndexOf_incompatibleElementType() { 147 assertEquals(-1, TERTIARY_LIST.indexOf(new Object())); 148 } 149 testIndexOf_notInList()150 public void testIndexOf_notInList() { 151 assertEquals(-1, UNARY_LIST.indexOf(2)); 152 } 153 testIndexOf_notInListWithDuplicates()154 public void testIndexOf_notInListWithDuplicates() { 155 IntArrayList listWithDupes = newImmutableIntArrayList(1, 1); 156 assertEquals(-1, listWithDupes.indexOf(2)); 157 } 158 testIndexOf_inList()159 public void testIndexOf_inList() { 160 assertEquals(1, TERTIARY_LIST.indexOf(2)); 161 } 162 testIndexOf_inListWithDuplicates_matchAtHead()163 public void testIndexOf_inListWithDuplicates_matchAtHead() { 164 IntArrayList listWithDupes = newImmutableIntArrayList(1, 1, 2); 165 assertEquals(0, listWithDupes.indexOf(1)); 166 } 167 testIndexOf_inListWithDuplicates_matchMidList()168 public void testIndexOf_inListWithDuplicates_matchMidList() { 169 IntArrayList listWithDupes = newImmutableIntArrayList(2, 1, 1, 2); 170 assertEquals(1, listWithDupes.indexOf(1)); 171 } 172 testContains_nullElement()173 public void testContains_nullElement() { 174 assertEquals(false, TERTIARY_LIST.contains(null)); 175 } 176 testContains_incompatibleElementType()177 public void testContains_incompatibleElementType() { 178 assertEquals(false, TERTIARY_LIST.contains(new Object())); 179 } 180 testContains_notInList()181 public void testContains_notInList() { 182 assertEquals(false, UNARY_LIST.contains(2)); 183 } 184 testContains_notInListWithDuplicates()185 public void testContains_notInListWithDuplicates() { 186 IntArrayList listWithDupes = newImmutableIntArrayList(1, 1); 187 assertEquals(false, listWithDupes.contains(2)); 188 } 189 testContains_inList()190 public void testContains_inList() { 191 assertEquals(true, TERTIARY_LIST.contains(2)); 192 } 193 testContains_inListWithDuplicates_matchAtHead()194 public void testContains_inListWithDuplicates_matchAtHead() { 195 IntArrayList listWithDupes = newImmutableIntArrayList(1, 1, 2); 196 assertEquals(true, listWithDupes.contains(1)); 197 } 198 testContains_inListWithDuplicates_matchMidList()199 public void testContains_inListWithDuplicates_matchMidList() { 200 IntArrayList listWithDupes = newImmutableIntArrayList(2, 1, 1, 2); 201 assertEquals(true, listWithDupes.contains(1)); 202 } 203 testSize()204 public void testSize() { 205 assertEquals(0, IntArrayList.emptyList().size()); 206 assertEquals(1, UNARY_LIST.size()); 207 assertEquals(3, TERTIARY_LIST.size()); 208 209 list.addInt(3); 210 list.addInt(4); 211 list.addInt(6); 212 list.addInt(8); 213 assertEquals(4, list.size()); 214 215 list.remove(0); 216 assertEquals(3, list.size()); 217 218 list.add(17); 219 assertEquals(4, list.size()); 220 } 221 testSet()222 public void testSet() { 223 list.addInt(2); 224 list.addInt(4); 225 226 assertEquals(2, (int) list.set(0, 3)); 227 assertEquals(3, list.getInt(0)); 228 229 assertEquals(4, (int) list.set(1, 0)); 230 assertEquals(0, list.getInt(1)); 231 232 try { 233 list.set(-1, 0); 234 fail(); 235 } catch (IndexOutOfBoundsException e) { 236 // expected 237 } 238 239 try { 240 list.set(2, 0); 241 fail(); 242 } catch (IndexOutOfBoundsException e) { 243 // expected 244 } 245 } 246 testSetInt()247 public void testSetInt() { 248 list.addInt(1); 249 list.addInt(3); 250 251 assertEquals(1, list.setInt(0, 0)); 252 assertEquals(0, list.getInt(0)); 253 254 assertEquals(3, list.setInt(1, 0)); 255 assertEquals(0, list.getInt(1)); 256 257 try { 258 list.setInt(-1, 0); 259 fail(); 260 } catch (IndexOutOfBoundsException e) { 261 // expected 262 } 263 264 try { 265 list.setInt(2, 0); 266 fail(); 267 } catch (IndexOutOfBoundsException e) { 268 // expected 269 } 270 } 271 testAdd()272 public void testAdd() { 273 assertEquals(0, list.size()); 274 275 assertTrue(list.add(2)); 276 assertEquals(asList(2), list); 277 278 assertTrue(list.add(3)); 279 list.add(0, 4); 280 assertEquals(asList(4, 2, 3), list); 281 282 list.add(0, 1); 283 list.add(0, 0); 284 // Force a resize by getting up to 11 elements. 285 for (int i = 0; i < 6; i++) { 286 list.add(Integer.valueOf(5 + i)); 287 } 288 assertEquals(asList(0, 1, 4, 2, 3, 5, 6, 7, 8, 9, 10), list); 289 290 try { 291 list.add(-1, 5); 292 } catch (IndexOutOfBoundsException e) { 293 // expected 294 } 295 296 try { 297 list.add(4, 5); 298 } catch (IndexOutOfBoundsException e) { 299 // expected 300 } 301 } 302 testAddInt()303 public void testAddInt() { 304 assertEquals(0, list.size()); 305 306 list.addInt(2); 307 assertEquals(asList(2), list); 308 309 list.addInt(3); 310 assertEquals(asList(2, 3), list); 311 } 312 testAddAll()313 public void testAddAll() { 314 assertEquals(0, list.size()); 315 316 assertTrue(list.addAll(Collections.singleton(1))); 317 assertEquals(1, list.size()); 318 assertEquals(1, (int) list.get(0)); 319 assertEquals(1, list.getInt(0)); 320 321 assertTrue(list.addAll(asList(2, 3, 4, 5, 6))); 322 assertEquals(asList(1, 2, 3, 4, 5, 6), list); 323 324 assertTrue(list.addAll(TERTIARY_LIST)); 325 assertEquals(asList(1, 2, 3, 4, 5, 6, 1, 2, 3), list); 326 327 assertFalse(list.addAll(Collections.<Integer>emptyList())); 328 assertFalse(list.addAll(IntArrayList.emptyList())); 329 } 330 testEquals()331 public void testEquals() { 332 IntArrayList list1 = new IntArrayList(); 333 IntArrayList list2 = new IntArrayList(); 334 335 assertEquals(list1, list2); 336 } 337 testRemove()338 public void testRemove() { 339 list.addAll(TERTIARY_LIST); 340 assertEquals(1, (int) list.remove(0)); 341 assertEquals(asList(2, 3), list); 342 343 assertTrue(list.remove(Integer.valueOf(3))); 344 assertEquals(asList(2), list); 345 346 assertFalse(list.remove(Integer.valueOf(3))); 347 assertEquals(asList(2), list); 348 349 assertEquals(2, (int) list.remove(0)); 350 assertEquals(asList(), list); 351 352 try { 353 list.remove(-1); 354 fail(); 355 } catch (IndexOutOfBoundsException e) { 356 // expected 357 } 358 359 try { 360 list.remove(0); 361 } catch (IndexOutOfBoundsException e) { 362 // expected 363 } 364 } 365 testRemoveEnd_listAtCapacity()366 public void testRemoveEnd_listAtCapacity() { 367 IntList toRemove = IntArrayList.emptyList().mutableCopyWithCapacity(1); 368 toRemove.addInt(3); 369 toRemove.remove(0); 370 assertEquals(0, toRemove.size()); 371 } 372 testRemove_listAtCapacity()373 public void testRemove_listAtCapacity() { 374 IntList toRemove = IntArrayList.emptyList().mutableCopyWithCapacity(2); 375 toRemove.addInt(3); 376 toRemove.addInt(4); 377 toRemove.remove(0); 378 assertEquals(1, toRemove.size()); 379 assertEquals(4, (int) toRemove.get(0)); 380 } 381 testSublistRemoveEndOfCapacity()382 public void testSublistRemoveEndOfCapacity() { 383 IntList toRemove = IntArrayList.emptyList().mutableCopyWithCapacity(1); 384 toRemove.addInt(3); 385 toRemove.subList(0, 1).clear(); 386 assertEquals(0, toRemove.size()); 387 } 388 assertImmutable(IntList list)389 private void assertImmutable(IntList list) { 390 if (list.contains(1)) { 391 throw new RuntimeException("Cannot test the immutability of lists that contain 1."); 392 } 393 394 try { 395 list.add(1); 396 fail(); 397 } catch (UnsupportedOperationException e) { 398 // expected 399 } 400 401 try { 402 list.add(0, 1); 403 fail(); 404 } catch (UnsupportedOperationException e) { 405 // expected 406 } 407 408 try { 409 list.addAll(Collections.<Integer>emptyList()); 410 fail(); 411 } catch (UnsupportedOperationException e) { 412 // expected 413 } 414 415 try { 416 list.addAll(Collections.singletonList(1)); 417 fail(); 418 } catch (UnsupportedOperationException e) { 419 // expected 420 } 421 422 try { 423 list.addAll(new IntArrayList()); 424 fail(); 425 } catch (UnsupportedOperationException e) { 426 // expected 427 } 428 429 try { 430 list.addAll(UNARY_LIST); 431 fail(); 432 } catch (UnsupportedOperationException e) { 433 // expected 434 } 435 436 try { 437 list.addAll(0, Collections.singleton(1)); 438 fail(); 439 } catch (UnsupportedOperationException e) { 440 // expected 441 } 442 443 try { 444 list.addAll(0, UNARY_LIST); 445 fail(); 446 } catch (UnsupportedOperationException e) { 447 // expected 448 } 449 450 try { 451 list.addAll(0, Collections.<Integer>emptyList()); 452 fail(); 453 } catch (UnsupportedOperationException e) { 454 // expected 455 } 456 457 try { 458 list.addInt(0); 459 fail(); 460 } catch (UnsupportedOperationException e) { 461 // expected 462 } 463 464 try { 465 list.clear(); 466 fail(); 467 } catch (UnsupportedOperationException e) { 468 // expected 469 } 470 471 try { 472 list.remove(1); 473 fail(); 474 } catch (UnsupportedOperationException e) { 475 // expected 476 } 477 478 try { 479 list.remove(new Object()); 480 fail(); 481 } catch (UnsupportedOperationException e) { 482 // expected 483 } 484 485 try { 486 list.removeAll(Collections.<Integer>emptyList()); 487 fail(); 488 } catch (UnsupportedOperationException e) { 489 // expected 490 } 491 492 try { 493 list.removeAll(Collections.singleton(1)); 494 fail(); 495 } catch (UnsupportedOperationException e) { 496 // expected 497 } 498 499 try { 500 list.removeAll(UNARY_LIST); 501 fail(); 502 } catch (UnsupportedOperationException e) { 503 // expected 504 } 505 506 try { 507 list.retainAll(Collections.<Integer>emptyList()); 508 fail(); 509 } catch (UnsupportedOperationException e) { 510 // expected 511 } 512 513 try { 514 list.retainAll(Collections.singleton(1)); 515 fail(); 516 } catch (UnsupportedOperationException e) { 517 // expected 518 } 519 520 try { 521 list.retainAll(UNARY_LIST); 522 fail(); 523 } catch (UnsupportedOperationException e) { 524 // expected 525 } 526 527 try { 528 list.set(0, 0); 529 fail(); 530 } catch (UnsupportedOperationException e) { 531 // expected 532 } 533 534 try { 535 list.setInt(0, 0); 536 fail(); 537 } catch (UnsupportedOperationException e) { 538 // expected 539 } 540 } 541 newImmutableIntArrayList(int... elements)542 private static IntArrayList newImmutableIntArrayList(int... elements) { 543 IntArrayList list = new IntArrayList(); 544 for (int element : elements) { 545 list.addInt(element); 546 } 547 list.makeImmutable(); 548 return list; 549 } 550 } 551