def test_recente(): station1 = Node("Westminster") station2 = Node("Waterloo", None, [station1]) station3 = Node("Trafalgar Square", None, [station1, station2]) station4 = Node("Canary Wharf", None, [station2, station3]) assert depth_first_search(station3, station4) == False
def test_graph_with_cycles(): node1 = Node("1") node2 = Node("2") node3 = Node("3") node4 = Node("4", None, [node1]) node5 = Node("5", None, [node2]) node6 = Node("6", None, [node5, node4, node3]) node2.successors = [node6] assert breadth_first_search(node6, node1) == True
def test_strongly_connected_graph(): station1 = Node("Westminster") station2 = Node("Waterloo", None, [station1]) station3 = Node("Trafalgar Square", None, [station1, station2]) station4 = Node("Canary Wharf", None, [station2, station3]) station5 = Node("London Bridge", None, [station4, station3]) station6 = Node("Tottenham Court Road", None, [station5, station4]) assert depth_first_search(station6, station1) == True
def test_one_node_graph(): nodef = Node("F") nodee = Node("E") noded = Node("D") nodec = Node("C", None, [nodef]) nodeb = Node("B", None, [nodee]) nodea = Node("A", None, [nodeb, nodec, noded]) assert breadth_first_search(nodef, nodef) == True
def test_two_unconnected_nodes_in_graph(): nodef = Node("F") nodee = Node("E") noded = Node("D") nodec = Node("C", None, [nodef]) nodeb = Node("B", None, [nodee]) nodea = Node("A", None, [nodeb, nodec, noded]) assert breadth_first_search(nodef, nodee) == False
def test_5_node_list_input(): node1 = Node(1) node2 = Node(2, node1) node3 = Node(3, node2) node4 = Node(4, node3) node5 = Node(5, node4) #Input: 5 -> 4 -> 3 -> 2 -> 1 result = reverse_linked_list(node5) #Expected Output: 1 2 3 4 5 #Para que possa fazer um assert é preciso criar uma string com esse modelo expected_output = '1 2 3 4 5 ' #agora vemos qual a string que result produz string_result = '' while result: string_result += str(result.value) + ' ' result = result.successor assert string_result == expected_output
def test_1_node_list_input(): # Case 2: 1-node list input # Expected Output: 0 node = Node(0) result = reverse_linked_list(node) expected_output = '0 ' #agora vemos qual a string que result produz string_result = '' while result: string_result += str(result.value) + ' ' result = result.successor #por fim fazemos o assert assert string_result == expected_output
def test_nextnode_in_visited_nodes(): node5 = Node("5") node4 = Node("4", None, [node5]) node3 = Node("3", None, [node4]) node1 = Node("1",None, [node4, node3, node5]) node2 = Node("2", None, [node1, node3, node4]) node0 = Node("0", None, [node2, node5]) length_by_edge = { (node0, node2): 3, (node0, node5): 10, (node1, node4): 7, (node1, node3): 8, (node1, node5): 9, (node2, node1): 5, (node2, node3): 2, (node2, node4): 4, (node3, node4): 1, (node4, node5): 1 } assert shortest_path_length(length_by_edge, node0, node5) == 7
def test_Cooking_with_InteractivePython(): expected_output = '3/4 cup milk 1 egg 1 Tbl oil heat griddle 1 cup mix pour 1/4 cup heat syrup turn when bubbly eat ' milk = Node("3/4 cup milk") egg = Node("1 egg") oil = Node("1 Tbl oil") mix = Node("1 cup mix") syrup = Node("heat syrup") griddle = Node("heat griddle") pour = Node("pour 1/4 cup") turn = Node("turn when bubbly") eat = Node("eat") milk.outgoing_nodes = [mix] egg.outgoing_nodes = [mix] oil.outgoing_nodes = [mix] mix.incoming_nodes = [milk, egg, oil] mix.outgoing_nodes = [syrup, pour] griddle.outgoing_nodes = [pour] pour.incoming_nodes = [mix, griddle] pour.outgoing_nodes = [turn] turn.incoming_nodes = [pour] turn.outgoing_nodes = [eat] syrup.incoming_nodes = [mix] syrup.outgoing_nodes = [eat] eat.incoming_nodes = [syrup, turn] """ try: [print(x.value, end=" ") for x in topological_ordering([milk, egg, oil, mix, syrup, griddle, pour, turn, eat])] except Exception as e: print(e) print() """ string_result = '' for x in topological_ordering([milk, egg, oil, mix, syrup, griddle, pour, turn, eat]): string_result += str(x.value) + ' ' assert string_result == expected_output
def test_Wikipedia_graph(): expected_output = '5 7 3 11 8 10 2 9 ' five = Node(5) seven = Node(7) three = Node(3) eleven = Node(11) eight = Node(8) two = Node(2) nine = Node(9) ten = Node(10) five.outgoing_nodes = [eleven] seven.outgoing_nodes = [eleven, eight] three.outgoing_nodes = [eight, ten] eleven.incoming_nodes = [five, seven] eleven.outgoing_nodes = [two, nine, ten] eight.incoming_nodes = [seven, three] eight.outgoing_nodes = [nine] two.incoming_nodes = [eleven] nine.incoming_nodes = [eleven, eight] ten.incoming_nodes = [eleven, three] #try: # [print(x.value, end=" ") for x in topological_ordering([five, seven, three, eleven, eight, two, nine, ten])] # print(type(x)) #except Exception as e: # print(e) string_result = '' for x in topological_ordering([five, seven, three, eleven, eight, two, nine, ten]): string_result += str(x.value) + ' ' assert string_result == expected_output
def test_GeekforGeeks_example(): expected_output = '4 5 0 2 3 1 ' five = Node(5) zero = Node(0) four = Node(4) one = Node(1) two = Node(2) three = Node(3) five.outgoing_nodes = [two, zero] four.outgoing_nodes = [zero, one] two.incoming_nodes = [five] two.outgoing_nodes = [three] zero.incoming_nodes = [five, four] one.incoming_nodes = [four, three] three.incoming_nodes = [two] three.outgoing_nodes = [one] """ try: [print(x.value, end=" ") for x in topological_ordering([zero, one, two, three, four, five]) string_result += str(x.value) + ' '] except Exception as e: string_result += str(x.value) + ' ' print(e) print() """ string_result = '' for x in topological_ordering([zero, one, two, three, four, five]): string_result += str(x.value) + ' ' assert string_result == expected_output
from python_programs.node import Node from python_programs.depth_first_search import depth_first_search nodef = Node("F") nodee = Node("E") noded = Node("D") nodec = Node("C", None, [nodef]) nodeb = Node("B", None, [nodee]) nodea = Node("A", None, [nodeb, nodec, noded]) def test_strongly_connected_graph(): station1 = Node("Westminster") station2 = Node("Waterloo", None, [station1]) station3 = Node("Trafalgar Square", None, [station1, station2]) station4 = Node("Canary Wharf", None, [station2, station3]) station5 = Node("London Bridge", None, [station4, station3]) station6 = Node("Tottenham Court Road", None, [station5, station4]) assert depth_first_search(station6, station1) == True def test_branching_graph(): assert depth_first_search(nodea, nodee) == True def test_two_unconnected_nodes_in_graph(): assert depth_first_search(nodef, nodee) == False def test_one_node_graph(): assert depth_first_search(nodef, nodef) == True def test_graph_with_cycles():
def test_1_node_list(): node = Node(0) assert detect_cycle(node) == False
def test_2_node_list_with_no_cycle(): node6 = Node(6) node7 = Node(7, node6) assert detect_cycle(node7) == False
from python_programs.node import Node from python_programs.detect_cycle import detect_cycle """ Driver to test reverse linked list """ node1 = Node(1) node2 = Node(2, node1) node3 = Node(3, node2) node4 = Node(4, node3) node5 = Node(5, node4) def test_5_node_list_input_with_no_cycle(): assert detect_cycle(node5) == False def test_5_node_list_input_with_cycle(): node1.successor = node5 assert detect_cycle(node5) == True def test_2_node_list_with_cycle(): node1.successor = node2 assert detect_cycle(node2) == True def test_2_node_list_with_no_cycle(): node6 = Node(6) node7 = Node(7, node6) assert detect_cycle(node7) == False
from python_programs.node import Node from python_programs.shortest_path_length import shortest_path_length node1 = Node("1") node5 = Node("5") node4 = Node("4", None, [node5]) node3 = Node("3", None, [node4]) node2 = Node("2", None, [node1, node3, node4]) node0 = Node("0", None, [node2, node5]) length_by_edge = { (node0, node2): 3, (node0, node5): 10, (node2, node1): 1, (node2, node3): 2, (node2, node4): 4, (node3, node4): 1, (node4, node5): 1 } def test_One_path(): assert shortest_path_length(length_by_edge, node0, node1) == 4 def test_Multiple_path(): assert shortest_path_length(length_by_edge, node0, node5) == 7 def test_Start_point_is_same_as_end_point(): assert shortest_path_length(length_by_edge, node2, node2) == 0