def test_mistyped_relative_to(): with pytest.raises(RuntimeParseError): compileScenic('ego = Object facing 0 relative to 1@2')
def test_vector_operator(): scenario = compileScenic('ego = Object at (-3, 3) @ 0 + (100, 110) @ 0') xs = [sampleEgo(scenario).position.x for i in range(100)] assert all(97 <= x <= 113 for x in xs) assert any(x < 105 for x in xs) assert any(105 < x for x in xs)
def test_uniform_interval_wrong_type(): with pytest.raises(InterpreterParseError): compileScenic('x = ([], 4)') with pytest.raises(InterpreterParseError): compileScenic('x = (-10, [])')
def test_independence(): scenario = compileScenic('ego = Object at Range(0, 1) @ Range(0, 1)') pos = sampleEgo(scenario).position assert pos.x != pos.y
def test_options(dist): scenario = compileScenic(f'ego = Object at {dist}({{0: 1, 1: 9}}) @ 0') xs = [sampleEgo(scenario).position.x for i in range(200)] assert all(x == 0 or x == 1 for x in xs) assert 145 <= sum(xs) < 200
def test_heading_wrong_type(): with pytest.raises(RuntimeParseError): compileScenic('ego = Object with heading 4 @ 1')
def test_relative_heading_inconsistent(): """A special case where we can detect inconsistency of the requirements.""" with pytest.raises(InconsistentScenarioError): scenario = compileScenic('ego = Object\n' 'other = Object at 10@10\n' 'require abs(relative heading of other) < -1')
def test_nowhere(): with pytest.raises(InvalidScenarioError): compileScenic('ego = Object with regionContainedIn nowhere')
def test_everywhere(): scenario = compileScenic('ego = Object with regionContainedIn everywhere') sampleScene(scenario, maxIterations=1)
def test_noninterference(): for i in range(2): scenario = compileScenic('ego = Object at VerifaiRange(0, 1) @ 0') for j in range(5): scene, iterations = scenario.generate(maxIterations=1) assert len(scenario.externalSampler.cachedSample) == 1
def test_halton(): scenario = compileScenic('param verifaiSamplerType = "halton"\n' 'ego = Object at VerifaiRange(5, 15) @ 0') xs = [sampleEgo(scenario).position.x for i in range(60)] assert all(5 <= x <= 15 for x in xs) assert 29 <= sum(x < 10 for x in xs) <= 31
def test_distance_no_ego(): with pytest.raises(RuntimeParseError): compileScenic(""" other = Object ego = Object at 2@2, facing distance to other """)
def test_apparent_heading_no_ego(): with pytest.raises(RuntimeParseError): compileScenic(""" other = Object ego = Object at 2@2, facing apparent heading of other """)
def test_mistyped_relative_to_lazy(): with pytest.raises(RuntimeParseError): compileScenic(""" vf = VectorField("Foo", lambda pos: 0.5) ego = Object facing 1@2 relative to (0 relative to vf) """)
def test_specifier_layout(template, continuation, indent, gap): """Test legal specifier layouts, with and without line continuations.""" preamble = template.format(continuation=continuation, indent=indent) program = preamble + gap + 'Object at 20@20' print('TESTING PROGRAM:', program) compileScenic(program)
def test_static_containment_violation(): with pytest.raises(InvalidScenarioError): compileScenic(""" foo = RectangularRegion(0@0, 0, 5, 5) ego = Object at 10@10, with regionContainedIn foo """)
def test_dangling_specifier_list(): with pytest.raises(TokenParseError): compileScenic('ego = Object with width 4,') with pytest.raises(TokenParseError): compileScenic('ego = Object with width 4, # comment')
def test_static_empty_container(): with pytest.raises(InvalidScenarioError): compileScenic(""" foo = PolylineRegion([0@0, 1@1]).intersect(PolylineRegion([1@0, 2@1])) ego = Object at Range(0, 2) @ Range(0, 1), with regionContainedIn foo """)
def test_position_wrong_type(): with pytest.raises(RuntimeParseError): compileScenic('ego = Object with position 4')
def test_static_visibility_violation(): with pytest.raises(InvalidScenarioError): compileScenic(""" ego = Object at 10@0, facing -90 deg, with viewAngle 90 deg Object at 0@10 """)
def test_list_param_lazy(): with pytest.raises(InvalidScenarioError): compileScenic('vf = VectorField("Foo", lambda pos: 2 * pos.x)\n' 'x = 0 relative to vf\n' 'param p = Uniform([0, x], [0, x*2])[1]\n' 'ego = Object')
def test_static_intersection_violation(): with pytest.raises(InvalidScenarioError): compileScenic(""" ego = Object at 0@0 Object at 1@0 """)
def test_uniform_interval_wrong_type(): with pytest.raises(RuntimeParseError): compileScenic('x = Range([], 4)') with pytest.raises(RuntimeParseError): compileScenic('x = Range(-10, [])')
def test_param_in_requirement(): with pytest.raises(ScenicSyntaxError): compileScenic(""" require param x = 4 ego = Object """)
def test_operator(): scenario = compileScenic('ego = Object at -(100 + (0, 100)) @ 0') xs = [sampleEgo(scenario).position.x for i in range(60)] assert all(-200 <= x <= -100 for x in xs) assert any(x < -150 for x in xs) assert any(-150 < x for x in xs)
def test_mutate_in_requirement(): with pytest.raises(ScenicSyntaxError): compileScenic(""" require mutate ego = Object """)
def test_shared_dependency(): scenario = compileScenic('x = (0, 1)\n' 'ego = Object at (x + x) @ 0') xs = [sampleEgo(scenario).position.x for i in range(60)] assert all(0 <= x <= 2 for x in xs) assert any(x < 1 for x in xs) assert any(1 < x for x in xs)
def test_require_in_requirement(): with pytest.raises(ScenicSyntaxError): compileScenic(""" require (require True) ego = Object """)
def test_uniform_interval(): scenario = compileScenic('ego = Object at (100, 200) @ 0') xs = [sampleEgo(scenario).position.x for i in range(60)] assert all(100 <= x <= 200 for x in xs) assert any(x < 150 for x in xs) assert any(150 < x for x in xs)
def test_inherit_constructors(runLocally): with runLocally(): scenario = compileScenic('from helper import Caerbannog\n' 'ego = Caerbannog')