Commit 66f51378 authored by Benedikt Kleinmeier's avatar Benedikt Kleinmeier
Browse files

Implemented unit test "TargetChangerControllerTest"

parent 0a83a448
Pipeline #164444 passed with stages
in 127 minutes and 55 seconds
......@@ -54,6 +54,11 @@ public class TargetChangerController {
binomialDistribution = new BinomialDistribution(randomGenerator, trials, probabilityToChangeTarget);
}
// Getters
public Map<Integer, Agent> getProcessedAgents() {
return processedAgents;
}
// Public Methods
public void update(double simTimeInSec) {
for (DynamicElement element : getDynamicElementsNearTargetChangerArea()) {
......
package org.vadere.simulator.control;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.attributes.scenario.AttributesTarget;
import org.vadere.state.attributes.scenario.AttributesTargetChanger;
import org.vadere.state.scenario.*;
import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.geometry.shapes.VRectangle;
import java.util.*;
import java.util.stream.Collectors;
import static org.junit.Assert.*;
public class TargetChangerControllerTest {
/**
* The topography for this test contains
* - 2 pedestrians (P1 and P2)
* - 2 targets (T1 and T2)
* - 1 target changer (TC1)
* and looks like this:
*
* <pre>
* +----------------------+
* | +----+ +----+ |
* | | | | | |
* | | | | T2 | |
* | | | +----+ |
* | | | |
* | |TC1 | | 10 meters
* | | | |
* | | | |
* | | | +----+ |
* | | P2 | | | |
* | P1 | * | | T1 | |
* | * +----+ +----+ |
* +----------------------+
* 10 meters
* </pre>
*/
private Topography topography;
private List<Pedestrian> pedestrians;
private List<Target> targets;
double simTimeInSec = 0;
// The "TargetChanger" is added by each test individually
// to meet the requirements of the test.
@Before
public void setUp() throws Exception {
topography = new Topography();
pedestrians = createTwoPedestrianWithTargetT1();
targets = createTwoTargets();
simTimeInSec = 0;
for (Pedestrian pedestrian : pedestrians) {
topography.addElement(pedestrian);
}
for (Target target : targets) {
topography.addTarget(target);
}
}
private List<Pedestrian> createTwoPedestrianWithTargetT1() {
int seed = 0;
Random random = new Random(seed);
LinkedList<Integer> targetsPed1 = new LinkedList<>();
targetsPed1.add(1);
LinkedList<Integer> targetsPed2 = new LinkedList<>();
targetsPed2.add(1);
Pedestrian pedestrian1 = new Pedestrian(new AttributesAgent(1), random);
pedestrian1.setPosition(new VPoint(1, 1));
pedestrian1.setTargets(targetsPed1);
Pedestrian pedestrian2 = new Pedestrian(new AttributesAgent(2), random);
pedestrian2.setPosition(new VPoint(5, 2));
pedestrian2.setTargets(targetsPed2);
// Watch out: Use an "ArrayList" to keep order and
// index 0 refers to pedestrian p1!
List<Pedestrian> pedestrians = new ArrayList<>();
pedestrians.add(pedestrian1);
pedestrians.add(pedestrian2);
return pedestrians;
}
private List<Target> createTwoTargets() {
boolean absorbing = true;
AttributesTarget attributesTarget1 = new AttributesTarget(new VRectangle(7, 1, 2, 2), 1, absorbing);
AttributesTarget attributesTarget2 = new AttributesTarget(new VRectangle(7, 7, 2, 2), 2, absorbing);
Target target1 = new Target(attributesTarget1);
Target target2 = new Target(attributesTarget2);
List<Target> targets = new ArrayList<>();
targets.add(target1);
targets.add(target2);
return targets;
}
private AttributesTargetChanger createAttributesWithFixedRectangle() {
return new AttributesTargetChanger(new VRectangle(4, 1, 2, 8), 1);
}
private TargetChangerController createTargetChangerController(TargetChanger targetChanger) {
int seed = 0;
Random random = new Random(seed);
return new TargetChangerController(topography, targetChanger, random);
}
@After
public void tearDown() throws Exception {
}
@Test
public void getProcessedAgentsReturnsEmptyMapIfUpdateWasNotInvoked() {
AttributesTargetChanger attributesTargetChanger = createAttributesWithFixedRectangle();
TargetChanger targetChanger = new TargetChanger(attributesTargetChanger);
TargetChangerController controllerUnderTest = createTargetChangerController(targetChanger);
Map<Integer, Agent> processedAgents = controllerUnderTest.getProcessedAgents();
assertTrue(processedAgents.isEmpty());
}
@Test
public void updateProcessesOnlyAgentsWithinTargetChanger() {
AttributesTargetChanger attributesTargetChanger = createAttributesWithFixedRectangle();
TargetChanger targetChanger = new TargetChanger(attributesTargetChanger);
TargetChangerController controllerUnderTest = createTargetChangerController(targetChanger);
controllerUnderTest.update(simTimeInSec);
Map<Integer, Agent> processedAgents = controllerUnderTest.getProcessedAgents();
Agent processedAgent = processedAgents.get(pedestrians.get(1).getId());
assertEquals(1, processedAgents.size());
assertEquals(pedestrians.get(1).getId(), processedAgent.getId());
}
@Test
public void updateChangesTargetListOfAffectedPedestrianIfProbabilityIsOne() {
int nextTarget = 2;
double probability = 1.0;
AttributesTargetChanger attributesTargetChanger = createAttributesWithFixedRectangle();
attributesTargetChanger.setNextTarget(nextTarget);
attributesTargetChanger.setProbabilityToChangeTarget(probability);
TargetChanger targetChanger = new TargetChanger(attributesTargetChanger);
TargetChangerController controllerUnderTest = createTargetChangerController(targetChanger);
int expectedTargetId = 1;
assertListContainsSingleTarget(pedestrians.get(0).getTargets(), expectedTargetId);
assertListContainsSingleTarget(pedestrians.get(1).getTargets(), expectedTargetId);
controllerUnderTest.update(simTimeInSec);
assertListContainsSingleTarget(pedestrians.get(0).getTargets(), expectedTargetId);
assertListContainsSingleTarget(pedestrians.get(1).getTargets(), nextTarget);
}
@Test
public void updateDoesNotChangeTargetListOfAffectedPedestrianIfProbabilityIsZero() {
int nextTarget = 2;
double probability = 0.0;
AttributesTargetChanger attributesTargetChanger = createAttributesWithFixedRectangle();
attributesTargetChanger.setNextTarget(nextTarget);
attributesTargetChanger.setProbabilityToChangeTarget(probability);
TargetChanger targetChanger = new TargetChanger(attributesTargetChanger);
TargetChangerController controllerUnderTest = createTargetChangerController(targetChanger);
int expectedTargetId = 1;
assertListContainsSingleTarget(pedestrians.get(0).getTargets(), expectedTargetId);
assertListContainsSingleTarget(pedestrians.get(1).getTargets(), expectedTargetId);
controllerUnderTest.update(simTimeInSec);
assertListContainsSingleTarget(pedestrians.get(0).getTargets(), expectedTargetId);
assertListContainsSingleTarget(pedestrians.get(1).getTargets(), expectedTargetId);
}
@Test
public void updateAddsTargetPedestrianToTopographyIfTargetIsDynamic() {
int nextTarget = 1;
double probability = 1.0;
AttributesTargetChanger attributesTargetChanger = createAttributesWithFixedRectangle();
attributesTargetChanger.setNextTarget(nextTarget);
attributesTargetChanger.setNextTargetIsPedestrian(true);
attributesTargetChanger.setProbabilityToChangeTarget(probability);
TargetChanger targetChanger = new TargetChanger(attributesTargetChanger);
TargetChangerController controllerUnderTest = createTargetChangerController(targetChanger);
List<Target> targetPedestrians = topography.getTargets().stream().filter(target -> target instanceof TargetPedestrian).collect(Collectors.toList());
assertEquals(0, targetPedestrians.size());
controllerUnderTest.update(simTimeInSec);
targetPedestrians = topography.getTargets().stream().filter(target -> target instanceof TargetPedestrian).collect(Collectors.toList());
assertEquals(1, targetPedestrians.size());
}
@Test
public void updateChangesTargetListOfAffectedPedestrianIfTargetIsDynamic() {
int nextTarget = 1;
double probability = 1.0;
AttributesTargetChanger attributesTargetChanger = createAttributesWithFixedRectangle();
attributesTargetChanger.setNextTarget(nextTarget);
attributesTargetChanger.setNextTargetIsPedestrian(true);
attributesTargetChanger.setProbabilityToChangeTarget(probability);
TargetChanger targetChanger = new TargetChanger(attributesTargetChanger);
TargetChangerController controllerUnderTest = createTargetChangerController(targetChanger);
int expectedTargetId = 1;
assertListContainsSingleTarget(pedestrians.get(0).getTargets(), expectedTargetId);
assertListContainsSingleTarget(pedestrians.get(1).getTargets(), expectedTargetId);
controllerUnderTest.update(simTimeInSec);
int expectedTargetIdForPed2 = pedestrians.get(0).getId() + TargetPedestrian.UNIQUE_ID_OFFSET;
assertListContainsSingleTarget(pedestrians.get(0).getTargets(), expectedTargetId);
assertListContainsSingleTarget(pedestrians.get(1).getTargets(), expectedTargetIdForPed2);
}
@Test
public void updateModifiesFollowersIfTargetIsDynamic() {
int nextTarget = 1;
double probability = 1.0;
AttributesTargetChanger attributesTargetChanger = createAttributesWithFixedRectangle();
attributesTargetChanger.setNextTarget(nextTarget);
attributesTargetChanger.setNextTargetIsPedestrian(true);
attributesTargetChanger.setProbabilityToChangeTarget(probability);
TargetChanger targetChanger = new TargetChanger(attributesTargetChanger);
TargetChangerController controllerUnderTest = createTargetChangerController(targetChanger);
assertTrue(pedestrians.get(0).getFollowers().isEmpty());
controllerUnderTest.update(simTimeInSec);
LinkedList<Agent> followers = pedestrians.get(0).getFollowers();
assertEquals(1, followers.size());
assertEquals(pedestrians.get(1).getId(), followers.get(0).getId());
}
@Test
public void updateUseStaticTargetAsFallbackIfNoPedestrianIsFoundIfTargetIsDynamic() {
int nextTarget = 3;
double probability = 1.0;
AttributesTargetChanger attributesTargetChanger = createAttributesWithFixedRectangle();
attributesTargetChanger.setNextTarget(nextTarget);
attributesTargetChanger.setNextTargetIsPedestrian(true);
attributesTargetChanger.setProbabilityToChangeTarget(probability);
TargetChanger targetChanger = new TargetChanger(attributesTargetChanger);
TargetChangerController controllerUnderTest = createTargetChangerController(targetChanger);
int expectedTargetId = 1;
assertListContainsSingleTarget(pedestrians.get(0).getTargets(), expectedTargetId);
assertListContainsSingleTarget(pedestrians.get(1).getTargets(), expectedTargetId);
controllerUnderTest.update(simTimeInSec);
assertListContainsSingleTarget(pedestrians.get(0).getTargets(), 1);
assertListContainsSingleTarget(pedestrians.get(1).getTargets(), nextTarget);
List<Target> targetPedestrians = topography.getTargets().stream().filter(target -> target instanceof TargetPedestrian).collect(Collectors.toList());
assertEquals(0, targetPedestrians.size());
}
private void assertListContainsSingleTarget(LinkedList<Integer> targetList, int targetId) {
assertTrue(targetList.size() == 1);
assertTrue(targetList.getFirst() == targetId);
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment