/** * Tests <code>toString</code> on a collection. */ public void testCollectionToString() { resetEmpty(); assertTrue("toString shouldn't return null", collection.toString() != null); resetFull(); assertTrue("toString shouldn't return null", collection.toString() != null); }
/** * Tests {@link Collection#size()}. */ public void testCollectionSize() { resetEmpty(); assertEquals("Size of new Collection is 0.", 0, collection.size()); resetFull(); assertTrue("Size of full collection should be greater than zero", collection.size() > 0); }
resetEmpty(); Collection col = new HashSet(); assertTrue("Every Collection should contain all elements of an " + "empty Collection.", collection.containsAll(col)); col.addAll(Arrays.asList(getOtherElements())); assertTrue("Empty Collection shouldn't contain all elements of " + "a non-empty Collection.", !collection.containsAll(col)); assertTrue("Full collection shouldn't contain other elements", !collection.containsAll(col)); assertTrue("Full collection should containAll full elements", collection.containsAll(col)); (getFullElements().length <= 5 ? getFullElements().length - 1 : 5)); col = Arrays.asList(getFullElements()).subList(min, max); assertTrue("Full collection should containAll partial full " + "elements", collection.containsAll(col)); assertTrue("Full collection should containAll itself", collection.containsAll(collection)); col.addAll(Arrays.asList(getFullElements())); col.addAll(Arrays.asList(getFullElements())); assertTrue("Full collection should containAll duplicate full " + "elements", collection.containsAll(col));
it1 = collection.iterator(); for (int i = 0; i < collection.size(); i++) { assertTrue("Iterator for full collection should haveNext", it1.hasNext()); it1.next(); assertTrue("Iterator should be finished", !it1.hasNext()); for (int i = 0; i < collection.size(); i++) { Object next = it1.next(); assertTrue("Collection should contain element returned by " + "its iterator", collection.contains(next)); list.add(next);
confirmed.addAll(Arrays.asList(elements)); verify(); assertTrue("Empty collection should change after addAll", r); for (int i = 0; i < elements.length; i++) { assertTrue("Collection should contain added element", collection.contains(elements[i])); confirmed.addAll(Arrays.asList(elements)); verify(); assertTrue("Full collection should change after addAll", r); for (int i = 0; i < elements.length; i++) { assertTrue("Full collection should contain added element", collection.contains(elements[i])); verify(); if (r) { assertTrue("Size should increase if addAll returns true", size < collection.size()); } else {
assertTrue("Emtpy collection removeAll should return false for " + "empty input", !collection.removeAll(Collections.EMPTY_SET)); verify(); assertTrue("Emtpy collection removeAll should return false for " + "nonempty input", !collection.removeAll(new ArrayList(collection))); assertTrue("Full collection removeAll should return false for " + "empty input", !collection.removeAll(Collections.EMPTY_SET)); verify(); assertTrue("Full collection removeAll should return false for other elements", !collection.removeAll(Arrays.asList(getOtherElements()))); verify(); assertTrue("Full collection removeAll should return true for full elements", collection.removeAll(new HashSet(collection))); confirmed.removeAll(new HashSet(confirmed)); (getFullElements().length <= 5 ? getFullElements().length - 1 : 5)); Collection all = Arrays.asList(getFullElements()).subList(min, max); assertTrue("Full collection removeAll should work", collection.removeAll(all)); confirmed.removeAll(all); verify();
elements = getFullElements(); for(int i = 0; i < elements.length; i++) { assertTrue("Empty collection shouldn't contain element[" + i + "]", !collection.contains(elements[i])); assertTrue("Empty collection shouldn't contain element[" + i + "]", !collection.contains(elements[i])); elements = getFullElements(); for(int i = 0; i < elements.length; i++) { assertTrue("Full collection should contain element[" + i + "]", collection.contains(elements[i])); elements = getOtherElements(); for(int i = 0; i < elements.length; i++) { assertTrue("Full collection shouldn't contain element", !collection.contains(elements[i]));
assertTrue("Empty retainAll() should return false", !collection.retainAll(Collections.EMPTY_SET)); verify(); assertTrue("Empty retainAll() should return false", !collection.retainAll(elements)); verify(); assertTrue("Collection should change from retainAll empty", collection.retainAll(Collections.EMPTY_SET)); confirmed.retainAll(Collections.EMPTY_SET); assertTrue("Collection changed from retainAll other", collection.retainAll(other)); confirmed.retainAll(other); assertTrue("Collection shouldn't change from retainAll elements", !collection.retainAll(elements)); verify(); int min = (getFullElements().length < 2 ? 0 : 2); int max = (getFullElements().length <= 5 ? getFullElements().length - 1 : 5); assertTrue("Collection should changed by partial retainAll", collection.retainAll(elements.subList(min, max))); confirmed.retainAll(elements.subList(min, max)); assertTrue("Collection only contains retained element", elements.subList(min, max).contains(iter.next()));
/** * Tests {@link Collection#add(Object)}. */ public void testCollectionAdd() { if (!isAddSupported()) return; Object[] elements = getFullElements(); for (int i = 0; i < elements.length; i++) { resetEmpty(); boolean r = collection.add(elements[i]); confirmed.add(elements[i]); verify(); assertTrue("Empty collection changed after add", r); assertEquals("Collection size is 1 after first add", 1, collection.size()); } resetEmpty(); int size = 0; for (int i = 0; i < elements.length; i++) { boolean r = collection.add(elements[i]); confirmed.add(elements[i]); verify(); if (r) size++; assertEquals("Collection size should grow after add", size, collection.size()); assertTrue("Collection should contain added element", collection.contains(elements[i])); } }
assertTrue("Collection should contain element in toArray", collection.contains(array[i]));
Object[] elements = getFullElements(); for (int i = 0; i < elements.length; i++) { assertTrue("Shouldn't remove nonexistent element", !collection.remove(elements[i])); verify(); assertTrue("Shouldn't remove nonexistent other element", !collection.remove(other[i])); verify(); for (int i = 0; i < elements.length; i++) { resetFull(); assertTrue("Collection should remove extant element: " + elements[i], collection.remove(elements[i]));
"iterator.remove", size, collection.size()); assertTrue("Collection should be empty after iterator purge", collection.isEmpty());