def test_new_instance(): tape1 = Tape("B", ['a', 'b'], ['b', 'b']) tape2 = Tape("B", ['x', 'y', 'z'], ['x', 'x']) tapes = [tape1, tape2] instance = Instance('A', tapes) assert instance.current_state == 'A' assert instance.tapes is not tapes
def __init__(self, program, start_index=0, initial_state='i1', empty_value='_', sigterm='SIGTERM'): """Create a new A-Machine Args: program: instruction dictionary start_index: integer index where the head should start initial_state: initial state of the A-Machine empty_value: the value that the machine's tape will treat as "empty" sigterm: the state for which the machine should stop running """ self.program = program self.start_index = start_index self.index = start_index self.initial_state = initial_state self.state = initial_state self.empty_value = empty_value self.tape = Tape(empty_value=empty_value) self.sigterm = sigterm
def setup_method(self): """ setup any state tied to the execution of the given method in a class. setup_method is invoked for every test method of a class. """ states = ['q0', 'q1', 'q2', 'q3', 'q4'] transitions = [] transitions_description = [ 'q0 q0 a a R B a R B B S', 'q0 q1 B B S B B L B B S', 'q0 q2 b b R B B S B b R', 'q1 q3 B B S B B S B B S', 'q2 q2 b b R B B S B b R', 'q2 q4 c c S B B L B B L', 'q4 q4 c c R a a L b b L', 'q4 q3 B B S B B S B B S' ] for description in transitions_description: splited_description = description.split() transition = Transition(splited_description[0], splited_description[1]) for tape_part in zip(*(splited_description[2:][i::3] for i in range(3))): transition.add_tape_part(tape_part[0], tape_part[1], tape_part[2]) transitions.append(transition) tapes = [ Tape("B", ['a', 'b', 'c', 'B'], []), Tape("B", ['a', 'B'], []), Tape("B", ['b', 'B'], []), ] self.tm = TuringMachine(states, 'q0', ['q3'], 'B', transitions, tapes)
def test_is_transition_valid_with_invalid_transition(): transition = Transition('A', 'B') transition.add_tape_part('x', 'y', 'L') transition.add_tape_part('y', 'x', 'R') tape1 = Tape("B", ['x', 'y'], ['x', 'y']) tape2 = Tape("B", ['x', 'y'], ['x', 'x']) tapes = [tape1, tape2] instance = Instance('A', tapes) assert not instance.is_transition_valid(transition)
def a_tape(request): tape = Tape('test') tape.access_map = request.param log.debug("created a tape %s", repr(tape)) for item in request.param: log.debug('fixture factory tape access %s', item) assert tape[item.idx] == item.sym log.debug("returning fixture %s", repr(tape)) return tape
def test_apply_transition_invalid_transition(): transition = Transition('A', 'B') transition.add_tape_part('a', 'b', 'L') transition.add_tape_part('x', 'y', 'R') tape1 = Tape("B", ['a', 'b', 'c'], ['a', 'c']) tape2 = Tape("B", ['x', 'y', 'z'], ['z', 'z']) tapes = [tape1, tape2] instance = Instance('A', tapes) new_instance = instance.apply_transition(transition) assert new_instance == None
def test_run_reject(self): tapes = [ Tape("B", ['a', 'b', 'c', 'B'], ['a', 'b']), Tape("B", ['a', 'B'], []), Tape("B", ['b', 'B'], []), ] self.tm.restart(tapes) result = self.tm.run() assert result == True assert self.tm.get_decision() == "Reject"
def test_set_content_empty_tape_left_left_right(): tape = Tape('B', ['a', 'b', 'X', 'B'], []) tape.move_left() tape.move_left() tape.move_right() tape.set_content('a') assert tape.get_content() == 'a' assert tape.position == 1 assert tape.content == ['B', 'a']
def test_restart(self): tapes = [ Tape("B", ['a', 'b', 'c', 'B'], ['c', 'b', 'a']), Tape("B", ['a', 'B'], []), Tape("B", ['b', 'B'], []), ] self.tm.restart(tapes) assert len(self.tm.current_configurations) == 1 assert self.tm.current_configurations[0].current_state == self.tm.initial_state assert self.tm.current_configurations[0].tapes == tapes assert self.tm.current_configurations[0].tapes[0].get_content() == 'c' assert self.tm.current_configurations[0].tapes[1].get_content() == 'B' assert self.tm.current_configurations[0].tapes[2].get_content() == 'B'
def test_apply_transition_valid_transition(): transition = Transition('A', 'B') transition.add_tape_part('a', 'b', 'L') transition.add_tape_part('x', 'y', 'R') tape1 = Tape("B", ['a', 'b', 'c'], ['a', 'c']) tape2 = Tape("B", ['x', 'y', 'z'], ['x', 'z']) tapes = [tape1, tape2] instance = Instance('A', tapes) new_instance = instance.apply_transition(transition) assert new_instance.current_state == 'B' assert new_instance.tapes[0].content == ['B', 'b', 'c'] assert new_instance.tapes[0].get_content() == 'B' assert new_instance.tapes[1].content == ['y', 'z'] assert new_instance.tapes[1].get_content() == 'z'
def test_set_string_empty_tape_left_left_right_a(): tape = Tape('B', ['a', 'b', 'X', 'B'], []) tape.move_left() tape.move_left() tape.move_right() tape.set_content('a') assert "(['B', 'a'])@1" == str(tape)
def __init__(self, initial_state=None, accepting_states=None, blank_symbol=None, tape_data="", transition_data=None): self._current_state = initial_state self._accepting_states = accepting_states if accepting_states else [] self._tape = Tape(tape_data, blank_symbol) self._transitions = TransitionTable() if transition_data: for transition_string in transition_data: self._transitions.add_transition( Transition.from_data_string(transition_string))
def test_get_valid_transitions(): transition1 = Transition('A', 'B') transition1.add_tape_part('x', 'y', 'L') transition1.add_tape_part('y', 'x', 'R') transition2 = Transition('A', 'B') transition2.add_tape_part('x', 'y', 'L') transition2.add_tape_part('x', 'x', 'R') transitions = [transition1, transition2] tape1 = Tape("B", ['x', 'y'], ['x', 'y']) tape2 = Tape("B", ['x', 'y'], ['y', 'x']) tapes = [tape1, tape2] instance = Instance('A', tapes) valid_transitions = instance.get_valid_transitions(transitions) assert transition1 in valid_transitions assert transition2 not in valid_transitions
def test_read(self): tape = Tape() for i in range(10): tape[i] = chr(i) for i in range(10): assert_equal(tape[i], chr(i))
def turing_machine(lines, cmdline_args): input_alphabet = lines[0].split() tape_alphabet = lines[1] whitespace = lines[2] states = lines[3].split() initial_state = lines[4] final_states = lines[5].split() number_of_tapes = lines[6] transitions = [] for description in lines[7:]: splited_description = description.split() transition = TuringTransition(splited_description[0], splited_description[1]) for tape_part in zip(*(splited_description[2:][i::3] for i in range(3))): transition.add_tape_part(tape_part[0], tape_part[1], tape_part[2]) transitions.append(transition) tapes = [] for i in range(0, int(number_of_tapes)): tapes.append(Tape(whitespace, tape_alphabet, list(cmdline_args[i]))) tm = TuringMachine(states, initial_state, final_states, whitespace, transitions) initial_configurations = tm.get_initial_configurations(tapes) tm.load_configurations(initial_configurations) result = tm.run() if result == True: if tm.get_decision() == "Accept": print("Aceitou") else: print("Rejeitou") else: print("Não sei (nunca parou)")
def test_rot13(self): tape = Tape.fromString("abcdefgh$") filename = self.getFile("../../test/xml/rot13_tm.xml") table = StatefulTable.fromXMLFile(filename) machine = TuringMachine(table, tape) machine.run() self.assertEqual("nopqrstu$", machine.tape.asPlainString().strip())
def test_add_one(self): tape = Tape.fromString("459") filename = self.getFile("../../test/xml/add_one_tm.xml") table = StatefulTable.fromXMLFile(filename) machine = TuringMachine(table, tape) machine.run() self.assertEqual("460", machine.tape.asPlainString().strip())
def test_string_length(self): tape = Tape.fromString("abaabba") filename = self.getFile("../../test/xml/string_length_tm.xml") table = StatefulTable.fromXMLFile(filename) machine = TuringMachine(table, tape) machine.run() self.assertEqual("7", machine.tape.asPlainString().strip())
def test_palindrome(self): tape = Tape.fromString("01210") filename = self.getFile("../../test/xml/palindrome_tm.xml") table = StatefulTable.fromXMLFile(filename) machine = TuringMachine(table, tape) machine.run() self.assertEqual("", machine.tape.asPlainString().strip())
def test_clear(self): tape = Tape() for i in range(10): tape[i] = chr(i) for i in range(20): tape[i] = EMPTY_VALUE
class UniversalTuringMachine(object): def __init__(self, initial_state=None, accepting_states=None, blank_symbol=None, tape_data="", transition_data=None): self._current_state = initial_state self._accepting_states = accepting_states if accepting_states else [] self._tape = Tape(tape_data, blank_symbol) self._transitions = TransitionTable() if transition_data: for transition_string in transition_data: self._transitions.add_transition( Transition.from_data_string(transition_string)) def run(self): while not self._current_state in self._accepting_states: symbol_at_head = self._tape.read_from_current_position() transition = self._transitions.get_transition_for( self._current_state, symbol_at_head) self._tape.write_to_current_position(transition.symbol_next) if transition.tape_motion == TapeMotion.RIGHT: self._tape.right() elif transition.tape_motion == TapeMotion.LEFT: self._tape.left() self._current_state = transition.state_next return self._tape
def test_put_fails_on_empty_tape(self): tape = Tape('') tape.put('a')
def test_move_head_stay(): tape = Tape('B', ['a', 'b', 'X', 'B'], ['a', 'b']) tape.move_head('S') assert tape.get_content() == 'a' assert tape.content == ['a', 'b'] assert tape.position == 0
def test_move_head_invalid_direction(): tape = Tape('B', ['a', 'b', 'X', 'B'], ['a', 'b']) with pytest.raises(ValueError): tape.move_head('T')
def test_get_content_of_non_empty_tape(): tape = Tape('B', ['a', 'b', 'X', 'B'], ['a', 'b']) assert tape.get_content() == 'a'
def test_get_fails_when_head_is_out_of_right_bound(self): tape = Tape('123') tape.get() tape.right() tape.get() tape.right() tape.get() tape.right()
def test_read_empty(self): tape = Tape() for i in range(10): assert_equal(tape[i], EMPTY_VALUE)
def test_get_content_of_non_empty_tape_at_end_with_head_moved_to_right(): tape = Tape('B', ['a', 'b', 'X', 'B'], ['a', 'b']) tape.move_right() tape.move_right() assert tape.get_content() == 'B'
def test_write(self): tape = Tape() for i in range(10): tape[i] = chr(i)
def test_init(self): tape = Tape() assert_equal(tape.tape_values, {}) assert_equal(tape.empty_value, EMPTY_VALUE)
def test_set_content_empty_tape(): tape = Tape('B', ['a', 'b', 'X', 'B'], []) tape.set_content('a') assert tape.get_content() == 'a' assert tape.content == ['a'] assert tape.position == 0
def test_get_fails_on_empty_tape(self): tape = Tape('') tape.get()
def test_move_head_right_left(): tape = Tape('B', ['a', 'b', 'X', 'B'], ['a', 'b']) tape.move_head('R') tape.move_head('L') assert tape.get_content() == 'a' assert tape.position == 0