run-math.test 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Test cases for the math module (compile and run)
  2. [case testMathOps]
  3. from typing import Any, Callable
  4. from typing_extensions import Final
  5. import math
  6. from math import pi, e, tau, inf, nan
  7. from testutil import assertRaises, float_vals, assertDomainError, assertMathRangeError
  8. pymath: Any = math
  9. def validate_one_arg(test: Callable[[float], float], validate: Callable[[float], float]) -> None:
  10. """Ensure that test and validate behave the same for various float args."""
  11. for x in float_vals:
  12. try:
  13. expected = validate(x)
  14. except Exception as e:
  15. try:
  16. test(x)
  17. assert False, f"no exception raised for {x!r}, expected {e!r}"
  18. except Exception as e2:
  19. assert repr(e) == repr(e2), f"actual for {x!r}: {e2!r}, expected: {e!r}"
  20. continue
  21. actual = test(x)
  22. assert repr(actual) == repr(expected), (
  23. f"actual for {x!r}: {actual!r}, expected {expected!r}")
  24. def validate_two_arg(test: Callable[[float, float], float],
  25. validate: Callable[[float, float], float]) -> None:
  26. """Ensure that test and validate behave the same for various float args."""
  27. for x in float_vals:
  28. for y in float_vals:
  29. args = f"({x!r}, {y!r})"
  30. try:
  31. expected = validate(x, y)
  32. except Exception as e:
  33. try:
  34. test(x, y)
  35. assert False, f"no exception raised for {args}, expected {e!r}"
  36. except Exception as e2:
  37. assert repr(e) == repr(e2), f"actual for {args}: {e2!r}, expected: {e!r}"
  38. continue
  39. try:
  40. actual = test(x, y)
  41. except Exception as e:
  42. assert False, f"no exception expected for {args}, got {e!r}"
  43. assert repr(actual) == repr(expected), (
  44. f"actual for {args}: {actual!r}, expected {expected!r}")
  45. def test_sqrt() -> None:
  46. validate_one_arg(lambda x: math.sqrt(x), pymath.sqrt)
  47. def test_sin() -> None:
  48. validate_one_arg(lambda x: math.sin(x), pymath.sin)
  49. def test_cos() -> None:
  50. validate_one_arg(lambda x: math.cos(x), pymath.cos)
  51. def test_tan() -> None:
  52. validate_one_arg(lambda x: math.tan(x), pymath.tan)
  53. def test_exp() -> None:
  54. validate_one_arg(lambda x: math.exp(x), pymath.exp)
  55. def test_log() -> None:
  56. validate_one_arg(lambda x: math.log(x), pymath.log)
  57. def test_floor() -> None:
  58. validate_one_arg(lambda x: math.floor(x), pymath.floor)
  59. def test_ceil() -> None:
  60. validate_one_arg(lambda x: math.ceil(x), pymath.ceil)
  61. def test_fabs() -> None:
  62. validate_one_arg(lambda x: math.fabs(x), pymath.fabs)
  63. def test_pow() -> None:
  64. validate_two_arg(lambda x, y: math.pow(x, y), pymath.pow)
  65. def test_copysign() -> None:
  66. validate_two_arg(lambda x, y: math.copysign(x, y), pymath.copysign)
  67. def test_isinf() -> None:
  68. for x in float_vals:
  69. assert repr(math.isinf(x)) == repr(pymath.isinf(x))
  70. def test_isnan() -> None:
  71. for x in float_vals:
  72. assert repr(math.isnan(x)) == repr(pymath.isnan(x))
  73. def test_pi_is_inlined_correctly() -> None:
  74. assert math.pi == pi == 3.141592653589793
  75. def test_e_is_inlined_correctly() -> None:
  76. assert math.e == e == 2.718281828459045
  77. def test_tau_is_inlined_correctly() -> None:
  78. assert math.tau == tau == 6.283185307179586
  79. def test_inf_is_inlined_correctly() -> None:
  80. assert math.inf == inf == float("inf")
  81. def test_nan_is_inlined_correctly() -> None:
  82. assert math.isnan(math.nan)
  83. assert math.isnan(nan)