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.BooleanList; 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 BooleanArrayList}. 43 * 44 * @author dweis@google.com (Daniel Weis) 45 */ 46 public class BooleanArrayListTest extends TestCase { 47 48 private static final BooleanArrayList UNARY_LIST = newImmutableBooleanArrayList(true); 49 private static final BooleanArrayList TERTIARY_LIST = 50 newImmutableBooleanArrayList(true, false, true); 51 52 private BooleanArrayList list; 53 54 @Override setUp()55 protected void setUp() throws Exception { 56 list = new BooleanArrayList(); 57 } 58 testEmptyListReturnsSameInstance()59 public void testEmptyListReturnsSameInstance() { 60 assertSame(BooleanArrayList.emptyList(), BooleanArrayList.emptyList()); 61 } 62 testEmptyListIsImmutable()63 public void testEmptyListIsImmutable() { 64 assertImmutable(BooleanArrayList.emptyList()); 65 } 66 testMakeImmutable()67 public void testMakeImmutable() { 68 list.addBoolean(true); 69 list.addBoolean(false); 70 list.addBoolean(true); 71 list.addBoolean(true); 72 list.makeImmutable(); 73 assertImmutable(list); 74 } 75 testModificationWithIteration()76 public void testModificationWithIteration() { 77 list.addAll(asList(true, false, true, false)); 78 Iterator<Boolean> iterator = list.iterator(); 79 assertEquals(4, list.size()); 80 assertEquals(true, (boolean) list.get(0)); 81 assertEquals(true, (boolean) iterator.next()); 82 list.set(0, true); 83 assertEquals(false, (boolean) iterator.next()); 84 85 list.remove(0); 86 try { 87 iterator.next(); 88 fail(); 89 } catch (ConcurrentModificationException e) { 90 // expected 91 } 92 93 iterator = list.iterator(); 94 list.add(0, false); 95 try { 96 iterator.next(); 97 fail(); 98 } catch (ConcurrentModificationException e) { 99 // expected 100 } 101 } 102 testGet()103 public void testGet() { 104 assertEquals(true, (boolean) TERTIARY_LIST.get(0)); 105 assertEquals(false, (boolean) TERTIARY_LIST.get(1)); 106 assertEquals(true, (boolean) TERTIARY_LIST.get(2)); 107 108 try { 109 TERTIARY_LIST.get(-1); 110 fail(); 111 } catch (IndexOutOfBoundsException e) { 112 // expected 113 } 114 115 try { 116 TERTIARY_LIST.get(3); 117 fail(); 118 } catch (IndexOutOfBoundsException e) { 119 // expected 120 } 121 } 122 testGetBoolean()123 public void testGetBoolean() { 124 assertEquals(true, TERTIARY_LIST.getBoolean(0)); 125 assertEquals(false, TERTIARY_LIST.getBoolean(1)); 126 assertEquals(true, TERTIARY_LIST.getBoolean(2)); 127 128 try { 129 TERTIARY_LIST.get(-1); 130 fail(); 131 } catch (IndexOutOfBoundsException e) { 132 // expected 133 } 134 135 try { 136 TERTIARY_LIST.get(3); 137 fail(); 138 } catch (IndexOutOfBoundsException e) { 139 // expected 140 } 141 } 142 testIndexOf_nullElement()143 public void testIndexOf_nullElement() { 144 assertEquals(-1, TERTIARY_LIST.indexOf(null)); 145 } 146 testIndexOf_incompatibleElementType()147 public void testIndexOf_incompatibleElementType() { 148 assertEquals(-1, TERTIARY_LIST.indexOf(new Object())); 149 } 150 testIndexOf_notInList()151 public void testIndexOf_notInList() { 152 assertEquals(-1, UNARY_LIST.indexOf(false)); 153 } 154 testIndexOf_notInListWithDuplicates()155 public void testIndexOf_notInListWithDuplicates() { 156 BooleanArrayList listWithDupes = newImmutableBooleanArrayList(true, true); 157 assertEquals(-1, listWithDupes.indexOf(false)); 158 } 159 testIndexOf_inList()160 public void testIndexOf_inList() { 161 assertEquals(1, TERTIARY_LIST.indexOf(false)); 162 } 163 testIndexOf_inListWithDuplicates_matchAtHead()164 public void testIndexOf_inListWithDuplicates_matchAtHead() { 165 BooleanArrayList listWithDupes = newImmutableBooleanArrayList(true, true, false); 166 assertEquals(0, listWithDupes.indexOf(true)); 167 } 168 testIndexOf_inListWithDuplicates_matchMidList()169 public void testIndexOf_inListWithDuplicates_matchMidList() { 170 BooleanArrayList listWithDupes = newImmutableBooleanArrayList(false, true, true, false); 171 assertEquals(1, listWithDupes.indexOf(true)); 172 } 173 testContains_nullElement()174 public void testContains_nullElement() { 175 assertEquals(false, TERTIARY_LIST.contains(null)); 176 } 177 testContains_incompatibleElementType()178 public void testContains_incompatibleElementType() { 179 assertEquals(false, TERTIARY_LIST.contains(new Object())); 180 } 181 testContains_notInList()182 public void testContains_notInList() { 183 assertEquals(false, UNARY_LIST.contains(false)); 184 } 185 testContains_notInListWithDuplicates()186 public void testContains_notInListWithDuplicates() { 187 BooleanArrayList listWithDupes = newImmutableBooleanArrayList(true, true); 188 assertEquals(false, listWithDupes.contains(false)); 189 } 190 testContains_inList()191 public void testContains_inList() { 192 assertEquals(true, TERTIARY_LIST.contains(false)); 193 } 194 testContains_inListWithDuplicates_matchAtHead()195 public void testContains_inListWithDuplicates_matchAtHead() { 196 BooleanArrayList listWithDupes = newImmutableBooleanArrayList(true, true, false); 197 assertEquals(true, listWithDupes.contains(true)); 198 } 199 testContains_inListWithDuplicates_matchMidList()200 public void testContains_inListWithDuplicates_matchMidList() { 201 BooleanArrayList listWithDupes = newImmutableBooleanArrayList(false, true, true, false); 202 assertEquals(true, listWithDupes.contains(true)); 203 } 204 testSize()205 public void testSize() { 206 assertEquals(0, BooleanArrayList.emptyList().size()); 207 assertEquals(1, UNARY_LIST.size()); 208 assertEquals(3, TERTIARY_LIST.size()); 209 210 list.addBoolean(true); 211 list.addBoolean(false); 212 list.addBoolean(false); 213 list.addBoolean(false); 214 assertEquals(4, list.size()); 215 216 list.remove(0); 217 assertEquals(3, list.size()); 218 219 list.add(true); 220 assertEquals(4, list.size()); 221 } 222 testSet()223 public void testSet() { 224 list.addBoolean(false); 225 list.addBoolean(false); 226 227 assertEquals(false, (boolean) list.set(0, true)); 228 assertEquals(true, list.getBoolean(0)); 229 230 assertEquals(false, (boolean) list.set(1, false)); 231 assertEquals(false, list.getBoolean(1)); 232 233 try { 234 list.set(-1, false); 235 fail(); 236 } catch (IndexOutOfBoundsException e) { 237 // expected 238 } 239 240 try { 241 list.set(2, false); 242 fail(); 243 } catch (IndexOutOfBoundsException e) { 244 // expected 245 } 246 } 247 testSetBoolean()248 public void testSetBoolean() { 249 list.addBoolean(true); 250 list.addBoolean(true); 251 252 assertEquals(true, list.setBoolean(0, false)); 253 assertEquals(false, list.getBoolean(0)); 254 255 assertEquals(true, list.setBoolean(1, false)); 256 assertEquals(false, list.getBoolean(1)); 257 258 try { 259 list.setBoolean(-1, false); 260 fail(); 261 } catch (IndexOutOfBoundsException e) { 262 // expected 263 } 264 265 try { 266 list.setBoolean(2, false); 267 fail(); 268 } catch (IndexOutOfBoundsException e) { 269 // expected 270 } 271 } 272 testAdd()273 public void testAdd() { 274 assertEquals(0, list.size()); 275 276 assertTrue(list.add(false)); 277 assertEquals(asList(false), list); 278 279 assertTrue(list.add(true)); 280 list.add(0, false); 281 assertEquals(asList(false, false, true), list); 282 283 list.add(0, true); 284 list.add(0, false); 285 // Force a resize by getting up to 11 elements. 286 for (int i = 0; i < 6; i++) { 287 list.add(i % 2 == 0); 288 } 289 assertEquals( 290 asList(false, true, false, false, true, true, false, true, false, true, false), list); 291 292 try { 293 list.add(-1, true); 294 } catch (IndexOutOfBoundsException e) { 295 // expected 296 } 297 298 try { 299 list.add(4, true); 300 } catch (IndexOutOfBoundsException e) { 301 // expected 302 } 303 } 304 testAddBoolean()305 public void testAddBoolean() { 306 assertEquals(0, list.size()); 307 308 list.addBoolean(false); 309 assertEquals(asList(false), list); 310 311 list.addBoolean(true); 312 assertEquals(asList(false, true), list); 313 } 314 testAddAll()315 public void testAddAll() { 316 assertEquals(0, list.size()); 317 318 assertTrue(list.addAll(Collections.singleton(true))); 319 assertEquals(1, list.size()); 320 assertEquals(true, (boolean) list.get(0)); 321 assertEquals(true, list.getBoolean(0)); 322 323 assertTrue(list.addAll(asList(false, true, false, true, false))); 324 assertEquals(asList(true, false, true, false, true, false), list); 325 326 assertTrue(list.addAll(TERTIARY_LIST)); 327 assertEquals(asList(true, false, true, false, true, false, true, false, true), list); 328 329 assertFalse(list.addAll(Collections.<Boolean>emptyList())); 330 assertFalse(list.addAll(BooleanArrayList.emptyList())); 331 } 332 testEquals()333 public void testEquals() { 334 BooleanArrayList list1 = new BooleanArrayList(); 335 BooleanArrayList list2 = new BooleanArrayList(); 336 337 assertEquals(list1, list2); 338 } 339 testRemove()340 public void testRemove() { 341 list.addAll(TERTIARY_LIST); 342 assertEquals(true, (boolean) list.remove(0)); 343 assertEquals(asList(false, true), list); 344 345 assertTrue(list.remove(Boolean.TRUE)); 346 assertEquals(asList(false), list); 347 348 assertFalse(list.remove(Boolean.TRUE)); 349 assertEquals(asList(false), list); 350 351 assertEquals(false, (boolean) list.remove(0)); 352 assertEquals(asList(), list); 353 354 try { 355 list.remove(-1); 356 fail(); 357 } catch (IndexOutOfBoundsException e) { 358 // expected 359 } 360 361 try { 362 list.remove(0); 363 } catch (IndexOutOfBoundsException e) { 364 // expected 365 } 366 } 367 testRemoveEnd_listAtCapacity()368 public void testRemoveEnd_listAtCapacity() { 369 BooleanList toRemove = BooleanArrayList.emptyList().mutableCopyWithCapacity(1); 370 toRemove.addBoolean(true); 371 toRemove.remove(0); 372 assertEquals(0, toRemove.size()); 373 } 374 testRemove_listAtCapacity()375 public void testRemove_listAtCapacity() { 376 BooleanList toRemove = BooleanArrayList.emptyList().mutableCopyWithCapacity(2); 377 toRemove.addBoolean(true); 378 toRemove.addBoolean(false); 379 toRemove.remove(0); 380 assertEquals(1, toRemove.size()); 381 assertEquals(false, (boolean) toRemove.get(0)); 382 } 383 testSublistRemoveEndOfCapacity()384 public void testSublistRemoveEndOfCapacity() { 385 BooleanList toRemove = BooleanArrayList.emptyList().mutableCopyWithCapacity(1); 386 toRemove.addBoolean(true); 387 toRemove.subList(0, 1).clear(); 388 assertEquals(0, toRemove.size()); 389 } 390 assertImmutable(BooleanList list)391 private void assertImmutable(BooleanList list) { 392 393 try { 394 list.add(true); 395 fail(); 396 } catch (UnsupportedOperationException e) { 397 // expected 398 } 399 400 try { 401 list.add(0, true); 402 fail(); 403 } catch (UnsupportedOperationException e) { 404 // expected 405 } 406 407 try { 408 list.addAll(Collections.<Boolean>emptyList()); 409 fail(); 410 } catch (UnsupportedOperationException e) { 411 // expected 412 } 413 414 try { 415 list.addAll(Collections.singletonList(true)); 416 fail(); 417 } catch (UnsupportedOperationException e) { 418 // expected 419 } 420 421 try { 422 list.addAll(new BooleanArrayList()); 423 fail(); 424 } catch (UnsupportedOperationException e) { 425 // expected 426 } 427 428 try { 429 list.addAll(UNARY_LIST); 430 fail(); 431 } catch (UnsupportedOperationException e) { 432 // expected 433 } 434 435 try { 436 list.addAll(0, Collections.singleton(true)); 437 fail(); 438 } catch (UnsupportedOperationException e) { 439 // expected 440 } 441 442 try { 443 list.addAll(0, UNARY_LIST); 444 fail(); 445 } catch (UnsupportedOperationException e) { 446 // expected 447 } 448 449 try { 450 list.addAll(0, Collections.<Boolean>emptyList()); 451 fail(); 452 } catch (UnsupportedOperationException e) { 453 // expected 454 } 455 456 try { 457 list.addBoolean(false); 458 fail(); 459 } catch (UnsupportedOperationException e) { 460 // expected 461 } 462 463 try { 464 list.clear(); 465 fail(); 466 } catch (UnsupportedOperationException e) { 467 // expected 468 } 469 470 try { 471 list.remove(1); 472 fail(); 473 } catch (UnsupportedOperationException e) { 474 // expected 475 } 476 477 try { 478 list.remove(new Object()); 479 fail(); 480 } catch (UnsupportedOperationException e) { 481 // expected 482 } 483 484 try { 485 list.removeAll(Collections.<Boolean>emptyList()); 486 fail(); 487 } catch (UnsupportedOperationException e) { 488 // expected 489 } 490 491 try { 492 list.removeAll(Collections.singleton(Boolean.TRUE)); 493 fail(); 494 } catch (UnsupportedOperationException e) { 495 // expected 496 } 497 498 try { 499 list.removeAll(UNARY_LIST); 500 fail(); 501 } catch (UnsupportedOperationException e) { 502 // expected 503 } 504 505 try { 506 list.retainAll(Collections.<Boolean>emptyList()); 507 fail(); 508 } catch (UnsupportedOperationException e) { 509 // expected 510 } 511 512 try { 513 list.removeAll(Collections.singleton(Boolean.TRUE)); 514 fail(); 515 } catch (UnsupportedOperationException e) { 516 // expected 517 } 518 519 try { 520 list.retainAll(UNARY_LIST); 521 fail(); 522 } catch (UnsupportedOperationException e) { 523 // expected 524 } 525 526 try { 527 list.set(0, false); 528 fail(); 529 } catch (UnsupportedOperationException e) { 530 // expected 531 } 532 533 try { 534 list.setBoolean(0, false); 535 fail(); 536 } catch (UnsupportedOperationException e) { 537 // expected 538 } 539 } 540 newImmutableBooleanArrayList(boolean... elements)541 private static BooleanArrayList newImmutableBooleanArrayList(boolean... elements) { 542 BooleanArrayList list = new BooleanArrayList(); 543 for (boolean element : elements) { 544 list.addBoolean(element); 545 } 546 list.makeImmutable(); 547 return list; 548 } 549 } 550