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 DoubleArrayList}. 43 * 44 * @author dweis@google.com (Daniel Weis) 45 */ 46 public class DoubleArrayListTest extends TestCase { 47 48 private static final DoubleArrayList UNARY_LIST = 49 newImmutableDoubleArrayList(1); 50 private static final DoubleArrayList TERTIARY_LIST = 51 newImmutableDoubleArrayList(1, 2, 3); 52 53 private DoubleArrayList list; 54 55 @Override setUp()56 protected void setUp() throws Exception { 57 list = new DoubleArrayList(); 58 } 59 testEmptyListReturnsSameInstance()60 public void testEmptyListReturnsSameInstance() { 61 assertSame(DoubleArrayList.emptyList(), DoubleArrayList.emptyList()); 62 } 63 testEmptyListIsImmutable()64 public void testEmptyListIsImmutable() { 65 assertImmutable(DoubleArrayList.emptyList()); 66 } 67 testMakeImmutable()68 public void testMakeImmutable() { 69 list.addDouble(3); 70 list.addDouble(4); 71 list.addDouble(5); 72 list.addDouble(7); 73 list.makeImmutable(); 74 assertImmutable(list); 75 } 76 testModificationWithIteration()77 public void testModificationWithIteration() { 78 list.addAll(asList(1D, 2D, 3D, 4D)); 79 Iterator<Double> iterator = list.iterator(); 80 assertEquals(4, list.size()); 81 assertEquals(1D, (double) list.get(0)); 82 assertEquals(1D, (double) iterator.next()); 83 list.set(0, 1D); 84 assertEquals(2D, (double) 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, 0D); 96 try { 97 iterator.next(); 98 fail(); 99 } catch (ConcurrentModificationException e) { 100 // expected 101 } 102 } 103 testGet()104 public void testGet() { 105 assertEquals(1D, (double) TERTIARY_LIST.get(0)); 106 assertEquals(2D, (double) TERTIARY_LIST.get(1)); 107 assertEquals(3D, (double) 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 testGetDouble()124 public void testGetDouble() { 125 assertEquals(1D, TERTIARY_LIST.getDouble(0)); 126 assertEquals(2D, TERTIARY_LIST.getDouble(1)); 127 assertEquals(3D, TERTIARY_LIST.getDouble(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, DoubleArrayList.emptyList().size()); 146 assertEquals(1, UNARY_LIST.size()); 147 assertEquals(3, TERTIARY_LIST.size()); 148 149 list.addDouble(3); 150 list.addDouble(4); 151 list.addDouble(6); 152 list.addDouble(8); 153 assertEquals(4, list.size()); 154 155 list.remove(0); 156 assertEquals(3, list.size()); 157 158 list.add(17D); 159 assertEquals(4, list.size()); 160 } 161 testSet()162 public void testSet() { 163 list.addDouble(2); 164 list.addDouble(4); 165 166 assertEquals(2D, (double) list.set(0, 3D)); 167 assertEquals(3D, list.getDouble(0)); 168 169 assertEquals(4D, (double) list.set(1, 0D)); 170 assertEquals(0D, list.getDouble(1)); 171 172 try { 173 list.set(-1, 0D); 174 fail(); 175 } catch (IndexOutOfBoundsException e) { 176 // expected 177 } 178 179 try { 180 list.set(2, 0D); 181 fail(); 182 } catch (IndexOutOfBoundsException e) { 183 // expected 184 } 185 } 186 testSetDouble()187 public void testSetDouble() { 188 list.addDouble(1); 189 list.addDouble(3); 190 191 assertEquals(1D, list.setDouble(0, 0)); 192 assertEquals(0D, list.getDouble(0)); 193 194 assertEquals(3D, list.setDouble(1, 0)); 195 assertEquals(0D, list.getDouble(1)); 196 197 try { 198 list.setDouble(-1, 0); 199 fail(); 200 } catch (IndexOutOfBoundsException e) { 201 // expected 202 } 203 204 try { 205 list.setDouble(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(2D)); 216 assertEquals(asList(2D), list); 217 218 assertTrue(list.add(3D)); 219 list.add(0, 4D); 220 assertEquals(asList(4D, 2D, 3D), list); 221 222 list.add(0, 1D); 223 list.add(0, 0D); 224 // Force a resize by getting up to 11 elements. 225 for (int i = 0; i < 6; i++) { 226 list.add(Double.valueOf(5 + i)); 227 } 228 assertEquals( 229 asList(0D, 1D, 4D, 2D, 3D, 5D, 6D, 7D, 8D, 9D, 10D), 230 list); 231 232 try { 233 list.add(-1, 5D); 234 } catch (IndexOutOfBoundsException e) { 235 // expected 236 } 237 238 try { 239 list.add(4, 5D); 240 } catch (IndexOutOfBoundsException e) { 241 // expected 242 } 243 } 244 testAddDouble()245 public void testAddDouble() { 246 assertEquals(0, list.size()); 247 248 list.addDouble(2); 249 assertEquals(asList(2D), list); 250 251 list.addDouble(3); 252 assertEquals(asList(2D, 3D), list); 253 } 254 testAddAll()255 public void testAddAll() { 256 assertEquals(0, list.size()); 257 258 assertTrue(list.addAll(Collections.singleton(1D))); 259 assertEquals(1, list.size()); 260 assertEquals(1D, (double) list.get(0)); 261 assertEquals(1D, list.getDouble(0)); 262 263 assertTrue(list.addAll(asList(2D, 3D, 4D, 5D, 6D))); 264 assertEquals(asList(1D, 2D, 3D, 4D, 5D, 6D), list); 265 266 assertTrue(list.addAll(TERTIARY_LIST)); 267 assertEquals(asList(1D, 2D, 3D, 4D, 5D, 6D, 1D, 2D, 3D), list); 268 269 assertFalse(list.addAll(Collections.<Double>emptyList())); 270 assertFalse(list.addAll(DoubleArrayList.emptyList())); 271 } 272 testRemove()273 public void testRemove() { 274 list.addAll(TERTIARY_LIST); 275 assertEquals(1D, (double) list.remove(0)); 276 assertEquals(asList(2D, 3D), list); 277 278 assertTrue(list.remove(Double.valueOf(3))); 279 assertEquals(asList(2D), list); 280 281 assertFalse(list.remove(Double.valueOf(3))); 282 assertEquals(asList(2D), list); 283 284 assertEquals(2D, (double) 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(DoubleArrayList list)301 private void assertImmutable(DoubleArrayList list) { 302 if (list.contains(1D)) { 303 throw new RuntimeException("Cannot test the immutability of lists that contain 1."); 304 } 305 306 try { 307 list.add(1D); 308 fail(); 309 } catch (UnsupportedOperationException e) { 310 // expected 311 } 312 313 try { 314 list.add(0, 1D); 315 fail(); 316 } catch (UnsupportedOperationException e) { 317 // expected 318 } 319 320 try { 321 list.addAll(Collections.<Double>emptyList()); 322 fail(); 323 } catch (UnsupportedOperationException e) { 324 // expected 325 } 326 327 try { 328 list.addAll(Collections.singletonList(1D)); 329 fail(); 330 } catch (UnsupportedOperationException e) { 331 // expected 332 } 333 334 try { 335 list.addAll(new DoubleArrayList()); 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(1D)); 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.<Double>emptyList()); 364 fail(); 365 } catch (UnsupportedOperationException e) { 366 // expected 367 } 368 369 try { 370 list.addDouble(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.<Double>emptyList()); 399 fail(); 400 } catch (UnsupportedOperationException e) { 401 // expected 402 } 403 404 try { 405 list.removeAll(Collections.singleton(1D)); 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.<Double>emptyList()); 420 fail(); 421 } catch (UnsupportedOperationException e) { 422 // expected 423 } 424 425 try { 426 list.retainAll(Collections.singleton(1D)); 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, 0D); 441 fail(); 442 } catch (UnsupportedOperationException e) { 443 // expected 444 } 445 446 try { 447 list.setDouble(0, 0); 448 fail(); 449 } catch (UnsupportedOperationException e) { 450 // expected 451 } 452 } 453 newImmutableDoubleArrayList(double... elements)454 private static DoubleArrayList newImmutableDoubleArrayList(double... elements) { 455 DoubleArrayList list = new DoubleArrayList(); 456 for (double element : elements) { 457 list.addDouble(element); 458 } 459 list.makeImmutable(); 460 return list; 461 } 462 } 463