def test_neg_assign(): x = Bits(4)(-1) assert x == 0b1111 assert x.uint() == 0b1111 x = Bits(4)(-2) assert x == 0b1110 assert x.uint() == 0b1110
def test_set_single_bit(): x = Bits(4)(0b1100) x[3] = 0 assert x.uint() == 0b0100 x[2] = 1 assert x.uint() == 0b0100 x[1] = 1 assert x.uint() == 0b0110
def test_gt(): x = Bits(4)(0b1100) y = Bits(4)(0b0011) assert x.uint() > y.uint() assert x.uint() > 2 assert x > y.uint() assert x > 2 assert x > y assert 9 > y
def test_lt(): x = Bits(4)(0b1100) y = Bits(4)(0b0011) assert y.uint() < x.uint() assert y.uint() < 10 assert y < x.uint() assert y < 10 assert y < x assert 1 < y
def test_ne(): x = Bits(4)(0b1100) y = Bits(4)(0b0011) # TODO: check width? assert x.uint() != y.uint() assert x != y # added for bug z = Bits(1)(0) assert z.uint() != 1L assert z != 1L assert 5 != x assert z != None assert Bits(4)(0) != None
def test_set_slice(): x = Bits(4)(0b1100) x[:] = 0b0010 assert x.uint() == 0b0010 x[2:4] = 0b11 assert x.uint() == 0b1110 x[0:1] = 0b1 assert x.uint() == 0b1111 x[1:3] = 0b10 assert x.uint() == 0b1101 # check open ended ranges x[1:] = 0b001 assert x.uint() == 0b0011 x[:3] = 0b110 assert x.uint() == 0b0110 with pytest.raises(ValueError): x[1:3] = 0b110 x[:] = 0b1111 assert x.uint() == 0b1111 with pytest.raises(ValueError): x[:] = 0b10000 with pytest.raises(IndexError): x[::2] = 0b00 assert x == 0b1010
def test_set_slice(): x = Bits(4)(0b1100) x[:] = 0b0010 assert x.uint() == 0b0010 x[2:4] = 0b11 assert x.uint() == 0b1110 x[0:1] = 0b1 assert x.uint() == 0b1111 x[1:3] = 0b10 assert x.uint() == 0b1101 # check open ended ranges x[1:] = 0b001 assert x.uint() == 0b0011 x[:3] = 0b110 assert x.uint() == 0b0110 with pytest.raises( ValueError ): x[1:3] = 0b110 x[:] = 0b1111 assert x.uint() == 0b1111 with pytest.raises( ValueError ): x[:] = 0b10000 with pytest.raises( IndexError ): x[::2] = 0b00 assert x == 0b1010
def test_return_type(): x = Bits(8)(0b1100) Bits1 = Bits(1) Bits4 = Bits(4) assert isinstance(x.uint(), int) assert isinstance(x.int(), int) assert isinstance(x[1:2], Bits1) assert isinstance(x[0:4], Bits4) assert isinstance(x[2], Bits1)
def test_return_type(): x = Bits(8)(0b1100) Bits1 = Bits(1) Bits4 = Bits(4) assert isinstance( x.uint(), int ) assert isinstance( x.int(), int ) assert isinstance( x[1:2], Bits1 ) assert isinstance( x[0:4], Bits4 ) assert isinstance( x[2], Bits1 )
def test_constructor(): Bits4 = Bits(4) assert Bits4(2).uint() == 2 assert Bits4(4).uint() == 4 assert Bits4(15).uint() == 15 assert Bits4(-2).uint() == 0b1110 assert Bits4(-4).uint() == 0b1100 # Bits(N) returns a class, Bits(N)(value) creates an instance! with pytest.raises(AssertionError): assert Bits4 == Bits(4)(0) with pytest.raises(AssertionError): assert Bits(4) == Bits(4)(0) # Bits(N) returns a class, not an instance! # Does not have methods until instantiated! with pytest.raises(TypeError): assert Bits4.uint() == 0 with pytest.raises(TypeError): assert Bits(4).uint() == 0 # Sanity checks of subclassing. Note that all types (classes) are # objects, but object instances are objects, but not **not** types! assert isinstance(Bits4, type) assert isinstance(Bits4, object) assert not isinstance(Bits4(0), type) assert isinstance(Bits4(0), object) assert isinstance(Bits(4), type) assert isinstance(Bits(4), object) assert not isinstance(Bits(4)(0), type) assert isinstance(Bits(4)(0), object)
def test_constructor(): Bits4 = Bits(4) assert Bits4( 2).uint() == 2 assert Bits4( 4).uint() == 4 assert Bits4(15).uint() == 15 assert Bits4(-2).uint() == 0b1110 assert Bits4(-4).uint() == 0b1100 # Bits(N) returns a class, Bits(N)(value) creates an instance! with pytest.raises( AssertionError ): assert Bits4 == Bits(4)(0) with pytest.raises( AssertionError ): assert Bits(4) == Bits(4)(0) # Bits(N) returns a class, not an instance! # Does not have methods until instantiated! with pytest.raises( TypeError ): assert Bits4.uint() == 0 with pytest.raises( TypeError ): assert Bits(4).uint() == 0 # Sanity checks of subclassing. Note that all types (classes) are # objects, but object instances are objects, but not **not** types! assert isinstance( Bits4, type ) assert isinstance( Bits4, object ) assert not isinstance( Bits4(0), type ) assert isinstance( Bits4(0), object ) assert isinstance( Bits(4), type ) assert isinstance( Bits(4), object ) assert not isinstance( Bits(4)(0), type ) assert isinstance( Bits(4)(0), object )
def test_lte(): x = Bits(4)(0b1100) y = Bits(4)(0b0011) z = Bits(4)(0b0011) assert y.uint() <= x.uint() assert y.uint() <= 10 assert y.uint() <= z.uint() assert y.uint() <= 0b0011 assert y <= x.uint() assert y <= 10 assert y <= z.uint() assert y <= 0b0011 assert y <= x assert y <= z assert z <= x assert z <= z assert 1 <= y assert 3 <= y
def test_gte(): x = Bits(4)(0b1100) y = Bits(4)(0b0011) z = Bits(4)(0b1100) assert x.uint() >= y.uint() assert x.uint() >= 2 assert x.uint() >= z.uint() assert x.uint() >= 0b1100 assert x >= y.uint() assert x >= 2 assert x >= z.uint() assert x >= 0b1100 assert x >= y assert x >= z assert z >= y assert z >= x assert x >= x assert 5 >= y assert 3 <= y
def test_eq(): x = Bits(4)(0b1010) assert x.uint() == x.uint() # Compare objects by value, not id assert x == x # Check the value assert x.uint() == 0b1010 assert x.uint() == 0xA assert x.uint() == 10 # Checking the equality operator assert x == 0b1010 assert x == 0xA assert x == 10 # Checking comparison with another bit container y = Bits(4)(0b1010) assert x.uint() == y.uint() assert x == y y = Bits(8)(0b1010) assert x.uint() == y.uint() # TODO: How should equality between Bits objects work? # Just same value or same value and width? #assert x == y # Check the negatives x = Bits(4)(-1) assert x.uint() == 0b1111 assert x.uint() == 0xF assert x.uint() == 15 # Checking the equality operator assert x == 0b1111 assert x == 0xF assert x == 15 assert x.uint() == Bits(4)(-1).uint() assert x == Bits(4)(-1).uint() assert 15 == x assert not x == None assert not Bits(4)(0) == None
def test_compare_uint_neg(): x = Bits(4)(2) assert x.uint() != -1 assert x.uint() > -1 assert x.uint() >= -1
def test_eq(): x = Bits(4)(0b1010) assert x.uint() == x.uint() # Compare objects by value, not id assert x == x # Check the value assert x.uint() == 0b1010 assert x.uint() == 0xA assert x.uint() == 10 # Checking the equality operator assert x == 0b1010 assert x == 0xA assert x == 10 # Checking comparison with another bit container y = Bits(4)(0b1010) assert x.uint() == y.uint() assert x == y y = Bits( 8 )(0b1010) assert x.uint() == y.uint() # TODO: How should equality between Bits objects work? # Just same value or same value and width? #assert x == y # Check the negatives x = Bits(4)(-1) assert x.uint() == 0b1111 assert x.uint() == 0xF assert x.uint() == 15 # Checking the equality operator assert x == 0b1111 assert x == 0xF assert x == 15 assert x.uint() == Bits(4)(-1).uint() assert x == Bits(4)(-1).uint() assert 15 == x assert not x == None assert not Bits(4)(0) == None