Skip to content

Commit

Permalink
Merge fafa1a4 into d945781
Browse files Browse the repository at this point in the history
  • Loading branch information
cdonovick committed Jul 1, 2019
2 parents d945781 + fafa1a4 commit 7339938
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 32 deletions.
10 changes: 1 addition & 9 deletions hwtypes/bit_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,7 @@ def ite(self, t_branch, f_branch):
t_branch = fb_t(t_branch)
T = fb_t
else:
t_branch = BV_t(t_branch)
f_branch = BV_t(f_branch)
ext = t_branch.size - f_branch.size
if ext > 0:
f_branch = f_branch.zext(ext)
elif ext < 0:
t_branch = t_branch.zext(-ext)

T = type(t_branch)
raise TypeError(f'Atleast one branch must be a {self.get_family().BitVector}')


return t_branch if self else f_branch
Expand Down
14 changes: 5 additions & 9 deletions hwtypes/bit_vector_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@ class AbstractBitVectorMeta(ABCMeta):
# BitVectorType, size : BitVectorType[size]
_class_cache = weakref.WeakValueDictionary()

def __call__(cls, value=_MISSING, size=_MISSING, *args, **kwargs):
if cls.is_sized and size is not _MISSING:
raise TypeError('Cannot use old style construction on sized types')
elif cls.is_sized:
def __call__(cls, value=_MISSING, *args, **kwargs):
if cls.is_sized:
if value is _MISSING:
return super().__call__(*args, **kwargs)
else:
return super().__call__(value, *args, **kwargs)
elif size is _MISSING or size is None:
if size is None:
warnings.warn('DEPRECATION WARNING: Use of BitVectorT(value, size) is deprecated')
else:
warnings.warn('DEPRECATION WARNING: Use of implicitly sized '
'BitVectors is deprecated', DeprecationWarning)

if value is _MISSING:
raise TypeError('Cannot construct {} without a value'.format(cls, value))
Expand All @@ -41,8 +39,6 @@ def __call__(cls, value=_MISSING, size=_MISSING, *args, **kwargs):
size = max(int(value).bit_length(), 1)
else:
raise TypeError('Cannot construct {} from {}'.format(cls, value))
else:
warnings.warn('DEPRECATION WARNING: Use of BitVectorT(value, size) is deprecated')

return type(cls).__call__(cls[size], value)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
url='https://github.com/leonardt/hwtypes',
author='Leonard Truong',
author_email='lenny@cs.stanford.edu',
version='1.0.13',
version='1.1.0',
description='Python implementations of fixed size hardware types (Bit, '
'BitVector, UInt, SInt, ...) based on the SMT-LIB2 semantics',
scripts=[],
Expand Down
13 changes: 12 additions & 1 deletion tests/test_bv.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,22 @@ def test_eq():


def test_setitem():
bv = BitVector(5)
bv = BitVector[3](5)
assert bv.as_uint() ==5
bv[0] = 0
assert repr(bv) == 'BitVector[3](4)'
bv[1] = 1
assert repr(bv) == 'BitVector[3](6)'
bv[2] = 0
assert repr(bv) == 'BitVector[3](2)'

@pytest.mark.parametrize("val", [
BitVector.random(8),
BitVector.random(8).as_sint(),
BitVector.random(8).as_uint(),
[0,1,1,0],
])
def test_deprecated(val):
with pytest.warns(DeprecationWarning):
BitVector(val)

4 changes: 0 additions & 4 deletions tests/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ class C(A, B): pass
with pytest.raises(TypeError):
C[6]

x = BV(5)
assert x.size == 3
assert type(x).size == 3

def test_instance():
x = BV[8](0)
assert isinstance(x, ABV)
Expand Down
10 changes: 2 additions & 8 deletions tests/test_optypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,13 @@ def test_ite(t_constructor, t_size, f_constructor, f_size):
if t_constructor is f_constructor is _rand_bv and t_size == f_size:
res = pred.ite(t, f)
assert type(res) is type(t)
elif t_constructor is f_constructor is _rand_bv:
elif t_constructor is f_constructor:
# either both ints or BV with different size
with pytest.raises(TypeError):
res = pred.ite(t, f)
elif t_constructor is f_constructor: #rand int
res = pred.ite(t, f)
t_size = max(t.bit_length(), 1)
f_size = max(f.bit_length(), 1)
assert type(res) is BitVector[max(t_size, f_size)]
elif t_constructor is _rand_bv:
res = pred.ite(t, f)
assert type(res) is BitVector[t_size]
else: #t_constructor is _rand_int
print(type(t), t, t_constructor, t_size)
print(type(f), f, f_constructor, f_size)
res = pred.ite(t, f)
assert type(res) is BitVector[f_size]

0 comments on commit 7339938

Please sign in to comment.