Skip to content
Merged
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 35 additions & 27 deletions src/conversions/ordered_float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,22 @@ float_conversions!(NotNan, f64, |val| NotNan::new(val)
#[cfg(test)]
mod test_ordered_float {
use super::*;
use crate::py_run;
use crate::types::dict::IntoPyDict;
use crate::types::PyAnyMethods;
use std::ffi::CString;

#[cfg(not(target_arch = "wasm32"))]
use proptest::prelude::*;

fn py_run<'py>(py: Python<'py>, script: String, locals: impl IntoPyDict<'py>) {
py.run(
&CString::new(script).unwrap(),
None,
Some(&locals.into_py_dict(py).unwrap()),
)
.unwrap()
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn py_run<'py>(py: Python<'py>, script: String, locals: impl IntoPyDict<'py>) {
py.run(
&CString::new(script).unwrap(),
None,
Some(&locals.into_py_dict(py).unwrap()),
)
.unwrap()
}
fn py_run<'py>(py: Python<'py>, script: &CStr, locals: impl IntoPyDict<'py>) {
py.run(
script,
None,
Some(&locals.into_py_dict(py).unwrap()),
)
.unwrap()
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll have a look at fixing the calls to work with this


macro_rules! float_roundtrip_tests {
($wrapper:ident, $float_type:ty, $constructor:expr, $standard_test:ident, $wasm_test:ident, $infinity_test:ident, $zero_test:ident) => {
#[cfg(not(target_arch = "wasm32"))]
Expand All @@ -118,15 +128,11 @@ mod test_ordered_float {
Python::attach(|py| {
let f_py: Bound<'_, PyFloat> = f.into_pyobject(py).unwrap();

py_run!(
py,
f_py,
&format!(
py_run(py, format!(
"import math\nassert math.isclose(f_py, {})",
inner_f as f64 // Always interpret the literal rs float value as f64
// so that it's comparable with the python float
)
);
), [("f_py", &f_py)]);

let roundtripped_f: $wrapper<$float_type> = f_py.extract().unwrap();

Expand All @@ -142,16 +148,16 @@ mod test_ordered_float {
let f = $constructor(inner_f);

Python::attach(|py| {
let f_py: Bound<'_, PyFloat> = f.into_pyobject(py).unwrap();
let f_py: Bound<'_, PyFloat> = f.into_pyobject(py).unwrap();

py_run!(
py_run(
py,
f_py,
&format!(
format!(
"import math\nassert math.isclose(f_py, {})",
inner_f as f64 // Always interpret the literal rs float value as f64
// so that it's comparable with the python float
)
),
[("f_py", &f_py)],
);

let roundtripped_f: $wrapper<$float_type> = f_py.extract().unwrap();
Expand All @@ -169,15 +175,17 @@ mod test_ordered_float {
let ninf = $constructor(inner_ninf);

Python::attach(|py| {
let pinf_py: Bound<'_, PyFloat> = pinf.into_pyobject(py).unwrap();
let ninf_py: Bound<'_, PyFloat> = ninf.into_pyobject(py).unwrap();
let pinf_py: Bound<'_, PyFloat> = pinf.into_pyobject(py).unwrap();
let ninf_py: Bound<'_, PyFloat> = ninf.into_pyobject(py).unwrap();

py_run!(
py_run(
py,
pinf_py ninf_py,
"\
format!(
"\
assert pinf_py == float('inf')\n\
assert ninf_py == float('-inf')"
),
[("pinf_py", &pinf_py), ("ninf_py", &ninf_py)],
);

let roundtripped_pinf: $wrapper<$float_type> = pinf_py.extract().unwrap();
Expand All @@ -197,20 +205,22 @@ mod test_ordered_float {
let nzero = $constructor(inner_nzero);

Python::attach(|py| {
let pzero_py: Bound<'_, PyFloat> = pzero.into_pyobject(py).unwrap();
let nzero_py: Bound<'_, PyFloat> = nzero.into_pyobject(py).unwrap();
let pzero_py: Bound<'_, PyFloat> = pzero.into_pyobject(py).unwrap();
let nzero_py: Bound<'_, PyFloat> = nzero.into_pyobject(py).unwrap();

// This python script verifies that the values are 0.0 in magnitude
// and that the signs are correct(+0.0 vs -0.0)
py_run!(
py_run(
py,
pzero_py nzero_py,
"\
format!(
"\
import math\n\
assert pzero_py == 0.0\n\
assert math.copysign(1.0, pzero_py) > 0.0\n\
assert nzero_py == 0.0\n\
assert math.copysign(1.0, nzero_py) < 0.0"
),
[("pzero_py", &pzero_py), ("nzero_py", &nzero_py)],
);

let roundtripped_pzero: $wrapper<$float_type> = pzero_py.extract().unwrap();
Expand Down Expand Up @@ -271,12 +281,10 @@ mod test_ordered_float {
Python::attach(|py| {
let nan_py: Bound<'_, PyFloat> = nan.into_pyobject(py).unwrap();

py_run!(
py_run(
py,
nan_py,
"\
import math\n\
assert math.isnan(nan_py)"
format!("import math\nassert math.isnan(nan_py)"),
[("nan_py", &nan_py)],
);

let roundtripped_nan: OrderedFloat<$float_type> = nan_py.extract().unwrap();
Expand Down
Loading