# 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.) from test_util import with_output_to_string, assert_equals, expect_exception from turing_tape import turing_tape,binarytape, uninterleave_2, interleave_2 from turing_tape import tape_concat, tape_tuple, tape_nth, make_tape from bits import parsebinary t1 = turing_tape(length=3, bits=6) t1b = turing_tape(length=3, bits=6) assert t1 == t1b assert not (t1 != t1b) assert turing_tape(length=4, bits=6) != t1 # Check that dicts to work for tapes. d = {} d[t1]="x" assert_equals(d[t1b], "x") #assert_equals(str(t1), "turing_tape(length=3, bits=6)") assert_equals(str(t1), "<110>") # eval doesn't work with the new shorter representation. #assert_equals(eval(str(t1)), t1) assert_equals(repr(t1), str(t1)) assert_equals(str(binarytape("001100")), "<001100>") assert_equals(binarytape("101"), turing_tape(length=3, bits=5)) def checkinterleave(tape): assert_equals(uninterleave_2(interleave_2(tape)), tape) def t(length, bits): return turing_tape(length=length, bits=bits) checkinterleave(t(0, 0)) checkinterleave(t(10, 234)) # Check that there's a meaningful error report when the argument to # bits_interleave_2 is None. s = str(with_output_to_string(lambda: interleave_2(None))["exception"]) assert "Couldn't unpack tape None" in s, "s is %r" % (s,) assert_equals(tape_concat(t(2,2), t(3, 4)), t(5, 20)) tup = tape_tuple(t(2, 2), t(3, 4)) assert_equals(tup, t(12, (1<<4) # The 4. 4 = 1<<2. + (1 << (6+2+2)) # The 2. 2 = 1<<1. + (1<<(6+1)))) # The separator assert_equals(tape_tuple(t(2, 0), t(1, 1L), t(1, 1)), t(12, parsebinary("000010011001"))) assert_equals(tape_nth(tup, 0), t(2, 2)) assert_equals(tape_nth(tup, 1), t(3, 4)) def checktuple(*args): tup = tape_tuple(*args) for i in range(len(args)): assert_equals(tape_nth(tup, i), args[i]) checktuple(t(2,2),t(2,3),t(1,1),t(0,0),t(12,1023),t(5,13)) assert_equals(make_tape(0), t(1, 0)) assert_equals(make_tape(13), t(4, 13)) # The argument to make_tape should be a long. This was to detect # some old bug. assert "must be a string or a number" in str(expect_exception(lambda:make_tape((1,0))))