Skip to content

Commit 78c73b3

Browse files
Add test for functions returning a single value
When a function returns a single value, Scheduler should return a List of these values alone (not wrapped in tuples). For functions returning multiple values, Scheduler should return a List of tuples which each contain the result of a function call. Both of these cases are now tested.
1 parent 7b7c969 commit 78c73b3

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

test/test_scheduler.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,34 @@
3333
_long_task,
3434
_get_input_output,
3535
_get_input_output_numpy,
36+
_get_input_output_single_result,
37+
_get_input_output_two_results,
3638
assert_results,
3739
assert_results_numpy,
3840
_func,
3941
_funcq,
4042
_func_no_params,
4143
_func_numpy,
4244
_func_no_return,
45+
_func_returns_single_value,
46+
_func_returns_two_values,
4347
)
4448
except:
4549
from utils import (
4650
_long_task,
4751
_get_input_output,
4852
_get_input_output_numpy,
53+
_get_input_output_single_result,
54+
_get_input_output_two_results,
4955
assert_results,
5056
assert_results_numpy,
5157
_func,
5258
_funcq,
5359
_func_no_params,
5460
_func_numpy,
5561
_func_no_return,
62+
_func_returns_single_value,
63+
_func_returns_two_values,
5664
)
5765

5866

@@ -187,6 +195,39 @@ def test_map():
187195
assert scheduler.finished
188196

189197

198+
def test_map_one_return_value():
199+
"""Tests whether `map()` works correctly when the target function only returns a single result."""
200+
scheduler = Scheduler()
201+
202+
args, expected = _get_input_output_single_result()
203+
204+
loop = asyncio.get_event_loop()
205+
results: List[Tuple[int]] = loop.run_until_complete(
206+
scheduler.map(target=_func_returns_single_value, args=args)
207+
)
208+
209+
assert len(expected) == len(results)
210+
assert all([not hasattr(r, "__len__") for r in results])
211+
212+
assert scheduler.finished
213+
214+
215+
def test_map_two_return_values():
216+
"""Tests whether `map()` works correctly when the target function returns two results."""
217+
scheduler = Scheduler()
218+
219+
args, expected = _get_input_output_two_results()
220+
221+
loop = asyncio.get_event_loop()
222+
results: List[Tuple[int, int]] = loop.run_until_complete(
223+
scheduler.map(target=_func_returns_two_values, args=args)
224+
)
225+
226+
assert all([hasattr(r, "__len__") for r in results])
227+
assert_results(expected, results)
228+
assert scheduler.finished
229+
230+
190231
def test_map_no_args():
191232
"""
192233
Tests whether `map()` works correctly with no arguments.
@@ -307,4 +348,5 @@ async def terminate():
307348

308349
if __name__ == "__main__":
309350
test_shared_memory_numpy()
351+
test_map_two_return_values()
310352
print("Finished.")

test/utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ def _get_input_output() -> Tuple[List, List]:
4747
return args, expected_output
4848

4949

50+
def _get_input_output_single_result() -> Tuple[List, List]:
51+
args = [(i,) for i in range(20)]
52+
expected_output = [(_func_returns_single_value(*a)) for a in args]
53+
return args, expected_output
54+
55+
56+
def _get_input_output_two_results() -> Tuple[List, List]:
57+
args = [(i,) for i in range(20)]
58+
expected_output = [(_func_returns_two_values(*a)) for a in args]
59+
return args, expected_output
60+
61+
5062
def _get_input_output_numpy() -> Tuple[List, List]:
5163
args = [(i, 500000 + i ** 2) for i in range(15)]
5264
expected_output = [(_func_numpy(*a)) for a in args]
@@ -77,6 +89,14 @@ def _func_numpy(x, y) -> ndarray:
7789
return numpy.arange(x, y)
7890

7991

92+
def _func_returns_single_value(x: int) -> int:
93+
return 3 * x
94+
95+
96+
def _func_returns_two_values(x: int) -> Tuple[int, int]:
97+
return 5 * x, x ** 2
98+
99+
80100
def _func_no_return(x: int) -> None:
81101
pass
82102

0 commit comments

Comments
 (0)