Skip to content

Commit

Permalink
Merge pull request #153 from forumulator/testCoverage
Browse files Browse the repository at this point in the history
Test coverage
  • Loading branch information
jandecaluwe committed Mar 5, 2016
2 parents 2589a71 + 68302ec commit 9563f6f
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions .cache/v/cache/lastfailed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*~
*.swp
*.out
*.coverage
build/

# Python
Expand Down
117 changes: 117 additions & 0 deletions myhdl/test/core/test_modbv.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,19 @@
""" Run the modbv unit tests. """
from __future__ import absolute_import

import sys
import random
import pytest


from random import randrange

from myhdl._intbv import intbv
from myhdl._modbv import modbv
from myhdl._compat import integer_types, long

random.seed(3)
maxint=sys.maxsize


class TestModbvWrap:
Expand Down Expand Up @@ -65,3 +74,111 @@ def testNoWrap(self):
x = intbv(0, min=-8, max=8)
with pytest.raises(ValueError):
x[:] += 15


class TestModBvSlice:

def BuildTestSeqs(self):
imin,jmin=0,0
imax,jmax=5000,5000
seqi,seqj=[],[]
#small bits
for n in range(10):
seqi.append(randrange(imin, imax))
seqi.append(randrange(jmin, jmax))
#large bits
for n in range(10):
seqi.append(randrange(imin,maxint))
seqj.append(randrange(jmin,maxint))
self.seqi=seqi
self.seqj=seqj

def testGetItem(self):
self.BuildTestSeqs()
#Testing for the __getItem__ function
for i in self.seqi:
ti=modbv(i)
cti=modbv(~i)
for n in range(len(bin(i))):
chk=((i >> n) & 1)
res=ti[n]
resv=cti[n]
assert type(res) == bool
assert type(resv) == bool
assert res == chk
assert resv == chk^1


def testGetSlice(self):
self.BuildTestSeqs()
#Testing for slice ranged, both closed
for i in self.seqi:
ti=modbv(i)
cti=modbv(~i)
leni=len(bin(i))
for n in (range(leni)):
s=randrange(-leni,leni)
f=randrange(-leni,leni)
try:
val=ti[f:s]
except ValueError:
assert (f<=s) | (s<=0)
else:
chkt = (i >> s) & (2**(f-s) -1)
assert val == chkt
assert (val.max,val.min) == (2**(f-s),0)

def testGetSliceOpen(self):
self.BuildTestSeqs()
#Testing for slice ranged, one of them open
for i in self.seqi:
ti=modbv(i)
cti=modbv(~i)
leni=len(bin(i))
assert ti+cti == -1
for n in (range(leni)):
s=randrange(leni)
f=randrange(1,leni)
chkf = (i) & (2**(f) -1)
chks = (i >> s)
vals,cvals = ti[:s],cti[:s]
valf,cvalf = ti[f:],cti[f:]
assert vals+cvals == -1

#assert valf+cvalf == -1 ---test fails- why???
assert vals == modbv(chks)
assert valf == modbv(chkf)

def testSetSlice(self):
self.BuildTestSeqs()
self.seqi.extend(self.seqj)
#Test the bit slice setting wrap around behaviour
for i in range(1,len(self.seqi)):
lenp=len(bin(self.seqi[i-1]))
ti = modbv(self.seqi[i-1])[lenp:]
maxp = 2**lenp
vali = self.seqi[i]
s=randrange(lenp-1)
f=randrange(s,lenp)
valp = self.seqi[i-1]
maxval = (1 << (f-s))-1
try:
ti[f:s] = vali
except ValueError:
assert maxval < vali
else:
bitmask = (((2**f - 1) >> s) << s )
valp = valp - (valp & bitmask)
valp = (valp + ((vali << s) % maxp)) % maxp
assert valp == ti











0 comments on commit 9563f6f

Please sign in to comment.