Best Python code snippet using hypothesis
test_beap.py
Source:test_beap.py
...26def test_invariants():27 beap = VerifiedBeap()28 beap.arr = BEAP_DATA.copy()29 beap.height = 630 beap.check_invariants()31 beap.arr[0], beap.arr[1] = beap.arr[1], beap.arr[0]32 try:33 beap.check_invariants()34 failed = True35 except AssertionError:36 failed = False37 assert not failed, "Invariant checks don't work"38 beap.arr = BEAP_DATA.copy()39 beap.arr[0], beap.arr[len(beap.arr) - 1] = beap.arr[len(beap.arr) - 1], beap.arr[0]40 try:41 beap.check_invariants()42 failed = True43 except AssertionError:44 failed = False45 assert not failed, "Invariant checks don't work"46def test_search_once_off():47 beap = VerifiedBeap()48 beap.arr = BEAP_DATA.copy()49 beap.height = 650 assert beap.search(51) == (9, 3)51 assert beap.search(53) == None52def test_search():53 beap = VerifiedBeap()54 beap.arr = BEAP_DATA.copy()55 beap.height = 656 for i in range(101):57 res = beap.search(i)58 if i not in BEAP_DATA:59 assert res is None60 else:61 idx, height = res62 assert beap.arr[idx] == i63 assert height <= beap.height64def test_insert_trace_few():65 beap = VerifiedBeap()66 beap.insert(1)67 assert beap.height == 068 assert beap.arr == [1]69 beap.check_invariants()70 beap.insert(2)71 assert beap.height == 172 assert beap.arr == [2, 1]73 beap.check_invariants()74 beap.insert(3)75 assert beap.height == 176 assert beap.arr == [3, 1, 2]77 beap.check_invariants()78 beap.insert(4)79 assert beap.height == 280 assert beap.arr == [4, 3, 2, 1]81 beap.check_invariants()82 beap.insert(5)83 assert beap.height == 284 assert beap.arr == [5, 3, 4, 1, 2]85 beap.check_invariants()86 beap.insert(6)87 assert beap.height == 288 assert beap.arr == [6, 3, 5, 1, 2, 4]89 beap.check_invariants()90 beap.insert(7)91 assert beap.height == 392 assert beap.arr == [7, 6, 5, 3, 2, 4, 1]93 beap.check_invariants()94def test_insert():95 beap = VerifiedBeap()96 data = sorted(BEAP_DATA)97 for v in data:98 beap.insert(v)99 beap.check_invariants()100 # Doesn't work and doesn't have to work, as there're many different101 # partial orderings compatible with beap.102 #assert beap.arr == BEAP_DATA103 beap2 = VerifiedBeap()104 data = sorted(BEAP_DATA, reverse=True)105 for v in data:106 beap2.insert(v)107 beap2.check_invariants()108 for i in range(101):109 res1 = bool(beap.search(i))110 res2 = bool(beap2.search(i))111 assert res1 == res2112def test_delete():113 beap = VerifiedBeap()114 beap.arr = BEAP_DATA.copy()115 beap.height = 6116 res = []117 while beap.arr:118 res.append(beap.arr[0])119 beap.delete(0, 0)120 beap.check_invariants()121 assert res == sorted(res, reverse=True)122def test_insert_remove_random():123 beap = VerifiedBeap()124 items = []125 res = []126 RANGE = 100127 for i in range(RANGE):128 v = random.randrange(RANGE * 2)129 items.append(v)130 beap.insert(v)131 beap.check_invariants()132 while items:133 idx = random.randrange(len(items))134 v = items.pop(idx)135 beap.remove(v)136 beap.check_invariants()137 assert len(beap.arr) == 0138def test_op_complexity():139 beap = VerifiedBeap()140 items = []141 res = []142 RANGE = 100143 for i in range(RANGE):144 v = random.randrange(RANGE * 2)145 items.append(v)146 max_iters = math.ceil(math.sqrt(2 * len(beap.arr)))147 beap.iters = 0148 beap.insert(v)149 assert beap.iters <= max_iters150 max_iters = 2 * math.ceil(math.sqrt(2 * len(beap.arr)))...
test_red_black_tree.py
Source:test_red_black_tree.py
...5 if not node:6 return []7 return helper(node.left) + [(node.key, node.color)] + helper(node.right)8 return helper(tree)9def check_invariants(tree):10 # 1. Root must be black11 assert tree.color == Node.BLACK12 number_blacks = set()13 def helper(node, acc):14 nonlocal number_blacks15 if not node:16 number_blacks.add(acc)17 return18 # 2. If a node is red, all of its children must be black19 if node.color == Node.RED:20 assert not node.left or node.left.color == Node.BLACK21 assert not node.right or node.right.color == Node.BLACK22 if node.color == Node.BLACK:23 acc += 124 helper(node.left, acc)25 helper(node.right, acc)26 helper(tree, 0)27 # 3. For any given node u, every possible path from u to a "null reference"28 # must contain the same number of black nodes29 assert len(number_blacks) == 130def test_add_and_get_one_key():31 rbt = RedBlackTree()32 rbt.add(5, 10)33 check_invariants(rbt._tree)34 assert rbt.get(5) == 1035 assert rbt._tree.color == Node.BLACK36def test_state_after_adding_2_values():37 rbt = RedBlackTree()38 rbt.add(5, 10)39 rbt.add(10, 20)40 check_invariants(rbt._tree)41 assert rbt._tree.key == 542 assert rbt._tree.color == Node.BLACK43 assert rbt._tree.right.key == 1044 assert rbt._tree.right.color == Node.RED45def test_state_after_adding_3_values_asc():46 rbt = RedBlackTree()47 rbt.add(3, 10)48 rbt.add(4, 40)49 rbt.add(5, 50)50 check_invariants(rbt._tree)51 assert rbt._tree.key == 452 assert rbt._tree.color == Node.BLACK53 assert rbt._tree.right.key == 554 assert rbt._tree.right.color == Node.RED55 assert rbt._tree.left.key == 356 assert rbt._tree.left.color == Node.RED57def test_state_after_adding_3_values_desc():58 rbt = RedBlackTree()59 rbt.add(5, 50)60 rbt.add(4, 40)61 rbt.add(3, 10)62 check_invariants(rbt._tree)63 assert rbt._tree.key == 464 assert rbt._tree.color == Node.BLACK65 assert rbt._tree.right.key == 566 assert rbt._tree.right.color == Node.RED67 assert rbt._tree.left.key == 368 assert rbt._tree.left.color == Node.RED69def test_add_6_values():70 rbt = RedBlackTree()71 rbt.add(10, None)72 rbt.add(6, None)73 rbt.add(8, None)74 check_invariants(rbt._tree)75 assert (rbt._tree.left.key, rbt._tree.key, rbt._tree.right.key) == (6, 8, 10)76 assert (rbt._tree.left.color, rbt._tree.color, rbt._tree.right.color) == (77 Node.RED,78 Node.BLACK,79 Node.RED,80 )81 rbt.add(4, None)82 check_invariants(rbt._tree)83 assert (84 rbt._tree.left.left.key,85 rbt._tree.left.key,86 rbt._tree.key,87 rbt._tree.right.key,88 ) == (4, 6, 8, 10)89 assert (90 rbt._tree.left.left.color,91 rbt._tree.left.color,92 rbt._tree.color,93 rbt._tree.right.color,94 ) == (Node.RED, Node.BLACK, Node.BLACK, Node.BLACK)95 rbt.add(7, None)96 rbt.add(6.5, None)97 check_invariants(rbt._tree)98 assert in_order(rbt._tree) == [99 (4, Node.BLACK),100 (6, Node.RED),101 (6.5, Node.RED),102 (7, Node.BLACK),103 (8, Node.BLACK),104 (10, Node.BLACK),105 ]106def test_random_sequence():107 random.seed(100)108 rbt = RedBlackTree()109 generated = {}110 for i in range(100):111 k, v = random.randint(0, 100), random.randint(0, 100)112 generated[k] = v113 rbt.add(k, v)114 check_invariants(rbt._tree)115 for k, v in generated.items():...
test.py
Source:test.py
...4 def testInvariants(self):5 node1 = Node(1)6 node2 = Node(2)7 node1.connect(node2)8 check_invariants(node1, True)9 check_invariants(node2, True)10 node3 = Node(3)11 node4 = Node(4)12 node3.connect(node1)13 node4.connect(node1)14 check_invariants(node1, True)15 node2_1 = Node(21)16 node2_2 = Node(22)17 node2.connect(node2_1)18 node2.connect(node2_2)19 for node in [node1, node2, node3, node4, node2_1, node2_2]:20 check_invariants(node, True)21 def testInitialization(self):22 # init_graph(1) fails because it has 0 edges.23 for i in range(2, 8):24 g = init_graph(i)25 check_invariants(g)26if __name__ == "__main__":...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!