test_hypothesis.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. from hypothesis import given
  2. from hypothesis import strategies as st
  3. from sympy.abc import x
  4. from sympy.polys.polytools import Poly
  5. def polys(*, nonzero=False, domain="ZZ"):
  6. # This is a simple strategy, but sufficient the tests below
  7. elems = {"ZZ": st.integers(), "QQ": st.fractions()}
  8. coeff_st = st.lists(elems[domain])
  9. if nonzero:
  10. coeff_st = coeff_st.filter(any)
  11. return st.builds(Poly, coeff_st, st.just(x), domain=st.just(domain))
  12. @given(f=polys(), g=polys(), r=polys())
  13. def test_gcd_hypothesis(f, g, r):
  14. gcd_1 = f.gcd(g)
  15. gcd_2 = g.gcd(f)
  16. assert gcd_1 == gcd_2
  17. # multiply by r
  18. gcd_3 = g.gcd(f + r * g)
  19. assert gcd_1 == gcd_3
  20. @given(f_z=polys(), g_z=polys(nonzero=True))
  21. def test_poly_hypothesis_integers(f_z, g_z):
  22. remainder_z = f_z.rem(g_z)
  23. assert g_z.degree() >= remainder_z.degree() or remainder_z.degree() == 0
  24. @given(f_q=polys(domain="QQ"), g_q=polys(nonzero=True, domain="QQ"))
  25. def test_poly_hypothesis_rationals(f_q, g_q):
  26. remainder_q = f_q.rem(g_q)
  27. assert g_q.degree() >= remainder_q.degree() or remainder_q.degree() == 0