def test_is_accepted(self): M = machine.TM(os.path.join("..", "configs", "ex_821.tm")) M.load(machine.TMTape("aa")) M.exec() self.assertTrue(M.is_accepted()) M.load(machine.TMTape("ab")) M.exec() self.assertFalse(M.is_accepted())
def test_reset(self): M = machine.TM(os.path.join("..", "configs", "ex_821.tm")) M.load(machine.TMTape("aa")) M.exec() self.assertNotEqual(M.current_position, 0) self.assertNotEqual(M.current_state, M.start) M.reset() self.assertEqual(M.current_position, 0) self.assertEqual(M.current_state, M.start)
def test_exec(self): M = machine.TM(os.path.join("..", "configs", "ex_821.tm")) M.load(machine.TMTape("aa")) trace = M.exec() expected = [ "q0БaaБ", "⊢Бq1aaБ", "⊢Бaq2aБ", "⊢Бaaq3Б", "Accepted: БaaБ" ] self.assertListEqual(trace, expected)
def test_read(self): T = machine.TMTape("") self.assertEqual(T.read(13), machine.kBLANK) self.assertEqual(T.read(-7), machine.kBLANK) T.write("a", 5) T.write(T.read(6), 6) test = "" for i in range(5): test = test + machine.kBLANK test = test + "a" test = test + machine.kBLANK self.assertEqual(str(T), test)
def test_write(self): T = machine.TMTape("") for i in range(10): if i > 0: T.write("a", i) #T.write(T.read(10), 10) self.assertEqual(str(T), machine.kBLANK + "aaaaaaaaa" + machine.kBLANK) for i in range(10): i = -i if i < 0: T.write("b", i) #T.write(T.read(-10), -10) self.assertEqual( str(T), machine.kBLANK + "bbbbbbbbb" + machine.kBLANK + "aaaaaaaaa" + machine.kBLANK)
import machine import re import os import sys if __name__ == "__main__": print("Please input a valid path name? Path name must be in this format: /home/Documents/....../file.tm") path = input("Enter:") #This line is for testing: "/home/brandon/Documents/5800/CS5800-proj/configs/ex_821.tm" if os.path.isfile(path) and os.access(path, os.R_OK): tm = machine.TM(path) value = input("Please input the tape you want to use?") valuelist = list(value) fox = tm.alpha if (fox.issubset(valuelist)): mytape = machine.TMTape(valuelist) try: tm.load(mytape) for config in tm.exec(): print(config) if 'Rejected:' in config: print("The input: " + value + " entered did NOT the final state. The final state:") print(tm.accept) elif 'Accepted:' in config: print("Congratulations: " + value + " was ACCEPTED.") except machine.InvalidCharacterInTape as e: print(type(e), *e.args) else: print("The entered tape is not a subset of the Language:")
def test_load(self): M = machine.TM(os.path.join("..", "configs", "ex_821.tm")) with self.assertRaises(machine.InvalidCharacterInTape): M.load(machine.TMTape("abc")) self.assertIsNone(M.load(machine.TMTape("ab")))
def test_step(self): M = machine.TM(os.path.join("..", "configs", "ex_821.tm")) M.load(machine.TMTape("ab")) self.assertEqual(M.step(), "⊢"+machine.kBLANK+"q1ab"+machine.kBLANK)
def test_get_c(self): M = machine.TM(os.path.join("..", "configs", "ex_821.tm")) M.load(machine.TMTape("ab")) self.assertEqual(M.get_c(), "q0"+machine.kBLANK+"ab"+machine.kBLANK)
parts = rawpath.split(kMACSEP) first = parts.pop(0) return os.path.join(os.path.expanduser(first), *parts) if __name__ == "__main__": filepath = None w = None dump = False execute = False #parse command line for i in range(len(sys.argv)): if sys.argv[i] == kTOK_TM: filepath = pathfix(sys.argv[i+1]) if sys.argv[i] == kTOK_INPUT: w = sys.argv[i+1] execute = True if sys.argv[i] == kTOK_DUMP: dump = True #load a machine M = machine.TM(filepath) #if there's input output the trace if execute: T = machine.TMTape(w) M.load(T) trace = M.exec() for item in trace: print(item) #if a dump of the config is requested output that if dump: print(M.dumps()) exit(0)