def test_create_with_transitions(): q0 = State() t1 = Transition(q0, 'c', Direction.LEFT) t2 = Transition(q0, 'd', Direction.RIGHT) q1 = State({'a': t1, 'b': t2}) assert q1.transitions['a'] == t1 assert q1.transitions['b'] == t2
def test_create_transition(): q0 = State() q1 = State() #In q0 upon reading a move to q1, output b, and move the tape 1 right q0.create_transition('a', q1, 'b', Direction.RIGHT) assert q0.transitions['a'].new_state == q1 assert q0.transitions['a'].output_letter == 'b' assert q0.transitions['a'].movement_direction == Direction.RIGHT
def test_init(): s1 = State() s2 = State() tm = TuringMachine("ab", s1, s2, s1) assert tm.tape == ['a', 'b', '_'] assert tm.accept_state == s1 assert tm.reject_state == s2 assert tm.current_state == s1
def test_updating(): s1 = State() s2 = State() t = Transition(s2, 'b', Direction.RIGHT) s1.add_transition('a', t) tm = TuringMachine("acd", s1, s2, s1) assert tm.tape == ['a', 'c', 'd', '_'] tm._read('a') assert tm.tape[0] == 'b' assert tm.current_state == s2
def test_tape_never_negative(): s1 = State() s2 = State() t = Transition(s2, 'b', Direction.LEFT) s1.add_transition('a', t) tm = TuringMachine("acd", s1, s2, s1) assert tm.tape == ['a', 'c', 'd', '_'] tm._read('a') assert tm.tape[0] == 'b' assert tm.current_state == s2 assert tm.tape_position == 0
def test_create_multiple_transitions(): q0 = State() q1 = State() q2 = State() q0.create_transition('a', q1, 'b', Direction.RIGHT) q1.create_transition('c', q2, 'd', Direction.LEFT) with pytest.raises(KeyError): q0.transitions['b'] is None assert q0.transitions['a'].new_state.transitions['c'].new_state == q2 assert q1.transitions['c'].new_state == q2 assert q0.transitions['a'].new_state.transitions['c'].output_letter == 'd' assert q1.transitions['c'].output_letter == 'd' assert q0.transitions['a'].new_state.transitions[ 'c'].movement_direction == Direction.LEFT assert q1.transitions['c'].movement_direction == Direction.LEFT
def test_calc(): q0 = State() t1 = Transition(q0, 'a', Direction.RIGHT) q1 = State() q1.add_transition('b', t1) res = q1.calc('b') assert res == t1
def _parse_state(dict, line): #Match a series of valid word characters, followed by an optional space, #and then an optional +-. #This accepts 'q0 ' but not 'qa+' #These are then split into two groups. The name part and the optional #additional information regex = re.compile('(\w*) ?([+-]?)') res = regex.match(line) #Add all states to the dictionary of states #Do a small check name = res.group(1) assert dict != None if name in dict: raise InvalidInputFormat("Two or more states cannot have the same name") dict[name] = State() #Special test for reject state and accept state #Additional characters will be reject by the regex so not need to test return dict[name], res.group(2)
def test_add_transition(): q0 = State() q1 = State() t = Transition(q1, 'b', Direction.RIGHT) q0.add_transition('a', t) assert q0.transitions['a'] == t
import pytest import sys sys.path.append('.') from turingmachine import Transition, Direction, State state = State() def test_creation(): t = Transition(state, 'a', Direction.RIGHT) assert t.new_state == state assert t.output_letter == 'a' assert t.movement_direction == Direction.RIGHT def test_malformed_creation(): with pytest.raises(ValueError): t = Transition(None, 'a', Direction.LEFT) with pytest.raises(ValueError): t2 = Transition(state, None, Direction.RIGHT) with pytest.raises(ValueError): t3 = Transition(state, 'a', 'c')
def create_example_from_spec(): q0 = State() q1 = State() qa = State() qr = State() q0.create_transition('a', q0, 'a', Direction.RIGHT) q0.create_transition('b', q1, 'b', Direction.RIGHT) q0.create_transition('_', qr, '_', Direction.LEFT) q1.create_transition('a', qr, 'a', Direction.LEFT) q1.create_transition('b', qr, 'a', Direction.LEFT) q1.create_transition('_', qa, 'b', Direction.LEFT) return q0, q1, qa, qr