15
15
import unittest
16
16
import sys
17
17
import traceback
18
+ import random
18
19
19
20
class TestSuite (object ):
20
21
"""
@@ -76,7 +77,7 @@ class TestSuite(object):
76
77
77
78
Debugging tip: in case of failure of some test, use ``%pdb on`` to
78
79
turn on automatic debugging on error. Run the failing test
79
- independtly : the debugger will stop right where the first
80
+ independently : the debugger will stop right where the first
80
81
assertion fails. Then, introspection can be used to analyse what
81
82
exactly the problem is. See also the ``catch = False`` option to
82
83
:meth:`.run`.
@@ -269,16 +270,16 @@ def _test_b(self, tester): tester.fail()
269
270
...
270
271
AssertionError: None
271
272
272
- In conjunction with ``%pdb on``, this allows for the debbuger
273
+ In conjunction with ``%pdb on``, this allows for the debugger
273
274
to jump directly to the first failure location.
274
275
"""
275
276
if isinstance (skip , str ):
276
277
skip = [skip ]
277
278
else :
278
279
skip = tuple (skip )
279
280
280
- # The class of exceptions that will be catched and reported;
281
- # other exceptions will get trough . None catches nothing.
281
+ # The class of exceptions that will be caught and reported;
282
+ # other exceptions will get through . None catches nothing.
282
283
catch_exception = Exception if catch else None
283
284
284
285
tester = instance_tester (self ._instance , ** options )
@@ -373,7 +374,7 @@ class InstanceTester(unittest.TestCase):
373
374
Testing utilities for Rational Field
374
375
"""
375
376
376
- def __init__ (self , instance , elements = None , verbose = False , prefix = "" , max_runs = 4096 , ** options ):
377
+ def __init__ (self , instance , elements = None , verbose = False , prefix = "" , max_runs = 4096 , max_samples = None , ** options ):
377
378
"""
378
379
A gadget attached to an instance providing it with testing utilities.
379
380
@@ -383,7 +384,7 @@ def __init__(self, instance, elements = None, verbose = False, prefix = "", max_
383
384
sage: InstanceTester(instance = ZZ, verbose = True, elements = [1,2,3])
384
385
Testing utilities for Integer Ring
385
386
386
- This is used by ``SageObject._tester``, which see ::
387
+ This is used by ``SageObject._tester``, for example ::
387
388
388
389
sage: QQ._tester()
389
390
Testing utilities for Rational Field
@@ -394,6 +395,7 @@ def __init__(self, instance, elements = None, verbose = False, prefix = "", max_
394
395
self ._elements = elements
395
396
self ._prefix = prefix
396
397
self ._max_runs = max_runs
398
+ self ._max_samples = max_samples
397
399
398
400
def runTest (self ):
399
401
"""
@@ -448,9 +450,9 @@ def __repr__(self):
448
450
return "Testing utilities for %s" % self ._instance
449
451
450
452
451
- def some_elements (self , S = None ):
453
+ def some_elements (self , S = None , repeat = None ):
452
454
"""
453
- Returns a list (or iterable) of elements of ``self`` on which
455
+ Returns a list (or iterable) of elements of the instance on which
454
456
the tests should be run. This is only meaningful for container
455
457
objects like parents.
456
458
@@ -461,9 +463,13 @@ def some_elements(self, S=None):
461
463
time, or the result of :meth:`.some_elements` if no elements
462
464
were specified.
463
465
466
+ - ``repeat`` -- integer (default: None). If given, instead returns
467
+ a list of tuples of length ``repeat`` from ``S``.
468
+
464
469
OUTPUT:
465
470
466
- A list of at most ``self._max_runs`` elements of ``S``.
471
+ A list of at most ``self._max_runs`` elements of ``S^r``,
472
+ or a sample of at most ``self._max_samples`` if that is not ``None``.
467
473
468
474
EXAMPLES:
469
475
@@ -520,6 +526,18 @@ def some_elements(self, S=None):
520
526
sage: list(tester.some_elements())
521
527
[0, 1, 2]
522
528
529
+ The ``repeat`` keyword can give pairs or triples from ``S``::
530
+
531
+ sage: list(tester.some_elements(repeat=2))
532
+ [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1)]
533
+
534
+ You can use ``max_samples`` to sample at random, instead of in order::
535
+
536
+ sage: tester = InstanceTester(ZZ, elements = srange(8), max_samples = 4)
537
+ sage: list(tester.some_elements())
538
+ [0, 3, 7, 1]
539
+ sage: list(tester.some_elements(repeat=2))
540
+ [(1, 4), (3, 1), (4, 5), (5, 0)]
523
541
524
542
Test for :trac:`15919`, :trac:`16244`::
525
543
@@ -539,14 +557,9 @@ def some_elements(self, S=None):
539
557
sage: list(tester.some_elements())
540
558
[(0, 0, 0, 0), (0, 0, 0, 1), (0, 0, 0, 2), (0, 0, 0, 3)]
541
559
"""
542
- if S is None :
543
- if self ._elements is None :
544
- S = self ._instance .some_elements ()
545
- else :
546
- S = self ._elements
547
- import itertools
548
- return list (itertools .islice (S ,0 ,self ._max_runs ))
549
-
560
+ S = S or self ._elements or self ._instance .some_elements ()
561
+ from sage .misc .misc import some_tuples
562
+ return list (some_tuples (S , repeat , self ._max_runs , self ._max_samples ))
550
563
551
564
class PythonObjectWithTests (object ):
552
565
"""
0 commit comments