package com.michaelthomas.set.treeset; /** * @author Michael Thomas (www.michael-thomas.com) michael@michael-thomas.com * */ import static org.junit.Assert.assertEquals; import java.util.NavigableSet; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; import org.junit.Before; import org.junit.Test; import com.michaelthomas.collection.MyCollectionTraverse; public class MyTreeSetTest { private MyCollectionTraverse myCollectionTraverse = new MyCollectionTraverse(); MyTreeSet myObj = new MyTreeSet(); Set mySetObjPlay = new TreeSet(); private Set set1; private Set set2; private TreeSet tree1; private Set setNulls; @Before public void setUp() throws Exception { set1 = myObj.buildTreeSetCarVendors1(); set2 = myObj.buildTreeSetCarVendors2(); setNulls = myObj.buildTreeSetWithNulls(); tree1 = (TreeSet ) set1; } //TODO: Add MyItem java bean examples. //TODO: NavigableSet interface - add examples. //TODO: SortedSet interface - add examples. //TODO: clone() //TODO: comparator() @Test public void testBuildSetCarVendors1() { assertEquals(3, set1.size()); //Notice: Sorted! (Entry-order was not!) assertEquals("[BMW, Ford, Toyota]",set1.toString()); //Order: Sorted } @Test public void testBuildSetCarVendors2() { assertEquals(3, set2.size()); //Notice: Sorted! (Entry-order was not!) assertEquals("[BMW, Chevorlet, Mercedes]".toString(),set2.toString()); //Order: Sorted } @Test public void testSetWithNulls() { assertEquals(2, setNulls.size()); assertEquals("[, BMW]".toString(),setNulls.toString()); //Order: insertion order } @Test public void testNotInSetFirst() { //This function is not in the Set interface. //BMW, Ford, Toyota assertEquals("BMW", tree1.first()); //first element } @Test public void testNotInSetLast() { //This function is not in the Set interface. //BMW, Ford, Toyota assertEquals("Toyota", tree1.last()); //last element } @Test public void testNotInSetFloor() { //This function is not in the Set interface. //BMW, Ford, Toyota //floor(E e) - Returns the greatest element in this set less than or equal to the given element, or null if there is no such element. assertEquals(null, tree1.floor("")); assertEquals(null, tree1.floor("B")); assertEquals("BMW", tree1.floor("C")); assertEquals("Ford", tree1.floor("Ford")); assertEquals("Ford", tree1.floor("Fordxx")); assertEquals("Ford", tree1.floor("T")); assertEquals("Toyota", tree1.floor("Toyota")); assertEquals("Toyota", tree1.floor("ZZZ")); } @Test public void testNotInSetCeiling() { //This function is not in the Set interface. //BMW, Ford, Toyota //ceiling(E e) - Returns the least element in this set greater than or equal to the given element, or null if there is no such element. assertEquals("BMW", tree1.ceiling("")); assertEquals("BMW", tree1.ceiling("B")); assertEquals("Ford", tree1.ceiling("C")); assertEquals("Ford", tree1.ceiling("Ford")); assertEquals("Toyota", tree1.ceiling("Fordxx")); assertEquals("Toyota", tree1.ceiling("T")); assertEquals("Toyota", tree1.ceiling("Toyota")); assertEquals(null, tree1.ceiling("ZZZ")); } @Test public void testNotInSetDescendingSet() { //descendingSet() - Returns a reverse order view of the elements contained in this set. assertEquals("[BMW, Ford, Toyota]", tree1.toString()); NavigableSet navSet = tree1.descendingSet(); assertEquals("[Toyota, Ford, BMW]", navSet.toString()); } @Test public void testNotInSetDescendingIterator() { //Ex: Descending or reverse iteration. String setString = myCollectionTraverse.iterateForLoopExample(tree1.descendingIterator()); assertEquals("Toyota, Ford, BMW", setString ); } @Test public void testNotInSetHeadSet() { //headSet - Returns a view of the portion of this set whose elements are strictly less than toElement SortedSet sortSet = tree1.headSet("Ford"); assertEquals("[BMW]", sortSet.toString()); } @Test public void testNotInSetHeadSet_include() { //headSet - Returns a view of the portion of this set whose elements are less than (or equal to, if inclusive is true) toElement. NavigableSet navSet1 = tree1.headSet("Ford", true); assertEquals("[BMW, Ford]", navSet1.toString()); NavigableSet navSet2 = tree1.headSet("Ford", false); assertEquals("[BMW]", navSet2.toString()); } @Test public void testNotInSetTailSet() { //tailSet(E fromElement) - Returns a view of the portion of this set whose elements are greater than or equal to fromElement. SortedSet sortSet = tree1.tailSet("Ford"); assertEquals("[Ford, Toyota]", sortSet.toString()); } @Test public void testNotInSetTailSet_include() { //tailSet(E fromElement, boolean inclusive) - Returns a view of the portion of this set whose elements are greater than (or equal to, if inclusive is true) fromElement. NavigableSet navSet1 = tree1.tailSet("Ford", true); assertEquals("[Ford, Toyota]", navSet1.toString()); NavigableSet navSet2 = tree1.tailSet("Ford", false); assertEquals("[Toyota]", navSet2.toString()); } @Test public void testNotInSetHigher() { //higher - Returns the least element in this set strictly greater than the given element, or null if there is no such element. - Returns a view of the portion of this set whose elements are strictly less than toElement assertEquals("Toyota", tree1.higher("Ford")); } @Test public void testNotInSetLower() { //lower - Returns the greatest element in this set strictly less than the given element, or null if there is no such element. assertEquals("BMW", tree1.lower("Ford")); } @Test public void testNotInSetPollFirst() { TreeSet treeTempFirst = new TreeSet(tree1); //pollFirst - Retrieves and removes the first (lowest) element, or returns null if this set is empty. assertEquals(3, treeTempFirst.size()); assertEquals("BMW", treeTempFirst.pollFirst()); assertEquals(2, treeTempFirst.size()); assertEquals("Ford", treeTempFirst.pollFirst()); assertEquals(1, treeTempFirst.size()); } @Test public void testNotInSetPollLast() { TreeSet treeTempLast = new TreeSet(tree1); //pollFirst - Retrieves and removes the first (lowest) element, or returns null if this set is empty. assertEquals(3, treeTempLast.size()); assertEquals("Toyota", treeTempLast.pollLast()); assertEquals(2, treeTempLast.size()); assertEquals("Ford", treeTempLast.pollLast()); assertEquals(1, treeTempLast.size()); } @Test public void testNotInSetSubSet() { TreeSet both = new TreeSet(set1); both.addAll(set2); assertEquals("[BMW, Chevorlet, Ford, Mercedes, Toyota]", both.toString()); SortedSet ss = both.subSet("Chevorlet", "Mercedes"); assertEquals("[Chevorlet, Ford]", ss.toString()); } @Test public void testNotInSetSubSet_inclusive() { TreeSet both = new TreeSet(set1); both.addAll(set2); assertEquals("[BMW, Chevorlet, Ford, Mercedes, Toyota]", both.toString()); NavigableSet ss = both.subSet("Chevorlet", true, "Mercedes", true); assertEquals("[Chevorlet, Ford, Mercedes]", ss.toString()); ss = both.subSet("Chevorlet", false, "Mercedes", true); assertEquals("[Ford, Mercedes]", ss.toString()); ss = both.subSet("Chevorlet", true, "Mercedes", false); assertEquals("[Chevorlet, Ford]", ss.toString()); ss = both.subSet("Chevorlet", false, "Mercedes", false); assertEquals("[Ford]", ss.toString()); } @Test public void testIterateForLoopExampleIterator() { String setString = myCollectionTraverse.iterateForLoopExample(tree1.iterator()); assertEquals("BMW, Ford, Toyota", setString ); } @Test public void testIterateForLoopExampleCollection() { String setString = myCollectionTraverse.iterateForLoopExample(set1); assertEquals("BMW, Ford, Toyota", setString ); } }