Exemplo n.º 1
0
 def test_big_add(self):
     st = SplayTree()
     array = [randint(-10000, 10000) for i in range(100000)]
     for i in range(len(array)):
         st.add(array[i])
         if i > 3:
             for j in range(10):
                 ind = randint(0, i - 1)
                 self.assertTrue(st.contains(array[ind]))
Exemplo n.º 2
0
 def test_medium_add(self):
     st = SplayTree()
     array = [4, 8, 15, 16, 23, 42, 1, 1, 2, 3, 5, 8, 13, 1, 4, 9, 16, 25, 36, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
              31, 4, 2, 3, 5, 11, 12]
     for i in range(len(array)):
         st.add(array[i])
         if i > 3:
             for j in range(5):
                 ind = randint(0, i - 1)
                 self.assertTrue(st.contains(array[ind]))
Exemplo n.º 3
0
 def test_big_iterator(self):
     st = SplayTree()
     array = [i for i in range(100000)]
     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)
     ptr = 0
     for i in st:
         self.assertEqual(i, ptr)
         ptr += 1
Exemplo n.º 4
0
class Test_Empty_SplayTree(unittest.TestCase):
    """ A class Test empty SplayTree.
    """
    
    def setUp(self):
        ''' Setup test case each time. 
        '''
        self.test1 = SplayTree()
    
    def tearDown(self):
        ''' Reset test case to default
        '''
        self.test1 = None
    
    def test_emptptree_search(self):
        ''' test search in an empty tree.
        '''
        self.assertFalse(self.test1.search(2), "check search in empty tree")
        
    def test_emptyllist_max(self):
        ''' test max method in empty tree.
        '''        
        self.assertRaises(EmptyException, self.test1.max)
    
    def test_emptyllist_min(self):
        ''' test min method in empty tree.
        '''
        self.assertRaises(EmptyException, self.test1.min)
    
    def test_emptyllist_traverse(self):
        ''' test traverse method in empty tree.
        '''
        self.assertEqual(self.test1.traverse(), [], "traverse in empty tree")
    
    def test_emptyllist_remove(self):
        ''' test remove method in empty tree, raise EmptyException.
        '''
        self.assertRaises(EmptyException, self.test1.remove, 2)
        
    def test_emptyllist_insert(self):
        ''' test insert method in empty tree.
        '''
        self.test1.insert(5)
        self.assertEqual(self.test1.traverse(), [5], "insert in empty tree")
        
    def test_emptyllist_insert_2(self):
        ''' test insert an uncompare data in empty tree, 
            raise UnCompareExeption
        '''
        self.assertRaises(UnCompareExeption, self.test1.insert, {1:2})    
Exemplo n.º 5
0
 def test_nested_iterators(self):
     st = SplayTree()
     array = [i for i in range(100)]
     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)
     a = 0
     b = 0
     c = 0
     for i in st:
         for j in st:
             for k in st:
                 self.assertEqual(a, i)
                 self.assertEqual(b, j)
                 self.assertEqual(c, k)
                 c += 1
             b += 1
             c = 0
         a += 1
         b = 0
Exemplo n.º 6
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))
Exemplo n.º 7
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]))
Exemplo n.º 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
Exemplo n.º 9
0
 def test_empty(self):
     st = SplayTree()
     self.assertFalse(st.contains(0))
Exemplo n.º 10
0
 def test_remove_on_empty_set(self):
     st = SplayTree()
     for i in range(10000):
         st.remove(randint(-10000, 10000))
Exemplo n.º 11
0
 def setUp(self):
     ''' Setup test case each time. 
     '''
     self.test1 = SplayTree([5])
Exemplo n.º 12
0
 def setUp(self):
     ''' Setup test case each time. 
     '''
     self.test1 = SplayTree(['c', 'z', 'b', 'a', 'd', 's'])
Exemplo n.º 13
0
 def test_iterator(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)
     ptr = 1
     for i in st:
         self.assertEqual(i, ptr)
         ptr += 1
Exemplo n.º 14
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)
Exemplo n.º 15
0
 def test_add(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))
     self.assertTrue(st.contains(6))
     self.assertTrue(st.contains(5))
     self.assertTrue(st.contains(5))
     self.assertTrue(st.contains(0))
     st.add(1)
     st.add(3)
     self.assertTrue(st.contains(5))
     self.assertTrue(st.contains(5))
     self.assertTrue(st.contains(0))
     self.assertTrue(st.contains(1))
     self.assertTrue(st.contains(5))
     self.assertTrue(st.contains(3))
Exemplo n.º 16
0
 def setUp(self):
     ''' Setup test case each time. 
     '''
     self.test1 = SplayTree([-2, -7, 15, 7, 12, 9, 5])
Exemplo n.º 17
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)
Exemplo n.º 18
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))
Exemplo n.º 19
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')
Exemplo n.º 20
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)
Exemplo n.º 21
0
from splaytree import SplayTree
from utils.students import Stream, Student
from utils import fetch_stream_students

if __name__ == '__main__':
    stream_key = Stream('Інженерія програмного забезпечення', 2)

    students = fetch_stream_students(stream_key)
    student_tree = SplayTree([(student, None) for student in students])

    excellent_key = Student('Я', 'Я', 'Я', 90)

    exc_students = student_tree.split(excellent_key)

    for student, _ in exc_students:
        print(student)
Exemplo n.º 22
0
 def test_contains_on_empty_set(self):
     st = SplayTree()
     for i in range(10000):
         self.assertFalse(st.contains(randint(-10000, 10000)))