Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Trac 18411: fix testing issues
Browse files Browse the repository at this point in the history
The current testing framework does not care of _max_runs and wanted to
run the tests over an increible amount of elements. This commit fixes
the issue.
  • Loading branch information
videlec committed Sep 12, 2015
1 parent 0dcfed9 commit 613cef5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/sage/categories/additive_semigroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ def _test_additive_associativity(self, **options):
"""
tester = self._tester(**options)
S = tester.some_elements()
from itertools import product
for x,y,z in product(S, repeat=3):
from sage.misc.misc import bounded_number_of_tuples
for x,y,z in bounded_number_of_tuples(S, 3, tester._max_runs):
tester.assert_((x + y) + z == x + (y + z))

class Homsets(HomsetsCategory):
Expand Down
42 changes: 21 additions & 21 deletions src/sage/categories/euclidean_domains.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,16 @@ def _test_euclidean_degree(self, **options):
tester.assertGreaterEqual(a.euclidean_degree(), min_degree)
tester.assertEqual(a.euclidean_degree() == min_degree, a.is_unit())

for a in S:
for b in S:
p = a * b
# For rings which are not exact, we might get something that
# acts like a zero divisor.
# Therefore we skip the product if it evaluates to zero.
# Let the category of Domains handle the test for zero divisors.
if p.is_zero():
continue
tester.assertLessEqual(a.euclidean_degree(), p.euclidean_degree())
from sage.misc.misc import bounded_number_of_tuples
for a,b in bounded_number_of_tuples(S, 2, tester._max_runs):
p = a * b
# For rings which are not exact, we might get something that
# acts like a zero divisor.
# Therefore we skip the product if it evaluates to zero.
# Let the category of Domains handle the test for zero divisors.
if p.is_zero():
continue
tester.assertLessEqual(a.euclidean_degree(), p.euclidean_degree())

def _test_quo_rem(self, **options):
r"""
Expand All @@ -114,17 +114,17 @@ def _test_quo_rem(self, **options):
"""
tester = self._tester(**options)
S = tester.some_elements()
for a in S:
for b in S:
if b.is_zero():
tester.assertRaises(ZeroDivisionError, lambda: a.quo_rem(b))
else:
q,r = a.quo_rem(b)
tester.assertIn(q, self)
tester.assertIn(r, self)
tester.assertEqual(a,q*b+r)
if r != 0:
tester.assertLess(r.euclidean_degree(), b.euclidean_degree())
from sage.misc.misc import bounded_number_of_tuples
for a,b in bounded_number_of_tuples(S, 2, tester._max_runs):
if b.is_zero():
tester.assertRaises(ZeroDivisionError, lambda: a.quo_rem(b))
else:
q,r = a.quo_rem(b)
tester.assertIn(q, self)
tester.assertIn(r, self)
tester.assertEqual(a,q*b+r)
if r != 0:
tester.assertLess(r.euclidean_degree(), b.euclidean_degree())

class ElementMethods:
@abstract_method
Expand Down
4 changes: 2 additions & 2 deletions src/sage/categories/semigroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ def _test_associativity(self, **options):
"""
tester = self._tester(**options)
S = tester.some_elements()
from itertools import product
for x,y,z in product(S, repeat=3):
from sage.misc.misc import bounded_number_of_tuples
for x,y,z in bounded_number_of_tuples(S, 3, tester._max_runs):
tester.assert_((x * y) * z == x * (y * z))

@abstract_method(optional=True)
Expand Down
17 changes: 10 additions & 7 deletions src/sage/misc/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1724,24 +1724,27 @@ def random_sublist(X, s):

def bounded_number_of_tuples(elements, repeat, bound):
r"""
Return at most ``bound`` number of ``repeat``-tuples of ``elements``.
Return an iterator over at most ``bound`` number of ``repeat``-tuples of
``elements``.
TESTS::
sage: from sage.misc.misc import bounded_number_of_tuples
sage: l = bounded_number_of_tuples([0,1,2,3], 2, 3)
sage: l # random
sage: list(l) # random
[(0,3), (1,2), (3,4)]
sage: len(l)
3
sage: l = bounded_number_of_tuples(range(50), 3, 10)
sage: len(list(l))
10
"""
from itertools import product
tuples = product(elements, repeat=repeat)
if len(elements) ** repeat < bound:
return tuples
from itertools import product
return product(elements, repeat=repeat)
else:
from sage.misc.prandom import sample
return sample(list(tuples), bound)
from sage.misc.prandom import sample, choice
return (tuple(choice(elements) for _ in range(repeat)) for _ in range(bound))

def powerset(X):
r"""
Expand Down

0 comments on commit 613cef5

Please sign in to comment.