def test_getItem_withKeyNotInBST_shouldReturnNone(self): bst = SplayTree() for i in range(1, 20): bst.put(i, str(i)) for i in range(20, 30): self.assertIsNone(bst[i])
def test_getItem_withKeyInBST_shouldReturnAssociatedValue(self): bst = SplayTree() for i in range(1, 20): bst.put(i, str(i)) for i in range(1, 20): self.assertEqual(str(i), bst[i])
def test_delete_withNoneTypeArgumentKey_shouldRaiseValueError(self): bst = SplayTree() with self.assertRaises(ValueError): bst.delete(None) with self.assertRaises(ValueError): del bst[None]
def test_keysInOrder_withNotEmptyTree_shouldReturnListOfKeysInOrder(self): bst = SplayTree() inputs = random.sample(range(1, 1000), 100) for i in inputs: bst.put(i, i) self.assertEqual(sorted(inputs), bst.keys_in_order())
def test_setItem_withEqualKeys_shouldReplaceOldValueByNewValue(self): bst = SplayTree() 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))
def test_setItem_withNotEqualKeys_shouldInsertNewPairKeyValueIntoTree( self): bst = SplayTree() 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))
def test_setItem_withNoneTypeArgumentValue_shouldDeleteKeyFromTree(self): bst = SplayTree() 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)
def test_deleteMin_withNotEmptyTree_shouldDeleteSmallestKeyFromTree(self): bst = SplayTree() 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)
def test_deleteMax_withNotEmptyTree_shouldDeleteLargestKeyFromTree(self): bst = SplayTree() 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)
def test_delete_withKeyNotInTree_shouldNotModifyTree(self): bst = SplayTree() 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)
def test_delete_withKeyInTree_shouldDeleteKeyFromTree(self): bst = SplayTree() 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)
def test_put_withNoneTypeArgumentValue_shouldDeleteKeyFromTree(self): bst = SplayTree() 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)
def test_max_withNotEmptyTree_shouldReturnLargestKeyFromTree(self): bst = SplayTree() for i in range(1, 20): bst.put(i, i) self.assertEqual(i, bst.max())
def test_put_withNoneTypeArgumentKey_shouldRaiseValueError(self): bst = SplayTree() with self.assertRaises(ValueError): bst.put(None, 0)
def test_constructor_shouldCreateEmptySplayTree(self): bst = SplayTree() self.assertTrue(bst.empty()) self.assertEqual(0, len(bst)) self.assertEqual(-1, bst.height())
def test_get_withNoneTypeArgument_shouldRaiseValueError(self): with self.assertRaises(ValueError): SplayTree().get(None)
def test_keysInOrder_withEmptyTree_shouldReturnEmptyList(self): bst = SplayTree() self.assertEqual([], bst.keys_in_order())
def test_deleteMax_withEmptyTree_shouldNotModifyTree(self): bst = SplayTree() bst.delete_max() self.assertEqual(0, len(bst)) self.assertEqual(-1, bst.height())
def test_max_withEmptyTree_shouldReturnNone(self): bst = SplayTree() self.assertIsNone(bst.max())
def test_min_withNotEmptyTree_shouldReturnSmallestKeyFromTree(self): bst = SplayTree() for i in range(-1, -20, -1): bst.put(i, i) self.assertEqual(i, bst.min())
def test_setItem_withNoneTypeArgumentKey_shouldRaiseValueError(self): bst = SplayTree() with self.assertRaises(ValueError): bst[None] = 0