Skip to content

Commit

Permalink
more storeAtEnd tests (getting coverage back over 90%)
Browse files Browse the repository at this point in the history
  • Loading branch information
mscuthbert committed Aug 21, 2018
1 parent cb8a27b commit ca4d4e1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
16 changes: 13 additions & 3 deletions music21/sieve.py
Expand Up @@ -180,29 +180,39 @@ def rabinMiller(n):
True
>>> sieve.rabinMiller(4)
False
>>> sieve.rabinMiller(123986234193)
>>> sieve.rabinMiller(6**4 + 1) # prime
True
>>> sieve.rabinMiller(123986234193) # divisible by 3, runs fast
False
'''
n = abs(n)
if n in (2, 3):
return True

m = n % 6 # if n (except 2 and 3) mod 6 is not 1 or 5, then n isn't prime
if m not in (1, 5):
return False

# primes up to 100; 2, 3 handled by mod 6
primes = [5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
primes = [ 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

if n <= 100:
if n in primes:
return True # must include 2, 3
return False

for prime in primes:
if n % prime == 0:
return 0

s, r = n - 1, 1
while not s & 1:
s >>= 1
r = r + 1

for i in range(10): # random tests
# calculate a^s mod n, where a is a random number
y = pow(random.randint(1, n - 1), s, n)
Expand Down Expand Up @@ -630,7 +640,7 @@ def setSegmentFormat(self, fmt):
#fmt = drawer.strScrub(fmt, 'l')
fmt = fmt.strip().lower()
if fmt in self._segmentFormatOptions:
raise ResidualException('format not in format optoins: %s' % fmt)
raise ResidualException('format not in format options: %s' % fmt)
self._segmentFormat = fmt

#---------------------------------------------------------------------------
Expand Down
13 changes: 13 additions & 0 deletions music21/stream/__init__.py
Expand Up @@ -1953,6 +1953,18 @@ def storeAtEnd(self, itemOrList, ignoreSort=False):
As sorting is done only by priority and class,
it cannot avoid setting isSorted to False.
>>> s = stream.Stream()
>>> b = bar.Repeat()
>>> s.storeAtEnd(b)
>>> b in s
True
>>> s.elementOffset(b)
0.0
>>> s.elementOffset(b, stringReturns=True)
'highestTime'
Only elements of zero duration can be stored. Otherwise a
`StreamException` is raised.
'''
if isinstance(itemOrList, list):
for item in itemOrList:
Expand All @@ -1961,6 +1973,7 @@ def storeAtEnd(self, itemOrList, ignoreSort=False):
return
else:
item = itemOrList

try:
unused = item.isStream # will raise attribute error
element = item
Expand Down
28 changes: 28 additions & 0 deletions music21/test/testStream.py
Expand Up @@ -4436,6 +4436,34 @@ def testElementsHighestTimeA(self):
# index is now 1
self.assertEqual(s.index(b1), 1)

def testStoreAtEndFailures(self):
from music21 import stream, bar

s = Stream()
with self.assertRaises(stream.StreamException):
s.storeAtEnd(6)

n = note.Note()
n.duration.quarterLength = 2.0
with self.assertRaises(stream.StreamException):
s.storeAtEnd(n)

# also test that lists work...
b = bar.Barline()
s.storeAtEnd([b])

# also test that element may not be in stream twice.
with self.assertRaises(stream.StreamException):
s.storeAtEnd([b])


# test that element may not be in stream elements and at end.
b2 = bar.Barline()
s.insert(0, b2)
with self.assertRaises(stream.StreamException):
s.storeAtEnd(b2)



def testElementsHighestTimeB(self):
'''Test adding elements at the highest time position
Expand Down

0 comments on commit ca4d4e1

Please sign in to comment.