Skip to content

Commit

Permalink
Add tests for shuffle_do()
Browse files Browse the repository at this point in the history
  • Loading branch information
EwoutH committed Sep 21, 2024
1 parent bc06c74 commit b24bc52
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions tests/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,40 @@ def test_agentset_map_callable():
assert all(i == entry for i, entry in zip(results, range(1, 11)))


def test_agentset_shuffle_do():
"""Test AgentSet.shuffle_do method."""
model = Model()

class TestAgentShuffleDo(Agent):
def __init__(self, model):
super().__init__(model)
self.called = False

def test_method(self):
self.called = True

agents = [TestAgentShuffleDo(model) for _ in range(100)]
agentset = AgentSet(agents, model)

# Test shuffle_do with a string method name
agentset.shuffle_do("test_method")
assert all(agent.called for agent in agents)

# Reset the called flag
for agent in agents:
agent.called = False

# Test shuffle_do with a callable
agentset.shuffle_do(lambda agent: setattr(agent, 'called', True))
assert all(agent.called for agent in agents)

# Verify that the order is indeed shuffled
original_order = list(agentset)
shuffled_order = []
agentset.shuffle_do(lambda agent: shuffled_order.append(agent))
assert original_order != shuffled_order, "The order should be different after shuffle_do"


def test_agentset_get_attribute():
"""Test AgentSet.get for attributes."""
model = Model()
Expand Down

0 comments on commit b24bc52

Please sign in to comment.