Exemple #1
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])
Exemple #2
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])
Exemple #3
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())
Exemple #4
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)
Exemple #5
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)
Exemple #6
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)
Exemple #7
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)
Exemple #8
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)
Exemple #9
0
    def test_put_withEqualKeys_shouldReplaceOldValueByNewValue(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.get(i))

            bst.put(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))
Exemple #10
0
    def test_put_withNotEqualKeys_shouldInsertNewPairKeyValueIntoTree(self):
        bst = Treap()

        for i in range(1, 20):
            bst.put(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))
Exemple #11
0
 def test_max_withNotEmptyTree_shouldReturnLargestKeyFromTree(self):
     bst = Treap()
     for i in range(1, 20):
         bst.put(i, i)
         self.assertEqual(i, bst.max())
Exemple #12
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())
Exemple #13
0
 def test_put_withNoneTypeArgumentKey_shouldRaiseValueError(self):
     bst = Treap()
     with self.assertRaises(ValueError):
         bst.put(None, 0)