From 86cac292cb463d50872d3f8c3cae28c446da2389 Mon Sep 17 00:00:00 2001 From: Ari Trachtenberg Date: Mon, 1 Apr 2024 22:30:02 -0400 Subject: [PATCH] Added test cases --- src/edu/bu/ec504/hw3/Tests/TestBSTSet.java | 47 +++++++++++++++++++ .../TestExtendedBST/TestParentPointer.java | 41 ++++++++++++++++ .../Tests/TestExtendedBST/TestRotation.java | 29 ++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 src/edu/bu/ec504/hw3/Tests/TestBSTSet.java create mode 100644 src/edu/bu/ec504/hw3/Tests/TestExtendedBST/TestParentPointer.java create mode 100644 src/edu/bu/ec504/hw3/Tests/TestExtendedBST/TestRotation.java diff --git a/src/edu/bu/ec504/hw3/Tests/TestBSTSet.java b/src/edu/bu/ec504/hw3/Tests/TestBSTSet.java new file mode 100644 index 0000000..3d50afb --- /dev/null +++ b/src/edu/bu/ec504/hw3/Tests/TestBSTSet.java @@ -0,0 +1,47 @@ +package edu.bu.ec504.hw3.Tests; + +import edu.bu.ec504.hw3.bst.BSTSet; + +import java.util.Arrays; +import java.util.Set; + +public class TestBSTSet { + + public static void main(String[] args) { + Set strSet = new BSTSet(); + String strs[] = new String[]{"Alpha","Beta","Gamma","Delta","Iota"}; + strSet.addAll(Arrays.asList(strs)); + + // check size + System.out.print("Testing size"); + if (strSet.size()!=strs.length) { + throw new RuntimeException("BSTSet size is "+strSet.size()+" and not the expected "+strs.length); + } + System.out.println("... ok"); + + // use contains + System.out.print("Testing contains"); + if (!strSet.contains("Alpha") + || !strSet.contains("Beta") + || !strSet.contains("Delta") + || strSet.contains("Epsilon")) + throw new RuntimeException("BSTSet not functioning as a set - provides incorrect responses."); + System.out.println("... ok"); + + // use set iteration + System.out.print("Testing iteration"); + boolean found=false; + for (String str: strSet) { + if (str.equals("Iota")) + found=true; + } + if (!found) { + throw new RuntimeException("Set iteration failed."); + } + System.out.println("... ok"); + } + + + + +} diff --git a/src/edu/bu/ec504/hw3/Tests/TestExtendedBST/TestParentPointer.java b/src/edu/bu/ec504/hw3/Tests/TestExtendedBST/TestParentPointer.java new file mode 100644 index 0000000..72584c7 --- /dev/null +++ b/src/edu/bu/ec504/hw3/Tests/TestExtendedBST/TestParentPointer.java @@ -0,0 +1,41 @@ +package edu.bu.ec504.hw3.Tests.TestExtendedBST; + +import edu.bu.ec504.hw3.bst.ExtendedBSTSet; + +import java.util.Arrays; + +public class TestParentPointer { + + /** + * @param node The root of thet ree to search. + * @return The number of null parents in the nodes of the subtree rooted at node. + */ + private static int numNullParents(ExtendedBSTSet node) { + if (node == null) + return 0; + + return (node.getParent() == null ? 1 : 0) // me + + numNullParents((ExtendedBSTSet) node.getLeftChild()) // left child + + numNullParents((ExtendedBSTSet) node.getRightChild()); // right child + } + public static void main(String[] args) { + ExtendedBSTSet intSet = new ExtendedBSTSet<>(); + Integer ints[] = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + intSet.addAll(Arrays.asList(ints)); + System.out.println("The tree:\n"+intSet); + + // check root node + System.out.print("Checking root node"); + if (intSet.getKey()!=0 || intSet.getParent()!=null) + throw new RuntimeException("Root node is misformed."); + System.out.println("... ok"); + + // check that all but one element has a parent + System.out.print("Checking the number of null parents in the tree"); + if (numNullParents(intSet)!=1) + throw new RuntimeException("Too many null parents found: "+numNullParents(intSet)+" vs. 1"); + System.out.println("... ok"); + } + + +} diff --git a/src/edu/bu/ec504/hw3/Tests/TestExtendedBST/TestRotation.java b/src/edu/bu/ec504/hw3/Tests/TestExtendedBST/TestRotation.java new file mode 100644 index 0000000..e6fb376 --- /dev/null +++ b/src/edu/bu/ec504/hw3/Tests/TestExtendedBST/TestRotation.java @@ -0,0 +1,29 @@ +package edu.bu.ec504.hw3.Tests.TestExtendedBST; + +import edu.bu.ec504.hw3.bst.BST.BSTRotation; +import edu.bu.ec504.hw3.bst.BST.BSTRotation.RotationType; +import edu.bu.ec504.hw3.bst.ExtendedBSTSet; + +import java.util.Arrays; + +public class TestRotation { + + public static void main(String[] args) { + ExtendedBSTSet intSet = new ExtendedBSTSet<>(); + Integer ints[] = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + intSet.addAll(Arrays.asList(ints)); + System.out.println("Original tree:\n"+intSet); + + for (int ii=0; ii<10; ii++) + intSet.rotate(new BSTRotation<>(ii, RotationType.ZAG)); + System.out.println("Tree after ZAG on each node:\n"+intSet); + if (intSet.getKey()!=9) + throw new RuntimeException("ZAG rotations did not produce the correct root."); + + for (int ii=9; ii>=0; ii--) + intSet.rotate(new BSTRotation<>(ii, RotationType.ZIG)); + System.out.println("Tree after ZIG on each node, in reverse order:\n"+intSet); + if (intSet.getKey()!=0) + throw new RuntimeException("ZIG rotations did not produce the correct root."); + } +} -- GitLab