2019-11-05 10:00:30 -05:00
|
|
|
import random
|
|
|
|
|
2019-11-05 22:29:47 -05:00
|
|
|
import pytest
|
|
|
|
|
2019-02-06 08:57:13 -05:00
|
|
|
import unit_converter
|
|
|
|
|
2019-02-06 09:04:04 -05:00
|
|
|
|
2019-11-05 22:29:47 -05:00
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def batch_numbers():
|
|
|
|
return [random.random() * 100 for x in range(1000)]
|
|
|
|
|
|
|
|
|
2019-10-30 10:37:01 -04:00
|
|
|
def python_unit_converter(celsius):
|
|
|
|
return celsius * 1.8 + 32.0
|
|
|
|
|
|
|
|
|
2019-11-05 22:29:47 -05:00
|
|
|
def batch_python_unit_converter(temperatures):
|
|
|
|
return [celsius * 1.8 + 32.0 for celsius in temperatures]
|
|
|
|
|
|
|
|
|
2019-02-06 09:04:04 -05:00
|
|
|
def test_using_unit_converter():
|
|
|
|
assert unit_converter.convert_celsius_to_fahrenheit(25.0) == 77.0
|
2019-10-30 10:37:01 -04:00
|
|
|
assert python_unit_converter(25.0) == 77.0
|
|
|
|
|
|
|
|
|
2019-11-05 10:00:30 -05:00
|
|
|
def test_using_python_constant(benchmark):
|
2019-10-30 10:37:01 -04:00
|
|
|
result = benchmark(python_unit_converter, 25.0)
|
2019-11-05 10:00:30 -05:00
|
|
|
assert round(result, 3) == round(77.0, 3)
|
2019-10-30 10:37:01 -04:00
|
|
|
|
|
|
|
|
2019-11-05 10:00:30 -05:00
|
|
|
def test_using_rust_constant(benchmark):
|
2019-10-30 10:37:01 -04:00
|
|
|
result = benchmark(unit_converter.convert_celsius_to_fahrenheit, 25.0)
|
2019-11-05 10:00:30 -05:00
|
|
|
assert round(result, 3) == round(77.0, 3)
|
|
|
|
|
|
|
|
|
|
|
|
def test_using_python(benchmark):
|
|
|
|
temperature = random.random() * 100
|
|
|
|
result = benchmark(python_unit_converter, temperature)
|
|
|
|
assert round(result, 3) == round(python_unit_converter(temperature), 3)
|
|
|
|
|
|
|
|
|
|
|
|
def test_using_rust(benchmark):
|
|
|
|
temperature = random.random() * 100
|
|
|
|
result = benchmark(unit_converter.convert_celsius_to_fahrenheit, temperature)
|
|
|
|
assert round(result, 3) == round(python_unit_converter(temperature), 3)
|
2019-11-05 22:29:47 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_using_python_batch(benchmark, batch_numbers):
|
|
|
|
benchmark(batch_python_unit_converter, batch_numbers)
|
|
|
|
|
|
|
|
|
|
|
|
def test_using_rust_batch(benchmark, batch_numbers):
|
|
|
|
benchmark(unit_converter.batch_convert_celsius_to_fahrenheit, batch_numbers)
|