|
1 | 1 | mod err;
|
2 | 2 | mod gamma;
|
| 3 | +mod m; |
| 4 | +#[cfg(test)] |
| 5 | +mod test; |
3 | 6 |
|
4 |
| -pub use err::Error; |
5 |
| -pub use gamma::{lgamma, tgamma as gamma}; |
| 7 | +pub use err::{Error, Result}; |
| 8 | +pub use gamma::{gamma, lgamma}; |
| 9 | + |
| 10 | +macro_rules! libm { |
| 11 | + // Reset errno and handle errno when return type contains Result |
| 12 | + (fn $name:ident($arg:ident: $ty:ty) -> Result<$ret:ty>) => { |
| 13 | + #[inline(always)] |
| 14 | + pub fn $name($arg: $ty) -> Result<$ret> { |
| 15 | + errno::set_errno(errno::Errno(0)); |
| 16 | + let r = unsafe { m::$name($arg) }; |
| 17 | + crate::is_error(r) |
| 18 | + } |
| 19 | + }; |
| 20 | + // Skip errno checking when return type is not Result |
| 21 | + (fn $name:ident($arg:ident: $ty:ty) -> $ret:ty) => { |
| 22 | + #[inline(always)] |
| 23 | + pub fn $name($arg: $ty) -> $ret { |
| 24 | + unsafe { m::$name($arg) } |
| 25 | + } |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +macro_rules! pyo3_proptest { |
| 30 | + ($fn_name:ident(Result<_>), $test_name:ident, $proptest_name:ident, $edgetest_name:ident) => { |
| 31 | + #[cfg(test)] |
| 32 | + fn $test_name(x: f64) { |
| 33 | + use pyo3::prelude::*; |
| 34 | + |
| 35 | + let rs_result = $fn_name(x); |
| 36 | + |
| 37 | + pyo3::prepare_freethreaded_python(); |
| 38 | + Python::with_gil(|py| { |
| 39 | + let math = PyModule::import(py, "math").unwrap(); |
| 40 | + let py_func = math |
| 41 | + .getattr(stringify!($fn_name)) |
| 42 | + .unwrap(); |
| 43 | + let r = py_func.call1((x,)); |
| 44 | + let Some((py_result, rs_result)) = crate::test::unwrap(py, r, rs_result) else { |
| 45 | + return; |
| 46 | + }; |
| 47 | + let py_result_repr = py_result.to_bits(); |
| 48 | + let rs_result_repr = rs_result.to_bits(); |
| 49 | + assert_eq!(py_result_repr, rs_result_repr, "x = {x}, py_result = {py_result}, rs_result = {rs_result}"); |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + crate::pyo3_proptest!(@proptest, $test_name, $proptest_name); |
| 54 | + crate::pyo3_proptest!(@edgetest, $test_name, $edgetest_name); |
| 55 | + }; |
| 56 | + ($fn_name:ident(_), $test_name:ident, $proptest_name:ident, $edgetest_name:ident) => { |
| 57 | + #[cfg(test)] |
| 58 | + fn $test_name(x: f64) { |
| 59 | + use pyo3::prelude::*; |
| 60 | + |
| 61 | + let rs_result = Ok($fn_name(x)); |
| 62 | + |
| 63 | + pyo3::prepare_freethreaded_python(); |
| 64 | + Python::with_gil(|py| { |
| 65 | + let math = PyModule::import(py, "math").unwrap(); |
| 66 | + let py_func = math |
| 67 | + .getattr(stringify!($fn_name)) |
| 68 | + .unwrap(); |
| 69 | + let r = py_func.call1((x,)); |
| 70 | + let Some((py_result, rs_result)) = crate::test::unwrap(py, r, rs_result) else { |
| 71 | + return; |
| 72 | + }; |
| 73 | + let py_result_repr = py_result.to_bits(); |
| 74 | + let rs_result_repr = rs_result.to_bits(); |
| 75 | + assert_eq!(py_result_repr, rs_result_repr, "x = {x}, py_result = {py_result}, rs_result = {rs_result}"); |
| 76 | + }); |
| 77 | + } |
| 78 | + crate::pyo3_proptest!(@proptest, $test_name, $proptest_name); |
| 79 | + }; |
| 80 | + (@proptest, $test_name:ident, $proptest_name:ident) => { |
| 81 | + #[cfg(test)] |
| 82 | + proptest::proptest! { |
| 83 | + #[test] |
| 84 | + fn $proptest_name(x: f64) { |
| 85 | + $test_name(x); |
| 86 | + } |
| 87 | + } |
| 88 | + }; |
| 89 | + (@edgetest, $test_name:ident, $edgetest_name:ident) => { |
| 90 | + #[test] |
| 91 | + fn $edgetest_name() { |
| 92 | + $test_name(f64::MIN); |
| 93 | + $test_name(-f64::MIN); |
| 94 | + $test_name(f64::NAN); |
| 95 | + $test_name(-f64::NAN); |
| 96 | + $test_name(f64::INFINITY); |
| 97 | + $test_name(-f64::NEG_INFINITY); |
| 98 | + $test_name(0.0); |
| 99 | + $test_name(-0.0); |
| 100 | + } |
| 101 | + }; |
| 102 | +} |
| 103 | + |
| 104 | +libm!(fn erf(n: f64) -> f64); |
| 105 | +pyo3_proptest!(erf(_), test_erf, proptest_erf, edgetest_erf); |
| 106 | + |
| 107 | +libm!(fn erfc(n: f64) -> f64); |
| 108 | +pyo3_proptest!(erfc(_), test_erfc, proptest_erfc, edgetest_erfc); |
| 109 | + |
| 110 | +/// Call is_error when errno != 0, and where x is the result libm |
| 111 | +/// returned. is_error will usually set up an exception and return |
| 112 | +/// true (1), but may return false (0) without setting up an exception. |
| 113 | +// fn is_error(x: f64) -> crate::Result<f64> { |
| 114 | +// match errno::errno() { |
| 115 | +// errno::Errno(0) => Ok(x), |
| 116 | +// errno::Errno(libc::ERANGE) if x.abs() < 1.5 => Ok(0f64), |
| 117 | +// errno::Errno(errno) => Err(errno.try_into().unwrap()), |
| 118 | +// } |
| 119 | +// } |
| 120 | +use pyo3_proptest; |
0 commit comments