Update API.
This commit is contained in:
parent
b6943545be
commit
57bfde36c0
|
@ -21,4 +21,4 @@ def test_using_python_batch(benchmark, batch_numbers):
|
||||||
|
|
||||||
|
|
||||||
def test_using_rust_batch(benchmark, batch_numbers):
|
def test_using_rust_batch(benchmark, batch_numbers):
|
||||||
benchmark(unit_converter.batch_convert_celsius_to_fahrenheit, batch_numbers)
|
benchmark(unit_converter.batch_convert_to_fahrenheit, batch_numbers)
|
||||||
|
|
14
src/lib.rs
14
src/lib.rs
|
@ -6,32 +6,32 @@ extern crate pyo3;
|
||||||
use pyo3::prelude::*;
|
use pyo3::prelude::*;
|
||||||
|
|
||||||
#[pyfunction]
|
#[pyfunction]
|
||||||
fn convert_celsius_to_fahrenheit(celsius: f32) -> f32 {
|
fn convert_to_fahrenheit(celsius: f32) -> f32 {
|
||||||
celsius * 1.8 + 32.0
|
celsius * 1.8 + 32.0
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pyfunction]
|
#[pyfunction]
|
||||||
fn batch_convert_celsius_to_fahrenheit(celsius: Vec<f32>) -> Vec<f32> {
|
fn batch_convert_to_fahrenheit(celsius: Vec<f32>) -> Vec<f32> {
|
||||||
celsius
|
celsius
|
||||||
.iter()
|
.iter()
|
||||||
.map(|temperature| convert_celsius_to_fahrenheit(temperature.clone()))
|
.map(|temperature| convert_to_fahrenheit(temperature.clone()))
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pymodule]
|
#[pymodule]
|
||||||
fn unit_converter(_py: Python, m: &PyModule) -> PyResult<()> {
|
fn unit_converter(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||||
m.add_wrapped(wrap_pyfunction!(convert_celsius_to_fahrenheit))?;
|
m.add_wrapped(wrap_pyfunction!(convert_to_fahrenheit))?;
|
||||||
m.add_wrapped(wrap_pyfunction!(batch_convert_celsius_to_fahrenheit))?;
|
m.add_wrapped(wrap_pyfunction!(batch_convert_to_fahrenheit))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
||||||
use super::convert_celsius_to_fahrenheit;
|
use super::convert_to_fahrenheit;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn conversion_celsius_to_fahrenheit() {
|
fn conversion_celsius_to_fahrenheit() {
|
||||||
assert_eq!(convert_celsius_to_fahrenheit(25.0), 77.0);
|
assert_eq!(convert_to_fahrenheit(25.0), 77.0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
10
test.py
10
test.py
|
@ -7,12 +7,8 @@ def python_unit_converter(celsius):
|
||||||
return celsius * 1.8 + 32.0
|
return celsius * 1.8 + 32.0
|
||||||
|
|
||||||
|
|
||||||
def batch_python_unit_converter(temperatures):
|
|
||||||
return [celsius * 1.8 + 32.0 for celsius in temperatures]
|
|
||||||
|
|
||||||
|
|
||||||
def test_using_unit_converter():
|
def test_using_unit_converter():
|
||||||
assert unit_converter.convert_celsius_to_fahrenheit(25.0) == 77.0
|
assert unit_converter.convert_to_fahrenheit(25.0) == 77.0
|
||||||
assert python_unit_converter(25.0) == 77.0
|
assert python_unit_converter(25.0) == 77.0
|
||||||
|
|
||||||
|
|
||||||
|
@ -22,7 +18,7 @@ def test_using_python_constant(benchmark):
|
||||||
|
|
||||||
|
|
||||||
def test_using_rust_constant(benchmark):
|
def test_using_rust_constant(benchmark):
|
||||||
result = benchmark(unit_converter.convert_celsius_to_fahrenheit, 25.0)
|
result = benchmark(unit_converter.convert_to_fahrenheit, 25.0)
|
||||||
assert round(result, 3) == round(77.0, 3)
|
assert round(result, 3) == round(77.0, 3)
|
||||||
|
|
||||||
|
|
||||||
|
@ -34,5 +30,5 @@ def test_using_python(benchmark):
|
||||||
|
|
||||||
def test_using_rust(benchmark):
|
def test_using_rust(benchmark):
|
||||||
temperature = random.random() * 100
|
temperature = random.random() * 100
|
||||||
result = benchmark(unit_converter.convert_celsius_to_fahrenheit, temperature)
|
result = benchmark(unit_converter.convert_to_fahrenheit, temperature)
|
||||||
assert round(result, 3) == round(python_unit_converter(temperature), 3)
|
assert round(result, 3) == round(python_unit_converter(temperature), 3)
|
||||||
|
|
Loading…
Reference in New Issue