Update API.

This commit is contained in:
Dorian 2019-11-06 17:38:19 -05:00
parent b6943545be
commit 57bfde36c0
3 changed files with 11 additions and 15 deletions

View File

@ -21,4 +21,4 @@ def test_using_python_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)

View File

@ -6,32 +6,32 @@ extern crate pyo3;
use pyo3::prelude::*;
#[pyfunction]
fn convert_celsius_to_fahrenheit(celsius: f32) -> f32 {
fn convert_to_fahrenheit(celsius: f32) -> f32 {
celsius * 1.8 + 32.0
}
#[pyfunction]
fn batch_convert_celsius_to_fahrenheit(celsius: Vec<f32>) -> Vec<f32> {
fn batch_convert_to_fahrenheit(celsius: Vec<f32>) -> Vec<f32> {
celsius
.iter()
.map(|temperature| convert_celsius_to_fahrenheit(temperature.clone()))
.map(|temperature| convert_to_fahrenheit(temperature.clone()))
.collect()
}
#[pymodule]
fn unit_converter(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_wrapped(wrap_pyfunction!(convert_celsius_to_fahrenheit))?;
m.add_wrapped(wrap_pyfunction!(batch_convert_celsius_to_fahrenheit))?;
m.add_wrapped(wrap_pyfunction!(convert_to_fahrenheit))?;
m.add_wrapped(wrap_pyfunction!(batch_convert_to_fahrenheit))?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::convert_celsius_to_fahrenheit;
use super::convert_to_fahrenheit;
#[test]
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
View File

@ -7,12 +7,8 @@ def python_unit_converter(celsius):
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():
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
@ -22,7 +18,7 @@ def test_using_python_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)
@ -34,5 +30,5 @@ def test_using_python(benchmark):
def test_using_rust(benchmark):
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)