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 junit.framework.TestCase; 36 37 import java.util.Collections; 38 import java.util.ConcurrentModificationException; 39 import java.util.Iterator; 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 = 49 newImmutableIntArrayList(1); 50 private static final IntArrayList TERTIARY_LIST = 51 newImmutableIntArrayList(1, 2, 3); 52 53 private IntArrayList list; 54 55 @Override setUp()56 protected void setUp() throws Exception { 57 list = new IntArrayList(); 58 } 59 testEmptyListReturnsSameInstance()60 public void testEmptyListReturnsSameInstance() { 61 assertSame(IntArrayList.emptyList(), IntArrayList.emptyList()); 62 } 63 testEmptyListIsImmutable()64 public void testEmptyListIsImmutable() { 65 assertImmutable(IntArrayList.emptyList()); 66 } 67 testMakeImmutable()68 public void testMakeImmutable() { 69 list.addInt(3); 70 list.addInt(4); 71 list.addInt(5); 72 list.addInt(7); 73 list.makeImmutable(); 74 assertImmutable(list); 75 } 76 testModificationWithIteration()77 public void testModificationWithIteration() { 78 list.addAll(asList(1, 2, 3, 4)); 79 Iterator<Integer> iterator = list.iterator(); 80 assertEquals(4, list.size()); 81 assertEquals(1, (int) list.get(0)); 82 assertEquals(1, (int) iterator.next()); 83 list.set(0, 1); 84 assertEquals(2, (int) iterator.next()); 85 86 list.remove(0); 87 try { 88 iterator.next(); 89 fail(); 90 } catch (ConcurrentModificationException e) { 91 // expected 92 } 93 94 iterator = list.iterator(); 95 list.add(0, 0); 96 try { 97 iterator.next(); 98 fail(); 99 } catch (ConcurrentModificationException e) { 100 // expected 101 } 102 } 103 testGet()104 public void testGet() { 105 assertEquals(1, (int) TERTIARY_LIST.get(0)); 106 assertEquals(2, (int) TERTIARY_LIST.get(1)); 107 assertEquals(3, (int) TERTIARY_LIST.get(2)); 108 109 try { 110 TERTIARY_LIST.get(-1); 111 fail(); 112 } catch (IndexOutOfBoundsException e) { 113 // expected 114 } 115 116 try { 117 TERTIARY_LIST.get(3); 118 fail(); 119 } catch (IndexOutOfBoundsException e) { 120 // expected 121 } 122 } 123 testGetInt()124 public void testGetInt() { 125 assertEquals(1, TERTIARY_LIST.getInt(0)); 126 assertEquals(2, TERTIARY_LIST.getInt(1)); 127 assertEquals(3, TERTIARY_LIST.getInt(2)); 128 129 try { 130 TERTIARY_LIST.get(-1); 131 fail(); 132 } catch (IndexOutOfBoundsException e) { 133 // expected 134 } 135 136 try { 137 TERTIARY_LIST.get(3); 138 fail(); 139 } catch (IndexOutOfBoundsException e) { 140 // expected 141 } 142 } 143 testSize()144 public void testSize() { 145 assertEquals(0, IntArrayList.emptyList().size()); 146 assertEquals(1, UNARY_LIST.size()); 147 assertEquals(3, TERTIARY_LIST.size()); 148 149 list.addInt(3); 150 list.addInt(4); 151 list.addInt(6); 152 list.addInt(8); 153 assertEquals(4, list.size()); 154 155 list.remove(0); 156 assertEquals(3, list.size()); 157 158 list.add(17); 159 assertEquals(4, list.size()); 160 } 161 testSet()162 public void testSet() { 163 list.addInt(2); 164 list.addInt(4); 165 166 assertEquals(2, (int) list.set(0, 3)); 167 assertEquals(3, list.getInt(0)); 168 169 assertEquals(4, (int) list.set(1, 0)); 170 assertEquals(0, list.getInt(1)); 171 172 try { 173 list.set(-1, 0); 174 fail(); 175 } catch (IndexOutOfBoundsException e) { 176 // expected 177 } 178 179 try { 180 list.set(2, 0); 181 fail(); 182 } catch (IndexOutOfBoundsException e) { 183 // expected 184 } 185 } 186 testSetInt()187 public void testSetInt() { 188 list.addInt(1); 189 list.addInt(3); 190 191 assertEquals(1, list.setInt(0, 0)); 192 assertEquals(0, list.getInt(0)); 193 194 assertEquals(3, list.setInt(1, 0)); 195 assertEquals(0, list.getInt(1)); 196 197 try { 198 list.setInt(-1, 0); 199 fail(); 200 } catch (IndexOutOfBoundsException e) { 201 // expected 202 } 203 204 try { 205 list.setInt(2, 0); 206 fail(); 207 } catch (IndexOutOfBoundsException e) { 208 // expected 209 } 210 } 211 testAdd()212 public void testAdd() { 213 assertEquals(0, list.size()); 214 215 assertTrue(list.add(2)); 216 assertEquals(asList(2), list); 217 218 assertTrue(list.add(3)); 219 list.add(0, 4); 220 assertEquals(asList(4, 2, 3), list); 221 222 list.add(0, 1); 223 list.add(0, 0); 224 // Force a resize by getting up to 11 elements. 225 for (int i = 0; i < 6; i++) { 226 list.add(Integer.valueOf(5 + i)); 227 } 228 assertEquals( 229 asList(0, 1, 4, 2, 3, 5, 6, 7, 8, 9, 10), 230 list); 231 232 try { 233 list.add(-1, 5); 234 } catch (IndexOutOfBoundsException e) { 235 // expected 236 } 237 238 try { 239 list.add(4, 5); 240 } catch (IndexOutOfBoundsException e) { 241 // expected 242 } 243 } 244 testAddInt()245 public void testAddInt() { 246 assertEquals(0, list.size()); 247 248 list.addInt(2); 249 assertEquals(asList(2), list); 250 251 list.addInt(3); 252 assertEquals(asList(2, 3), list); 253 } 254 testAddAll()255 public void testAddAll() { 256 assertEquals(0, list.size()); 257 258 assertTrue(list.addAll(Collections.singleton(1))); 259 assertEquals(1, list.size()); 260 assertEquals(1, (int) list.get(0)); 261 assertEquals(1, list.getInt(0)); 262 263 assertTrue(list.addAll(asList(2, 3, 4, 5, 6))); 264 assertEquals(asList(1, 2, 3, 4, 5, 6), list); 265 266 assertTrue(list.addAll(TERTIARY_LIST)); 267 assertEquals(asList(1, 2, 3, 4, 5, 6, 1, 2, 3), list); 268 269 assertFalse(list.addAll(Collections.<Integer>emptyList())); 270 assertFalse(list.addAll(IntArrayList.emptyList())); 271 } 272 testRemove()273 public void testRemove() { 274 list.addAll(TERTIARY_LIST); 275 assertEquals(1, (int) list.remove(0)); 276 assertEquals(asList(2, 3), list); 277 278 assertTrue(list.remove(Integer.valueOf(3))); 279 assertEquals(asList(2), list); 280 281 assertFalse(list.remove(Integer.valueOf(3))); 282 assertEquals(asList(2), list); 283 284 assertEquals(2, (int) list.remove(0)); 285 assertEquals(asList(), list); 286 287 try { 288 list.remove(-1); 289 fail(); 290 } catch (IndexOutOfBoundsException e) { 291 // expected 292 } 293 294 try { 295 list.remove(0); 296 } catch (IndexOutOfBoundsException e) { 297 // expected 298 } 299 } 300 assertImmutable(IntArrayList list)301 private void assertImmutable(IntArrayList list) { 302 if (list.contains(1)) { 303 throw new RuntimeException("Cannot test the immutability of lists that contain 1."); 304 } 305 306 try { 307 list.add(1); 308 fail(); 309 } catch (UnsupportedOperationException e) { 310 // expected 311 } 312 313 try { 314 list.add(0, 1); 315 fail(); 316 } catch (UnsupportedOperationException e) { 317 // expected 318 } 319 320 try { 321 list.addAll(Collections.<Integer>emptyList()); 322 fail(); 323 } catch (UnsupportedOperationException e) { 324 // expected 325 } 326 327 try { 328 list.addAll(Collections.singletonList(1)); 329 fail(); 330 } catch (UnsupportedOperationException e) { 331 // expected 332 } 333 334 try { 335 list.addAll(new IntArrayList()); 336 fail(); 337 } catch (UnsupportedOperationException e) { 338 // expected 339 } 340 341 try { 342 list.addAll(UNARY_LIST); 343 fail(); 344 } catch (UnsupportedOperationException e) { 345 // expected 346 } 347 348 try { 349 list.addAll(0, Collections.singleton(1)); 350 fail(); 351 } catch (UnsupportedOperationException e) { 352 // expected 353 } 354 355 try { 356 list.addAll(0, UNARY_LIST); 357 fail(); 358 } catch (UnsupportedOperationException e) { 359 // expected 360 } 361 362 try { 363 list.addAll(0, Collections.<Integer>emptyList()); 364 fail(); 365 } catch (UnsupportedOperationException e) { 366 // expected 367 } 368 369 try { 370 list.addInt(0); 371 fail(); 372 } catch (UnsupportedOperationException e) { 373 // expected 374 } 375 376 try { 377 list.clear(); 378 fail(); 379 } catch (UnsupportedOperationException e) { 380 // expected 381 } 382 383 try { 384 list.remove(1); 385 fail(); 386 } catch (UnsupportedOperationException e) { 387 // expected 388 } 389 390 try { 391 list.remove(new Object()); 392 fail(); 393 } catch (UnsupportedOperationException e) { 394 // expected 395 } 396 397 try { 398 list.removeAll(Collections.<Integer>emptyList()); 399 fail(); 400 } catch (UnsupportedOperationException e) { 401 // expected 402 } 403 404 try { 405 list.removeAll(Collections.singleton(1)); 406 fail(); 407 } catch (UnsupportedOperationException e) { 408 // expected 409 } 410 411 try { 412 list.removeAll(UNARY_LIST); 413 fail(); 414 } catch (UnsupportedOperationException e) { 415 // expected 416 } 417 418 try { 419 list.retainAll(Collections.<Integer>emptyList()); 420 fail(); 421 } catch (UnsupportedOperationException e) { 422 // expected 423 } 424 425 try { 426 list.retainAll(Collections.singleton(1)); 427 fail(); 428 } catch (UnsupportedOperationException e) { 429 // expected 430 } 431 432 try { 433 list.retainAll(UNARY_LIST); 434 fail(); 435 } catch (UnsupportedOperationException e) { 436 // expected 437 } 438 439 try { 440 list.set(0, 0); 441 fail(); 442 } catch (UnsupportedOperationException e) { 443 // expected 444 } 445 446 try { 447 list.setInt(0, 0); 448 fail(); 449 } catch (UnsupportedOperationException e) { 450 // expected 451 } 452 } 453 newImmutableIntArrayList(int... elements)454 private static IntArrayList newImmutableIntArrayList(int... elements) { 455 IntArrayList list = new IntArrayList(); 456 for (int element : elements) { 457 list.addInt(element); 458 } 459 list.makeImmutable(); 460 return list; 461 } 462 } 463