Commit 86cac292 authored by Ari Trachtenberg's avatar Ari Trachtenberg
Browse files

Added test cases

parent 313c8709
Loading
Loading
Loading
Loading
+47 −0
Original line number Diff line number Diff line
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<String> strSet = new BSTSet<String>();
        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");
    }




}
+41 −0
Original line number Diff line number Diff line
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 <code>node</code>.
   */
  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<Integer> 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");
  }


}
+29 −0
Original line number Diff line number Diff line
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<Integer> 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.");
  }
}