Guest User

Untitled

a guest
Sep 7th, 2016
492
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.27 KB | None | 0 0
  1. package dictionary;
  2.  
  3. import static org.junit.Assert.assertEquals;
  4.  
  5. import java.util.Comparator;
  6.  
  7. import org.junit.Before;
  8. import org.junit.Test;
  9.  
  10. public class RBTreeWParentDictionaryTest {
  11.   private class IntegerComparator implements Comparator<Integer> {
  12.     public int compare(Integer i1, Integer i2) {
  13.       return i1.compareTo(i2);
  14.     }
  15.   }
  16.  
  17.   RBTreeWParentDictionary<Integer, String> startRBT;
  18.   IntegerComparator                        comparator;
  19.  
  20.   @Before
  21.   public void setUp() {
  22.     comparator = new IntegerComparator();
  23.     startRBT = new RBTreeWParentDictionary<Integer, String>(comparator);
  24.    
  25.     startRBT.root = new RBTreeWParentNode<Integer, String>(10, "10");
  26.     startRBT.root.setBlack();
  27.   }
  28.  
  29. /*
  30.  * Used for first tests, now removed.
  31.   @Test
  32.   public void rotateLeftTest() {
  33.     startRBT.root.setRed();
  34.     startRBT.root.setRight(new RBTreeWParentNode<Integer, String>(30, "30"));
  35.     startRBT.root.getRight()
  36.         .setRight(new RBTreeWParentNode<Integer, String>(40, "40"));
  37.    
  38.     RBTreeWParentDictionary<Integer, String> testRbt = new RBTreeWParentDictionary<Integer, String>(
  39.         comparator);
  40.     testRbt.root = new RBTreeWParentNode<Integer, String>(30, "30");
  41.     testRbt.root.setRed();
  42.     testRbt.root.setLeft(new RBTreeWParentNode<Integer, String>(10, "10"));
  43.     testRbt.root.setRight(new RBTreeWParentNode<Integer, String>(40, "40"));
  44.    
  45.     startRBT.testRotateLeft();
  46.     assertEquals(startRBT, testRbt);
  47.    
  48.   }
  49.  
  50.   @Test
  51.   public void rotateRightTest() {
  52.     startRBT.root.setRed();
  53.     startRBT.root.setLeft(new RBTreeWParentNode<Integer, String>(9, "9"));
  54.     startRBT.root.getLeft()
  55.         .setLeft(new RBTreeWParentNode<Integer, String>(8, "8"));
  56.    
  57.     RBTreeWParentDictionary<Integer, String> testRbt = new RBTreeWParentDictionary<Integer, String>(
  58.         comparator);
  59.     testRbt.root = new RBTreeWParentNode<Integer, String>(9, "9");
  60.     testRbt.root.setRed();
  61.     testRbt.root.setLeft(new RBTreeWParentNode<Integer, String>(8, "8"));
  62.     testRbt.root.setRight(new RBTreeWParentNode<Integer, String>(10, "10"));
  63.    
  64.     startRBT.testRotateRight();
  65.     assertEquals(startRBT, testRbt);
  66.   }*/
  67.  
  68.   // Testing case -1
  69.   @Test
  70.   public void testInsertInRoot() {
  71.     RBTreeWParentDictionary<Integer, String> testRbt = new RBTreeWParentDictionary<Integer, String>(
  72.         comparator);
  73.     testRbt.insert(10, "10");
  74.    
  75.     assertEquals(startRBT, testRbt);
  76.   }
  77.  
  78.   // Testing case 0
  79.   @Test
  80.   public void testInsertLeft() {
  81.     startRBT.root.setLeft(new RBTreeWParentNode<Integer, String>(1, "1"));
  82.    
  83.     RBTreeWParentDictionary<Integer, String> testRbt = new RBTreeWParentDictionary<Integer, String>(
  84.         comparator);
  85.     testRbt.insert(10, "10");
  86.     testRbt.insert(1, "1");
  87.    
  88.     assertEquals(startRBT, testRbt);
  89.   }
  90.  
  91.   @Test
  92.   public void testInsertRight() {
  93.     startRBT.root.setRight(new RBTreeWParentNode<Integer, String>(30, "30"));
  94.    
  95.     RBTreeWParentDictionary<Integer, String> testRbt = new RBTreeWParentDictionary<Integer, String>(
  96.         comparator);
  97.     testRbt.insert(10, "10");
  98.     testRbt.insert(30, "30");
  99.    
  100.     assertEquals(startRBT, testRbt);
  101.   }
  102.  
  103.   @Test
  104.   public void testInsertTwoSons() {
  105.     startRBT.root.setRight(new RBTreeWParentNode<Integer, String>(30, "30"));
  106.     startRBT.root.setLeft(new RBTreeWParentNode<Integer, String>(1, "1"));
  107.    
  108.     RBTreeWParentDictionary<Integer, String> testRbt = new RBTreeWParentDictionary<Integer, String>(
  109.         comparator);
  110.     testRbt.insert(10, "10");
  111.     testRbt.insert(30, "30");
  112.     testRbt.insert(1, "1");
  113.    
  114.     assertEquals(startRBT, testRbt);
  115.   }
  116.  
  117.   // Testing case 1
  118.   public void testInsertLeftNephew() {
  119.     startRBT.root.setRight(new RBTreeWParentNode<Integer, String>(30, "30"));
  120.     startRBT.root.setLeft(new RBTreeWParentNode<Integer, String>(5, "5"));
  121.     startRBT.root.getLeft().setBlack();
  122.     startRBT.root.getRight().setBlack();
  123.     startRBT.root.getLeft()
  124.         .setLeft(new RBTreeWParentNode<Integer, String>(4, "4"));
  125.    
  126.     RBTreeWParentDictionary<Integer, String> testRbt = new RBTreeWParentDictionary<Integer, String>(
  127.         comparator);
  128.     testRbt.insert(10, "10");
  129.     testRbt.insert(30, "30");
  130.     testRbt.insert(5, "5");
  131.     testRbt.insert(4, "4");
  132.    
  133.     assertEquals(startRBT, testRbt);
  134.   }
  135.  
  136.   @Test
  137.   public void testInsertRightNephew() {
  138.     startRBT.root.setRight(new RBTreeWParentNode<Integer, String>(30, "30"));
  139.     startRBT.root.setLeft(new RBTreeWParentNode<Integer, String>(5, "5"));
  140.     startRBT.root.getLeft().setBlack();
  141.     startRBT.root.getRight().setBlack();
  142.     startRBT.root.getRight()
  143.         .setRight(new RBTreeWParentNode<Integer, String>(35, "35"));
  144.    
  145.     RBTreeWParentDictionary<Integer, String> testRbt = new RBTreeWParentDictionary<Integer, String>(
  146.         comparator);
  147.     testRbt.insert(10, "10");
  148.     testRbt.insert(30, "30");
  149.     testRbt.insert(5, "5");
  150.     testRbt.insert(35, "35");
  151.    
  152.     assertEquals(startRBT, testRbt);
  153.   }
  154.  
  155.   // Testing case 2
  156.   @Test
  157.   public void testInsertLeftNephewLeftNoUncle() {
  158.     startRBT.root.setKey(5);
  159.     startRBT.root.setValue("5");
  160.     startRBT.root.setBlack();
  161.     startRBT.root.setLeft(new RBTreeWParentNode<Integer, String>(2, "2"));
  162.     startRBT.root.setRight(new RBTreeWParentNode<Integer, String>(10,"10"));
  163.    
  164.     RBTreeWParentDictionary<Integer, String> testRbt = new RBTreeWParentDictionary<Integer, String>(
  165.         comparator);
  166.     testRbt.insert(10, "10");
  167.     testRbt.insert(5, "5");
  168.     testRbt.insert(2, "2");
  169.    
  170.     assertEquals(startRBT, testRbt);
  171.   }
  172.  
  173.   @Test
  174.   public void testInsertRightNephewRightNoUncle() {
  175.     startRBT.root.setKey(20);
  176.     startRBT.root.setValue("20");
  177.     startRBT.root.setBlack();
  178.     startRBT.root.setLeft(new RBTreeWParentNode<Integer, String>(10, "10"));
  179.     startRBT.root.setRight(new RBTreeWParentNode<Integer, String>(30,"30"));
  180.    
  181.     RBTreeWParentDictionary<Integer, String> testRbt = new RBTreeWParentDictionary<Integer, String>(
  182.         comparator);
  183.     testRbt.insert(10, "10");
  184.     testRbt.insert(20, "20");
  185.     testRbt.insert(30, "30");
  186.    
  187.     assertEquals(startRBT, testRbt);
  188.   }
  189.  
  190.   // Testing case 3
  191.   @Test
  192.   public void testInsertLeftNephewRightNoUncle() {
  193.     startRBT.root.setKey(5);
  194.     startRBT.root.setValue("5");  
  195.     startRBT.root.setBlack();
  196.     startRBT.root.setLeft(new RBTreeWParentNode<Integer, String>(3, "3"));
  197.     startRBT.root.setRight(new RBTreeWParentNode<Integer, String>(10,"10"));
  198.    
  199.     RBTreeWParentDictionary<Integer, String> testRbt = new RBTreeWParentDictionary<Integer, String>(
  200.         comparator);
  201.     testRbt.insert(10, "10");
  202.     testRbt.insert(3, "3");
  203.     testRbt.insert(5, "5");
  204.    
  205.     assertEquals(startRBT, testRbt);
  206.   }
  207.  
  208.   @Test
  209.   public void testInsertRightNephewLeftNoUncle() {
  210.     startRBT.root.setKey(15);
  211.     startRBT.root.setValue("15");  
  212.     startRBT.root.setBlack();
  213.     startRBT.root.setLeft(new RBTreeWParentNode<Integer, String>(10, "10"));
  214.     startRBT.root.setRight(new RBTreeWParentNode<Integer, String>(20,"20"));
  215.    
  216.     RBTreeWParentDictionary<Integer, String> testRbt = new RBTreeWParentDictionary<Integer, String>(
  217.         comparator);
  218.     testRbt.insert(10, "10");
  219.     testRbt.insert(20, "20");
  220.     testRbt.insert(15, "15");
  221.    
  222.     assertEquals(startRBT, testRbt);
  223.   }
  224.  
  225.   /*
  226.    * Testing deletion
  227.    */
  228.   @Test
  229.   public void testDeleteRoot() {
  230.     startRBT.root = null;
  231.    
  232.     RBTreeWParentDictionary<Integer, String> testRbt = new RBTreeWParentDictionary<Integer, String>(
  233.         comparator);
  234.     testRbt.insert(10, "10");
  235.     testRbt.delete(10);
  236.    
  237.     assertEquals(startRBT, testRbt);
  238.   }
  239.  
  240.   @Test
  241.   public void testDeleteLeftChildLeaf() {    
  242.     RBTreeWParentDictionary<Integer, String> testRbt = new RBTreeWParentDictionary<Integer, String>(
  243.         comparator);
  244.     testRbt.insert(10, "10");
  245.     testRbt.insert(1, "1");
  246.     testRbt.delete(1);
  247.    
  248.     assertEquals(startRBT, testRbt);
  249.   }
  250.  
  251.   @Test
  252.   public void testDeleteRightChildLeaf() {
  253.     RBTreeWParentDictionary<Integer, String> testRbt = new RBTreeWParentDictionary<Integer, String>(
  254.         comparator);
  255.     testRbt.insert(10, "10");
  256.     testRbt.insert(100, "100");
  257.     testRbt.delete(100);
  258.    
  259.     assertEquals(startRBT, testRbt);
  260.   }
  261.  
  262.   @Test
  263.   public void testDeleteLeftChild() {    
  264.     startRBT.root.setLeft(new RBTreeWParentNode<Integer, String>(3, "3"));
  265.     RBTreeWParentDictionary<Integer, String> testRbt = new RBTreeWParentDictionary<Integer, String>(
  266.         comparator);
  267.     testRbt.insert(10, "10");
  268.     testRbt.insert(5, "5");
  269.     testRbt.root.getLeft().setLeft(new RBTreeWParentNode<Integer, String>(3, "3"));
  270.     testRbt.delete(5);
  271.    
  272.     assertEquals(startRBT, testRbt);
  273.   }  
  274.  
  275.   @Test
  276.   public void testDeleteRightChild() {    
  277.     startRBT.root.setRight(new RBTreeWParentNode<Integer, String>(20, "20"));
  278.     RBTreeWParentDictionary<Integer, String> testRbt = new RBTreeWParentDictionary<Integer, String>(
  279.         comparator);
  280.     testRbt.insert(10, "10");
  281.     testRbt.insert(15, "15");
  282.     testRbt.root.getRight().setRight(new RBTreeWParentNode<Integer, String>(20, "20"));
  283.     testRbt.delete(15);
  284.    
  285.     assertEquals(startRBT, testRbt);
  286.   }  
  287.  
  288.   @Test
  289.   public void testDeleteDoubles() {
  290.     startRBT.root.setRight(new RBTreeWParentNode<Integer, String>(100, "100"));
  291.     startRBT.root.getRight().setBlack();
  292.     startRBT.root.setLeft(new RBTreeWParentNode<Integer, String>(6, "6"));
  293.     startRBT.root.getLeft().setRed();
  294.     startRBT.root.getLeft().setLeft(new RBTreeWParentNode<Integer, String>(5, "5"));
  295.     startRBT.root.getLeft().getLeft().setBlack();
  296.     startRBT.root.getLeft().setRight(new RBTreeWParentNode<Integer, String>(9, "9"));
  297.     startRBT.root.getLeft().getRight().setBlack();    
  298.     startRBT.root.getLeft().getLeft().setLeft(new RBTreeWParentNode<Integer, String>(2, "2"));
  299.     RBTreeWParentDictionary<Integer, String> testRbt = new RBTreeWParentDictionary<Integer, String>(
  300.         comparator);
  301.     testRbt.insert(10, "10");
  302.     testRbt.insert(100, "100");
  303.     testRbt.insert(8, "8");
  304.     testRbt.insert(5, "5");
  305.     testRbt.insert(9, "9");
  306.     testRbt.insert(2, "2");
  307.     testRbt.insert(6, "6");
  308.     testRbt.delete(8);
  309.     assertEquals(startRBT, testRbt);
  310.   }
  311. }
Advertisement
Add Comment
Please, Sign In to add comment