# Copyright (c) 2009 Tim Freeman # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. # # (This is the standard MIT License, copied from # http://www.opensource.org/licenses/mit-license.php on 24 Apr 2007.) import test_util from test_util import assert_in from compile import turing_program, turing_compile, compile_actions from constants import left, right, end, done, truncate assert compile_actions(actions={0:(1, left, "haszero"), 1:(1, left, "hasone"), end:(1, left, "end")}, state_numbers={"haszero":0, "hasone":1, "end":2}, bits_per_stateno=3, state=1) == 0211101 # Verify that we get proper "Error when compiling state xxx" messages. def exercise(): turing_compile(turing_program("trunc", trunc={(0,1,end):("blort", right, "done")})) v = test_util.with_output_to_string(exercise) assert v["exception"] is not None assert_in("Error when compiling state 'trunc'", v["output"]) def exercise2(): turing_compile(turing_program("trunc", trunc={(0,1,end):"trunc"})) v = test_util.with_output_to_string(exercise2) assert v["exception"] is not None assert_in("Could not interpret 'trunc' as a Turing machine action", str(v["exception"])) # Verify that pretty_octal works right. empty_as = turing_program("trunc", trunc={(0,1,end):(truncate, right, "done")}, done=done) c = turing_compile(empty_as, pretty_octal = True) assert c.program == 01313131616163L