1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
|
// Copyright (c) Corporation for National Research Initiatives
package javatests;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import org.python.util.Generic;
/**
* @author updikca1
*/
public abstract class ListTest {
public static ListTest getArrayListTest(final boolean makeReadOnly) {
return new ListTest() {
public List<Object> newInstance(Collection<Object> c) {
List<Object> l = null;
if (c == null) {
l = Generic.list();
} else {
l = new ArrayList<Object>(c);
}
return (makeReadOnly) ? Collections.unmodifiableList(l) : l;
}
public boolean isReadOnly() {
return makeReadOnly;
}
};
}
public static void verifyImutability(List<Object> l) {
String message = "Expected UnsupportedOperationException.";
try {
l.add(0, 0);
TestSupport.fail(message);
} catch (UnsupportedOperationException e) {}
try {
l.add(0);
TestSupport.fail(message);
} catch (UnsupportedOperationException e) {}
try {
l.addAll(null);
TestSupport.fail(message);
} catch (UnsupportedOperationException e) {}
try {
l.addAll(0, null);
TestSupport.fail(message);
} catch (UnsupportedOperationException e) {}
try {
l.clear();
TestSupport.fail(message);
} catch (UnsupportedOperationException e) {}
try {
l.remove(0);
TestSupport.fail(message);
} catch (UnsupportedOperationException e) {}
try {
l.remove(new Object());
TestSupport.fail(message);
} catch (UnsupportedOperationException e) {}
try {
l.removeAll(null);
TestSupport.fail(message);
} catch (UnsupportedOperationException e) {}
try {
l.retainAll(null);
TestSupport.fail(message);
} catch (UnsupportedOperationException e) {}
try {
l.set(0, 0);
TestSupport.fail(message);
} catch (UnsupportedOperationException e) {}
}
private final List<Object> nullList;
protected List<Object> defaultList() {
List<Object> l = Generic.list();
for (int i = 0; i < 4; i++) {
l.add(i);
}
return newInstance(l);
}
/**
* Implementations must supply an empty list if the collection is null.
*
* @param c
* Initial collection or null for empty.
* @return the List instance
*/
public List<Object> newInstance(Collection<Object> c) {
throw new UnsupportedOperationException("This method must be overridden");
}
/**
* @return true if the list is read-only (like PyTuple)
*/
public boolean isReadOnly() {
throw new UnsupportedOperationException("This method must be overridden");
}
{
nullList = newInstance(Arrays.asList(new Object[] {null}));
}
public void testAll() {
test_get();
test_equals();
test_size();
test_contains();
test_containsAll();
try {
defaultList().hashCode();
test_hashCode();
} catch (Exception e) {
// skip unhashable types
}
test_subList();
test_indexOf();
test_lastIndexOf();
test_listIterator();
test_toArray();
test_toArray_typed();
if (!isReadOnly()) {
test_add();
test_add_index();
test_set();
test_clear();
test_addAll();
test_addAll_index();
test_remove();
test_remove_index();
test_removeAll();
test_retainAll();
} else {
verifyImutability(newInstance(null));
}
}
/** Tests get(int index) */
public void test_get() {
List<Object> l = defaultList();
TestSupport.assertThat(l.get(0).equals(0),
"get() did not return expected value of Integer(0)");
try {
l.get(-1);
TestSupport.fail("get(<negative index>) did not throw IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {}
try {
l.get(l.size());
TestSupport.fail("get(<index too big>) did not throw IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {}
}
/** Tests set(int index, Object element) */
public void test_set() {
try {
newInstance(null).set(-1, "spam");
TestSupport.fail("get(<negative index>) did not throw IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {}
try {
newInstance(null).set(0, "spam");
TestSupport.fail("set(<index too big>) did not throw IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {}
List<Object> a = defaultList();
a.set(a.size() - 1, "spam");
TestSupport.assertThat(a.get(a.size() - 1).equals("spam"),
"set() object was not retrieved via get()");
}
/** Tests add(Object o) */
public void test_add() {
List<Object> a = newInstance(null);
for (int i = 0; i < 4; i++) {
a.add(i);
}
TestSupport.assertEquals(a, defaultList(), "add(Object o) failed");
}
/** Tests isEmpty() */
public void test_isEmpty() {
List<Object> a = newInstance(null);
TestSupport.assertThat(a.isEmpty(), "isEmpty() is false on an emtpy List");
a.addAll(defaultList());
TestSupport.assertThat(!a.isEmpty(), "isEmpty() is true on a non-empty List)");
a.clear();
TestSupport.assertThat(a.isEmpty(), "isEmpty() is false on an emtpy List");
}
/** Tests size() */
public void test_size() {
List<Object> b = newInstance(null);
TestSupport.assertThat(b.size() == 0, "empty list size was not 0");
TestSupport.assertThat(defaultList().size() == 4, "default list did not have a size of 4");
}
/** Tests add(int index, Object element) */
public void test_add_index() {
List<Object> a = newInstance(null);
List<Object> b = defaultList();
for (int i = 0; i < b.size(); i++) {
a.add(i, b.get(i));
}
try {
a.add(a.size() + 1, new Integer(a.size() + 1));
TestSupport.fail("expected IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {}
try {
a.add(-1, new Integer(-1));
TestSupport.fail("expected IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {}
}
/** Tests equals(Object o) */
public void test_equals() {
TestSupport.assertEquals(defaultList(), defaultList(), "Identical lists weren't equal()");
TestSupport.assertNotEquals(newInstance(null),
defaultList(),
"Different lists were equal()");
TestSupport.assertNotEquals(newInstance(null),
new Object(),
"List was equal to a non-List type");
}
/** Tests addAll(Collection c) */
public void test_addAll() {
List<Object> a = defaultList();
List<Object> b = defaultList();
TestSupport.assertThat(a.addAll(b) == true, "Mutating addAll(Collection) returned false");
TestSupport.assertThat(a.addAll(newInstance(null)) == false,
"Idempotent addAll(Collection) returned true");
TestSupport.assertThat(b.addAll(b) == true, "Mutating addAll(Collection) returned false");
TestSupport.assertEquals(a, b, "Expected equal objects from addAll(collection)");
TestSupport.assertThat(a.size() == 8,
"Expected List to have size 8 after addAll(Collection)");
}
/** Tests indexOf(Object o) */
public void test_indexOf() {
TestSupport.assertThat(defaultList().indexOf(3) == 3, "indexOf(3) did not return 3");
TestSupport.assertThat(defaultList().indexOf(42) == -1,
"indexOf() non-existing entry did not return -1");
TestSupport.assertThat(defaultList().indexOf(null) == -1,
"indexOf() non-existing null did not return -1");
}
/** Tests contains(Object o) */
public void test_contains() {
TestSupport.assertThat(defaultList().contains(42) == false,
"contains() returned true for non-existing entry");
TestSupport.assertThat(defaultList().contains(0) == true,
"contains() returned false for existing entry");
TestSupport.assertThat(nullList.contains(null) == true,
"contains() returned false for existing null entry");
TestSupport.assertThat(defaultList().contains(null) == false,
"contains() returned true for non-existing null entry");
}
/** Tests remove(Object o) */
public void test_remove() {
List<Object> a = defaultList();
a.add(null);
TestSupport.assertThat(a.remove(null) == true,
"remove() existing null entry returned false");
TestSupport.assertThat(a.remove(null) == false,
"remove() non-existing null entry returned false");
a.add("spam");
TestSupport.assertThat(a.remove("spam") == true, "remove() existing entry returned false");
TestSupport.assertThat(a.remove("spam") == false,
"remove() non-existing entry returned true");
}
/** Tests remove(int index) */
public void test_remove_index() {
List<Object> a = defaultList();
for (int i = 0, n = a.size(); i < n; i++) {
a.remove(0);
}
TestSupport.assertThat(a.size() == 0, "remove()-d all entries but size() not 0");
try {
a.remove(0);
TestSupport.fail("removing a non-existing index did not throw exception");
} catch (IndexOutOfBoundsException e) {}
}
/** Tests lastIndexOf(Object o) */
public void test_lastIndexOf() {
// avoid calling any mutable methods
List<Object> l = new ArrayList<Object>(defaultList());
l.add(0);
// now get the immutable version
List<Object> a = newInstance(l);
TestSupport.assertThat(a.lastIndexOf(0) == 4, "lastIndexOf() did not return 4");
TestSupport.assertThat(a.lastIndexOf(42) == -1,
"lastIndexOf() non-existing value did not return -1");
}
/** Tests removeAll(Collection c) */
public void test_removeAll() {
List<Object> a = defaultList();
TestSupport.assertThat(a.removeAll(a) == true, "mutating removeAll() did not return true");
TestSupport.assertThat(a.removeAll(a) == false, "idempotent removeAll did not return false");
TestSupport.assertThat(a.removeAll(nullList) == false,
"idempotent removeAll did not return false");
List<Object> yanl = newInstance(null);
yanl.addAll(nullList);
TestSupport.assertThat(yanl.removeAll(nullList) == true,
"mutating removeAll() did not return true");
TestSupport.assertThat(yanl.size() == 0, "empty list had non-zero size");
TestSupport.assertThat(yanl.removeAll(newInstance(null)) == false,
"idempotent removeAll did not return false");
}
/** Tests addAll(int index, Collection c) */
public void test_addAll_index() {
List<Object> a = defaultList();
List<Object> b = newInstance(null);
TestSupport.assertThat(b.addAll(0, a) == true,
"mutating addAll(index, Collection) did not return true");
TestSupport.assertEquals(a, b, "addAll(index, Collection) instances failed equals test");
TestSupport.assertThat(a.addAll(0, newInstance(null)) == false,
"idempotent addAll(index, Collection) did not return false");
TestSupport.assertThat(b.addAll(0, b) == true,
"mutating addAll(index, Collection) did not return true");
// Since PyObjectList has some specific handling when it detects
// addAll on a PySequenceList, make sure the general case works.
b = newInstance(null);
b.addAll(new ArrayList<Object>(defaultList()));
TestSupport.assertEquals(defaultList(), b, "addAll(index, <ArrayList>) failed equals test");
}
/** Tests hashCode() */
public void test_hashCode() {
List<Object> a = defaultList();
TestSupport.assertThat(a.hashCode() == defaultList().hashCode(),
"Instances with same internal state have different hashcode");
TestSupport.assertThat(a.hashCode() != newInstance(null).hashCode(),
"Instances with different internal state have the same hashcode");
if (isReadOnly() == false) {
List<Object> b = newInstance(null);
b.addAll(a);
b.remove(0);
TestSupport.assertThat(a.hashCode() != b.hashCode(),
"Instances with different internal state have the same hashcode");
}
}
/** Tests clear() */
public void test_clear() {
List<Object> a = defaultList();
a.clear();
TestSupport.assertThat(a.size() == 0, "clear()-ed list did not have size of 0");
}
/** Tests subList(int fromIndex, int toIndex) */
public void test_subList() {
List<Object> a = defaultList();
TestSupport.assertThat((a.subList(0, a.size()) != a),
"subList() returned the same instance");
TestSupport.assertEquals(a.subList(0, a.size()),
a,
"Complete subList() did not equal original List");
TestSupport.assertThat(a.subList(0, 0).size() == 0, "empty subList had non-zero size");
try {
a.subList(-1, 1);
TestSupport.fail("Expected IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {}
try {
a.subList(1, 0);
TestSupport.fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {}
try {
a.subList(0, a.size() + 1);
TestSupport.fail("Expected IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {}
if (!isReadOnly()) {
a.subList(0, a.size()).clear();
TestSupport.assertThat(a.size() == 0, "clear()-ed sublist did not have zero size");
List<Object> c = newInstance(null);
c.addAll(defaultList());
List<Object> d = c.subList(1, 3);
TestSupport.assertThat(d.size() == 2, "Expected subList to have size of 2");
TestSupport.assertThat(c.set(1, "canned").equals(1),
"subList.set() did not return Integer(1) from index 1"
+ " of defaultList");
TestSupport.assertThat(d.get(0).equals("canned"),
"subList does not update with changes to parent");
d.set(0, "spam");
TestSupport.assertThat(c.get(1).equals("spam"),
"parent does not update with changes to subList child");
} else {
List<Object> b = a.subList(0, a.size());
verifyImutability(b);
}
}
/** Tests retainAll(Collection c) */
public void test_retainAll() {
List<Object> a = defaultList();
a.retainAll(defaultList());
TestSupport.assertEquals(a,
defaultList(),
"retainAll(<equal List>) does not equal original list");
a = defaultList();
a.retainAll(newInstance(null));
TestSupport.assertThat(a.size() == 0, "retainAll(<empty List>))does not have size of zero");
a = defaultList();
a.remove(0);
a.remove(0);
a.add(4);
a.add(5);
List<Object> b = newInstance(null);
b.add(2);
b.add(3);
a.retainAll(b);
TestSupport.assertEquals(a,
b,
"retainAll() on overlap of indices [2,3] did not return that List");
}
/** Tests containsAll(Collection c) */
public void test_containsAll() {
TestSupport.assertThat(defaultList().containsAll(defaultList()),
"containsAll(<identical List> was false");
TestSupport.assertThat(defaultList().containsAll(newInstance(null)),
"containsAll(<empty List>) was false");
TestSupport.assertThat(newInstance(null).containsAll(defaultList()) == false,
"containsAll(<disjoint List>) returned true");
TestSupport.assertThat(defaultList().containsAll(defaultList().subList(1, 3)),
"containsAll(<subList>) was false");
}
/** Tests iterator() */
public void test_iterator() {
TestSupport.assertThat(newInstance(null).iterator().hasNext() == false,
"Iterator for empty list thinks it hasNext()");
try {
newInstance(null).iterator().next();
TestSupport.fail("expected NoSuchElementException");
} catch (NoSuchElementException e) {}
List<Object> a = defaultList();
int i = 0;
for (Object element : a) {
TestSupport.assertThat(element == a.get(i++), "Iterator next() failed identity test");
}
TestSupport.assertThat(i == a.size(), "Iterator did not iterator over entire list");
}
public void test_listIterator() {
ListIterator<Object> li = newInstance(null).listIterator();
TestSupport.assertThat(!li.hasNext(), "ListIterator.hasNext() is true for empty List");
TestSupport.assertThat(!li.hasPrevious(),
"ListIterator.hasPrevious() is true for empty List");
try {
li.next();
TestSupport.fail("expected NoSuchElementException");
} catch (NoSuchElementException e) {}
try {
li.previous();
TestSupport.fail("expected NoSuchElementException");
} catch (NoSuchElementException e) {}
int nextIndex = li.nextIndex();
TestSupport.assertThat(nextIndex == 0,
"ListIterator.nextIndex() on empty List did not return 0");
int prevIndex = li.previousIndex();
TestSupport.assertThat(prevIndex == -1,
"ListIterator.previousIndex() on empty List did not return -1");
List<Object> l = Generic.list();
l.add(1);
li = newInstance(l).listIterator();
TestSupport.assertThat(!li.hasPrevious(),
"ListIterator.hasPrevious() is true with nothing previous");
TestSupport.assertThat(li.hasNext(), "ListIterator.hasNext() is false with next present");
TestSupport.assertThat(li.next().equals(1),
"ListIterator.next() did not return expected Integer(1)");
if (!isReadOnly()) {
li.remove();
TestSupport.assertThat(!li.hasNext(), "ListIterator.hasNext() is true for empty List");
TestSupport.assertThat(!li.hasPrevious(),
"ListIterator.hasPrevious() is true for empty List");
try {
li.set(42);
TestSupport.fail("expected IllegalStateException");
} catch (IllegalStateException e) {}
try {
li.remove();
TestSupport.fail("expected IllegalStateException");
} catch (IllegalStateException e) {}
}
l = Generic.list(new Object[]{0, 1, 2});
li = newInstance(l).listIterator();
for (int i = 0, n = l.size(); i < n; i++) {
TestSupport.assertThat(li.next().equals(i),
"ListIterator.previous did not return expected value");
}
while (!isReadOnly() && li.hasNext()) {
li.next();
li.set(42);
TestSupport.assertThat(li.previous().equals(42),
"ListIterator.previous() did not return the value that was set()");
li.remove();
}
if (isReadOnly()) {
li = newInstance(null).listIterator();
}
li = defaultList().listIterator(2);
TestSupport.assertThat(li.next().equals(2),
"List.listIteraor(index) did not return expected value");
TestSupport.assertThat(li.next().equals(3),
"List.listIteraor(index) did not return expected value");
TestSupport.assertThat(!li.hasNext(), "listIterator.hasNext() at end of list returned true");
}
/** Tests toArray() */
public void test_toArray() {
Object[] intObjArray = {0, 1, 2, 3};
TestSupport.assertThat(Arrays.equals(defaultList().toArray(), intObjArray),
"toArray() did not return the expected Integer[] array");
}
/** Tests toArray(Object[] a) */
public void test_toArray_typed() {
Object[] intObjArray = {0, 1, 2, 3};
TestSupport.assertThat(Arrays.equals(defaultList().toArray(new Integer[0]), intObjArray),
"toArray(Integer[]) did not return the expected Integer[] array");
}
// Can't test clone since it's not visible, but it is from jython.
// /** Tests clone() */
// public void test_clone() {
// List a = defaultList();
// TestSupport.assertEquals(((Object)a).clone(), a,
// "clone() was not equal to original");
// TestSupport.assertThat(a.clone() != a,
// "clone() returned same instance");
// }
}
|