package editortrees; import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; public class EditTreeTest { static int points = 0; // Feel free to use these strings in your test cases, or not. static String s1 = "short string", s2 = "a moderately short string", s3 = "This string is not as short as the others", s4 = "This is a great, big, juicy, longer-than-the-others, string", s5 = "You can't always get the strings you want, " + "but if you try sometimes, you just might find you get the strings you need."; /** * The variables whose first two digits are your team number are available * for your team's test cases to use. If you want to intiialize them once * and for all instead of in individual test cases, put that code in * setUpXY, where XY is your team number. You may use other variables, of * course, but they should be local to your methods. */ EditTree t110, t111, t112, t113, t114, t115, t116, t117, t118, t119; EditTree t120, t121, t122, t123, t124, t125, t126, t127, t128, t129; EditTree t130, t131, t132, t133, t134, t135, t136, t137, t138, t139; EditTree t140, t141, t142, t143, t144, t145, t146, t147, t148, t149; EditTree t150, t151, t152, t153, t154, t155, t156, t157, t158, t159; EditTree t160, t161, t162, t163, t164, t165, t166, t167, t168, t169; EditTree t210, t211, t212, t213, t214, t215, t216, t217, t218, t219; EditTree t220, t221, t222, t223, t224, t225, t226, t227, t228, t229; EditTree t230, t231, t232, t233, t234, t235, t236, t237, t238, t239; EditTree t240, t241, t242, t243, t244, t245, t246, t247, t248, t249; EditTree t250, t251, t252, t253, t254, t255, t256, t257, t258, t259; EditTree t260, t261, t262, t263, t264, t265, t266, t267, t268, t269; EditTree ti0, ti1, ti2, ti3, ti4, ti5, ti6, ti7, ti8, ti9; // Do not modify this method @Before public void setUp() throws Exception { try { setUpInstructors(); } catch (Exception e) { System.out.println("Error in setUpInstructors:"); e.printStackTrace(); } try { setUp11(); } catch (Exception e) { System.out.println("Error in setUp11:"); e.printStackTrace(); } try { setUp12(); } catch (Exception e) { System.out.println("Error in setUp12:"); e.printStackTrace(); } try { setUp13(); } catch (Exception e) { System.out.println("Error in setUp13:"); e.printStackTrace(); } try { setUp14(); } catch (Exception e) { System.out.println("Error in setUp14:"); e.printStackTrace(); } try { setUp15(); } catch (Exception e) { System.out.println("Error in setUp15:"); e.printStackTrace(); } try { setUp16(); } catch (Exception e) { System.out.println("Error in setUp16:"); e.printStackTrace(); } try { setUp21(); } catch (Exception e) { System.out.println("Error in setUp21:"); e.printStackTrace(); } try { setUp22(); } catch (Exception e) { System.out.println("Error in setUp22:"); e.printStackTrace(); } try { setUp23(); } catch (Exception e) { System.out.println("Error in setUp23:"); e.printStackTrace(); } try { setUp24(); } catch (Exception e) { System.out.println("Error in setUp24:"); e.printStackTrace(); } try { setUp25(); } catch (Exception e) { System.out.println("Error in setUp25:"); e.printStackTrace(); } try { setUp26(); } catch (Exception e) { System.out.println("Error in setUp26:"); e.printStackTrace(); } } public void setUpInstructors() throws Exception { } public void setUp11() throws Exception { } public void setUp12() throws Exception { } public void setUp13() throws Exception { } public void setUp14() throws Exception { } public void setUp15() throws Exception { } public void setUp16() throws Exception { } public void setUp21() throws Exception { } public void setUp22() throws Exception { } public void setUp23() throws Exception { t230 = new EditTree(); t231 = new EditTree('a'); t232 = new EditTree('b'); t233 = new EditTree('d'); t234 = new EditTree('f'); t232.getRoot().left = new Node('c'); t233.getRoot().right = new Node('e'); t234.getRoot().left = new Node('g'); t234.getRoot().right = new Node('h'); EditTree t235, t236, t237, t238, t239; } public void setUp24() throws Exception { } public void setUp25() throws Exception { } public void setUp26() throws Exception { } // The name of each of your team's tests should end with an underscore // followed by your team number, // for example, testSize3_13 if you are on team 13. @Test(timeout = 1000) public void testAdd1_23() { this.t230.add('a'); assertEquals("a", this.t230.toString()); this.t230.add('b'); assertEquals("ab", this.t230.toString()); this.t230.add('c'); assertEquals("abc", this.t230.toString()); assertEquals(3, this.t230.size()); EditTreeTest.points++; } @Test(timeout = 1000) public void testAdd2_23() { this.t230.add('a', 0); assertEquals("a", this.t230.toString()); this.t230.add('b', 0); assertEquals("ba", this.t230.toString()); this.t230.add('c', 2); assertEquals("bac", this.t230.toString()); this.t230.add('d', 2); assertEquals("badc", this.t230.toString()); this.t230.add('e', 1); assertEquals("beadc", this.t230.toString()); assertEquals(5, this.t230.size()); EditTreeTest.points++; } @Test(timeout = 1000) public void testDelete1_23() { try { this.t230.delete(-1); fail("Failed to throw IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException exception) { // Do nothing. } try { this.t230.delete(1); fail("Failed to throw IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException exception) { // Do nothing. } // Tested IndexOutOfBounds this.t231.delete(0); assertEquals("", this.t231.toString()); this.t232.delete(0); assertEquals("c", this.t232.toString()); this.t234.delete(2); assertEquals("gf", this.t234.toString()); assertEquals(2, this.t234.size()); this.t234.delete(0); assertEquals("f", this.t234.toString()); assertEquals(1, this.t234.size()); EditTreeTest.points++; } @Test(timeout = 1000) public void testDelete2_23() { EditTree t1 = new EditTree('a'); t1.getRoot().left = new Node('b'); t1.getRoot().right = new Node('c'); t1.getRoot().left.left = new Node('d'); t1.delete(3, 1); assertEquals("dba", t1.toString()); assertTrue(t1.isHeightBalanced()); // tests the deletion of a single element that requires a single right // rotation EditTree t2 = new EditTree('a'); t2.getRoot().left = new Node('b'); t2.getRoot().right = new Node('c'); t2.getRoot().right.right = new Node('d'); t2.delete(0, 1); assertEquals("acd", t2.toString()); assertTrue(t2.isHeightBalanced()); // tests the deletion of a single element that requires a single left // rotation EditTree t3 = new EditTree('a'); t3.getRoot().left = new Node('b'); t3.getRoot().right = new Node('c'); t3.getRoot().left.right = new Node('d'); t3.delete(3, 1); assertEquals("bda", t3.toString()); assertTrue(t3.isHeightBalanced()); // tests the deletion of a single element that requires a double right // rotation EditTree t4 = new EditTree('a'); t4.getRoot().left = new Node('b'); t4.getRoot().right = new Node('c'); t4.getRoot().right.left = new Node('d'); t4.delete(0, 1); assertEquals("abc", t4.toString()); assertTrue(t4.isHeightBalanced()); // tests the deletion of a single element that requires a double left // rotation try { this.t230.delete(0, 1); fail("Should have thrown an exception."); } catch (IndexOutOfBoundsException exception) { } // tests to see if an exception is thrown when attempting to delete from // a null tree. try { this.t232.delete(1, 4); fail("Should have thrown an exception."); } catch (IndexOutOfBoundsException exception) { } // tests to see if an exception is thrown when attempting to delete out // of range try { this.t232.delete(-1, 4); fail("Should have thrown an exception."); } catch (IndexOutOfBoundsException exception) { } // tests to see if an exception is thrown when attempting to delete out // of range, with the starting position being negative EditTree t5 = new EditTree('d'); t5.getRoot().left = new Node('b'); t5.getRoot().right = new Node('f'); t5.getRoot().left.left = new Node('a'); t5.getRoot().left.right = new Node('c'); t5.getRoot().right.left = new Node('e'); t5.getRoot().right.right = new Node('g'); t5.delete(1, 2); assertEquals("adefg", t5.toString()); // tests a multi delete of two elements that does not requires any // rotation EditTree t6 = new EditTree('d'); t6.getRoot().left = new Node('b'); t6.getRoot().right = new Node('f'); t6.getRoot().left.left = new Node('a'); t6.getRoot().left.right = new Node('c'); t6.delete(0, 3); assertEquals("ac", t6.toString()); // tests a multi delete of three elements that does not requires any // rotation t5.delete(0, t5.size()); assertEquals("", t5.toString()); // tests the deletion of all elements in a tree this.t234.delete(1, 1); assertEquals("gh", this.t234.toString()); // tests deletion of the root in a three element tree EditTree t7 = new EditTree('a'); t7.getRoot().left = new Node('b'); t7.getRoot().right = new Node('c'); t7.getRoot().left.left = new Node('d'); t7.getRoot().left.right = new Node('e'); t7.getRoot().right.left = new Node('f'); t7.getRoot().right.right = new Node('g'); t7.getRoot().left.left.left = new Node('h'); t7.getRoot().left.left.right = new Node('i'); t7.getRoot().left.right.left = new Node('j'); t7.getRoot().left.right.right = new Node('k'); t7.getRoot().right.left.left = new Node('l'); t7.getRoot().right.left.right = new Node('m'); t7.getRoot().right.right.left = new Node('n'); t7.getRoot().right.right.right = new Node('o'); t7.delete(2, 7); assertTrue(t7.isHeightBalanced()); EditTree t8 = new EditTree('a'); t8.getRoot().left = new Node('b'); t8.getRoot().right = new Node('c'); t8.getRoot().left.left = new Node('d'); t8.getRoot().left.right = new Node('e'); t8.getRoot().right.left = new Node('f'); t8.getRoot().right.right = new Node('g'); t8.getRoot().left.left.left = new Node('h'); t8.getRoot().left.left.right = new Node('i'); t8.getRoot().left.right.left = new Node('j'); t8.getRoot().left.right.right = new Node('k'); t8.getRoot().right.left.left = new Node('l'); t8.getRoot().right.left.right = new Node('m'); t8.getRoot().right.right.left = new Node('n'); t8.getRoot().right.right.right = new Node('o'); t8.delete(3, 5); assertTrue(t8.isHeightBalanced()); EditTree t9 = new EditTree('a'); t9.getRoot().left = new Node('b'); t9.getRoot().right = new Node('c'); t9.getRoot().left.left = new Node('d'); t9.getRoot().left.right = new Node('e'); t9.getRoot().right.left = new Node('f'); t9.getRoot().right.right = new Node('g'); t9.getRoot().left.left.left = new Node('h'); t9.getRoot().left.left.right = new Node('i'); t9.getRoot().left.right.left = new Node('j'); t9.getRoot().left.right.right = new Node('k'); t9.getRoot().right.left.left = new Node('l'); t9.getRoot().right.left.right = new Node('m'); t9.getRoot().right.right.left = new Node('n'); t9.getRoot().right.right.right = new Node('o'); t9.delete(10, 4); assertTrue(t9.isHeightBalanced()); EditTreeTest.points++; } @Test(timeout = 1000) public void testConcatenate_23() { EditTree t1 = new EditTree(); this.t230.concatenate(t1); assertEquals("", this.t230.toString()); // Tested null-null concatenation this.t230.concatenate(this.t231); assertEquals("a", this.t230.toString()); // Tested null-filled concatenation this.t231.concatenate(t1); assertEquals("a", this.t231.toString()); // Tested filled-null concatenation EditTree t2 = new EditTree('z'); this.t230.concatenate(t2); assertEquals("az", this.t230.toString()); EditTree t3 = new EditTree('a'); t3.getRoot().left = new Node('b'); t3.getRoot().right = new Node('c'); t3.getRoot().left.left = new Node('d'); t3.getRoot().left.right = new Node('e'); t3.getRoot().right.left = new Node('f'); t3.getRoot().right.right = new Node('g'); EditTree t4 = new EditTree('h'); t4.getRoot().left = new Node('i'); t4.getRoot().right = new Node('j'); t4.getRoot().left.left = new Node('k'); t4.getRoot().left.right = new Node('l'); t4.getRoot().right.left = new Node('m'); t4.getRoot().right.right = new Node('n'); t3.concatenate(t4); assertEquals("dbeafcgkilhmjn", t3.toString()); assertEquals(14, t3.size()); assertTrue(t3.isHeightBalanced()); // Tested a larger tree. Made sure more information was accounted for. // Write a test that checks balancing if there is time. EditTreeTest.points++; } @Test(timeout = 1000) public void testSplit_23() { EditTree t1 = new EditTree('a'); t1.getRoot().left = new Node('b'); t1.getRoot().right = new Node('c'); t1.getRoot().left.left = new Node('d'); t1.getRoot().left.right = new Node('e'); t1.getRoot().right.left = new Node('f'); t1.getRoot().right.right = new Node('g'); t1.getRoot().left.left.left = new Node('h'); t1.getRoot().left.left.right = new Node('i'); t1.getRoot().left.right.left = new Node('j'); t1.getRoot().left.right.right = new Node('k'); t1.getRoot().right.left.left = new Node('l'); t1.getRoot().right.left.right = new Node('m'); t1.getRoot().right.right.left = new Node('n'); t1.getRoot().right.right.right = new Node('o'); EditTree t2 = t1.split(0); assertEquals(1, t1.size()); assertEquals("h", t1.toString()); assertEquals(14, t2.size()); assertEquals("dibjekalfmcngo", t2.toString()); assertTrue(t2.isHeightBalanced()); assertTrue(t1.isHeightBalanced()); EditTree t3 = new EditTree('a'); t3.getRoot().left = new Node('b'); t3.getRoot().right = new Node('c'); t3.getRoot().left.left = new Node('d'); t3.getRoot().left.right = new Node('e'); t3.getRoot().right.left = new Node('f'); t3.getRoot().right.right = new Node('g'); t3.getRoot().left.left.left = new Node('h'); t3.getRoot().left.left.right = new Node('i'); t3.getRoot().left.right.left = new Node('j'); t3.getRoot().left.right.right = new Node('k'); t3.getRoot().right.left.left = new Node('l'); t3.getRoot().right.left.right = new Node('m'); t3.getRoot().right.right.left = new Node('n'); t3.getRoot().right.right.right = new Node('o'); EditTree t4 = t3.split(15); assertEquals(15, t1.size()); assertEquals("hdibjekalfmcngo", t1.toString()); assertEquals(0, t2.size()); assertEquals("", t2.toString()); assertTrue(t2.isHeightBalanced()); assertTrue(t1.isHeightBalanced()); EditTree t5 = new EditTree('a'); t5.getRoot().left = new Node('b'); t5.getRoot().right = new Node('c'); t5.getRoot().left.left = new Node('d'); t5.getRoot().left.right = new Node('e'); t5.getRoot().right.left = new Node('f'); t5.getRoot().right.right = new Node('g'); t5.getRoot().left.left.left = new Node('h'); t5.getRoot().left.left.right = new Node('i'); t5.getRoot().left.right.left = new Node('j'); t5.getRoot().left.right.right = new Node('k'); t5.getRoot().right.left.left = new Node('l'); t5.getRoot().right.left.right = new Node('m'); t5.getRoot().right.right.left = new Node('n'); t5.getRoot().right.right.right = new Node('o'); EditTree t6 = t5.split(8); assertEquals(8, t1.size()); assertEquals("hdibjeka", t1.toString()); assertEquals(7, t2.size()); assertEquals("lfmcngo", t2.toString()); assertTrue(t2.isHeightBalanced()); assertTrue(t1.isHeightBalanced()); EditTreeTest.points++; } @Test(timeout = 1000) public void testGet1_23() { EditTree t1 = new EditTree('a'); t1.getRoot().left = new Node('b'); t1.getRoot().right = new Node('c'); t1.getRoot().left.left = new Node('d'); t1.getRoot().left.right = new Node('e'); t1.getRoot().right.left = new Node('f'); t1.getRoot().right.right = new Node('g'); t1.getRoot().left.left.left = new Node('h'); t1.getRoot().left.left.right = new Node('i'); t1.getRoot().left.right.left = new Node('j'); t1.getRoot().left.right.right = new Node('k'); t1.getRoot().right.left.left = new Node('l'); t1.getRoot().right.left.right = new Node('m'); t1.getRoot().right.right.left = new Node('n'); t1.getRoot().right.right.right = new Node('o'); try { t1.get(-1); fail("Failed to catch IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException exception) { // Do Nothing } try { t1.get(15); fail("Failed to catch IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException exception) { // Do Nothing } assertEquals("h", t1.get(0)); assertEquals("o", t1.get(15)); assertEquals("e", t1.get(5)); assertEquals("m", t1.get(10)); EditTreeTest.points++; } @Test(timeout = 1000) public void testGet2_23() { EditTree t1 = new EditTree('a'); t1.getRoot().left = new Node('b'); t1.getRoot().right = new Node('c'); t1.getRoot().left.left = new Node('d'); t1.getRoot().left.right = new Node('e'); t1.getRoot().right.left = new Node('f'); t1.getRoot().right.right = new Node('g'); t1.getRoot().left.left.left = new Node('h'); t1.getRoot().left.left.right = new Node('i'); t1.getRoot().left.right.left = new Node('j'); t1.getRoot().left.right.right = new Node('k'); t1.getRoot().right.left.left = new Node('l'); t1.getRoot().right.left.right = new Node('m'); t1.getRoot().right.right.left = new Node('n'); t1.getRoot().right.right.right = new Node('o'); try { t1.get(-1, 3); fail("Failed to catch IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException exception) { // Do Nothing } try { t1.get(10, 10); fail("Failed to catch IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException exception) { // Do Nothing } assertEquals("hdibjekalfmcngo", t1.get(0, 15)); assertEquals("o", t1.get(15, 1)); assertEquals("ekalfm", t1.get(5, 6)); assertEquals("mcg", t1.get(10, 3)); EditTreeTest.points++; } @Test(timeout = 1000) public void testHeight_23() { EditTree t1 = new EditTree('a'); t1.add('c'); t1.add('d'); assertEquals(2, t1.height()); EditTree t2 = new EditTree('r'); assertEquals(0, t2.height()); assertEquals(-1, this.t230.height()); EditTreeTest.points++; } @Test(timeout = 1000) public void testSize_23() { EditTree t1 = new EditTree('a'); t1.add('c'); t1.add('d'); assertEquals(3, t1.size()); EditTree t2 = new EditTree('r'); assertEquals(1, t2.size()); assertEquals(0, this.t230.size()); EditTreeTest.points++; } @Test(timeout = 1000) public void testToString_23() { EditTree t1 = new EditTree("abc"); assertEquals("abc", t1.toString()); // from the string constructor for editTree assertEquals("", this.t230.toString()); // null tree EditTree t2 = new EditTree('a'); assertEquals("a", t2.toString()); // one character tree EditTreeTest.points++; } @Test(timeout = 1000) public void testConstructor1_23() { EditTree t1 = new EditTree(""); assertEquals("", t1.toString()); EditTree t2 = new EditTree("a"); assertEquals("a", t2.toString()); EditTree t3 = new EditTree("abc"); assertEquals("abc", t3.toString()); EditTreeTest.points++; } @Test(timeout = 1000) public void testConstructor2_23() { EditTree t1 = new EditTree(); assertEquals("", t1.toString()); EditTreeTest.points++; } @Test(timeout = 1000) public void testConstructor3_23() { EditTree t1 = new EditTree('a'); t1.add('c'); t1.add('d'); EditTree t2 = new EditTree(t1); assertEquals(t1.toString(), t2.toString()); EditTreeTest.points++; } @Test(timeout = 1000) public void testIsHeightBalanced_23() { EditTree t1 = new EditTree('a'); t1.getRoot().right = new Node('b'); t1.getRoot().right.right = new Node('c'); t1.getRoot().right.right.right = new Node('d'); assertFalse(t1.isHeightBalanced()); // testing isHeight balanced on a purposely unbalanced tree EditTree t2 = new EditTree('a'); t1.add('c'); t1.add('d'); assertTrue(t2.isHeightBalanced()); // testing with a (hopefully) balanced tree EditTreeTest.points++; } // @Test(timeout = 1000) // public void testCheckRanks_23() { // //Not necessary. Depends completely on individual implementation. // } @Test(timeout = 1000) public void testSingleLeft_23() { EditTree t1 = new EditTree('a'); t1.add('b'); t1.add('c'); assertEquals("abc", t1.toString()); assertEquals(1, t1.height()); // Single rotate left about root t1.add('d'); t1.add('e'); assertEquals("abcde", t1.toString()); assertEquals(2, t1.height()); // Second rotate about a non-root node EditTreeTest.points++; } @Test(timeout = 1000) public void testSingleRight_23() { EditTree t1 = new EditTree('a'); t1.add('b', 0); t1.add('c', 0); assertEquals("cba", t1.toString()); assertEquals(1, t1.height()); // Single rotate right about root t1.add('d', 0); t1.add('e', 0); assertEquals("edcba", t1.toString()); assertEquals(2, t1.height()); // Second rotate about a non-root node EditTreeTest.points++; } @Test(timeout = 1000) public void testDoubleLeft_23() { EditTree t1 = new EditTree('a'); t1.add('b', 1); t1.add('c', 1); assertEquals("acb", t1.toString()); assertEquals(1, t1.height()); // double rotate left about root t1.add('d', 3); t1.add('e', 3); assertEquals("acbed", t1.toString()); assertEquals(2, t1.height()); // Second rotate about a non-root node EditTreeTest.points++; } @Test(timeout = 1000) public void testDoubleRight_23() { EditTree t1 = new EditTree('a'); t1.add('b', 0); t1.add('c', 1); assertEquals("bca", t1.toString()); assertEquals(1, t1.height()); // double rotate right about root t1.add('d', 0); t1.add('e', 1); assertEquals("debca", t1.toString()); assertEquals(2, t1.height()); // Second rotate about a non-root node EditTreeTest.points++; } // Make sure that each of your tests has a timeout. // The timeout should be 1 or 2 seconds unless your // test involves VERY complicated operations. // Each of your tests should be worth one point. // A sample test to remind you of the format: @Test(timeout = 1000) // one second public void testSize3_i() { // i is for instructor ti3 = new EditTree(s1); assertEquals(12, ti3.size()); points += 1; // only incremented if test passes. } }