def test_bitnum_overflow(): with pytest.raises(InvalidNumericType, match=r"overflow"): Bitnum("0b10", 1, False) with pytest.raises(InvalidNumericType, match=r"overflow"): Bitnum("-2", 1, True) with pytest.raises(InvalidNumericType, match=r"overflow"): Bitnum("0x2", 1, False)
def convert(x): if not is_fp: return Bitnum(x, **shape[k]).base_64_encode() try: return FixedPoint(x, **shape[k]).base_64_encode() except InvalidNumericType as error: if round_float_to_fixed: # Only round if it is not already representable. fractional_width = width - int_width x = float_to_fixed(float(x), fractional_width) x = str(x) return FixedPoint(x, **shape[k]).base_64_encode() else: raise error
def bitnum_round_trip(bit_string: str) -> Bitnum: # Round-trips the bitnum conversion. bin = Bitnum(f"0b{bit_string}", width, is_signed).str_value() hex = Bitnum(f"0x{hex_string}", width, is_signed).str_value() assert bin == hex return Bitnum(bin, width, is_signed)
def test_non_string_initialization(): with pytest.raises(InvalidNumericType, match=r"string"): Bitnum(16, 5, False) with pytest.raises(InvalidNumericType, match=r"string"): FixedPoint(0.5, 2, 1, False)
def test_empty_string(): with pytest.raises(InvalidNumericType, match=r"non-empty string"): Bitnum("", 2, False) with pytest.raises(InvalidNumericType, match=r"non-empty string"): FixedPoint("", 2, 1, False)
def test_unsigned_negative_number(): with pytest.raises(InvalidNumericType, match=r"negative value"): FixedPoint("-0.5", 2, 1, False) with pytest.raises(InvalidNumericType, match=r"negative value"): Bitnum("-1", 2, False)