def test_reals(exclude_zero, shape, dtype): strategy = testing.reals(min_value=None, max_value=None, exclude_zero=exclude_zero, shape=shape, dtype=dtype) for _ in range(10): strategy.example() assert True
import numpy as np from hypothesis import given from crowddynamics.core.motion.adjusting import force_adjust, torque_adjust from crowddynamics.core.motion.contact import force_contact from crowddynamics.core.motion.fluctuation import force_fluctuation, \ torque_fluctuation from crowddynamics.testing import reals SIZE = 10 @given(mass=reals(min_value=0, shape=SIZE), scale=reals(min_value=0, shape=SIZE)) def test_force_fluctuation(mass, scale): ans = force_fluctuation(mass, scale) assert isinstance(ans, np.ndarray) assert ans.dtype.type is np.float64 assert ans.shape == (SIZE, 2) @given(mass=reals(min_value=0), tau_adj=reals(min_value=0, exclude_zero='near'), v0=reals(min_value=0), e0=reals(shape=2), v=reals(shape=2)) def test_force_adjust(mass, tau_adj, v0, e0, v): ans = force_adjust(mass, tau_adj, v0, e0, v) assert isinstance(ans, np.ndarray) assert ans.dtype.type is np.float64 assert ans.shape == (2,)
import hypothesis.strategies as st import numpy as np from hypothesis import given from crowddynamics.core.distance import distance_circles, \ distance_three_circles, distance_circle_line, distance_three_circle_line from crowddynamics.testing import reals @given(x0=reals(-10, 10, shape=2), r0=reals(0.0, 1.0), x1=reals(-10, 10, shape=2), r1=reals(0.0, 1.0)) def test_distance_circle_circle(x0, r0, x1, r1): h, n = distance_circles(x0, r0, x1, r1) x = x0 - x1 r_tot = r0 + r1 assert isinstance(h, float) assert isinstance(n, np.ndarray) assert n.dtype.type is np.float64 assert h >= -r_tot @given(x0=st.tuples(*3 * [reals(-10, 10, shape=2)]), r0=st.tuples(*3 * [reals(0.0, 1.0)]), x1=st.tuples(*3 * [reals(-10, 10, shape=2)]), r1=st.tuples(*3 * [reals(0.0, 1.0)])) def test_distance_three_circle(x0, r0, x1, r1): h, n, r_moment0, r_moment1 = distance_three_circles(x0, r0, x1, r1)
def all_unique(data: np.ndarray) -> bool: """Test that all data rows have unique data""" ncols = data.shape[1] dtype = data.dtype.descr * ncols struct = data.view(dtype) return len(np.unique(struct)) == len(data) def does_not_raise_Qhull_error(points): """Test that Voronoi tesselation does raise errors""" try: vor = Voronoi(points) return True except QhullError: return False @pytest.mark.skip('Fixme') # @given(points=reals(1, 10, shape=(3, 2))) @given(points=reals(1, 10, shape=(10, 2))) # @given(points=reals(1, 10, shape=(100, 2))) def test_density_voronoi_1(points): assume(does_not_raise_Qhull_error(points)) assume(all_unique(points)) cell_size = 0.1 density = density_voronoi_1(points, cell_size=cell_size) assert True
import hypothesis.strategies as st from hypothesis.core import given from hypothesis.extra.numpy import arrays import crowddynamics.testing as testing from crowddynamics.core.interactions import ( interaction_agent_agent_circular, interaction_agent_agent_three_circle, agent_agent_block_list, agent_circular_obstacle, agent_three_circle_obstacle) from crowddynamics.core.structures import obstacle_type_linear from crowddynamics.simulation.agents import Circular, ThreeCircle CELL_SIZE = 3.6 agent_attributes = { 'radius': testing.reals(0, 1.0, exclude_zero='near'), 'mass': testing.reals(0, 1.0, exclude_zero='near'), 'positions': testing.reals(-10, 10, shape=2), 'velocity': testing.reals(-1, 1, shape=2), 'body_type': st.just('adult') } # Agent-agent @given( testing.agents(size_strategy=st.just(2), agent_type=Circular, attributes=agent_attributes)) def test_agent_interactions_circular(agents): interaction_agent_agent_circular(0, 1, agents) assert True
import hypothesis.strategies as st import numpy as np from hypothesis import given, assume from hypothesis.extra.numpy import arrays from crowddynamics.core.rand import poisson_clock, poisson_timings from crowddynamics.testing import reals @given(interval=reals(0.001, 0.01), dt=reals(0.001, 0.01)) def test_poisson_clock(interval, dt): # Limit the ratio so test doesn't run forever assume(1/100 < interval / dt < 100) time_prev = 0.0 for time in poisson_clock(interval, dt): assert isinstance(time, float) assert time_prev < time < dt time_prev = time @given(players=arrays(elements=st.integers(0, 10 ** 7), shape=10, dtype=np.int64), interval=reals(0.001, 0.01), dt=reals(0.001, 0.01)) def test_poisson_timings(players, interval, dt): # Limit the ratio so test doesn't run forever assume(1 / 100 < interval / dt < 100) for index in poisson_timings(players, interval, dt): assert isinstance(index, int)
import numpy as np import pytest from hypothesis import given, assume from shapely.geometry import Polygon, LineString from crowddynamics.core.geom2D import polygon_area, line_intersect from crowddynamics.core.vector2D import length from crowddynamics.testing import reals @given(reals(-10.0, 10.0, shape=(5, 2))) def test_polygon_area(vertices): poly = Polygon(vertices).convex_hull assume(poly.area > 0.0) area = polygon_area(np.asarray(poly.exterior)) assert np.isclose(area, poly.area) # @pytest.mark.skip('Fix line_intersect function') @given(x0=reals(0.0, 1.0, shape=2, exclude_zero='near'), x1=reals(0.0, 1.0, shape=2, exclude_zero='near'), y0=reals(0.0, 1.0, shape=2, exclude_zero='near'), y1=reals(0.0, 1.0, shape=2, exclude_zero='near')) def test_line_intersect(x0, x1, y0, y1): assume(not np.isclose(length(x1 - x0), 0.0)) assume(not np.isclose(length(y1 - y0), 0.0)) res = line_intersect(x0, x1, y0, y1) correct = LineString([x0, x1]).intersects(LineString([y0, y1])) assert res == correct
import numpy as np import pytest from hypothesis.core import given from hypothesis.extra.numpy import arrays from crowddynamics.core.steering.quickest_path import direction_map, \ distance_map, meshgrid from crowddynamics.testing import reals @given(step=reals(0.1, 1)) def test_meshgrid(step): mgrid = meshgrid(step, minx=0.0, miny=0.0, maxx=10.0, maxy=10.0) assert True @pytest.mark.skip def test_distance_map(mgrid, targets, obstacles): dmap = distance_map(mgrid, targets, obstacles) assert True @given(dmap=arrays(dtype=np.float64, shape=(5, 5), elements=reals(-10, 10, exclude_zero=True))) def test_direction_map(dmap): u, v = direction_map(dmap) assert u.shape == dmap.shape assert v.shape == dmap.shape
import hypothesis.strategies as st import numpy as np from hypothesis import given, assume from crowddynamics.core.evacuation import agent_closer_to_exit, \ narrow_exit_capacity from crowddynamics.testing import reals @given(c_door=reals(shape=2), position=reals(shape=(10, 2))) def test_agent_closer_to_exit(c_door, position): indices = agent_closer_to_exit(c_door, position) assert isinstance(indices, np.ndarray) assert indices.dtype.type is np.int64 @given(d_door=reals(0.0, 3.0, exclude_zero='near'), d_agent=reals(0.0, 3.0, exclude_zero='near'), d_layer=reals(0.0, 1.0, exclude_zero='near') | st.none(), coeff=reals(0.0, 3.0, exclude_zero='near')) def test_narrow_exit_capacity(d_door, d_agent, d_layer, coeff): assume(d_door >= d_agent) capacity = narrow_exit_capacity(d_door, d_agent, d_layer, coeff) assert isinstance(capacity, float) assert capacity >= 0.0
from hypothesis import given, assume from crowddynamics.core.integrator import adaptive_timestep, \ euler_integrator, velocity_verlet_integrator from crowddynamics.testing import reals @given(dt_min=reals(min_value=0, max_value=10.0, exclude_zero='near'), dt_max=reals(min_value=0, max_value=10.0, exclude_zero='near')) def test_adaptive_timestep1(agents_circular, dt_min, dt_max): assume(0 < dt_min < dt_max) dt = adaptive_timestep(agents_circular.array, dt_min, dt_max) assert 0 < dt_min <= dt <= dt_max @given(dt_min=reals(min_value=0, max_value=10.0, exclude_zero='near'), dt_max=reals(min_value=0, max_value=10.0, exclude_zero='near')) def test_adaptive_timestep2(agents_three_circle, dt_min, dt_max): assume(0 < dt_min < dt_max) dt = adaptive_timestep(agents_three_circle.array, dt_min, dt_max) assert 0 < dt_min <= dt <= dt_max @given(dt_min=reals(min_value=0, max_value=10.0, exclude_zero='near'), dt_max=reals(min_value=0, max_value=10.0, exclude_zero='near')) def test_euler_integration1(agents_circular, dt_min, dt_max): assume(0 < dt_min < dt_max) dt = euler_integrator(agents_circular.array, dt_min, dt_max) assert 0 < dt_min <= dt <= dt_max
import numpy as np from hypothesis import given, assume from crowddynamics.core.vector2D import cross, wrap_to_pi, truncate, \ rotate270, normalize, length, angle, rotate90, dot, unit_vector from crowddynamics.testing import reals @given(phi=reals()) def test_wrap_to_pi(phi): ans = wrap_to_pi(phi) assert isinstance(ans, float) assert -np.pi <= ans <= np.pi if (phi + np.pi) % (2 * np.pi) == 0.0: if phi > 0: assert ans == np.pi else: assert ans == -np.pi @given(a=reals(shape=2)) def test_rotate90(a): ans = rotate90(a) assert isinstance(ans, np.ndarray) @given(a=reals(shape=2)) def test_rotate270(a): ans = rotate270(a) assert isinstance(ans, np.ndarray)
for _ in range(10): strategy.example() assert True @pytest.mark.parametrize('num_verts', (3, 4, 5)) @pytest.mark.parametrize('has_holes', (False, True)) def test_polygons(num_verts, has_holes): strategy = testing.polygons(num_verts=num_verts, has_holes=has_holes) for _ in range(10): strategy.example() assert True agent_attributes = { 'radius': testing.reals(0.1, 1.0), 'mass': testing.reals(0.1, 1.0), 'body_type': st.sampled_from(('adult', 'male', 'female')) } @pytest.mark.parametrize('agent_type, attributes', [(Circular, agent_attributes), (ThreeCircle, agent_attributes)]) def test_agents(agent_type, attributes): agents = testing.agents(size_strategy=st.just(10), agent_type=agent_type, attributes=attributes) assert agents.example().shape == (10, )
def test_linestring_sample(poly): ls = poly.exterior vertices = np.asarray(ls) for i, p in zip(range(10), linestring_sample(vertices)): assert np.isclose(ls.distance(Point(p)), 0.0) @given(arrays(np.float64, (10, 3, 2), st.floats(-100, 100, False, False))) def test_triangle_area_cumsum(trimesh): cumsum = triangle_area_cumsum(trimesh) assert isinstance(cumsum, np.ndarray) assert cumsum.dtype.type is np.float64 assert np.all(np.sort(cumsum) == cumsum) @given(reals(min_value=-100, max_value=100, shape=2), reals(min_value=-100, max_value=100, shape=2), reals(min_value=-100, max_value=100, shape=2)) def test_random_sample_triangle(a, b, c): # Assume that the area of the triangle is not zero. area = polygon_area(np.stack((a, b, c))) assume(not np.isclose(area, 0.0)) triangle = Polygon((a, b, c)) p = random_sample_triangle(a, b, c) point = Point(p) distance = triangle.distance(point) assert isinstance(p, np.ndarray) assert triangle.intersects(point) or np.isclose(distance, 0.0)