/** * Tests {@link Collection#isEmpty()}. */ public void testCollectionIsEmpty() { resetEmpty(); assertEquals("New Collection should be empty.", true, collection.isEmpty()); // make sure calls to "isEmpty() don't change anything verify(); resetFull(); assertEquals("Full collection shouldn't be empty", false, collection.isEmpty()); // make sure calls to "isEmpty() don't change anything verify(); }
/** * 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); }
assertEquals("Empty Collection should return empty array for toArray", 0, collection.toArray().length); assertEquals("Full collection toArray should be same size as " + "collection", array.length, collection.size()); Object[] confirmedArray = confirmed.toArray(); assertEquals("length of array from confirmed collection should " + "match the length of the collection's array", confirmedArray.length, array.length); assertEquals("Collection should return all its elements in " + "toArray", true, matched[i]);
public void testSerializeDeserializeThenCompare() throws Exception { Object obj = makeCollection(); if (obj instanceof Serializable && isTestSerialization()) { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(buffer); out.writeObject(obj); out.close(); ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); Object dest = in.readObject(); in.close(); if (isEqualsCheckable()) { assertEquals("obj != deserialize(serialize(obj)) - EMPTY Collection", obj, dest); } } obj = makeFullCollection(); if (obj instanceof Serializable && isTestSerialization()) { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(buffer); out.writeObject(obj); out.close(); ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); Object dest = in.readObject(); in.close(); if (isEqualsCheckable()) { assertEquals("obj != deserialize(serialize(obj)) - FULL Collection", obj, dest); } } }
assertEquals("Collection size should match confirmed collection's", confirmedSize, collection.size()); assertEquals("Collection isEmpty() result should match confirmed " + " collection's", confirmed.isEmpty(), collection.isEmpty());
Object[] a = new Object[] { new Object(), null, null }; Object[] array = collection.toArray(a); assertEquals("Given array shouldn't shrink", array, a); assertEquals("Last element should be set to null", a[0], null); verify(); assertEquals("toArrays should be equal", Arrays.asList(array), Arrays.asList(a)); assertEquals("toArray(Object[]) should return correct array type", a.getClass(), array.getClass()); assertEquals("type-specific toArrays should be equal", Arrays.asList(array), Arrays.asList(collection.toArray()));
/** * 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])); } }
resetEmpty(); Iterator it1 = collection.iterator(); assertEquals("Iterator for empty Collection shouldn't have next.", false, it1.hasNext()); try {
collection.contains(elements[i])); assertEquals("Size should increase after addAll", size + elements.length, collection.size()); size < collection.size()); } else { assertEquals("Size should not change if addAll returns false", size, collection.size());
assertEquals("Collection should shrink by one after " + "iterator.remove", size, collection.size());
!collection.retainAll(elements)); verify(); assertEquals("Collection size shouldn't change", size, collection.size()); "duplicate elements", !collection.retainAll(set)); verify(); assertEquals("Collection size didn't change from nonduplicate " + "retainAll", size, collection.size());
assertEquals("Collection should shrink after remove", size - 1, collection.size());