def test_setitem_crash(self): from mamba import Bits input = Bits( 465, 0x00095700000000000000003f950000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f5d ) input[363:441] = Bits(78, 0x000000000000000000b0)
def test_sub_bug(self): from mamba import Bits def make_long(x): return x + 2**100 - 2**100 for bitwidth in [2, 8, 32, 64, 100]: res = Bits(bitwidth, 0) - Bits(bitwidth, 1) assert res == Bits(bitwidth, 2**bitwidth - 1) assert Bits(bitwidth, 0) - 1 == res assert Bits(bitwidth, 0) - make_long(1) == res
def bits(draw): if draw(strategies.booleans()): # small bits nbits = draw(strategies.integers(min_value=1, max_value=63)) else: # big bits nbits = draw(strategies.integers(min_value=64, max_value=512)) value = draw( strategies.integers(min_value=-(1 << (nbits - 1)), max_value=(1 << nbits) - 1)) return Bits(nbits, value)
def test_arith_sub(bits): bits1, bits2 = bits op = operator.sub # not commutative res = op(bits1, bits2) assert res == Bits(max(bits1.nbits, bits2.nbits), op(int(bits1), int(bits2)), trunc_int=True) assert op(bits1, int(bits2)) == res assert op(bits1, as_long(bits2)) == res assert op(int(bits1), bits2) == res assert op(as_long(bits1), bits2) == res
def several_bits(draw): if same_bitwidth or draw(strategies.booleans()): # all same bitwidth first = draw(bits()) return [first] + [ Bits( first.nbits, draw( strategies.integers(min_value=-(1 << (first.nbits - 1)), max_value=(1 << first.nbits) - 1))) for i in range(n - 1) ] return [draw(bits()) for i in range(n)]
def test_arith_commutative(bits, op): bits1, bits2 = bits res = op(bits1, bits2) assert res == op(bits2, bits1) # commutativity assert res == Bits(max(bits1.nbits, bits2.nbits), op(int(bits1), int(bits2)), trunc_int=True) assert op(bits1, int(bits2)) == res assert op(bits1, as_long(bits2)) == res assert op(int(bits2), bits1) == res assert op(as_long(bits2), bits1) == res assert op(int(bits1), bits2) == res assert op(as_long(bits1), bits2) == res assert op(bits2, int(bits1)) == res assert op(bits2, as_long(bits1)) == res
def test_concat(self): from mamba import Bits, concat assert concat(Bits(2, 1), Bits(2, 0b10)) == Bits(4, 0b0110) assert concat(Bits(1, 1), Bits(64, 0)) == Bits(65, 1 << 64) assert concat(Bits(1, 1), Bits(128, 1 << 64)) == Bits(129, (1 << 128) | (1 << 64))