def __init__(self): self.__top = None self.__op = Tree() self.__push = AVLTree() self.__pop = AVLTree() self.__cur_time = 0
def test_ceiling_withCompareFunctionAndArgumentNotInBSTHavingCeilingValue_shouldReturnSmallerStrictlyGreaterElementFromBST( self): bst = AVLTree(self.cmp) for i in range(10): bst.put([0, i], i) for i in range(10): self.assertEqual([0, i], bst.ceiling([0, i - 0.5]))
def test_floor_withArgumentNotInBSTHavingFloorValue_shouldReturnGreaterStrictlyLowerElementFromBST( self): bst = AVLTree() for i in range(10): bst.put(i, i) for i in range(10): self.assertEqual(i, bst.floor(i + 0.5))
def test_rank_withCompareFunctionAndKeyNotInBSTAndGreaterThanEveryKey_shouldReturnBSTSize( self): bst = AVLTree(self.cmp) for i in range(10): bst.put([i, 0], 0) for i in range(10, 20): self.assertEqual(bst.size(), bst.rank([i, 0]))
def test_rank_withKeyNotInBSTAndAmongKeys_shouldReturnCorrectNumberOfKeysStrictlySmallerThanGivenKey( self): bst = AVLTree() for i in range(0, 20, 2): bst.put(i, 0) for i in range(1, 20, 2): self.assertEqual((i + 1) / 2, bst.rank(i))
def test_put_withKeyNotInBST_shouldAddNewKeyIncreaseBSTLengthAndMaintainBalanceFactor( self): bst = AVLTree() self.assertEqual(0, len(bst)) bst.put(10, 0) self.assertEqual(1, len(bst)) self.assertEqual(0, bst.height()) bst.put(20, 0) self.assertEqual(2, len(bst)) self.assertEqual(1, bst.height()) bst.put(30, 0) self.assertEqual(3, len(bst)) self.assertEqual(1, bst.height()) bst.put(40, 0) self.assertEqual(4, len(bst)) self.assertEqual(2, bst.height()) bst.put(50, 0) self.assertEqual(5, len(bst)) self.assertEqual(2, bst.height()) bst.put(25, 0) self.assertEqual(6, len(bst)) self.assertEqual(2, bst.height())
def test_rank_withCompareFunctionAndKeyInBST_shouldReturnCorrectNumberOfKeysStrictlySmallerThanGivenKey( self): bst = AVLTree(self.cmp) for i in range(20): bst.put([i, 0], 0) for i in range(20): self.assertEqual(i, bst.rank([i, 0]))
def test_ceiling_withArgumentNotInBSTHavingCeilingValue_shouldReturnSmallerStrictlyGreaterElementFromBST( self): bst = AVLTree() for i in range(10): bst.put(i, i) for i in range(10): self.assertEqual(i, bst.ceiling(i - 0.5))
def test_rank_withCompareFunctionAndKeyNotInBSTAndAmongKeys_shouldReturnCorrectNumberOfKeysStrictlySmallerThanGivenKey( self): bst = AVLTree(self.cmp) for i in range(0, 20, 2): bst.put([i, 0], 0) for i in range(1, 20, 2): self.assertEqual((i + 1) / 2, bst.rank([i, 0]))
def test_keys_withNoneTypeArgument_shouldRaiseValueError(self): bst = AVLTree() with self.assertRaises(ValueError): bst.keys(None, 1) with self.assertRaises(ValueError): bst.keys(1, None)
def test_rank_withKeyInBST_shouldReturnCorrectNumberOfKeysStrictlySmallerThanGivenKey( self): bst = AVLTree() for i in range(20): bst.put(i, 0) for i in range(20): self.assertEqual(i, bst.rank(i))
def select_withValidK_shouldReturnTheKthSmallestElementInBST(self): bst = AVLTree() for i in range(20): bst.put(i, i) for i in range(20): self.assertEqual(i, bst.select(i))
def test_rank_withCompareFunctionAndKeyNotInBSTAndSmallerThanEveryKey_shouldReturnZero( self): bst = AVLTree(self.cmp) for i in range(10, 20): bst.put([i, 0], 0) for i in range(10): self.assertEqual(0, bst.rank([i, 0]))
def test_rank_withKeyNotInBSTAndGreaterThanEveryKey_shouldReturnBSTSize( self): bst = AVLTree() for i in range(10): bst.put(i, 0) for i in range(10, 20): self.assertEqual(bst.size(), bst.rank(i))
def test_floor_withCompareFunctionAndArgumentNotInBSTHavingFloorValue_shouldReturnGreaterStrictlyLowerElementFromBST( self): bst = AVLTree(self.cmp) for i in range(10): bst.put([0, i], i) for i in range(10): self.assertEqual([0, i], bst.floor([0, i + 0.5]))
def select_withCompareFunctionAndValidK_shouldReturnTheKthSmallestElementInBST( self): bst = AVLTree(self.cmp) for i in range(20): bst.put([i, 0], i) for i in range(20): self.assertEqual(i, bst.select(i))
def test_successor_withCompareFunctionAndKeyInBST_shouldReturnInOrderSuccessorOfGivenKey( self): bst = AVLTree(self.cmp) for i in range(10): bst.put([i, 0], 0) for i in range(10): if i == 9: self.assertIsNone(bst.successor([i, 0])) else: self.assertEqual([i + 1, 0], bst.successor([i, 0]))
def test_contains_withKeyNotInBST_shouldReturnFalse(self): bst = AVLTree() bst.put("A", 1) bst.put("B", 2) bst.put("C", 3) bst.put("D", 4) self.assertNotIn("E", bst) self.assertNotIn("F", bst) self.assertNotIn("G", bst)
def test_successor_withKeyInBST_shouldReturnInOrderSuccessorOfGivenKey( self): bst = AVLTree() for i in range(10): bst.put(i, 0) for i in range(10): if i == 9: self.assertIsNone(bst.successor(i)) else: self.assertEqual(i + 1, bst.successor(i))
def test_getItem_withKeyInBST_shouldReturnAssociatedValue(self): bst = AVLTree() bst.put("A", 1) bst.put("B", 2) bst.put("C", 3) bst.put("D", 4) self.assertEqual(1, bst["A"]) self.assertEqual(2, bst["B"]) self.assertEqual(3, bst["C"]) self.assertEqual(4, bst["D"])
def test_select_withArgumentOutOfBSTRange_shouldRaiseValueError(self): bst = AVLTree() for i in range(20): bst.put(i, i) for i in range(-20, 0): with self.assertRaises(ValueError): bst.select(i) for i in range(20, 40): with self.assertRaises(ValueError): bst.select(i)
def test_contains_withKeyInBST_shouldReturnTrue(self): bst = AVLTree() bst.put("A", 1) bst.put("B", 2) bst.put("C", 3) bst.put("D", 4) self.assertIn("A", bst) self.assertIn("B", bst) self.assertIn("C", bst) self.assertIn("D", bst)
def test_contains_withCompareFunctionAndKeyInBST_shouldReturnTrue(self): bst = AVLTree(self.cmp) bst.put([0, 0], 1) bst.put([0, 1], 2) bst.put([0, 2], 3) bst.put([0, 3], 4) self.assertIn([0, 1], bst) self.assertIn([0, 2], bst) self.assertIn([0, 3], bst) self.assertIn([0, 3], bst)
def test_getItem_withCompareFunctionAndKeyInBST_shouldReturnAssociatedValue( self): bst = AVLTree(self.cmp) bst.put([0, 0], 1) bst.put([0, 1], 2) bst.put([0, 2], 3) bst.put([0, 3], 4) self.assertEqual(1, bst[[0, 0]]) self.assertEqual(2, bst[[0, 1]]) self.assertEqual(3, bst[[0, 2]]) self.assertEqual(4, bst[[0, 3]])
def test_select_withCompareFunctionAndArgumentOutOfBSTRange_shouldRaiseValueError( self): bst = AVLTree(self.cmp) for i in range(20): bst.put([i, 0], i) for i in range(-20, 0): with self.assertRaises(ValueError): bst.select(i) for i in range(20, 40): with self.assertRaises(ValueError): bst.select(i)
def test_delete_withCompareFunctionAndKeyNotInBST_shouldNotModifyBST(self): bst = AVLTree(self.cmp) bst.put([0, 0], 1) bst.put([0, 1], 2) bst.put([0, 2], 3) bst.put([0, 3], 4) bst.delete([0, 69]) self.assertEqual(4, len(bst)) self.assertIn([0, 1], bst) self.assertIn([0, 2], bst) self.assertIn([0, 3], bst) self.assertIn([0, 3], bst)
def test_delete_withKeyNotInBST_shouldNotModifyBST(self): bst = AVLTree() bst.put("A", 1) bst.put("B", 2) bst.put("C", 3) bst.put("D", 4) bst.delete("G") self.assertEqual(4, len(bst)) self.assertIn("A", bst) self.assertIn("B", bst) self.assertIn("C", bst) self.assertIn("D", bst)
def test_keys_withNotEmptyBSTAndArgumentsNotInBST_shouldReturnListOfKeysInGivenRangeExcludingGivenArguments( self): bst = AVLTree() for i in range(0, 20, 2): bst.put(i, i) for i in range(-1, 20, 2): for j in range(i, 20, 2): if i == j: expected = [] else: expected = [e for e in range(i + 1, j + 1, 2)] self.assertEqual(expected, bst.keys(i, j))
def test_keys_withCompareFunctionAndNotEmptyBST_shouldReturnListOfKeysInGivenRangeIncludingGivenArguments( self): bst = AVLTree(self.cmp) for i in range(20): bst.put([0, i], i) for i in range(20): for j in range(i, 20): if i == j: expected = [[0, i]] else: expected = [[0, e] for e in range(i, j + 1)] self.assertEqual(expected, bst.keys([0, i], [0, j]))
def test_put_withCompareFunctionAndKeyInBSTAndValueNotNone_shouldReplaceAssociatedValueByNewOneAndNotModifyBSTLength( self): bst = AVLTree(self.cmp) bst.put([0, 0], 0) self.assertEqual(1, len(bst)) self.assertEqual(0, bst.get([0, 0])) bst.put([0, 0], 1) self.assertEqual(1, len(bst)) self.assertEqual(1, bst.get([0, 0])) bst.put([0, 0], 12) self.assertEqual(1, len(bst)) self.assertEqual(12, bst.get([0, 0]))