Refactored App.java to remove duplicate code and elegantly demonstrate each implementation of the Iterator interface. Removed the redundant ItemIterator interface. Added insert() method to TreeNode class to allow for more elegant construction of BSTs.
This commit is contained in:
@ -1,33 +1,34 @@
|
||||
/**
|
||||
* The MIT License Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
*
|
||||
* <p>
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
||||
* associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
||||
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* <p>
|
||||
* The above copyright notice and this permission notice shall be included in all copies or
|
||||
* substantial portions of the Software.
|
||||
*
|
||||
* <p>
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
||||
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.iterator;
|
||||
|
||||
import org.junit.platform.runner.JUnitPlatform;
|
||||
import org.junit.platform.suite.api.SelectPackages;
|
||||
import org.junit.platform.suite.api.SuiteDisplayName;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Runs a test suite containing all tests within the com.iluwatar.iterator package
|
||||
* Application Test
|
||||
*/
|
||||
@RunWith(JUnitPlatform.class)
|
||||
@SuiteDisplayName("Iterator Test Suite")
|
||||
@SelectPackages("com.iluwatar.iterator")
|
||||
public class AppTest {
|
||||
}
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
void testApp() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
/**
|
||||
* The MIT License Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
||||
* associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
||||
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies or
|
||||
* substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
||||
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.iterator.binarysearchtree;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Comprehensive tests for TreeNode POJO
|
||||
* @param <T> generically typed for TreeNode
|
||||
*/
|
||||
public class TreeNodeTest<T> {
|
||||
|
||||
@Test
|
||||
void treeNodeGetters() {
|
||||
TreeNode<T> node = new TreeNode(1);
|
||||
assertEquals(node.getVal(), 1);
|
||||
assertNull(node.getLeft(), "Left child should be initialized as null.");
|
||||
assertNull(node.getRight(), "Right child should be initialized as null.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void treeNodeSetters() {
|
||||
TreeNode<T> node = new TreeNode(3);
|
||||
node.setLeft(new TreeNode(1));
|
||||
assertEquals(node.getLeft().getVal(), 1, "Left node has a value of 1.");
|
||||
node.setRight(new TreeNode(5));
|
||||
assertEquals(node.getRight().getVal(), 5, "Right node has a value of 5");
|
||||
}
|
||||
|
||||
@Test
|
||||
void treeNodeToString() {
|
||||
TreeNode<T> node = new TreeNode(3);
|
||||
assertEquals(node.toString(), "3", "node.toString() should return string value.");
|
||||
}
|
||||
|
||||
}
|
@ -16,82 +16,83 @@
|
||||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.iterator.binarysearchtree;
|
||||
package com.iluwatar.iterator.bst;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import com.iluwatar.iterator.AppTest;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||
|
||||
@TestInstance(Lifecycle.PER_CLASS)
|
||||
class BstIteratorTest<T> extends AppTest {
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
private TreeNode<T> nonEmptyRoot;
|
||||
private TreeNode<T> emptyRoot;
|
||||
@TestInstance(Lifecycle.PER_CLASS)
|
||||
class BstIteratorTest {
|
||||
|
||||
private TreeNode<Integer> nonEmptyRoot;
|
||||
private TreeNode<Integer> emptyRoot;
|
||||
|
||||
@BeforeAll
|
||||
void createTrees() {
|
||||
nonEmptyRoot = new TreeNode(Integer.valueOf(5));
|
||||
nonEmptyRoot.setLeft(new TreeNode(3));
|
||||
nonEmptyRoot.setRight(new TreeNode(7));
|
||||
nonEmptyRoot.getLeft().setLeft(new TreeNode(1));
|
||||
nonEmptyRoot.getLeft().setRight(new TreeNode(4));
|
||||
nonEmptyRoot.getRight().setLeft(new TreeNode(6));
|
||||
nonEmptyRoot = new TreeNode<>(5);
|
||||
nonEmptyRoot.insert(3);
|
||||
nonEmptyRoot.insert(7);
|
||||
nonEmptyRoot.insert(1);
|
||||
nonEmptyRoot.insert(4);
|
||||
nonEmptyRoot.insert(6);
|
||||
|
||||
emptyRoot = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
void nextWithEmptyTree() {
|
||||
BstIterator iter = new BstIterator(emptyRoot);
|
||||
assertThrows(IllegalStateException.class, iter::next,
|
||||
void nextForEmptyTree() {
|
||||
BstIterator iter = new BstIterator<>(emptyRoot);
|
||||
assertThrows(NoSuchElementException.class, iter::next,
|
||||
"next() should throw an IllegalStateException if hasNext() is false.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void nextWithPopulatedTree() {
|
||||
BstIterator iter = new BstIterator(nonEmptyRoot);
|
||||
assertEquals(iter.next().getVal(), 1, "First Node is 1.");
|
||||
assertEquals(iter.next().getVal(), 3, "Second Node is 3.");
|
||||
assertEquals(iter.next().getVal(), 4, "Third Node is 4.");
|
||||
assertEquals(iter.next().getVal(), 5, "Fourth Node is 5.");
|
||||
assertEquals(iter.next().getVal(), 6, "Fifth Node is 6.");
|
||||
assertEquals(iter.next().getVal(), 7, "Sixth Node is 7.");
|
||||
void nextOverEntirePopulatedTree() {
|
||||
BstIterator iter = new BstIterator<>(nonEmptyRoot);
|
||||
assertEquals(1, iter.next().getVal(), "First Node is 1.");
|
||||
assertEquals(3, iter.next().getVal(), "Second Node is 3.");
|
||||
assertEquals(4, iter.next().getVal(), "Third Node is 4.");
|
||||
assertEquals(5, iter.next().getVal(), "Fourth Node is 5.");
|
||||
assertEquals(6, iter.next().getVal(), "Fifth Node is 6.");
|
||||
assertEquals(7, iter.next().getVal(), "Sixth Node is 7.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void hasNextWithEmptyTree() {
|
||||
BstIterator iter = new BstIterator(emptyRoot);
|
||||
void hasNextForEmptyTree() {
|
||||
BstIterator iter = new BstIterator<>(emptyRoot);
|
||||
assertFalse(iter.hasNext(), "hasNext() should return false for empty tree.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void hasNextWithPopulatedTree() {
|
||||
BstIterator iter = new BstIterator(nonEmptyRoot);
|
||||
void hasNextForPopulatedTree() {
|
||||
BstIterator iter = new BstIterator<>(nonEmptyRoot);
|
||||
assertTrue(iter.hasNext(), "hasNext() should return true for populated tree.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void nextAndHasNextAcrossEntirePopulatedTree() {
|
||||
BstIterator iter = new BstIterator(nonEmptyRoot);
|
||||
void nextAndHasNextOverEntirePopulatedTree() {
|
||||
BstIterator iter = new BstIterator<>(nonEmptyRoot);
|
||||
assertTrue(iter.hasNext(), "Iterator hasNext() should be true.");
|
||||
assertEquals(iter.next().getVal(), 1, "First Node is 1.");
|
||||
assertEquals(1, iter.next().getVal(), "First Node is 1.");
|
||||
assertTrue(iter.hasNext(), "Iterator hasNext() should be true.");
|
||||
assertEquals(iter.next().getVal(), 3, "Second Node is 3.");
|
||||
assertEquals(3, iter.next().getVal(), "Second Node is 3.");
|
||||
assertTrue(iter.hasNext(), "Iterator hasNext() should be true.");
|
||||
assertEquals(iter.next().getVal(), 4, "Third Node is 4.");
|
||||
assertEquals(4, iter.next().getVal(), "Third Node is 4.");
|
||||
assertTrue(iter.hasNext(), "Iterator hasNext() should be true.");
|
||||
assertEquals(iter.next().getVal(), 5, "Fourth Node is 5.");
|
||||
assertEquals(5, iter.next().getVal(), "Fourth Node is 5.");
|
||||
assertTrue(iter.hasNext(), "Iterator hasNext() should be true.");
|
||||
assertEquals(iter.next().getVal(), 6, "Fifth Node is 6.");
|
||||
assertEquals(6, iter.next().getVal(), "Fifth Node is 6.");
|
||||
assertTrue(iter.hasNext(), "Iterator hasNext() should be true.");
|
||||
assertEquals(iter.next().getVal(), 7, "Sixth Node is 7.");
|
||||
assertEquals(7, iter.next().getVal(), "Sixth Node is 7.");
|
||||
assertFalse(iter.hasNext(), "Iterator hasNext() should be false, end of tree.");
|
||||
}
|
||||
|
@ -1,41 +1,32 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
* The MIT License Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
||||
* associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
||||
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
* The above copyright notice and this permission notice shall be included in all copies or
|
||||
* substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
||||
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.iterator.list;
|
||||
|
||||
import com.iluwatar.iterator.list.Item;
|
||||
import com.iluwatar.iterator.list.ItemIterator;
|
||||
import com.iluwatar.iterator.list.ItemType;
|
||||
import com.iluwatar.iterator.list.TreasureChest;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
/**
|
||||
* Date: 12/14/15 - 2:58 PM
|
||||
*
|
||||
@ -64,13 +55,14 @@ public class TreasureChestTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the expected item can be retrieved from the chest using the {@link ItemIterator}
|
||||
* Test if the expected item can be retrieved from the chest using the {@link
|
||||
* TreasureChestItemIterator}
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("dataProvider")
|
||||
public void testIterator(Item expectedItem) {
|
||||
final TreasureChest chest = new TreasureChest();
|
||||
final ItemIterator iterator = chest.iterator(expectedItem.getType());
|
||||
final TreasureChestItemIterator iterator = chest.iterator(expectedItem.getType());
|
||||
assertNotNull(iterator);
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
|
Reference in New Issue
Block a user