Skip to content

Commit

Permalink
sagemathgh-37561: enrich the test_karatsuba failure message with expl…
Browse files Browse the repository at this point in the history
…icit elements

    
To help debug the random failure in `test_karatsuba_multiplication`, let
us have a more precise error message in case of failure there.

cf sagemath#35715

### 📝 Checklist

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
    
URL: sagemath#37561
Reported by: Frédéric Chapoton
Reviewer(s): Martin Rubey
  • Loading branch information
Release Manager committed Mar 29, 2024
2 parents e087e70 + 9f15505 commit 644ba54
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/sage/rings/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,9 @@ def test_random_arith(level=MAX_LEVEL, trials=1):

@random_testing
def test_karatsuba_multiplication(base_ring, maxdeg1, maxdeg2,
ref_mul=lambda f, g: f._mul_generic(g), base_ring_random_elt_args=[],
numtests=10, verbose=False):
ref_mul=lambda f, g: f._mul_generic(g),
base_ring_random_elt_args=[],
numtests=10, verbose=False):
"""
Test univariate Karatsuba multiplication against other multiplication algorithms.
Expand Down Expand Up @@ -469,16 +470,21 @@ def test_karatsuba_multiplication(base_ring, maxdeg1, maxdeg2,
"""
from sage.misc.prandom import randint
from sage.misc.sage_input import sage_input
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
threshold = randint(0, min(maxdeg1, maxdeg2))
R = PolynomialRing(base_ring, 'x')
if verbose:
print("test_karatsuba_multiplication: ring={}, threshold={}".format(R, threshold))
print(f"test_karatsuba_multiplication: ring={R}, threshold={threshold}")
for _ in range(numtests):
f = R.random_element(randint(0, maxdeg1), False, *base_ring_random_elt_args)
g = R.random_element(randint(0, maxdeg2), False, *base_ring_random_elt_args)
if verbose:
print(" ({})*({})".format(f, g))
if ref_mul(f, g) - f._mul_karatsuba(g, threshold) != 0:
raise ValueError("Multiplication failed")
msg = "Multiplication failed for elements defined by\n"
msg += f"{sage_input(f)}\n"
msg += "and\n"
msg += f"{sage_input(g)}"
raise ValueError(msg)
return

0 comments on commit 644ba54

Please sign in to comment.