# 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 assert_equals, expect_exception, with_output_to_string from bits import log2 import bits from turing_tape import turing_tape assert_equals(log2(8), 3) assert_equals(bits.log2(1), 0) assert_equals(bits.log2(0.5), -1) assert_equals(bits.log2(0.75), -1) assert_equals(bits.log2(0.1), -4) assert_equals(bits.getbits(0xabcd, 8, 4), 0xb) assert_equals(bits.getbits(0xabcd, 0, 4), 0xd) assert_equals(bits.setbit(0x456, 0, 1), 0x457) assert_equals(bits.setbit(0x456, 0, 0), 0x456) assert_equals(bits.setbit(0x456, 4, 0), 0x446) assert_equals(bits.setbit(0x456, 4, 1), 0x456) assert_equals(bits.setbits(0x406, start=4, bits=0xa), 0x4a6) assert_equals(bits.exact_bits(3), [0, 1, 2, 3, 4, 5, 6, 7]) assert_equals(bits.many_bits(2, [bits.exact_bits, bits.exact_bits]), \ [[0, 0], [0, 1], [0, 2], [0, 3], [0, 0], [0, 1], [1, 0], [1, 1], [0, 0], [1, 0], [2, 0], [3, 0]]) def argmax_fn(x): return -((x-3) ** 2) assert_equals(bits.argmax(argmax_fn, range(5)), 3) assert_equals(bits.argmax(argmax_fn, range(5), paranoid=True), 3) expect_exception(lambda: bits.argmax(argmax_fn, [])) def argmax_fn2(x): return x ** 2 s = str(expect_exception( lambda:bits.argmax(argmax_fn2, range(-2, 3), paranoid=True))) assert s == "The two values -2 and 2 both give the maximal result 4" def argmax_fn3(x): return {0:1, 1:1, 2:2}[x] assert_equals(2, bits.argmax(argmax_fn3, range(3), paranoid=True)) # It is okay to have the same maximum more than once even if we're # paranoid, so long as the argument is the same in that case. assert_equals(1, bits.argmax(lambda x: x, [1, 1], paranoid=True)) assert_equals(bits.parsebinary(""), 0) assert_equals(bits.parsebinary("1011"), 11) s = str(expect_exception(lambda:bits.parsebinary("ouch"))) assert "invalid literal for" in s, "bad s %r" % (s,) assert "ouch" in s, "bad s %r" % (s,) assert_equals(bits.unparsebinary(0), "0") assert_equals(bits.unparsebinary(5), "101") # Check that I don't have the digits in the reverse order. assert_equals(bits.unparsebinary(6), "110") bits.ensure(True) assert "Doesnt_match" in repr( expect_exception(lambda: bits.ensure(False))) d = dict(a=3, b=4) assert_equals(bits.copy_add(d, "c", 7), dict(a=3,b=4,c=7)) assert_equals(d, dict(a=3,b=4)) assert_equals(bits.copy_add_all(d, dict(b=5,c=7)), dict(a=3,b=5,c=7)) assert_equals(d, dict(a=3,b=4))