Created
November 15, 2016 03:43
-
-
Save shreydesai/71c0a01ced1e38a8006ecaaeb8599b7d to your computer and use it in GitHub Desktop.
CS 314 Assignment 10 Tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
public class BSTTester { | |
/** | |
* Runs tests on primitive data types | |
* pre: exp != null, actual != null, test != null | |
* post: output test failure or success | |
*/ | |
private static boolean test(Object exp, Object actual, String test) { | |
String output = null; | |
if (exp.equals(actual)) { | |
output = "✔ Passed " + test; | |
System.out.println(output + "\n"); | |
return true; | |
} | |
output = "***** FAILED ***** " + test + "\n" + | |
"Expected: " + exp + ", Actual: " + actual; | |
System.out.println(output + "\n"); | |
return false; | |
} | |
private static ArrayList<Integer> arrayToList(int[] array) { | |
ArrayList<Integer> list = new ArrayList<Integer>(); | |
for (int i : array) list.add(i); | |
return list; | |
} | |
private static void testAdd() { | |
BinarySearchTree<Integer> t = new BinarySearchTree<Integer>(); | |
ArrayList<Integer> nodes; | |
boolean result; | |
result = t.add(1); | |
nodes = arrayToList(new int[]{1}); | |
test(1, t.size(), "add - Test 1a"); | |
test(true, result, "add - Test 1b"); | |
test(t.getAll(), nodes, "add - Test 1c"); | |
result = t.add(0); | |
nodes = arrayToList(new int[]{0, 1}); | |
test(2, t.size(), "add - Test 2a"); | |
test(true, result, "add - Test 2b"); | |
test(t.getAll(), nodes, "add - Test 2c"); | |
result = t.add(2); | |
nodes = arrayToList(new int[]{0, 1, 2}); | |
test(3, t.size(), "add - Test 3a"); | |
test(true, result, "add - Test 3b"); | |
test(t.getAll(), nodes, "add - Test 3c"); | |
t = new BinarySearchTree<Integer>(); | |
for (int i = 0; i < 10; i++) { | |
result = t.add(0); | |
} | |
test(1, t.size(), "add - Test 4a"); | |
test(false, result, "add - Test 4b"); | |
} | |
private static void testRemove() { | |
BinarySearchTree<Integer> t = new BinarySearchTree<Integer>(); | |
ArrayList<Integer> nodes; | |
boolean result; | |
for (int i = 0; i < 5; i++) { | |
t.add(i); | |
} | |
nodes = arrayToList(new int[]{0, 1, 2, 3, 4}); | |
test(5, t.size(), "remove - Test 1a"); | |
test(t.getAll(), nodes, "remove - Test 1b"); | |
result = t.remove(0); | |
nodes = arrayToList(new int[]{1, 2, 3, 4}); | |
test(4, t.size(), "remove - Test 1c"); | |
test(t.getAll(), nodes, "remove - Test 1d"); | |
test(true, result, "remove - Test 1e"); | |
result = t.remove(10); | |
test(false, result, "remove - Test 2a"); | |
test(4, t.size(), "remove - Test 2b"); | |
test(t.getAll(), nodes, "remove - Test 2c"); | |
for (int i = 1; i < 5; i++) { | |
t.remove(i); | |
} | |
test(0, t.size(), "remove - Test 3a"); | |
} | |
private static void testIsPresent() { | |
BinarySearchTree<Integer> t = new BinarySearchTree<Integer>(); | |
ArrayList<Integer> nodes; | |
boolean result; | |
result = t.isPresent(10); | |
test(false, result, "isPresent - Test 1a"); | |
for (int i = 1; i <= 3; i++) { | |
t.add(i); | |
} | |
result = t.isPresent(2); | |
test(true, result, "isPresent - Test 2a"); | |
result = t.isPresent(4); | |
test(false, result, "isPresent - Test 2b"); | |
t.remove(2); | |
result = t.isPresent(2); | |
test(false, result, "isPresent - Test 2c"); | |
} | |
private static void testSize() { | |
BinarySearchTree<Integer> t = new BinarySearchTree<Integer>(); | |
test(0, t.size(), "size - Test 1a"); | |
t.add(1); | |
test(1, t.size(), "size - Test 2a"); | |
t = new BinarySearchTree<Integer>(); | |
for (int i = 0; i < 10; i++) { | |
t.add(i); | |
} | |
test(10, t.size(), "size - Test 3a"); | |
for (int i = 0; i < 5; i++) { | |
t.remove(i); | |
} | |
test(5, t.size(), "size - Test 4a"); | |
} | |
private static void testHeight() { | |
BinarySearchTree<Integer> t = new BinarySearchTree<Integer>(); | |
test(-1, t.height(), "height - Test 1a"); | |
t.add(0); | |
test(0, t.height(), "height - Test 2a"); | |
t = new BinarySearchTree<Integer>(); | |
for (int i = 0; i < 10; i++) { | |
t.add(i); | |
} | |
test(9, t.height(), "height - Test 3a"); | |
t = new BinarySearchTree<Integer>(); | |
t.add(5); | |
t.add(6); | |
t.add(7); | |
t.add(3); | |
t.add(4); | |
t.add(-1); | |
t.add(0); | |
test(3, t.height(), "height - Test 4a"); | |
} | |
private static void testGetAll() { | |
BinarySearchTree<Integer> t = new BinarySearchTree<Integer>(); | |
ArrayList<Integer> nodes = new ArrayList<Integer>(); | |
nodes = arrayToList(new int[]{}); | |
test(t.getAll(), nodes, "getAll - Test 1a"); | |
t.add(1); | |
nodes = arrayToList(new int[]{1}); | |
test(t.getAll(), nodes, "getAll - Test 2a"); | |
nodes = arrayToList(new int[]{0, 1, 2, 3, 4}); | |
for (int i = 0; i < 5; i++) { | |
t.add(i); | |
} | |
test(t.getAll(), nodes, "getAll - Test 3a"); | |
} | |
private static void testMax() { | |
BinarySearchTree<Integer> t = new BinarySearchTree<Integer>(); | |
t.add(10); | |
test(10, t.max(), "max - Test 1a"); | |
t = new BinarySearchTree<Integer>(); | |
t.add(5); | |
t.add(6); | |
t.add(7); | |
t.add(3); | |
t.add(4); | |
t.add(-1); | |
t.add(0); | |
test(7, t.max(), "max - Test 2a"); | |
t = new BinarySearchTree<Integer>(); | |
for (int i = 0; i > -5; i--) { | |
t.add(i); | |
} | |
test(0, t.max(), "max - Test 3a"); | |
} | |
private static void testMin() { | |
BinarySearchTree<Integer> t = new BinarySearchTree<Integer>(); | |
t.add(10); | |
test(10, t.min(), "min - Test 1a"); | |
t = new BinarySearchTree<Integer>(); | |
t.add(5); | |
t.add(6); | |
t.add(7); | |
t.add(3); | |
t.add(4); | |
t.add(-1); | |
t.add(0); | |
test(-1, t.min(), "min - Test 2a"); | |
t = new BinarySearchTree<Integer>(); | |
for (int i = 0; i < 5; i++) { | |
t.add(i); | |
} | |
test(0, t.min(), "min - Test 3a"); | |
} | |
private static void testIterativeAdd() { | |
BinarySearchTree<Integer> t = new BinarySearchTree<Integer>(); | |
ArrayList<Integer> nodes; | |
boolean result; | |
result = t.iterativeAdd(1); | |
nodes = arrayToList(new int[]{1}); | |
test(1, t.size(), "iterativeAdd - Test 1a"); | |
test(true, result, "iterativeAdd - Test 1b"); | |
test(t.getAll(), nodes, "iterativeAdd - Test 1c"); | |
result = t.iterativeAdd(0); | |
nodes = arrayToList(new int[]{0, 1}); | |
test(2, t.size(), "iterativeAdd - Test 2a"); | |
test(true, result, "iterativeAdd - Test 2b"); | |
test(t.getAll(), nodes, "iterativeAdd - Test 2c"); | |
result = t.iterativeAdd(2); | |
nodes = arrayToList(new int[]{0, 1, 2}); | |
test(3, t.size(), "iterativeAdd - Test 3a"); | |
test(true, result, "iterativeAdd - Test 3b"); | |
test(t.getAll(), nodes, "iterativeAdd - Test 3c"); | |
t = new BinarySearchTree<Integer>(); | |
for (int i = 0; i < 10; i++) { | |
result = t.iterativeAdd(0); | |
} | |
test(1, t.size(), "iterativeAdd - Test 4a"); | |
test(false, result, "iterativeAdd - Test 4b"); | |
} | |
private static void testGet() { | |
BinarySearchTree<Integer> t = new BinarySearchTree<Integer>(); | |
t.add(10); | |
test(10, t.get(0), "get - Test 1a"); | |
t = new BinarySearchTree<Integer>(); | |
t.add(5); | |
t.add(6); | |
t.add(7); | |
t.add(3); | |
t.add(4); | |
t.add(-1); | |
t.add(0); | |
test(-1, t.get(0), "get - Test 2a"); | |
test(0, t.get(1), "get - Test 2b"); | |
test(4, t.get(3), "get - Test 2c"); | |
test(6, t.get(5), "get - Test 2d"); | |
test(7, t.get(6), "get - Test 2e"); | |
} | |
private static void testGetAllLessThan() { | |
BinarySearchTree<Integer> t = new BinarySearchTree<Integer>(); | |
ArrayList<Integer> nodes = new ArrayList<Integer>(); | |
t.add(5); | |
t.add(6); | |
t.add(7); | |
t.add(3); | |
t.add(4); | |
t.add(-1); | |
t.add(0); | |
nodes = arrayToList(new int[]{-1, 0, 3}); | |
test(t.getAllLessThan(4), nodes, "getAllLessThan - Test 1a"); | |
nodes = arrayToList(new int[]{}); | |
test(t.getAllLessThan(-1), nodes, "getAllLessThan - Test 2a"); | |
nodes = arrayToList(new int[]{-1, 0, 3, 4, 5, 6, 7}); | |
test(t.getAllLessThan(8), nodes, "getAllLessThan - Test 3a"); | |
} | |
private static void testGetAllGreaterThan() { | |
BinarySearchTree<Integer> t = new BinarySearchTree<Integer>(); | |
ArrayList<Integer> nodes = new ArrayList<Integer>(); | |
t.add(5); | |
t.add(6); | |
t.add(7); | |
t.add(3); | |
t.add(4); | |
t.add(-1); | |
t.add(0); | |
nodes = arrayToList(new int[]{5, 6, 7}); | |
test(t.getAllGreaterThan(4), nodes, "getAllGreaterThan - Test 1a"); | |
nodes = arrayToList(new int[]{}); | |
test(t.getAllGreaterThan(7), nodes, "getAllGreaterThan - Test 2a"); | |
nodes = arrayToList(new int[]{-1, 0, 3, 4, 5, 6, 7}); | |
test(t.getAllGreaterThan(-2), nodes, "getAllGreaterThan - Test 3a"); | |
} | |
private static void testNumNodesAtDepth() { | |
BinarySearchTree<Integer> t = new BinarySearchTree<Integer>(); | |
test(0, t.numNodesAtDepth(0), "numNodesAtDepth - Test 1a"); | |
test(0, t.numNodesAtDepth(10), "numNodesAtDepth - Test 1b"); | |
t.add(5); | |
t.add(3); | |
t.add(6); | |
test(1, t.numNodesAtDepth(0), "numNodesAtDepth - Test 2a"); | |
test(2, t.numNodesAtDepth(1), "numNodesAtDepth - Test 2b"); | |
t = new BinarySearchTree<Integer>(); | |
t.add(5); | |
t.add(6); | |
t.add(7); | |
t.add(3); | |
t.add(4); | |
t.add(-1); | |
t.add(0); | |
test(1, t.numNodesAtDepth(0), "numNodesAtDepth - Test 3a"); | |
test(2, t.numNodesAtDepth(1), "numNodesAtDepth - Test 3b"); | |
test(3, t.numNodesAtDepth(2), "numNodesAtDepth - Test 3c"); | |
test(1, t.numNodesAtDepth(3), "numNodesAtDepth - Test 3d"); | |
test(0, t.numNodesAtDepth(4), "numNodesAtDepth - Test 3e"); | |
} | |
public static void main(String[] args) { | |
// add | |
testAdd(); | |
// remove | |
testRemove(); | |
// isPresent | |
testIsPresent(); | |
// size | |
testSize(); | |
// height | |
testHeight(); | |
// getAll | |
testGetAll(); | |
// max | |
testMax(); | |
// min | |
testMin(); | |
// iterativeAdd | |
testIterativeAdd(); | |
// get | |
testGet(); | |
// getAllLessThan | |
testGetAllLessThan(); | |
// getAllGreaterThan | |
testGetAllGreaterThan(); | |
// numNodesAtDepth | |
testNumNodesAtDepth(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment