-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.rs
More file actions
214 lines (205 loc) · 6.2 KB
/
test.rs
File metadata and controls
214 lines (205 loc) · 6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use crate::Error;
use pyo3::{Python, prelude::*};
/// Edge values for testing floating-point functions.
/// Includes: zeros, infinities, various NaNs, subnormals, and values at different scales.
pub(crate) const EDGE_VALUES: [f64; 64] = [
// Zeros
0.0,
-0.0,
// Infinities
f64::INFINITY,
f64::NEG_INFINITY,
// Standard NaNs
f64::NAN,
-f64::NAN,
// Additional NaN with different payload (quiet NaN with payload 1)
f64::from_bits(0x7FF8_0000_0000_0001_u64),
// Signaling NaN (sNaN) - may trigger FP exceptions on some platforms
f64::from_bits(0x7FF0_0000_0000_0001_u64),
// Subnormal (denormalized) values
f64::MIN_POSITIVE * 0.5,
-f64::MIN_POSITIVE * 0.5,
5e-324,
-5e-324,
// Boundary values
f64::MIN_POSITIVE,
f64::MAX,
f64::MIN,
// Near-infinity large values
f64::MAX * 0.5,
-f64::MAX * 0.5,
1e308,
-1e308,
// Overflow/underflow thresholds for exp
710.0,
-745.0,
// Small scale
1e-10,
-1e-10,
1e-300,
-1e-300,
// Normal scale
1.0,
-1.0,
0.5,
-0.5,
2.0,
-2.0,
3.0, // for cbrt
-3.0,
// Values near 1.0 (log, expm1, log1p, acosh boundary)
1.0 - 1e-15,
1.0 + 1e-15,
f64::EPSILON,
1.0 - f64::EPSILON,
1.0 + f64::EPSILON,
// asin/acos domain boundaries [-1, 1]
1.0000000000000002, // just outside domain (1 + eps)
-1.0000000000000002,
// atanh domain boundaries (-1, 1)
0.9999999999999999, // just inside domain
-0.9999999999999999,
// log1p domain boundary (> -1)
-0.9999999999999999, // just above -1
-1.0 + 1e-15, // very close to -1
// gamma/lgamma poles (negative integers)
-1.0,
-2.0,
-3.0,
-0.5, // gamma(-0.5) = -2*sqrt(pi)
// Mathematical constants
std::f64::consts::E,
std::f64::consts::LN_2,
std::f64::consts::LOG10_E,
// Trigonometric special values
std::f64::consts::PI,
-std::f64::consts::PI,
std::f64::consts::FRAC_PI_2,
-std::f64::consts::FRAC_PI_2,
std::f64::consts::FRAC_PI_4,
std::f64::consts::TAU,
1.5 * std::f64::consts::PI, // 3π/2
// Large values for trig (precision loss)
1e15,
-1e15,
// Near-integer values (ceil, floor, trunc, round)
0.49999999999999994,
0.50000000000000006,
-0.49999999999999994,
-0.50000000000000006,
];
pub(crate) fn unwrap<'py>(
py: Python<'py>,
py_v: PyResult<Bound<'py, PyAny>>,
v: Result<f64, crate::Error>,
) -> Option<(f64, f64)> {
match py_v {
Ok(py_v) => {
let py_v: f64 = py_v.extract().ok().expect("failed to extract");
Some((py_v, v.unwrap()))
}
Err(e) => {
if e.is_instance_of::<pyo3::exceptions::PyValueError>(py) {
assert_eq!(v.err(), Some(Error::EDOM));
} else if e.is_instance_of::<pyo3::exceptions::PyOverflowError>(py) {
assert_eq!(v.err(), Some(Error::ERANGE));
} else {
panic!();
}
None
}
}
}
/// Test a 1-argument function that returns Result<f64>
pub(crate) fn test_math_1(x: f64, func_name: &str, rs_func: impl Fn(f64) -> crate::Result<f64>) {
let rs_result = rs_func(x);
pyo3::Python::attach(|py| {
let math = pyo3::types::PyModule::import(py, "math").unwrap();
let py_func = math.getattr(func_name).unwrap();
let r = py_func.call1((x,));
let Some((py_result, rs_result)) = unwrap(py, r, rs_result) else {
return;
};
if py_result.is_nan() && rs_result.is_nan() {
return;
}
assert_eq!(
py_result.to_bits(),
rs_result.to_bits(),
"{func_name}({x}): py={py_result} vs rs={rs_result}"
);
});
}
/// Run a test with Python math module
pub(crate) fn with_py_math<F, R>(f: F) -> R
where
F: FnOnce(Python, &pyo3::Bound<pyo3::types::PyModule>) -> R,
{
pyo3::Python::attach(|py| {
let math = pyo3::types::PyModule::import(py, "math").unwrap();
f(py, &math)
})
}
/// Assert two f64 values are equal (handles NaN)
pub(crate) fn assert_f64_eq(py: f64, rs: f64, context: impl std::fmt::Display) {
if py.is_nan() && rs.is_nan() {
return;
}
assert_eq!(
py.to_bits(),
rs.to_bits(),
"{}: py={} vs rs={}",
context,
py,
rs
);
}
/// Test a 2-argument function that returns Result<f64>
pub(crate) fn test_math_2(
x: f64,
y: f64,
func_name: &str,
rs_func: impl Fn(f64, f64) -> crate::Result<f64>,
) {
let rs_result = rs_func(x, y);
pyo3::Python::attach(|py| {
let math = pyo3::types::PyModule::import(py, "math").unwrap();
let py_func = math.getattr(func_name).unwrap();
let r = py_func.call1((x, y));
match r {
Ok(py_val) => {
let py_f: f64 = py_val.extract().unwrap();
let rs_val = rs_result.unwrap_or_else(|e| {
panic!("{func_name}({x}, {y}): py={py_f} but rs returned error {e:?}")
});
if py_f.is_nan() && rs_val.is_nan() {
return;
}
assert_eq!(
py_f.to_bits(),
rs_val.to_bits(),
"{func_name}({x}, {y}): py={py_f} vs rs={rs_val}"
);
}
Err(e) => {
// Check error type matches
let rs_err = rs_result.as_ref().err();
if e.is_instance_of::<pyo3::exceptions::PyValueError>(py) {
assert_eq!(
rs_err,
Some(&Error::EDOM),
"{func_name}({x}, {y}): py raised ValueError but rs={rs_err:?}"
);
} else if e.is_instance_of::<pyo3::exceptions::PyOverflowError>(py) {
assert_eq!(
rs_err,
Some(&Error::ERANGE),
"{func_name}({x}, {y}): py raised OverflowError but rs={rs_err:?}"
);
} else {
panic!("{func_name}({x}, {y}): py raised unexpected error {e}");
}
}
}
});
}