Skip to content

Commit

Permalink
Add tests for init_pos
Browse files Browse the repository at this point in the history
Reference: #119

Tests the init_pos feature for swarm generation. Checks
cases when:
  - init_pos and bounds is not None
  - init_pos and bounds is None
  - either init_pos or bounds is None (2 cases)

Signed-off-by: Lester James V. Miranda <ljvmiranda@gmail.com>
  • Loading branch information
ljvmiranda921 committed Jun 7, 2018
1 parent c15ba36 commit 71e5f07
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/backend/test_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,32 @@ def test_generate_swarm_return_values(bounds, center):
upper_bound = center * np.array(max_bounds)
assert (pos <= upper_bound).all() and (pos >= lower_bound).all()


@pytest.mark.parametrize('bounds', [None, ([1,1,1], [10,10,10])])
@pytest.mark.parametrize('init_pos', [None, np.array([[2,5,6],[7,2,1]])])
def test_generate_swarm_bounds_init_pos(bounds, init_pos):
"""Tests if generate_swarm() returns expected values given init_pos and bounds"""
pos = P.generate_swarm(n_particles=2, dimensions=3, bounds=bounds,
init_pos=init_pos)
if (bounds is None) and (init_pos is None):
min_bounds, max_bounds = (0.0, 1.00)
elif (bounds is None) and (init_pos is not None):
min_bounds, max_bounds = (-np.inf, np.inf)
else:
min_bounds, max_bounds = bounds
lower_bound = np.array(min_bounds)
upper_bound = np.array(max_bounds)
assert (pos <= upper_bound).all() and (pos >= lower_bound).all()

def test_generate_swarm_out_of_bounds():
"""Tests if generate_swarm() raises ValueError when initialized with the wrong value"""
bounds = ([1,1,1], [5,5,5])
init_pos = np.array([[-2,3,3], [6,8,1]])
with pytest.raises(ValueError):
pos = P.generate_swarm(n_particles=2, dimensions=3, bounds=bounds,
init_pos=init_pos)


@pytest.mark.parametrize('clamp', [None, (0,1), (2,5), (1,6)])
def test_generate_velocity_return_values(clamp):
"""Tests if generate_velocity() returns expected values"""
Expand Down

0 comments on commit 71e5f07

Please sign in to comment.