def make_child_by_variable_command(list1_name: str, list2_name: str, node_expression: str, memory: Memory, variables: dict): list1 = variables[list1_name] list2 = variables[list2_name] node, _ = list2.find_node_by_expression(node_expression) memory.set_node_down(node, memory.get_node_down(list1.root))
def test_status(self): memory = Memory(4) self.assertEqual(memory.status(), [ (None, None, 4, None), (None, None, 8, None), (None, None, 12, None), (None, None, None, None), ])
def make_child_by_list_expression_command(list_expression: str, list2_name: str, node_expression: str, memory: Memory, variables: dict): list2 = variables[list2_name] list1 = GeneralList.convert_expression_to_general_list( memory, list_expression) node, _ = list2.find_node_by_expression(node_expression) memory.set_node_down(node, memory.get_node_down(list1.root))
def test_allocate_node(self): memory = Memory(4) node1 = memory.allocate_node() memory.set_node_label(node1, "A") node2 = memory.allocate_node() memory.set_node_label(node2, "B") self.assertEqual(memory.status(), [ (None, "A", None, None), (None, "B", None, None), (None, None, 12, None), (None, None, None, None), ])
def test_push(self): mem = Memory(5) stack = Stack(mem) stack.push("A") stack.push("B") stack.push("C") self.assertEqual("C", stack.top())
def test_top_raising_error(self): mem = Memory(5) stack = Stack(mem) stack.push("A") stack.push("B") stack.pop() stack.pop() self.assertRaises(StackEmptyError, stack.top)
def is_list_expression_valid(list_expression: str) -> bool: stack = Stack(Memory(len(list_expression))) for ch in list_expression: if ch == "(": stack.push(ch) elif ch == ")": if stack.is_empty(): return False else: stack.pop() if stack.is_empty(): return True else: return False
def set_node_label_command(var_name1: str, node_expression: str, label: str, memory: Memory, variables: dict): list1 = variables[var_name1] node, _ = list1.find_node_by_expression(node_expression) memory.set_node_label(node, label)
def garbage_collect_command(memory: Memory, variables: dict): memory.garbage_collect(*variables.values())
def delete_command(var_name: str, node_expression: str, memory: Memory, variables: dict): general_list = variables[var_name] node, _ = general_list.find_node_by_expression(node_expression) memory.set_node_down(node, None)
def __init__(self, memory_size): self.__memory = Memory(memory_size) self.__variables = {}
def test_garbage_collection(self): memory = Memory(90) list1 = GeneralList.convert_expression_to_general_list( memory, "(a(b(c)d)e(AB(((F)E(H))C(D)))f(g)((i)h))") memory.garbage_collect(list1) node, _ = list1.find_node_by_expression("(***(***") list2 = GeneralList.convert_expression_to_general_list( memory, "((MN(P))Q(R)(ST(U)))") node2, _ = list2.find_node_by_expression("(***(***") memory.set_node_label(node, "X") memory.set_node_down(node, None) memory.set_node_label(node2, "Y") memory.set_node_down(node2, None) memory.garbage_collect(list1, list2) memory.set_node_down(node2, list1.root) memory.garbage_collect(list2) print(list1, list2)
def test_convert_expression_to_general_list(self): memory = Memory(100) result_list = GeneralList.convert_expression_to_general_list( memory, "(a(b(c)d)ef(g)((i)h))") self.assertEqual(str(result_list), "(a(b(c)d)ef(g)((i)h))")
def test_allocate_node_raising_error(self): memory = Memory(5) for _ in range(5): memory.allocate_node() self.assertRaises(NotEnoughMemoryNodesError, memory.allocate_node)
def test_free_node(self): memory = Memory(4) node_a = memory.allocate_node() memory.set_node_label(node_a, "A") node_b = memory.allocate_node() memory.set_node_label(node_b, "B") memory.free_node(node_a) memory.set_node_label(memory.allocate_node(), "C") memory.set_node_label(memory.allocate_node(), "D") memory.free_node(node_b) memory.set_node_label(memory.allocate_node(), "E") self.assertEqual(memory.status(), [ (None, "C", None, None), (None, "E", None, None), (None, "D", None, None), (None, None, None, None), ])
def test_find_node_by_expression(self): memory = Memory(100) result_list = GeneralList.convert_expression_to_general_list( memory, "(a(b(c)d)ef(g)((i)h))") _, actual = result_list.find_node_by_expression("(*(*(*") self.assertEqual(actual, "c")