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 { t131 = new EditTree(s1); t132 = new EditTree(s2); t133 = new EditTree(s3); t134 = new EditTree(s4); t135 = new EditTree(s5); t136 = new EditTree(s1); t137 = new EditTree(s2); t138 = new EditTree(s3); t139 = new EditTree(s4); t130 = new EditTree(s5); } 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 { } 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. // 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) public void testToString_13() { // test non-modified trees assertEquals(s1, t131.toString()); assertEquals(s2, t132.toString()); assertEquals(s3, t133.toString()); assertEquals(s4, t134.toString()); assertEquals(s5, t135.toString()); points +=1; System.out.println(" points = " + points); } @Test(timeout = 1000) public void testConstructors_13() { // make sure string constructor works assertEquals(new EditTree(s1).toString(), s1); assertEquals(new EditTree(s2).toString(), s2); assertEquals(new EditTree(s3).toString(), s3); assertEquals(new EditTree(s4).toString(), s4); assertEquals(new EditTree(s5).toString(), s5); // test single-character constructor assertEquals(new EditTree('!').toString(), "!"); // test tree-copier constructor EditTree testTree = new EditTree(t135); assertEquals(testTree.toString(), t135.toString()); assertEquals(testTree.size(), t135.size()); assertEquals(testTree.height(), t135.height()); assertEquals(testTree.get(65), t135.get(65)); points +=1; System.out.println(" points = " + points); } @Test(timeout = 1000) public void testSize3_i() { // i is for instructor // one second ti3 = new EditTree(s1); assertEquals(12, ti3.size()); points +=1; System.out.println(" points = " + points); // only // incremented // if // test // passes } @Test(timeout = 1000) public void testAdd_13() { // test add at index t131.add('z', 7); t132.add('h', 12); t133.add('6', 23); t134.add('8', 59); t134.add('9', 60); t135.add('j', 68); t135.add(' ', 68); assertEquals("short sztring", t131.toString()); assertEquals("a moderatelyh short string", t132.toString()); assertEquals("This string is not as s6hort as the others", t133.toString()); assertEquals( "This is a great, big, juicy, longer-than-the-others, string89", t134.toString()); assertEquals( "You can't always get the strings you want, but if you try sometimes, j you just might find you get the strings you need.", t135.toString()); // test add at end t131.add('w'); t132.add('o'); t133.add('r'); t134.add('k'); t135.add('!'); assertEquals("short sztringw", t131.toString()); assertEquals("a moderatelyh short stringo", t132.toString()); assertEquals("This string is not as s6hort as the othersr", t133.toString()); assertEquals( "This is a great, big, juicy, longer-than-the-others, string89k", t134.toString()); assertEquals( "You can't always get the strings you want, but if you try sometimes, j you just might find you get the strings you need.!", t135.toString()); points +=1; System.out.println(" points = " + points); } @Test(timeout = 1000) public void testDelete_13() { // test deleting at index assertEquals('t', t131.delete(7)); assertEquals(' ', t132.delete(12)); assertEquals('T', t133.delete(0)); assertEquals(',', t134.delete(15)); assertEquals(',', t134.delete(19)); assertEquals(',', t134.delete(25)); assertEquals('.', t135.delete(117)); assertEquals("short sring", t131.toString()); assertEquals("a moderatelyshort string", t132.toString()); assertEquals("his string is not as short as the others", t133.toString()); assertEquals( "This is a great big juicy longer-than-the-others, string", t134.toString()); assertEquals( "You can't always get the strings you want, but if you try sometimes, you just might find you get the strings you need", t135.toString()); // test deleting from index to a certain length away within the string assertEquals(new EditTree("short string"), t136.delete(0, 12)); assertEquals(new EditTree("rate"), t137.delete(5, 4)); assertEquals(new EditTree(""), t138.delete(4, 0)); assertEquals(new EditTree(", longer-than-the-others,"), t139.delete(27, 25)); assertEquals(new EditTree("You"), t130.delete(0, 3)); assertEquals("", t136.toString()); assertEquals("a modely short string", t137.toString()); assertEquals("This string is not as short as the others", t138.toString()); assertEquals("This is a great, big, juicy string", t139.toString()); assertEquals( " can't always get the strings you want, but if you try sometimes, you just might find you get the strings you need.", t130.toString()); points +=1; System.out.println(" points = " + points); } @Test(timeout = 1000) public void testConcatenate_13() { // a few tree concatenations t131.concatenate(t132); t133.concatenate(t134); t135.concatenate(t130); // check to see if "other" trees are made to be blank assertEquals("", t132.toString()); assertEquals("", t134.toString()); assertEquals("", t130.toString()); // make sure that trees are properly concatenated assertEquals("short stringa moderately short string", t131.toString()); assertEquals( "This string is not as short as the othersThis is a great, big, juicy, longer-than-the-others, string", t133.toString()); assertEquals( "You can't always get the strings you want, but if you try sometimes, you just might find you get the strings you need.You can't always get the strings you want, but if you try sometimes, you just might find you get the strings you need.", t135.toString()); // make sure concatenating a blank tree doesn't add anything t131.concatenate(t132); assertEquals("", t132.toString()); assertEquals("short stringa moderately short string", t131.toString()); points +=1; System.out.println(" points = " + points); // only // incremented // if // test // passes } @Test(timeout = 1000) public void testSplit_13() { // make sure split function returns correct tree (assumes one-based // indexing) assertEquals(new EditTree("short string"), t131.split(1)); assertEquals(new EditTree("rately short string"), t132.split(6)); assertEquals(new EditTree("s"), t133.split(41)); assertEquals(new EditTree( "at, big, juicy, longer-than-the-others, string"), t132.split(13)); // make sure correct letters are left in tree after it is split assertEquals("", t131.toString()); assertEquals("a mode", t132.toString()); assertEquals("This string is not as short as the other", t133.toString()); assertEquals("This is a gre", t132.toString()); points +=1; System.out.println(" points = " + points); } @Test(timeout = 1000) public void testHeight_13() { // test heights of non-modified trees assertEquals(3, t131.height()); assertEquals(4, t132.height()); assertEquals(5, t133.height()); assertEquals(5, t134.height()); assertEquals(6, t135.height()); points +=1; System.out.println(" points = " + points); } @Test(timeout = 1000) public void testGet_13() { // tests simple get operations (assumes 1-based indexing) assertEquals('s', t131.get(7)); assertEquals('Y', t135.get(1)); assertEquals('g', t132.get(25)); points +=1; System.out.println(" points = " + points); } @Test(timeout = 1000) public void testCheckRanks_13() { // tests assume that tree is correctly balanced after construction assertTrue(t131.checkRanks()); assertTrue(t132.checkRanks()); assertTrue(t133.checkRanks()); assertTrue(t134.checkRanks()); assertTrue(t135.checkRanks()); // false for empty tree assertFalse(new EditTree().checkRanks()); points +=1; System.out.println(" points = " + points); } @Test(timeout = 1000) public void testIsHeightBalanced_13() { // code assumes that rebalancing is implemented in the middle of // adds/deletes assertTrue(t131.isHeightBalanced()); assertTrue(t132.isHeightBalanced()); assertTrue(t133.isHeightBalanced()); assertTrue(t134.isHeightBalanced()); assertTrue(t135.isHeightBalanced()); // construct a non-height balanced tree node by node and make sure // method returns false Node root = new Node('r'); Node left1 = new Node('l'); Node left2 = new Node('L'); Node left3 = new Node('e'); Node right = new Node('g'); root.left = left1; root.right = right; left1.left = left2; left2.left = left3; assertFalse(root.isHeightBalanced()); points +=1; System.out.println(" points = " + points); } @Test(timeout = 1000) public void testFind_13() { // try to find strings that don't exist assertEquals(-1, t131.find("LALALALALALALALA")); assertEquals(-1, t132.find("ASDASDASDQAWHDAWDADAWDWADA")); assertEquals(-1, t133.find("This", 7)); // find strings that do in fact exist (assume 1-based indexing for // position) assertEquals(1, t131.find("short")); assertEquals(3,t132.find("moderately ")); assertEquals(5, t133.find(" ", 1)); points +=1; System.out.println(" points = " + points); } }