Exemplo n.º 1
0
 def test_tape_str_assignment(self):
     tm = TuringMachine() # Without rules
     tape = __import__("string").ascii_letters
     tm.tape = tape
     assert tm.tape == dict(enumerate(tape))
     for symbol in tape:
         assert tm.scan() == symbol
         tm.perform("R")
Exemplo n.º 2
0
 def test_tape_str_assignment(self):
     tm = TuringMachine()  # Without rules
     tape = __import__("string").ascii_letters
     tm.tape = tape
     assert tm.tape == dict(enumerate(tape))
     for symbol in tape:
         assert tm.scan() == symbol
         tm.perform("R")
Exemplo n.º 3
0
 def test_perform_one_task(self, task, index_delta,
                                 tape_list_before, tape_dict_after):
     tm = TuringMachine() # Without rules
     assert tm.index == 0
     assert tm.tape == {}
     tm.tape = tape_list_before
     assert tm.tape == dict(enumerate(tape_list_before))
     tm.perform(task)
     assert tm.index == index_delta
     assert tm.tape == tape_dict_after
Exemplo n.º 4
0
 def test_perform_one_task(self, task, index_delta, tape_list_before,
                           tape_dict_after):
     tm = TuringMachine()  # Without rules
     assert tm.index == 0
     assert tm.tape == {}
     tm.tape = tape_list_before
     assert tm.tape == dict(enumerate(tape_list_before))
     tm.perform(task)
     assert tm.index == index_delta
     assert tm.tape == tape_dict_after
Exemplo n.º 5
0
 def test_tape_list_assignment_with_blank(self, blank_index):
     tm = TuringMachine() # Without rules
     tape = list(__import__("string").ascii_letters)
     tape[blank_index] = "None"
     expected_tape = {k: v for k, v in enumerate(tape)
                           if k != blank_index % len(tape)}
     tm.tape = tape
     assert len(tm.tape) == len(tape) - 1
     assert tm.tape == expected_tape
     for symbol in tape:
         assert tm.scan() == symbol
         tm.perform("R")
Exemplo n.º 6
0
 def test_perform_and_move_unknown_task(self, task):
     tm_perform = TuringMachine() # Without rules
     with raises(ValueError):
         tm_perform.perform(task)
     if task.strip(): # Parsing rule makes sense
         tm_move_parsed = TuringMachine("a None -> {} a".format(task))
         with raises(ValueError):
             tm_move_parsed.move()
     tm_move_setitem = TuringMachine()
     tm_move_setitem.mconf = "a"
     tm_move_setitem["a", "None"] = ([task], "a")
     with raises(ValueError):
         tm_move_setitem.move()
Exemplo n.º 7
0
 def test_perform_and_move_unknown_task(self, task):
     tm_perform = TuringMachine()  # Without rules
     with raises(ValueError):
         tm_perform.perform(task)
     if task.strip():  # Parsing rule makes sense
         tm_move_parsed = TuringMachine("a None -> {} a".format(task))
         with raises(ValueError):
             tm_move_parsed.move()
     tm_move_setitem = TuringMachine()
     tm_move_setitem.mconf = "a"
     tm_move_setitem["a", "None"] = ([task], "a")
     with raises(ValueError):
         tm_move_setitem.move()
Exemplo n.º 8
0
 def test_tape_list_and_dict_assignment_without_blank(self, tape_list):
     tape_dict = dict(enumerate(tape_list))
     tm_list = TuringMachine() # Without rules
     tm_dict = TuringMachine()
     assert tm_list.index == tm_dict.index == 0
     assert tm_list.tape == tm_dict.tape == {}
     tm_list.tape = tape_list
     tm_dict.tape = tape_dict
     assert tm_list.index == tm_dict.index == 0
     assert tm_list.tape == tm_dict.tape == tape_dict
     for symbol in tape_list:
         assert tm_list.scan() == tm_dict.scan() == symbol
         tm_list.perform("R")
         tm_dict.perform("R")
Exemplo n.º 9
0
 def test_tape_list_assignment_with_blank(self, blank_index):
     tm = TuringMachine()  # Without rules
     tape = list(__import__("string").ascii_letters)
     tape[blank_index] = "None"
     expected_tape = {
         k: v
         for k, v in enumerate(tape) if k != blank_index % len(tape)
     }
     tm.tape = tape
     assert len(tm.tape) == len(tape) - 1
     assert tm.tape == expected_tape
     for symbol in tape:
         assert tm.scan() == symbol
         tm.perform("R")
Exemplo n.º 10
0
 def test_tape_list_and_dict_assignment_without_blank(self, tape_list):
     tape_dict = dict(enumerate(tape_list))
     tm_list = TuringMachine()  # Without rules
     tm_dict = TuringMachine()
     assert tm_list.index == tm_dict.index == 0
     assert tm_list.tape == tm_dict.tape == {}
     tm_list.tape = tape_list
     tm_dict.tape = tape_dict
     assert tm_list.index == tm_dict.index == 0
     assert tm_list.tape == tm_dict.tape == tape_dict
     for symbol in tape_list:
         assert tm_list.scan() == tm_dict.scan() == symbol
         tm_list.perform("R")
         tm_dict.perform("R")
Exemplo n.º 11
0
 def test_tape_dict_assignment(self, blank_index, delta):
     tm = TuringMachine() # Without rules
     tape = list(__import__("string").ascii_letters)
     tape[blank_index] = "None"
     expected_tape = {k + delta: v for k, v in enumerate(tape)
                                   if k != blank_index % len(tape)}
     tape_dict = dict(enumerate(tape, delta))
     tm.tape = tape_dict
     assert tm.tape == expected_tape
     assert tm.index == 0
     assert tm.scan() == tape[-delta] if delta <= 0 else "None"
     for task in delta * "R" + -delta * "L":
         tm.perform(task)
     assert tm.index == delta
     for symbol in tape:
         assert tm.scan() == symbol
         tm.perform("R")
Exemplo n.º 12
0
 def test_tape_dict_assignment(self, blank_index, delta):
     tm = TuringMachine()  # Without rules
     tape = list(__import__("string").ascii_letters)
     tape[blank_index] = "None"
     expected_tape = {
         k + delta: v
         for k, v in enumerate(tape) if k != blank_index % len(tape)
     }
     tape_dict = dict(enumerate(tape, delta))
     tm.tape = tape_dict
     assert tm.tape == expected_tape
     assert tm.index == 0
     assert tm.scan() == tape[-delta] if delta <= 0 else "None"
     for task in delta * "R" + -delta * "L":
         tm.perform(task)
     assert tm.index == delta
     for symbol in tape:
         assert tm.scan() == symbol
         tm.perform("R")
Exemplo n.º 13
0
 def test_copy_without_mconf_nor_rules(self):
     tm = TuringMachine()
     tm.tape = dict(enumerate("TestTestTest", -2))
     tm.perform("L")
     tm.perform("P0")
     tmcp = tm.copy()
     # Rules and inverted rules
     assert list(tmcp.items()) == list(tm.items()) == []
     assert list(tmcp.inv_dict.items()) == list(tm.inv_dict.items()) == []
     # Complete configuration
     assert tmcp.tape == dict(enumerate("T0stTestTest", -2)) == tm.tape
     assert tmcp.index == -1 == tm.index
     tmcp.perform("L")
     tmcp.perform("P1")
     assert tmcp.tape != tm.tape
     assert tmcp.index != tm.index
     assert tmcp.tape == dict(enumerate("10stTestTest", -2))
     assert tmcp.index == -2
     assert not hasattr(tm, "mconf")
     assert not hasattr(tmcp, "mconf")
Exemplo n.º 14
0
 def test_copy_without_mconf_nor_rules(self):
     tm = TuringMachine()
     tm.tape = dict(enumerate("TestTestTest", -2))
     tm.perform("L")
     tm.perform("P0")
     tmcp = tm.copy()
     # Rules and inverted rules
     assert list(tmcp.items()) == list(tm.items()) == []
     assert list(tmcp.inv_dict.items()) == list(tm.inv_dict.items()) == []
     # Complete configuration
     assert tmcp.tape == dict(enumerate("T0stTestTest", -2)) == tm.tape
     assert tmcp.index == -1 == tm.index
     tmcp.perform("L")
     tmcp.perform("P1")
     assert tmcp.tape != tm.tape
     assert tmcp.index != tm.index
     assert tmcp.tape == dict(enumerate("10stTestTest", -2))
     assert tmcp.index == -2
     assert not hasattr(tm, "mconf")
     assert not hasattr(tmcp, "mconf")
Exemplo n.º 15
0
 def test_copy(self):
     tm = TuringMachine("q1      -> P0 R q2\n" "q2 None -> P1 R q1")
     tm.tape = "Test"
     tm.perform("R")
     tm.perform("P1")
     tmcp = tm.copy()
     # Rules and inverted rules
     assert list(tmcp.items()) == list(tm.items())
     assert list(tmcp.inv_dict.items()) == list(tm.inv_dict.items())
     # Complete configuration
     assert tmcp.tape == dict(enumerate("T1st")) == tm.tape
     assert tmcp.index == 1 == tm.index
     assert tmcp.mconf == "q1" == tm.mconf
     tm.move()
     assert tmcp.tape != tm.tape
     assert tmcp.index != tm.index
     assert tmcp.mconf != tm.mconf
     assert tm.tape == dict(enumerate("T0st"))
     assert tm.index == 2
     assert tm.mconf == "q2"
Exemplo n.º 16
0
 def test_copy(self):
     tm = TuringMachine("q1      -> P0 R q2\n"
                        "q2 None -> P1 R q1")
     tm.tape = "Test"
     tm.perform("R")
     tm.perform("P1")
     tmcp = tm.copy()
     # Rules and inverted rules
     assert list(tmcp.items()) == list(tm.items())
     assert list(tmcp.inv_dict.items()) == list(tm.inv_dict.items())
     # Complete configuration
     assert tmcp.tape == dict(enumerate("T1st")) == tm.tape
     assert tmcp.index == 1 == tm.index
     assert tmcp.mconf == "q1" == tm.mconf
     tm.move()
     assert tmcp.tape != tm.tape
     assert tmcp.index != tm.index
     assert tmcp.mconf != tm.mconf
     assert tm.tape == dict(enumerate("T0st"))
     assert tm.index == 2
     assert tm.mconf == "q2"