2
2
3
3
import asyncio
4
4
5
- from ..learner import Learner2D
5
+ import pytest
6
+
7
+ from ..learner import Learner1D , Learner2D
6
8
from ..runner import simple , BlockingRunner , AsyncRunner , SequentialExecutor
7
9
8
10
9
- def test_nonconforming_output ():
11
+ def blocking_runner (learner , goal ):
12
+ BlockingRunner (learner , goal , executor = SequentialExecutor ())
13
+
14
+
15
+ def async_runner (learner , goal ):
16
+ runner = AsyncRunner (learner , goal , executor = SequentialExecutor ())
17
+ asyncio .get_event_loop ().run_until_complete (runner .task )
18
+
19
+
20
+ runners = [simple , blocking_runner , async_runner ]
21
+
22
+ def trivial_goal (learner ):
23
+ return learner .npoints > 10
24
+
25
+ @pytest .mark .parametrize ('runner' , runners )
26
+ def test_simple (runner ):
27
+ """Test that the runners actually run."""
28
+
29
+ def f (x ):
30
+ return x
31
+ learner = Learner1D (f , (- 1 , 1 ))
32
+ runner (learner , lambda l : l .npoints > 10 )
33
+ assert len (learner .data ) > 10
34
+
35
+
36
+ @pytest .mark .parametrize ('runner' , runners )
37
+ def test_nonconforming_output (runner ):
10
38
"""Test that using a runner works with a 2D learner, even when the
11
39
learned function outputs a 1-vector. This tests against the regression
12
40
flagged in https://gitlab.kwant-project.org/qt/adaptive/issues/58.
@@ -15,15 +43,14 @@ def test_nonconforming_output():
15
43
def f (x ):
16
44
return [0 ]
17
45
18
- def goal (l ):
19
- return l .npoints > 10
46
+ runner (Learner2D (f , [(- 1 , 1 ), (- 1 , 1 )]), trivial_goal )
20
47
21
- learner = Learner2D (f , [(- 1 , 1 ), (- 1 , 1 )])
22
- simple (learner , goal )
23
48
24
- learner = Learner2D (f , [(- 1 , 1 ), (- 1 , 1 )])
25
- BlockingRunner (learner , goal , executor = SequentialExecutor ())
49
+ def test_aync_def_function ():
26
50
27
- learner = Learner2D (f , [(- 1 , 1 ), (- 1 , 1 )])
28
- runner = AsyncRunner (learner , goal , executor = SequentialExecutor ())
51
+ async def f (x ):
52
+ return x
53
+
54
+ learner = Learner1D (f , (- 1 , 1 ))
55
+ runner = AsyncRunner (learner , trivial_goal )
29
56
asyncio .get_event_loop ().run_until_complete (runner .task )
0 commit comments