Beispiel #1
0
    def test_getItem_withKeyNotInBST_shouldReturnNone(self):
        bst = Treap()
        for i in range(1, 20):
            bst.put(i, str(i))

        for i in range(20, 30):
            self.assertIsNone(bst[i])
Beispiel #2
0
    def test_getItem_withKeyInBST_shouldReturnAssociatedValue(self):
        bst = Treap()
        for i in range(1, 20):
            bst.put(i, str(i))

        for i in range(1, 20):
            self.assertEqual(str(i), bst[i])
Beispiel #3
0
    def test_delete_withNoneTypeArgumentKey_shouldRaiseValueError(self):
        bst = Treap()

        with self.assertRaises(ValueError):
            bst.delete(None)
        with self.assertRaises(ValueError):
            del bst[None]
Beispiel #4
0
    def test_keysInOrder_withNotEmptyTree_shouldReturnListOfKeysInOrder(self):
        bst = Treap()
        inputs = random.sample(range(1, 1000), 100)

        for i in inputs:
            bst.put(i, i)

        self.assertEqual(sorted(inputs), bst.keys_in_order())
Beispiel #5
0
    def test_setItem_withEqualKeys_shouldReplaceOldValueByNewValue(self):
        bst = Treap()
        for i in range(1, 20):
            bst[i] = str(i)

        for i in range(1, 20):
            self.assertEqual(str(i), bst.get(i))

            bst[i] = str(i + 1)

            self.assertEqual(19, len(bst))
            self.assertIn(i, bst)
            self.assertEqual(str(i + 1), bst[i])
            self.assertEqual(str(i + 1), bst.get(i))
Beispiel #6
0
    def test_setItem_withNotEqualKeys_shouldInsertNewPairKeyValueIntoTree(
            self):
        bst = Treap()
        for i in range(1, 20):
            bst[i] = str(i)
            self.assertEqual(i, len(bst))

            self.assertIn(i, bst)
            self.assertEqual(str(i), bst[i])
            self.assertEqual(str(i), bst.get(i))

        for i in range(1, 20):
            self.assertIn(i, bst)
            self.assertEqual(str(i), bst[i])
            self.assertEqual(str(i), bst.get(i))
Beispiel #7
0
    def test_setItem_withNoneTypeArgumentValue_shouldDeleteKeyFromTree(self):
        bst = Treap()
        for i in range(1, 20):
            bst[i] = str(i)

        for i in range(1, 20):
            bst[i] = None

            self.assertEqual(19 - i, len(bst))
            self.assertNotIn(i, bst)

            for j in range(i + 1, 20):
                self.assertIn(j, bst)
Beispiel #8
0
 def test_deleteMin_withNotEmptyTree_shouldDeleteSmallestKeyFromTree(self):
     bst = Treap()
     for i in range(1, 20):
         bst.put(i, i)
     for i in range(1, 20):
         bst.delete_min()
         self.assertEqual(19 - i, len(bst))
         self.assertNotIn(i, bst)
         for j in range(i + 1, 20):
             self.assertIn(j, bst)
Beispiel #9
0
 def test_deleteMax_withNotEmptyTree_shouldDeleteLargestKeyFromTree(self):
     bst = Treap()
     for i in range(1, 20):
         bst.put(-i, i)
     for i in range(1, 20):
         bst.delete_max()
         self.assertEqual(19 - i, len(bst))
         self.assertNotIn(-i, bst)
         for j in range(i + 1, 20):
             self.assertIn(-j, bst)
Beispiel #10
0
    def test_delete_withKeyNotInTree_shouldNotModifyTree(self):
        bst = Treap()
        for i in range(1, 20):
            bst.put(i, str(i))

        for i in range(20, 30):
            bst.delete(i)

            self.assertEqual(19, len(bst))
            for j in range(1, 20):
                self.assertIn(j, bst)
Beispiel #11
0
    def test_delete_withKeyInTree_shouldDeleteKeyFromTree(self):
        bst = Treap()
        for i in range(1, 20):
            bst.put(i, str(i))

        for i in range(1, 20):
            bst.delete(i)

            self.assertEqual(19 - i, len(bst))
            self.assertNotIn(i, bst)
            for j in range(i + 1, 20):
                self.assertIn(j, bst)
Beispiel #12
0
    def test_put_withNoneTypeArgumentValue_shouldDeleteKeyFromTree(self):
        bst = Treap()
        for i in range(1, 20):
            bst.put(i, str(i))

        for i in range(1, 20):
            bst.put(i, None)

            self.assertEqual(19 - i, len(bst))
            self.assertNotIn(i, bst)

            for j in range(i + 1, 20):
                self.assertIn(j, bst)
Beispiel #13
0
 def test_max_withNotEmptyTree_shouldReturnLargestKeyFromTree(self):
     bst = Treap()
     for i in range(1, 20):
         bst.put(i, i)
         self.assertEqual(i, bst.max())
Beispiel #14
0
 def test_put_withNoneTypeArgumentKey_shouldRaiseValueError(self):
     bst = Treap()
     with self.assertRaises(ValueError):
         bst.put(None, 0)
Beispiel #15
0
 def test_constructor_shouldCreateEmptySplayTree(self):
     bst = Treap()
     self.assertTrue(bst.empty())
     self.assertEqual(0, len(bst))
     self.assertEqual(-1, bst.height())
Beispiel #16
0
 def test_get_withNoneTypeArgument_shouldRaiseValueError(self):
     with self.assertRaises(ValueError):
         Treap().get(None)
Beispiel #17
0
 def test_keysInOrder_withEmptyTree_shouldReturnEmptyList(self):
     bst = Treap()
     self.assertEqual([], bst.keys_in_order())
Beispiel #18
0
 def test_deleteMax_withEmptyTree_shouldNotModifyTree(self):
     bst = Treap()
     bst.delete_max()
     self.assertEqual(0, len(bst))
     self.assertEqual(-1, bst.height())
Beispiel #19
0
 def test_max_withEmptyTree_shouldReturnNone(self):
     bst = Treap()
     self.assertIsNone(bst.max())
Beispiel #20
0
 def test_min_withNotEmptyTree_shouldReturnSmallestKeyFromTree(self):
     bst = Treap()
     for i in range(-1, -20, -1):
         bst.put(i, i)
         self.assertEqual(i, bst.min())
Beispiel #21
0
 def test_setItem_withNoneTypeArgumentKey_shouldRaiseValueError(self):
     bst = Treap()
     with self.assertRaises(ValueError):
         bst[None] = 0