Example #1
0
 def test_big_combined(self):
     st = SplayTree()
     array = [i for i in range(100000)]
     removed = []
     for i in range(len(array)):
         ind = randint(0, len(array) - 1)
         array[i], array[ind] = array[ind], array[i]
     for i in array:
         st.add(i)
     for i in range(100000):
         st.remove(array[i])
         removed.append(array[i])
         if i > 3:
             for j in range(10):
                 ind = randint(0, len(removed) - 1)
                 self.assertFalse(st.contains(removed[ind]))
         if i < len(array) - 4:
             for j in range(10):
                 ind = randint(i + 2, len(array) - 2)
                 self.assertTrue(st.contains(array[ind]))
                 self.assertTrue(st.contains(array[ind + 1]))
                 self.assertTrue(st.contains(array[ind]))
                 self.assertTrue(st.contains(array[ind + 1]))
                 self.assertTrue(st.contains(array[ind - 1]))
                 self.assertTrue(st.contains(array[ind + 1]))
                 self.assertTrue(st.contains(array[ind]))
         if i > 0 and i % 5 == 0:
             for j in range(3):
                 ind = randint(0, len(removed) - 1)
                 num = removed[ind]
                 removed.remove(num)
                 array.append(num)
                 st.add(num)
Example #2
0
 def test_remove_elements_not_in_set(self):
     st = SplayTree()
     for i in range(1000):
         st.add(i)
     for i in range(1001, 10000):
         st.remove(i)
     for i in range(1000):
         self.assertTrue(st.contains(i))
Example #3
0
 def test_remove(self):
     st = SplayTree()
     st.add(5)
     st.add(6)
     self.assertFalse(st.contains(0))
     self.assertTrue(st.contains(5))
     self.assertTrue(st.contains(6))
     st.add(0)
     self.assertTrue(st.contains(0))
     st.remove(6)
     self.assertFalse(st.contains(6))
     self.assertTrue(st.contains(5))
     self.assertTrue(st.contains(0))
Example #4
0
 def test_big_remove(self):
     st = SplayTree()
     array = [i for i in range(100000)]
     removed = []
     for i in range(len(array)):
         ind = randint(0, len(array) - 1)
         array[i], array[ind] = array[ind], array[i]
     for i in array:
         st.add(i)
     for i in range(len(array)):
         st.remove(array[i])
         removed.append(array[i])
         if i > 3:
             for j in range(10):
                 ind = randint(0, len(removed) - 1)
                 self.assertFalse(st.contains(removed[ind]))
Example #5
0
 def test_medium_remove(self):
     st = SplayTree()
     st.add(9)
     st.add(1)
     st.add(5)
     st.add(8)
     st.add(3)
     st.add(2)
     st.add(7)
     st.add(4)
     st.add(6)
     for i in range(1, 9):
         st.remove(i)
         self.assertFalse(st.contains(i))
         self.assertTrue(st.contains(i + 1))
         self.assertFalse(st.contains(i - 1))
         st.add(i)
         self.assertTrue(st.contains(i))
         self.assertTrue(st.contains(i + 1))
         self.assertFalse(st.contains(i - 1))
         st.remove(i)
Example #6
0
class Test_OneElement_SplayTree(unittest.TestCase):
    """ A class Test OneElement SplayTree.
    """
    
    def setUp(self):
        ''' Setup test case each time. 
        '''
        self.test1 = SplayTree([5])
    
    def tearDown(self):
        ''' Reset test case to default
        '''
        self.test1 = None
    
    def test_search(self):
        ''' test search in tree.
        '''
        self.assertFalse(self.test1.search(2), "check search in tree")
        self.assertTrue(self.test1.search(5), "check search in tree")
        
    def test_max(self):
        ''' test max method in tree.
        '''        
        self.assertEqual(self.test1.max(), 5, "max method in tree")
    
    def test_min(self):
        ''' test min method in tree.
        '''
        self.assertEqual(self.test1.min(), 5, "min method in tree")
    
    def test_traverse(self):
        ''' test traverse method in tree.
        '''
        self.assertEqual(self.test1.traverse(), [5], "traverse in tree")
    
    def test_remove_1(self):
        ''' test remove an element not in tree, does nothing.
        '''
        self.test1.remove(2)
        self.assertEqual(self.test1.traverse(), [5], "traverse in tree")
        
    def test_remove_2(self):
        ''' test remove an element in tree.
        '''
        self.test1.remove(5)
        self.assertEqual(self.test1.traverse(), [], "traverse in tree")
        
    def test_remove_3(self):
        ''' test remove an uncomareable element in tree, raise UncompareExption.
        '''
        self.assertRaises(UnCompareExeption, self.test1.remove, 'a')
    
    def test_insert(self):
        ''' test insert duplicate element in tree.
        '''
        self.test1.insert(5)
        self.assertEqual(self.test1.traverse(), [5,5], "insert in tree")
    
    def test_insert_2(self):
        ''' test insert different element in tree and splay to the root.
        '''
        self.test1.insert(7)
        self.assertEqual(self.test1.traverse(), [5,7], "insert in tree")
        self.assertEqual(self.test1.root.data, 7, "insert in tree")
                
    def test_insert_3(self):
        ''' test insert uncompareable element in tree.
        '''
        self.assertRaises(UnCompareExeption, self.test1.insert, 'a')
Example #7
0
class Test_Multielement_Str_SplayTree(unittest.TestCase):
    """ A class Test Multielement SplayTree.
    """
    
    def setUp(self):
        ''' Setup test case each time. 
        '''
        self.test1 = SplayTree(['c', 'z', 'b', 'a', 'd', 's'])
    
    def tearDown(self):
        ''' Reset test case to default
        '''
        self.test1 = None
    
    def test_search(self):
        ''' test search in tree.
        '''
        self.assertFalse(self.test1.search(2), "check search in tree")
        self.assertTrue(self.test1.search('a'), "check search in tree")
        self.assertEqual(self.test1.root.data, 'a', "insert in tree")
        
    def test_max(self):
        ''' test max method in tree.
        '''        
        self.assertEqual(self.test1.max(), 'z', "max method in tree")
        self.assertEqual(self.test1.root.data, 'z', "insert in tree")
    
    def test_min(self):
        ''' test min method in tree.
        '''
        self.assertEqual(self.test1.min(), 'a', "min method in tree")
        self.assertEqual(self.test1.root.data, 'a', "insert in tree")
    
    def test_traverse(self):
        ''' test traverse method in tree.
        '''
        self.assertEqual(self.test1.traverse(), ['a', 'b', 'c', 'd', 's', 'z']\
                         , "traverse in tree")
    
    def test_remove_1(self):
        ''' test remove an uncomareable element in tree, raise UncompareExption.
        '''
        self.assertRaises(UnCompareExeption, self.test1.remove, 2)
        
    def test_remove_2(self):
        ''' test remove an element in tree.
        '''
        self.test1.remove('s')
        self.assertEqual(self.test1.traverse(), ['a', 'b', 'c', 'd', 'z']\
                         , "traverse in tree")
        self.assertEqual(self.test1.root.data, 'z', "insert in tree")
        
    def test_insert(self):
        ''' test insert duplicate element in tree.
        '''
        self.test1.insert('c')
        self.assertEqual(self.test1.traverse(), \
                         ['a', 'b', 'c', 'c', 'd', 's', 'z']\
                         , "traverse in tree")
        self.assertEqual(self.test1.root.data, 'c', "insert in tree")
    
    def test_insert_2(self):
        ''' test insert different element in tree and splay to the root.
        '''
        self.test1.insert('m')
        self.assertEqual(self.test1.traverse(),\
                         ['a', 'b', 'c', 'd', 'm', 's', 'z']\
                         , "traverse in tree")
        self.assertEqual(self.test1.root.data, 'm', "insert in tree")
                
    def test_insert_3(self):
        ''' test insert uncompareable element in tree.
        '''
        self.assertRaises(UnCompareExeption, self.test1.insert, 3)
Example #8
0
    def test(self):
        value = 5
        t = SplayTree()
        # test empty
        assert t.isEmpty()
        # we cannot splay if tree is empty
        assert t.splay(None) is None
        # we cannot find anything in empty tree
        assert t.find(1) is None
        assert t.findMin() is None
        assert t.findMax() is None
        # cannot remove from empty
        assert t.remove(123) is None

        # insert into empty tree
        t.insert(value)
        assert not t.isEmpty()

        # insert into non-empty tree same value
        t.insert(value)
        assert not t.isEmpty()

        # insert different values
        t = SplayTree()
        t.insert(3)
        t.insert(5)
        t.insert(1)

        assert t.findMin() == 1
        assert t.findMax() == 5
        assert t.find(3) == 3
        assert t.find(10) is None

        t = SplayTree()
        t.insert(5)
        t.insert(3)
        t.insert(1)
        assert t.find(3) == 3
        assert t.findMin() == 1

        # test remove
        t = SplayTree()
        t.insert(5)
        t.insert(3)
        t.insert(1)
        t.remove(5)
        t.remove(3)
        t.remove(1)
        assert t.isEmpty()

        # test Node.equals()
        n1 = Node(3)
        n2 = Node(2 + 1)
        n3 = Node(1)
        assert n1.equals(n2)
        assert not n1.equals(n3)

        t = SplayTree()
        t.insert(7)
        t.insert(3)
        t.insert(7)
        assert t.find(7) == 7

        # lets do som RANGE testing !
        max = 1000
        min = 100
        t = SplayTree()
        t.insert(min)
        t.insert(max)

        for i in range(101, 999, 10):
            t.insert(i)
            assert t.find(i) == i
            assert t.findMin() == min
            assert t.findMax() == max
Example #9
0
 def test_remove_on_empty_set(self):
     st = SplayTree()
     for i in range(10000):
         st.remove(randint(-10000, 10000))