-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cbf3bf4
commit a6d2922
Showing
5 changed files
with
240 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import Agent.BoSAgent.*; | ||
import Audit.Audit; | ||
import Mailer.Mailer; | ||
import Mailer.Messages.*; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class TestBoSAgent { | ||
Mailer mailer; | ||
Audit audit; | ||
List<Integer> neighbors; | ||
int agentId; | ||
int neighborId; | ||
@BeforeEach | ||
public void setup() { | ||
mailer = new Mailer(); | ||
audit = new Audit(); | ||
neighbors = List.of(1); | ||
agentId = 0; | ||
neighborId = neighbors.get(0); | ||
} | ||
|
||
@Test | ||
public void testAgentStuckWithoutPlayMessage() throws InterruptedException { | ||
BoSAgent agent = createAgentWithArguments(BoSAgentSex.HUSBAND); | ||
|
||
Thread t = new Thread(agent); | ||
|
||
t.start(); | ||
|
||
t.join(300); | ||
|
||
assertNull(mailer.readOne(neighborId)); | ||
} | ||
|
||
@Test | ||
public void testAgentRunsAndSendsMessageToNeighbor() throws InterruptedException { | ||
BoSAgent agent = createAgentWithArguments(BoSAgentSex.WIFE); | ||
|
||
Thread t = new Thread(agent); | ||
|
||
mailer.send(agentId, new PlayMessage(agentId)); | ||
|
||
t.start(); | ||
t.join(); | ||
|
||
MailerMessage message1 = mailer.readOne(neighborId); | ||
assertNotNull(message1); | ||
assertTrue(message1 instanceof BoSMessage); | ||
assertEquals(((BoSMessage) message1).getAgentSex(), BoSAgentSex.WIFE); | ||
assertEquals(message1.getSenderId(), agentId); | ||
|
||
MailerMessage message2 = mailer.readOne(neighborId); | ||
assertNotNull(message2); | ||
assertTrue(message2 instanceof PlayMessage); | ||
assertEquals(message1.getSenderId(), agentId); | ||
} | ||
|
||
@Test | ||
public void testAgentDoesNotSentMessageTwice() throws InterruptedException { | ||
BoSAgent agent = createAgentWithArguments(BoSAgentSex.HUSBAND); | ||
|
||
Thread t1 = new Thread(agent); | ||
|
||
mailer.send(agentId, new PlayMessage(agentId)); | ||
|
||
t1.start(); | ||
t1.join(); | ||
|
||
MailerMessage message1 = mailer.readOne(neighborId); | ||
assertNotNull(message1); | ||
|
||
|
||
Thread t2 = new Thread(agent); | ||
mailer.send(agentId, new PlayMessage(agentId)); | ||
|
||
t2.start(); | ||
t2.join(); | ||
|
||
MailerMessage message2 = mailer.readOne(neighborId); | ||
assertNotNull(message2); | ||
assertTrue(message2 instanceof PlayMessage); | ||
assertEquals(message2.getSenderId(), neighborId); | ||
} | ||
|
||
|
||
private BoSAgent createAgentWithArguments(BoSAgentSex sex){ | ||
BoSAgent agent = new BoSAgent(0, 2, mailer, audit, neighbors, sex); | ||
mailer.register(agentId); | ||
mailer.register(neighborId); | ||
|
||
return agent; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import Agent.PDAgent.*; | ||
import Audit.Audit; | ||
import Mailer.Mailer; | ||
import Mailer.Messages.*; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.util.List; | ||
|
||
public class TestPDAgent { | ||
Mailer mailer; | ||
Audit audit; | ||
List<Integer> neighbors; | ||
int agentId; | ||
int neighborId; | ||
@BeforeEach | ||
public void setup() { | ||
mailer = new Mailer(); | ||
audit = new Audit(); | ||
neighbors = List.of(1); | ||
agentId = 0; | ||
neighborId = neighbors.get(0); | ||
} | ||
|
||
@Test | ||
public void testAgentStuckWithoutPlayMessage() throws InterruptedException { | ||
PDAgent agent = createAgentWithArguments(); | ||
|
||
Thread t = new Thread(agent); | ||
|
||
t.start(); | ||
|
||
t.join(300); | ||
|
||
assertNull(mailer.readOne(neighborId)); | ||
} | ||
|
||
@Test | ||
public void testAgentRunsAndSendsMessageToNeighbor() throws InterruptedException { | ||
PDAgent agent = createAgentWithArguments(); | ||
|
||
Thread t = new Thread(agent); | ||
|
||
mailer.send(agentId, new PlayMessage(agentId)); | ||
|
||
t.start(); | ||
t.join(); | ||
|
||
MailerMessage message1 = mailer.readOne(neighborId); | ||
assertNotNull(message1); | ||
assertTrue(message1 instanceof PDMessage); | ||
assertEquals(message1.getSenderId(), agentId); | ||
|
||
MailerMessage message2 = mailer.readOne(neighborId); | ||
assertNotNull(message2); | ||
assertTrue(message2 instanceof PlayMessage); | ||
assertEquals(message1.getSenderId(), agentId); | ||
} | ||
|
||
@Test | ||
public void testAgentDoesNotSentMessageTwice() throws InterruptedException { | ||
PDAgent agent = createAgentWithArguments(); | ||
|
||
Thread t1 = new Thread(agent); | ||
|
||
mailer.send(agentId, new PlayMessage(agentId)); | ||
|
||
t1.start(); | ||
t1.join(); | ||
|
||
MailerMessage message1 = mailer.readOne(neighborId); | ||
assertNotNull(message1); | ||
|
||
|
||
Thread t2 = new Thread(agent); | ||
mailer.send(agentId, new PlayMessage(agentId)); | ||
|
||
t2.start(); | ||
t2.join(); | ||
|
||
MailerMessage message2 = mailer.readOne(neighborId); | ||
assertNotNull(message2); | ||
assertTrue(message2 instanceof PlayMessage); | ||
assertEquals(message2.getSenderId(), neighborId); | ||
} | ||
|
||
@Test | ||
public void testAgentPicksDefectWhenNeighborCooperates() throws InterruptedException{ | ||
testPickedStrategyByAgent(PDStrategy.COOPERATE, PDPayoff.I_DEFECT_HE_COOPERATE); | ||
} | ||
|
||
@Test | ||
public void testAgentPicksDefectWhenNeighborDefects() throws InterruptedException{ | ||
testPickedStrategyByAgent(PDStrategy.DEFECT, PDPayoff.BOTH_DEFECT); | ||
} | ||
|
||
|
||
private PDAgent createAgentWithArguments(){ | ||
PDAgent agent = new PDAgent(0, 2, mailer, audit, neighbors); | ||
mailer.register(agentId); | ||
mailer.register(neighborId); | ||
|
||
return agent; | ||
} | ||
|
||
private void testPickedStrategyByAgent(PDStrategy neighborStrategy, int expectedGain) throws InterruptedException { | ||
PDAgent agent = createAgentWithArguments(); | ||
|
||
// let agent pick his random strategy | ||
mailer.send(agentId, new PlayMessage(agentId)); | ||
Thread t1 = new Thread(agent); | ||
t1.start(); | ||
t1.join(); | ||
|
||
// send the agent that neighbor picked neighborStrategy | ||
mailer.send(agentId, new PDMessage(neighborId, neighborStrategy)); | ||
|
||
// restart agent pick his best strategy | ||
mailer.send(agentId, new PlayMessage(agentId)); | ||
Thread t2 = new Thread(agent); | ||
t2.start(); | ||
t2.join(); | ||
|
||
// strategy remained or change to DEFECT | ||
assertEquals(agent.getStrategy(), PDStrategy.DEFECT); | ||
assertEquals(agent.getPersonalGain(), expectedGain); | ||
} | ||
} |