package com.michaelthomas.set; /** * @author Michael Thomas (www.michael-thomas.com) michael@michael-thomas.com * */ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.Set; import java.util.TreeSet; import org.junit.Before; import org.junit.Test; import com.michaelthomas.collection.MyCollectionTraverse; public class MySetTest { private MyCollectionTraverse myCollectionTraverse = new MyCollectionTraverse(); MySet myObj = new MySet(); Set mySetObjPlay = new HashSet(); private Set set1; private Set set2; private Set setNulls; @Before public void setUp() throws Exception { set1 = myObj.buildHashSetCarVendors1(); set2 = myObj.buildHashSetCarVendors2(); setNulls = myObj.buildHashSetWithNulls(); } @Test public void testBasicUseSize() { //int size() - Returns the number of elements in this collection. assertEquals(3, set1.size()); } @Test public void testBasicUseContains() { // boolean contains(Object o) - Returns true if this collection contains the specified element. assertTrue(set1.contains("BMW")); assertTrue(set1.contains("Ford")); assertTrue(set1.contains("Toyota")); } @Test public void testBasicAddNoDuplicates() { //boolean add(E e) - Ensures that this collection contains the specified element (optional operation). Set setPlay = new HashSet(set1); assertEquals(3, setPlay.size()); assertFalse(setPlay.add("BMW")); //No duplicates allowed! False returned! assertEquals(3, setPlay.size()); //Notice the size did NOT change! assertTrue(setPlay.add("Mercedes")); assertEquals(4, setPlay.size()); } @Test public void testBasicAddRemove() { assertEquals(3, set1.size()); assertTrue(set1.add("Jeep")); assertEquals(4, set1.size()); assertTrue(set1.remove("Jeep")); assertEquals(3, set1.size()); } @Test public void testAddAll(){ // boolean addAll(Collection c) - Adds all of the elements in the specified collection to this collection (optional operation). Set setPlay = new HashSet(set1); assertEquals(3, setPlay.size()); setPlay.addAll(set2); assertEquals(5, setPlay.size()); //Why 5 and not 6? BMW exists in both sets and no dups allowed. } @Test public void testContainsAll() { // boolean containsAll(Collection c) - Returns true if this collection contains all of the elements in the specified collection. Set setPlay = new HashSet(set1); assertTrue(setPlay.containsAll(set1)); //Set was creates with this set! assertFalse(setPlay.containsAll(set2)); //playSet doesn't contain all of set2 items. setPlay.addAll(set2); //Add all of set2 to playSet. assertTrue(setPlay.containsAll(set2)); } @Test public void testIsEmptyClear() { //boolean isEmpty() - Returns true if this collection contains no elements. //void clear() - Removes all of the elements from this collection (optional operation). Set setPlay = new HashSet(set1); assertEquals(3, setPlay.size()); assertFalse(setPlay.isEmpty()); setPlay.clear(); assertTrue(setPlay.isEmpty()); } @Test public void testEquals() { // boolean equals(Object o) - Compares the specified object with this collection for equality. Set setPlay = new HashSet(set1); assertTrue(setPlay.equals(set1)); assertFalse(set1.equals(set2)); //There is differences in set1 & set2. } @Test public void testHashCode() { // int hashCode() - Returns the hash code value for this collection. assertEquals(-1781631131, set1.hashCode()); } @Test public void testRemove() { // boolean remove(Object o) - Removes a single instance of the specified element from this collection, if it is present (optional operation). Set setPlay = new HashSet(set1); assertEquals(3, setPlay.size()); //Exists... assertTrue(setPlay.contains("BMW")); //Remove it. setPlay.remove("BMW"); //Does NOT exist now. assertFalse(setPlay.contains("BMW")); } @Test public void testRemoveAll() { // boolean removeAll(Collection c) - Removes all of this collection's elements that are also contained in the specified collection (optional operation). Set setPlay = new TreeSet(set1); //Sorted order! assertEquals(3, setPlay.size()); assertEquals("BMW, Ford, Toyota", myCollectionTraverse.forEachExample(setPlay)); setPlay.addAll(set2); //Add set2 to setPlay assertEquals(5, setPlay.size()); //Note: No dups allows so "BMW" is listed only once! assertEquals("BMW, Chevorlet, Ford, Mercedes, Toyota", myCollectionTraverse.forEachExample(setPlay)); setPlay.removeAll(set2); } @Test public void testRetainAll() { //boolean retainAll(Collection c) - Retains only the elements in this collection that are contained in the specified collection (optional operation). Set setPlay = new TreeSet(set1); //Sorted order! assertEquals(3, setPlay.size()); assertEquals("BMW, Ford, Toyota", myCollectionTraverse.forEachExample(setPlay)); setPlay.addAll(set2); //Add set2 to setPlay assertEquals(5, setPlay.size()); //Note: No dups allows so "BMW" is listed only once! assertEquals("BMW, Chevorlet, Ford, Mercedes, Toyota", myCollectionTraverse.forEachExample(setPlay)); setPlay.retainAll(set1); assertEquals("BMW, Ford, Toyota", myCollectionTraverse.forEachExample(setPlay)); assertEquals(3, setPlay.size()); } @Test public void testIterator() { //Iterator iterator() - Returns an iterator over the elements in this collection. StringBuilder sb = new StringBuilder(); //For Each for (Iterator iterator = set1.iterator(); iterator.hasNext();) { sb.append((String) iterator.next() + ";"); } assertEquals("Ford;Toyota;BMW;", sb.toString()); StringBuilder sb2 = new StringBuilder(); //While Example Iterator myIterator = set1.iterator(); while (myIterator.hasNext()) { sb2.append((String) myIterator.next() + ";"); } assertEquals("Ford;Toyota;BMW;", sb2.toString()); } @Test public void testToArray() { //Object[] toArray() - Returns an array containing all of the elements in this collection. StringBuilder sb = new StringBuilder(); Object[] myCars = set1.toArray(); for (Object object : myCars) { sb.append((String) object + ";"); } assertEquals("Ford;Toyota;BMW;", sb.toString()); } @Test public void testToArrayDataType() { // T[] toArray(T[] a) - Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. StringBuilder sb = new StringBuilder(); String[] myCars = set1.toArray(new String[0]); for (Object object : myCars) { sb.append((String) object + ";"); } assertEquals("Ford;Toyota;BMW;", sb.toString()); } }