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 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, true, false); 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, false, true)); 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(true, (boolean) TERTIARY_LIST.get(1)); 106 assertEquals(false, (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 testGetInt()123 public void testGetInt() { 124 assertEquals(true, TERTIARY_LIST.getBoolean(0)); 125 assertEquals(true, TERTIARY_LIST.getBoolean(1)); 126 assertEquals(false, 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 testSize()143 public void testSize() { 144 assertEquals(0, BooleanArrayList.emptyList().size()); 145 assertEquals(1, UNARY_LIST.size()); 146 assertEquals(3, TERTIARY_LIST.size()); 147 148 list.addBoolean(true); 149 list.addBoolean(false); 150 list.addBoolean(false); 151 list.addBoolean(false); 152 assertEquals(4, list.size()); 153 154 list.remove(0); 155 assertEquals(3, list.size()); 156 157 list.add(true); 158 assertEquals(4, list.size()); 159 } 160 testSet()161 public void testSet() { 162 list.addBoolean(false); 163 list.addBoolean(false); 164 165 assertEquals(false, (boolean) list.set(0, true)); 166 assertEquals(true, list.getBoolean(0)); 167 168 assertEquals(false, (boolean) list.set(1, false)); 169 assertEquals(false, list.getBoolean(1)); 170 171 try { 172 list.set(-1, true); 173 fail(); 174 } catch (IndexOutOfBoundsException e) { 175 // expected 176 } 177 178 try { 179 list.set(2, false); 180 fail(); 181 } catch (IndexOutOfBoundsException e) { 182 // expected 183 } 184 } 185 testSetInt()186 public void testSetInt() { 187 list.addBoolean(true); 188 list.addBoolean(true); 189 190 assertEquals(true, list.setBoolean(0, false)); 191 assertEquals(false, list.getBoolean(0)); 192 193 assertEquals(true, list.setBoolean(1, false)); 194 assertEquals(false, list.getBoolean(1)); 195 196 try { 197 list.setBoolean(-1, false); 198 fail(); 199 } catch (IndexOutOfBoundsException e) { 200 // expected 201 } 202 203 try { 204 list.setBoolean(2, true); 205 fail(); 206 } catch (IndexOutOfBoundsException e) { 207 // expected 208 } 209 } 210 testAdd()211 public void testAdd() { 212 assertEquals(0, list.size()); 213 214 assertTrue(list.add(true)); 215 assertEquals(asList(true), list); 216 217 assertTrue(list.add(false)); 218 list.add(0, false); 219 assertEquals(asList(false, true, false), list); 220 221 list.add(0, false); 222 list.add(0, true); 223 // Force a resize by getting up to 11 elements. 224 for (int i = 0; i < 6; i++) { 225 list.add(true); 226 } 227 assertEquals(asList(true, false, false, true, false, true, true, true, true, true, true), list); 228 229 try { 230 list.add(-1, false); 231 } catch (IndexOutOfBoundsException e) { 232 // expected 233 } 234 235 try { 236 list.add(4, true); 237 } catch (IndexOutOfBoundsException e) { 238 // expected 239 } 240 } 241 testAddInt()242 public void testAddInt() { 243 assertEquals(0, list.size()); 244 245 list.addBoolean(true); 246 assertEquals(asList(true), list); 247 248 list.addBoolean(false); 249 assertEquals(asList(true, false), list); 250 } 251 testAddAll()252 public void testAddAll() { 253 assertEquals(0, list.size()); 254 255 assertTrue(list.addAll(Collections.singleton(false))); 256 assertEquals(1, list.size()); 257 assertEquals(false, (boolean) list.get(0)); 258 assertEquals(false, list.getBoolean(0)); 259 260 assertTrue(list.addAll(asList(true, false, false, false, true))); 261 assertEquals(asList(false, true, false, false, false, true), list); 262 263 assertTrue(list.addAll(TERTIARY_LIST)); 264 assertEquals(asList(false, true, false, false, false, true, true, true, false), list); 265 266 assertFalse(list.addAll(Collections.<Boolean>emptyList())); 267 assertFalse(list.addAll(BooleanArrayList.emptyList())); 268 } 269 testRemove()270 public void testRemove() { 271 list.addAll(TERTIARY_LIST); 272 assertEquals(true, (boolean) list.remove(0)); 273 assertEquals(asList(true, false), list); 274 275 assertTrue(list.remove(Boolean.TRUE)); 276 assertEquals(asList(false), list); 277 278 assertFalse(list.remove(Boolean.TRUE)); 279 assertEquals(asList(false), list); 280 281 assertEquals(false, (boolean) list.remove(0)); 282 assertEquals(asList(), list); 283 284 try { 285 list.remove(-1); 286 fail(); 287 } catch (IndexOutOfBoundsException e) { 288 // expected 289 } 290 291 try { 292 list.remove(0); 293 } catch (IndexOutOfBoundsException e) { 294 // expected 295 } 296 } 297 assertImmutable(BooleanArrayList list)298 private void assertImmutable(BooleanArrayList list) { 299 try { 300 list.add(false); 301 fail(); 302 } catch (UnsupportedOperationException e) { 303 // expected 304 } 305 306 try { 307 list.add(0, true); 308 fail(); 309 } catch (UnsupportedOperationException e) { 310 // expected 311 } 312 313 try { 314 list.addAll(Collections.<Boolean>emptyList()); 315 fail(); 316 } catch (UnsupportedOperationException e) { 317 // expected 318 } 319 320 try { 321 list.addAll(Collections.singletonList(false)); 322 fail(); 323 } catch (UnsupportedOperationException e) { 324 // expected 325 } 326 327 try { 328 list.addAll(new BooleanArrayList()); 329 fail(); 330 } catch (UnsupportedOperationException e) { 331 // expected 332 } 333 334 try { 335 list.addAll(UNARY_LIST); 336 fail(); 337 } catch (UnsupportedOperationException e) { 338 // expected 339 } 340 341 try { 342 list.addAll(0, Collections.singleton(true)); 343 fail(); 344 } catch (UnsupportedOperationException e) { 345 // expected 346 } 347 348 try { 349 list.addAll(0, UNARY_LIST); 350 fail(); 351 } catch (UnsupportedOperationException e) { 352 // expected 353 } 354 355 try { 356 list.addAll(0, Collections.<Boolean>emptyList()); 357 fail(); 358 } catch (UnsupportedOperationException e) { 359 // expected 360 } 361 362 try { 363 list.addBoolean(true); 364 fail(); 365 } catch (UnsupportedOperationException e) { 366 // expected 367 } 368 369 try { 370 list.clear(); 371 fail(); 372 } catch (UnsupportedOperationException e) { 373 // expected 374 } 375 376 try { 377 list.remove(1); 378 fail(); 379 } catch (UnsupportedOperationException e) { 380 // expected 381 } 382 383 try { 384 list.remove(new Object()); 385 fail(); 386 } catch (UnsupportedOperationException e) { 387 // expected 388 } 389 390 try { 391 list.removeAll(Collections.<Boolean>emptyList()); 392 fail(); 393 } catch (UnsupportedOperationException e) { 394 // expected 395 } 396 397 try { 398 list.removeAll(Collections.singleton(Boolean.TRUE)); 399 fail(); 400 } catch (UnsupportedOperationException e) { 401 // expected 402 } 403 404 try { 405 list.removeAll(UNARY_LIST); 406 fail(); 407 } catch (UnsupportedOperationException e) { 408 // expected 409 } 410 411 try { 412 list.retainAll(Collections.<Boolean>emptyList()); 413 fail(); 414 } catch (UnsupportedOperationException e) { 415 // expected 416 } 417 418 try { 419 list.retainAll(Collections.singleton(Boolean.TRUE)); 420 fail(); 421 } catch (UnsupportedOperationException e) { 422 // expected 423 } 424 425 try { 426 list.retainAll(UNARY_LIST); 427 fail(); 428 } catch (UnsupportedOperationException e) { 429 // expected 430 } 431 432 try { 433 list.set(0, true); 434 fail(); 435 } catch (UnsupportedOperationException e) { 436 // expected 437 } 438 439 try { 440 list.setBoolean(0, false); 441 fail(); 442 } catch (UnsupportedOperationException e) { 443 // expected 444 } 445 } 446 newImmutableBooleanArrayList(boolean... elements)447 private static BooleanArrayList newImmutableBooleanArrayList(boolean... elements) { 448 BooleanArrayList list = new BooleanArrayList(); 449 for (boolean element : elements) { 450 list.addBoolean(element); 451 } 452 list.makeImmutable(); 453 return list; 454 } 455 } 456