1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 */ 22 23 /* 24 * This file is available under and governed by the GNU General Public 25 * License version 2 only, as published by the Free Software Foundation. 26 * However, the following notice accompanied the original version of this 27 * file: 28 * 29 * Written by Doug Lea and Martin Buchholz with assistance from 30 * members of JCP JSR-166 Expert Group and released to the public 31 * domain, as explained at 32 * http://creativecommons.org/publicdomain/zero/1.0/ 33 */ 34 35 import java.util.ArrayList; 36 import java.util.Arrays; 37 import java.util.Collection; 38 import java.util.Collections; 39 import java.util.List; 40 import java.util.Vector; 41 import java.util.concurrent.ThreadLocalRandom; 42 43 import junit.framework.Test; 44 45 public class VectorTest extends JSR166TestCase { 46 public static void main(String[] args) { 47 main(suite(), args); 48 } 49 50 public static Test suite() { 51 class Implementation implements CollectionImplementation { 52 public Class<?> klazz() { return Vector.class; } 53 public List emptyCollection() { return new Vector(); } 54 public Object makeElement(int i) { return i; } 55 public boolean isConcurrent() { return false; } 56 public boolean permitsNulls() { return true; } 57 } 58 class SubListImplementation extends Implementation { 59 public List emptyCollection() { 60 List list = super.emptyCollection(); 61 ThreadLocalRandom rnd = ThreadLocalRandom.current(); 62 if (rnd.nextBoolean()) 63 list.add(makeElement(rnd.nextInt())); 64 int i = rnd.nextInt(list.size() + 1); 65 return list.subList(i, i); 66 } 67 } 68 return newTestSuite( 69 VectorTest.class, 70 CollectionTest.testSuite(new Implementation()), 71 CollectionTest.testSuite(new SubListImplementation())); 72 } 73 74 static Vector<Integer> populatedList(int n) { 75 Vector<Integer> list = new Vector<>(); 76 assertTrue(list.isEmpty()); 77 for (int i = 0; i < n; i++) 78 list.add(i); 79 assertEquals(n <= 0, list.isEmpty()); 80 assertEquals(n, list.size()); 81 return list; 82 } 83 84 /** 85 * addAll adds each element from the given collection, including duplicates 86 */ 87 public void testAddAll() { 88 List list = populatedList(3); 89 assertTrue(list.addAll(Arrays.asList(three, four, five))); 90 assertEquals(6, list.size()); 91 assertTrue(list.addAll(Arrays.asList(three, four, five))); 92 assertEquals(9, list.size()); 93 } 94 95 /** 96 * clear removes all elements from the list 97 */ 98 public void testClear() { 99 List list = populatedList(SIZE); 100 list.clear(); 101 assertEquals(0, list.size()); 102 } 103 104 /** 105 * Cloned list is equal 106 */ 107 public void testClone() { 108 Vector l1 = populatedList(SIZE); 109 Vector l2 = (Vector)(l1.clone()); 110 assertEquals(l1, l2); 111 l1.clear(); 112 assertFalse(l1.equals(l2)); 113 } 114 115 /** 116 * contains is true for added elements 117 */ 118 public void testContains() { 119 List list = populatedList(3); 120 assertTrue(list.contains(one)); 121 assertFalse(list.contains(five)); 122 } 123 124 /** 125 * adding at an index places it in the indicated index 126 */ 127 public void testAddIndex() { 128 List list = populatedList(3); 129 list.add(0, m1); 130 assertEquals(4, list.size()); 131 assertEquals(m1, list.get(0)); 132 assertEquals(zero, list.get(1)); 133 134 list.add(2, m2); 135 assertEquals(5, list.size()); 136 assertEquals(m2, list.get(2)); 137 assertEquals(two, list.get(4)); 138 } 139 140 /** 141 * lists with same elements are equal and have same hashCode 142 */ 143 public void testEquals() { 144 List a = populatedList(3); 145 List b = populatedList(3); 146 assertTrue(a.equals(b)); 147 assertTrue(b.equals(a)); 148 assertTrue(a.containsAll(b)); 149 assertTrue(b.containsAll(a)); 150 assertEquals(a.hashCode(), b.hashCode()); 151 a.add(m1); 152 assertFalse(a.equals(b)); 153 assertFalse(b.equals(a)); 154 assertTrue(a.containsAll(b)); 155 assertFalse(b.containsAll(a)); 156 b.add(m1); 157 assertTrue(a.equals(b)); 158 assertTrue(b.equals(a)); 159 assertTrue(a.containsAll(b)); 160 assertTrue(b.containsAll(a)); 161 assertEquals(a.hashCode(), b.hashCode()); 162 163 assertFalse(a.equals(null)); 164 } 165 166 /** 167 * containsAll returns true for collections with subset of elements 168 */ 169 public void testContainsAll() { 170 List list = populatedList(3); 171 assertTrue(list.containsAll(Arrays.asList())); 172 assertTrue(list.containsAll(Arrays.asList(one))); 173 assertTrue(list.containsAll(Arrays.asList(one, two))); 174 assertFalse(list.containsAll(Arrays.asList(one, two, six))); 175 assertFalse(list.containsAll(Arrays.asList(six))); 176 177 try { 178 list.containsAll(null); 179 shouldThrow(); 180 } catch (NullPointerException success) {} 181 } 182 183 /** 184 * get returns the value at the given index 185 */ 186 public void testGet() { 187 List list = populatedList(3); 188 assertEquals(0, list.get(0)); 189 } 190 191 /** 192 * indexOf(Object) returns the index of the first occurrence of the 193 * specified element in this list, or -1 if this list does not 194 * contain the element 195 */ 196 public void testIndexOf() { 197 List list = populatedList(3); 198 assertEquals(-1, list.indexOf(-42)); 199 int size = list.size(); 200 for (int i = 0; i < size; i++) { 201 assertEquals(i, list.indexOf(i)); 202 assertEquals(i, list.subList(0, size).indexOf(i)); 203 assertEquals(i, list.subList(0, i + 1).indexOf(i)); 204 assertEquals(-1, list.subList(0, i).indexOf(i)); 205 assertEquals(0, list.subList(i, size).indexOf(i)); 206 assertEquals(-1, list.subList(i + 1, size).indexOf(i)); 207 } 208 209 list.add(1); 210 assertEquals(1, list.indexOf(1)); 211 assertEquals(1, list.subList(0, size + 1).indexOf(1)); 212 assertEquals(0, list.subList(1, size + 1).indexOf(1)); 213 assertEquals(size - 2, list.subList(2, size + 1).indexOf(1)); 214 assertEquals(0, list.subList(size, size + 1).indexOf(1)); 215 assertEquals(-1, list.subList(size + 1, size + 1).indexOf(1)); 216 } 217 218 /** 219 * indexOf(E, int) returns the index of the first occurrence of the 220 * specified element in this list, searching forwards from index, 221 * or returns -1 if the element is not found 222 */ 223 public void testIndexOf2() { 224 Vector list = populatedList(3); 225 int size = list.size(); 226 assertEquals(-1, list.indexOf(-42, 0)); 227 228 // we might expect IOOBE, but spec says otherwise 229 assertEquals(-1, list.indexOf(0, size)); 230 assertEquals(-1, list.indexOf(0, Integer.MAX_VALUE)); 231 232 assertThrows( 233 IndexOutOfBoundsException.class, 234 () -> list.indexOf(0, -1), 235 () -> list.indexOf(0, Integer.MIN_VALUE)); 236 237 for (int i = 0; i < size; i++) { 238 assertEquals(i, list.indexOf(i, 0)); 239 assertEquals(i, list.indexOf(i, i)); 240 assertEquals(-1, list.indexOf(i, i + 1)); 241 } 242 243 list.add(1); 244 assertEquals(1, list.indexOf(1, 0)); 245 assertEquals(1, list.indexOf(1, 1)); 246 assertEquals(size, list.indexOf(1, 2)); 247 assertEquals(size, list.indexOf(1, size)); 248 } 249 250 /** 251 * isEmpty returns true when empty, else false 252 */ 253 public void testIsEmpty() { 254 List empty = new Vector(); 255 assertTrue(empty.isEmpty()); 256 assertTrue(empty.subList(0, 0).isEmpty()); 257 258 List full = populatedList(SIZE); 259 assertFalse(full.isEmpty()); 260 assertTrue(full.subList(0, 0).isEmpty()); 261 assertTrue(full.subList(SIZE, SIZE).isEmpty()); 262 } 263 264 /** 265 * iterator of empty collection has no elements 266 */ 267 public void testEmptyIterator() { 268 Collection c = new Vector(); 269 assertIteratorExhausted(c.iterator()); 270 } 271 272 /** 273 * lastIndexOf(Object) returns the index of the last occurrence of 274 * the specified element in this list, or -1 if this list does not 275 * contain the element 276 */ 277 public void testLastIndexOf1() { 278 List list = populatedList(3); 279 assertEquals(-1, list.lastIndexOf(-42)); 280 int size = list.size(); 281 for (int i = 0; i < size; i++) { 282 assertEquals(i, list.lastIndexOf(i)); 283 assertEquals(i, list.subList(0, size).lastIndexOf(i)); 284 assertEquals(i, list.subList(0, i + 1).lastIndexOf(i)); 285 assertEquals(-1, list.subList(0, i).lastIndexOf(i)); 286 assertEquals(0, list.subList(i, size).lastIndexOf(i)); 287 assertEquals(-1, list.subList(i + 1, size).lastIndexOf(i)); 288 } 289 290 list.add(1); 291 assertEquals(size, list.lastIndexOf(1)); 292 assertEquals(size, list.subList(0, size + 1).lastIndexOf(1)); 293 assertEquals(1, list.subList(0, size).lastIndexOf(1)); 294 assertEquals(0, list.subList(1, 2).lastIndexOf(1)); 295 assertEquals(-1, list.subList(0, 1).indexOf(1)); 296 } 297 298 /** 299 * lastIndexOf(E, int) returns the index of the last occurrence of the 300 * specified element in this list, searching backwards from index, or 301 * returns -1 if the element is not found 302 */ 303 public void testLastIndexOf2() { 304 Vector list = populatedList(3); 305 306 // we might expect IOOBE, but spec says otherwise 307 assertEquals(-1, list.lastIndexOf(0, -1)); 308 309 int size = list.size(); 310 assertThrows( 311 IndexOutOfBoundsException.class, 312 () -> list.lastIndexOf(0, size), 313 () -> list.lastIndexOf(0, Integer.MAX_VALUE)); 314 315 for (int i = 0; i < size; i++) { 316 assertEquals(i, list.lastIndexOf(i, i)); 317 assertEquals(list.indexOf(i), list.lastIndexOf(i, i)); 318 if (i > 0) 319 assertEquals(-1, list.lastIndexOf(i, i - 1)); 320 } 321 list.add(one); 322 list.add(three); 323 assertEquals(1, list.lastIndexOf(one, 1)); 324 assertEquals(1, list.lastIndexOf(one, 2)); 325 assertEquals(3, list.lastIndexOf(one, 3)); 326 assertEquals(3, list.lastIndexOf(one, 4)); 327 assertEquals(-1, list.lastIndexOf(three, 3)); 328 } 329 330 /** 331 * size returns the number of elements 332 */ 333 public void testSize() { 334 List empty = new Vector(); 335 assertEquals(0, empty.size()); 336 assertEquals(0, empty.subList(0, 0).size()); 337 338 List full = populatedList(SIZE); 339 assertEquals(SIZE, full.size()); 340 assertEquals(0, full.subList(0, 0).size()); 341 assertEquals(0, full.subList(SIZE, SIZE).size()); 342 } 343 344 /** 345 * sublists contains elements at indexes offset from their base 346 */ 347 public void testSubList() { 348 List a = populatedList(10); 349 assertTrue(a.subList(1,1).isEmpty()); 350 for (int j = 0; j < 9; ++j) { 351 for (int i = j ; i < 10; ++i) { 352 List b = a.subList(j,i); 353 for (int k = j; k < i; ++k) { 354 assertEquals(new Integer(k), b.get(k-j)); 355 } 356 } 357 } 358 359 List s = a.subList(2, 5); 360 assertEquals(3, s.size()); 361 s.set(2, m1); 362 assertEquals(a.get(4), m1); 363 s.clear(); 364 assertEquals(7, a.size()); 365 366 assertThrows( 367 IndexOutOfBoundsException.class, 368 () -> s.get(0), 369 () -> s.set(0, 42)); 370 } 371 372 /** 373 * toArray throws an ArrayStoreException when the given array 374 * can not store the objects inside the list 375 */ 376 public void testToArray_ArrayStoreException() { 377 List list = new Vector(); 378 // Integers are not auto-converted to Longs 379 list.add(86); 380 list.add(99); 381 assertThrows( 382 ArrayStoreException.class, 383 () -> list.toArray(new Long[0]), 384 () -> list.toArray(new Long[5])); 385 } 386 387 void testIndexOutOfBoundsException(List list) { 388 int size = list.size(); 389 assertThrows( 390 IndexOutOfBoundsException.class, 391 () -> list.get(-1), 392 () -> list.get(size), 393 () -> list.set(-1, "qwerty"), 394 () -> list.set(size, "qwerty"), 395 () -> list.add(-1, "qwerty"), 396 () -> list.add(size + 1, "qwerty"), 397 () -> list.remove(-1), 398 () -> list.remove(size), 399 () -> list.addAll(-1, Collections.emptyList()), 400 () -> list.addAll(size + 1, Collections.emptyList()), 401 () -> list.listIterator(-1), 402 () -> list.listIterator(size + 1), 403 () -> list.subList(-1, size), 404 () -> list.subList(0, size + 1)); 405 406 // Conversely, operations that must not throw 407 list.addAll(0, Collections.emptyList()); 408 list.addAll(size, Collections.emptyList()); 409 list.add(0, "qwerty"); 410 list.add(list.size(), "qwerty"); 411 list.get(0); 412 list.get(list.size() - 1); 413 list.set(0, "azerty"); 414 list.set(list.size() - 1, "azerty"); 415 list.listIterator(0); 416 list.listIterator(list.size()); 417 list.subList(0, list.size()); 418 list.remove(list.size() - 1); 419 } 420 421 /** 422 * IndexOutOfBoundsException is thrown when specified 423 */ 424 public void testIndexOutOfBoundsException() { 425 ThreadLocalRandom rnd = ThreadLocalRandom.current(); 426 List x = populatedList(rnd.nextInt(5)); 427 testIndexOutOfBoundsException(x); 428 429 int start = rnd.nextInt(x.size() + 1); 430 int end = rnd.nextInt(start, x.size() + 1); 431 432 // Vector#subList spec deviates slightly from List#subList spec 433 assertThrows( 434 IllegalArgumentException.class, 435 () -> x.subList(start, start - 1)); 436 437 List subList = x.subList(start, end); 438 testIndexOutOfBoundsException(x); 439 } 440 441 /** 442 * a deserialized/reserialized list equals original 443 */ 444 public void testSerialization() throws Exception { 445 List x = populatedList(SIZE); 446 List y = serialClone(x); 447 448 assertNotSame(x, y); 449 assertEquals(x.size(), y.size()); 450 assertEquals(x.toString(), y.toString()); 451 assertTrue(Arrays.equals(x.toArray(), y.toArray())); 452 assertEquals(x, y); 453 assertEquals(y, x); 454 while (!x.isEmpty()) { 455 assertFalse(y.isEmpty()); 456 assertEquals(x.remove(0), y.remove(0)); 457 } 458 assertTrue(y.isEmpty()); 459 } 460 461 /** 462 * tests for setSize() 463 */ 464 public void testSetSize() { 465 final Vector v = new Vector(); 466 for (int n : new int[] { 100, 5, 50 }) { 467 v.setSize(n); 468 assertEquals(n, v.size()); 469 assertNull(v.get(0)); 470 assertNull(v.get(n - 1)); 471 assertThrows( 472 ArrayIndexOutOfBoundsException.class, 473 new Runnable() { public void run() { v.setSize(-1); }}); 474 assertEquals(n, v.size()); 475 assertNull(v.get(0)); 476 assertNull(v.get(n - 1)); 477 } 478 } 479 480 }