Ejemplo n.º 1
0
def test_new_instance():
	tape1 = Tape("B", ['a', 'b'], ['b', 'b'])
	tape2 = Tape("B", ['x', 'y', 'z'], ['x', 'x'])
	tapes = [tape1, tape2]
	instance = Instance(None, 'A', tapes)
	assert instance.current_state == 'A'
	assert instance.tapes is not tapes 
Ejemplo n.º 2
0
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(None, 'A', tapes)
	assert not instance.is_transition_valid(transition)
Ejemplo n.º 3
0
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(None, 'A', tapes)
	new_instance = instance.apply_transition(transition)
	assert new_instance == None
Ejemplo n.º 4
0
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']
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
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(None, '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'
Ejemplo n.º 7
0
 def get_initial_configurations(self, cmdline_args):
     tapes = []
     for i in range(0, self.tapes_count):
         tape = Tape(self.whitespace, self.tapes_alphabet[i],
                     list(cmdline_args[i]))
         tapes.append(tape)
     return [Instance(self, self.initial_state, tapes)]
Ejemplo n.º 8
0
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]
	tm = TuringMachine(["A", "B"], "A", [], "E", 2, (('x', 'y'), ('x', 'y')), transitions)
	instance = Instance(tm, 'A', tapes)
	valid_transitions = instance.get_valid_transitions()
	assert transition1 in valid_transitions
	assert transition2 not in valid_transitions
Ejemplo n.º 9
0
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)")
Ejemplo n.º 10
0
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
Ejemplo n.º 11
0
def test_get_content_of_non_empty_tape_at_start_with_head_moved_to_left():
    tape = Tape('B', ['a', 'b', 'X', 'B'], ['a', 'b'])
    tape.move_left()
    assert tape.get_content() == 'B'
    assert tape.position == 0
Ejemplo n.º 12
0
def test_replace_content():
    tape = Tape('B', ['a', 'b', 'X', 'B'], [])
    new_content = ['b', 'a']
    tape.replace_content(new_content)
    assert tape.content == new_content
    assert tape.content is not new_content
Ejemplo n.º 13
0
def test_get_content_of_empty_tape():
    tape = Tape('B', ['a', 'b', 'X', 'B'], [])
    assert tape.get_content() == 'B'
Ejemplo n.º 14
0
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'
Ejemplo n.º 15
0
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
Ejemplo n.º 16
0
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
Ejemplo n.º 17
0
def test_move_head_invalid_direction():
    tape = Tape('B', ['a', 'b', 'X', 'B'], ['a', 'b'])
    with pytest.raises(ValueError):
        tape.move_head('T')