PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/java/javatests/ListTest.java

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