-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Tin Lai <[email protected]>
- Loading branch information
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from unittest import TestCase | ||
|
||
import numpy as np | ||
|
||
import env | ||
import visualiser | ||
from planners.basePlanner import Planner | ||
from samplers.baseSampler import Sampler | ||
from main import generate_args | ||
from tests.common_vars import DummyPlannerClass | ||
from utils import planner_registry | ||
|
||
|
||
class TestGenerateArgs(TestCase): | ||
def test_missing_argunment(self): | ||
with self.assertRaises(TypeError): | ||
generate_args() | ||
with self.assertRaises(TypeError): | ||
generate_args(planner_id="rrt") | ||
with self.assertRaises(TypeError): | ||
generate_args(map_fname="maps/4d.png") | ||
|
||
# should not raise error | ||
generate_args(planner_id="rrt", map_fname="maps/4d.png") | ||
|
||
# test error if the planner id has not been registered yet | ||
with self.assertRaises(ValueError): | ||
generate_args(planner_id="my_planner_id", map_fname="maps/4d.png") | ||
|
||
# test that after the planner id is registered, it will work. | ||
planner_registry.register_planner( | ||
planner_id="my_planner_id", | ||
planner_class=DummyPlannerClass, | ||
sampler_id="random", | ||
) | ||
generate_args(planner_id="my_planner_id", map_fname="maps/4d.png") | ||
|
||
def test_actual_planning(self): | ||
visualiser.VisualiserSwitcher.choose_visualiser("base") | ||
args = generate_args( | ||
planner_id="rrt", | ||
map_fname="maps/test.png", | ||
start_pt=np.array([25, 123]), | ||
goal_pt=np.array([225, 42]), | ||
) | ||
args.no_display = True | ||
|
||
e = env.Env(args, fixed_seed=0) | ||
ori_method = e.planner.run_once | ||
|
||
# prepare an exception to escape from the planning loop | ||
class PlanningSuccess(Exception): | ||
pass | ||
|
||
def planner_run_once_with_side_effect(*args, **kwargs): | ||
# pass through to planner | ||
ori_method(*args, **kwargs) | ||
if e.planner.c_max < float("inf"): | ||
raise PlanningSuccess() | ||
|
||
# patch the planner run_once such that it will terminates as soon as the | ||
# planning problem is finished. | ||
e.planner.run_once = planner_run_once_with_side_effect | ||
with self.assertRaises(PlanningSuccess): | ||
e.run() | ||
|
||
def test_get_attribute(self): | ||
visualiser.VisualiserSwitcher.choose_visualiser("pygame") | ||
args = generate_args( | ||
planner_id="rrt", | ||
map_fname="maps/test.png", | ||
start_pt=np.array([25, 123]), | ||
goal_pt=np.array([225, 42]), | ||
) | ||
args.no_display = True | ||
|
||
e = env.Env(args, fixed_seed=0) | ||
|
||
# test get planner | ||
assert isinstance(e.planner, Planner) | ||
|
||
# test get sampler | ||
assert isinstance(e.sampler, Sampler) |