Skip to content

Commit e3c8bc9

Browse files
committed
Update PropertyGrid tests
1 parent 62047c2 commit e3c8bc9

File tree

1 file changed

+86
-53
lines changed

1 file changed

+86
-53
lines changed

tests/test_space.py

Lines changed: 86 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -478,85 +478,118 @@ def test_remove_nonexistent_property_layer(self):
478478
with self.assertRaises(ValueError):
479479
self.grid.remove_property_layer("nonexistent_layer")
480480

481-
# Test selecting cells
482-
def test_select_cells_multi_properties(self):
483-
condition = lambda x: x == 0
484-
selected_cells = self.grid.select_cells_multi_properties({"layer1": condition})
485-
self.assertEqual(len(selected_cells), 100) # All cells should be selected
486-
487-
def test_select_cells_with_multiple_properties(self):
488-
condition1 = lambda x: x == 0
489-
condition2 = lambda x: x == 1
490-
selected_cells = self.grid.select_cells_multi_properties(
491-
{"layer1": condition1, "layer2": condition2}
492-
)
493-
self.assertEqual(
494-
len(selected_cells), 100
495-
) # All cells should meet both conditions
481+
# Test getting masks
482+
def test_get_empty_mask(self):
483+
empty_mask = self.grid.get_empty_mask()
484+
self.assertTrue(np.all(empty_mask == np.ones((10, 10), dtype=bool)))
485+
486+
def test_get_empty_mask_with_agent(self):
487+
agent = MockAgent(0, self.grid)
488+
self.grid.place_agent(agent, (4, 6))
489+
490+
empty_mask = self.grid.get_empty_mask()
491+
expected_mask = np.ones((10, 10), dtype=bool)
492+
expected_mask[4, 6] = False
496493

497-
def test_select_cells_with_neighborhood(self):
494+
self.assertTrue(np.all(empty_mask == expected_mask))
495+
496+
def test_get_neighborhood_mask(self):
497+
agent = MockAgent(0, self.grid)
498+
agent2 = MockAgent(1, self.grid)
499+
self.grid.place_agent(agent, (5, 5))
500+
self.grid.place_agent(agent2, (5, 6))
501+
neighborhood_mask = self.grid.get_neighborhood_mask((5, 5), True, False, 1)
502+
expected_mask = np.zeros((10, 10), dtype=bool)
503+
expected_mask[4:7, 4:7] = True
504+
expected_mask[5, 5] = False
505+
self.assertTrue(np.all(neighborhood_mask == expected_mask))
506+
507+
# Test selecting and moving to cells based on multiple conditions
508+
def test_select_cells_by_properties(self):
498509
condition = lambda x: x == 0
499-
selected_cells = self.grid.select_cells_multi_properties(
500-
{"layer1": condition}, only_neighborhood=True, pos=(5, 5), radius=1
501-
)
502-
# Expect a selection of cells around (5, 5)
503-
expected_selection = [
504-
(4, 4),
505-
(4, 5),
506-
(4, 6),
507-
(5, 4),
508-
(5, 6),
509-
(6, 4),
510-
(6, 5),
511-
(6, 6),
512-
]
513-
self.assertCountEqual(selected_cells, expected_selection)
510+
selected_cells = self.grid.select_cells_by_properties({"layer1": condition})
511+
self.assertEqual(len(selected_cells), 100)
514512

515-
def test_select_no_cells_due_to_conflicting_conditions(self):
516-
condition1 = lambda x: x == 0 # All cells in layer1 meet this
517-
condition2 = lambda x: x != 1 # No cells in layer2 meet this
518-
selected_cells = self.grid.select_cells_multi_properties(
519-
{"layer1": condition1, "layer2": condition2}
520-
)
521-
self.assertEqual(len(selected_cells), 0)
513+
def test_select_cells_by_properties_return_mask(self):
514+
condition = lambda x: x == 0
515+
selected_mask = self.grid.select_cells_by_properties({"layer1": condition}, return_list=False)
516+
self.assertTrue(isinstance(selected_mask, np.ndarray))
517+
self.assertTrue(selected_mask.all())
522518

523-
# Test moving agents to cells
524-
def test_move_agent_to_random_cell(self):
519+
def test_move_agent_to_cell_by_properties(self):
525520
agent = MockAgent(1, self.grid)
526521
self.grid.place_agent(agent, (5, 5))
527522
conditions = {"layer1": lambda x: x == 0}
528-
self.grid.move_agent_to_random_cell(agent, conditions)
523+
self.grid.move_agent_to_cell_by_properties(agent, conditions)
529524
self.assertNotEqual(agent.pos, (5, 5))
530525

531526
def test_move_agent_no_eligible_cells(self):
532527
agent = MockAgent(3, self.grid)
533528
self.grid.place_agent(agent, (5, 5))
534-
conditions = {"layer1": lambda x: x != 0} # No cell meets this condition
535-
self.grid.move_agent_to_random_cell(agent, conditions)
536-
# Agent should not move
529+
conditions = {"layer1": lambda x: x != 0}
530+
self.grid.move_agent_to_cell_by_properties(agent, conditions)
537531
self.assertEqual(agent.pos, (5, 5))
538532

539-
# Move to cells with the highest or lowest value in a layer
533+
# Test selecting and moving to cells based on extreme values
534+
def test_select_extreme_value_cells(self):
535+
self.grid.properties["layer2"].set_cell((3, 1), 1.1)
536+
target_cells = self.grid.select_extreme_value_cells("layer2", "highest")
537+
self.assertIn((3, 1), target_cells)
538+
539+
def test_select_extreme_value_cells_return_mask(self):
540+
self.grid.properties["layer2"].set_cell((3, 1), 1.1)
541+
target_mask = self.grid.select_extreme_value_cells("layer2", "highest", return_list=False)
542+
self.assertTrue(isinstance(target_mask, np.ndarray))
543+
self.assertTrue(target_mask[3, 1])
544+
540545
def test_move_agent_to_extreme_value_cell(self):
541546
agent = MockAgent(2, self.grid)
542547
self.grid.place_agent(agent, (5, 5))
543548
self.grid.properties["layer2"].set_cell((3, 1), 1.1)
544549
self.grid.move_agent_to_extreme_value_cell(agent, "layer2", "highest")
545550
self.assertEqual(agent.pos, (3, 1))
546551

547-
def test_move_agent_to_extreme_value_cell_lowest(self):
548-
agent = MockAgent(4, self.grid)
552+
# Test using masks
553+
def test_select_cells_by_properties_with_empty_mask(self):
554+
self.grid.place_agent(MockAgent(0, self.grid), (5, 5)) # Placing an agent to ensure some cells are not empty
555+
empty_mask = self.grid.get_empty_mask()
556+
condition = lambda x: x == 0
557+
selected_cells = self.grid.select_cells_by_properties({"layer1": condition}, mask=empty_mask)
558+
self.assertNotIn((5, 5), selected_cells) # (5, 5) should not be in the selection as it's not empty
559+
560+
def test_select_cells_by_properties_with_neighborhood_mask(self):
561+
neighborhood_mask = self.grid.get_neighborhood_mask((5, 5), True, False, 1)
562+
condition = lambda x: x == 0
563+
selected_cells = self.grid.select_cells_by_properties({"layer1": condition}, mask=neighborhood_mask)
564+
expected_selection = [
565+
(4, 4), (4, 5), (4, 6), (5, 4), (5, 6), (6, 4), (6, 5), (6, 6)
566+
] # Cells in the neighborhood of (5, 5)
567+
self.assertCountEqual(selected_cells, expected_selection)
568+
569+
def test_move_agent_to_cell_by_properties_with_empty_mask(self):
570+
agent = MockAgent(1, self.grid)
549571
self.grid.place_agent(agent, (5, 5))
550-
self.grid.properties["layer2"].set_cell((6, 7), 0)
551-
self.grid.move_agent_to_extreme_value_cell(agent, "layer2", "lowest")
552-
# Agent should move to a cell with the lowest value in layer2 (which is 1 for all cells, so position should not change)
553-
self.assertEqual(agent.pos, (6, 7))
572+
self.grid.place_agent(MockAgent(2, self.grid), (4, 5)) # Placing another agent to create a non-empty cell
573+
empty_mask = self.grid.get_empty_mask()
574+
conditions = {"layer1": lambda x: x == 0}
575+
self.grid.move_agent_to_cell_by_properties(agent, conditions, mask=empty_mask)
576+
self.assertNotEqual(agent.pos, (4, 5)) # Agent should not move to (4, 5) as it's not empty
577+
578+
def test_move_agent_to_cell_by_properties_with_neighborhood_mask(self):
579+
agent = MockAgent(1, self.grid)
580+
self.grid.place_agent(agent, (5, 5))
581+
neighborhood_mask = self.grid.get_neighborhood_mask((5, 5), True, False, 1)
582+
conditions = {"layer1": lambda x: x == 0}
583+
self.grid.move_agent_to_cell_by_properties(agent, conditions, mask=neighborhood_mask)
584+
self.assertIn(agent.pos, [
585+
(4, 4), (4, 5), (4, 6), (5, 4), (5, 6), (6, 4), (6, 5), (6, 6)
586+
]) # Agent should move within the neighborhood
554587

555-
# Edge Cases: Invalid property name or mode
588+
# Test invalid inputs
556589
def test_invalid_property_name_in_conditions(self):
557590
condition = lambda x: x == 0
558591
with self.assertRaises(KeyError):
559-
self.grid.select_cells_multi_properties({"nonexistent_layer": condition})
592+
self.grid.select_cells_by_properties({"nonexistent_layer": condition})
560593

561594
def test_invalid_mode_in_move_to_extreme(self):
562595
agent = MockAgent(6, self.grid)

0 commit comments

Comments
 (0)