Skip to content

Commit

Permalink
add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danny-prodbase committed Sep 4, 2023
1 parent cbf3bf4 commit a6d2922
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/Agent/PDAgent/PDAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* Represents an agent that plays the Prisoner's Dilemma game.
* Extends the Agent class and implements the PBPayoff interface.
*/
public class PDAgent extends Agent implements PBPayoff {
public class PDAgent extends Agent implements PDPayoff {
private final Map<Integer, PDStrategy> neighborsStrategies = new HashMap<>();
private PDStrategy strategy;

Expand Down Expand Up @@ -45,6 +45,10 @@ public int getPersonalGain() {
return totalGain;
}

public PDStrategy getStrategy(){
return strategy;
}

/**
* Handles incoming messages related to the Prisoner's Dilemma game.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* Defines the payoff values and strategies for the Prisoner's Dilemma (PD) game.
*/
public interface PBPayoff {
public interface PDPayoff {
// Payoff values for different combinations of strategies

int BOTH_COOPERATE = 8;
Expand Down
12 changes: 6 additions & 6 deletions tests/PBPayoffTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

public class PBPayoffTest {

private PBPayoff testClass;
private PDPayoff testClass;

@BeforeEach
public void setup() {
testClass = new PBPayoff() {}; // This is an anonymous class to create an instance of the interface
testClass = new PDPayoff() {}; // This is an anonymous class to create an instance of the interface
}

// Test cases for pickBestStrategy
Expand Down Expand Up @@ -47,22 +47,22 @@ public void testPickBestStrategyMixed() {
// Test cases for calculatePayoff
@Test
public void testCalculatePayoffBothCooperate() {
assertEquals(PBPayoff.BOTH_COOPERATE, testClass.calculatePayoff(PDStrategy.COOPERATE, PDStrategy.COOPERATE));
assertEquals(PDPayoff.BOTH_COOPERATE, testClass.calculatePayoff(PDStrategy.COOPERATE, PDStrategy.COOPERATE));
}

@Test
public void testCalculatePayoffICooperateHeDefect() {
assertEquals(PBPayoff.I_COOPERATE_HE_DEFECT, testClass.calculatePayoff(PDStrategy.COOPERATE, PDStrategy.DEFECT));
assertEquals(PDPayoff.I_COOPERATE_HE_DEFECT, testClass.calculatePayoff(PDStrategy.COOPERATE, PDStrategy.DEFECT));
}

@Test
public void testCalculatePayoffIDefectHeCooperates() {
assertEquals(PBPayoff.I_DEFECT_HE_COOPERATE, testClass.calculatePayoff(PDStrategy.DEFECT, PDStrategy.COOPERATE));
assertEquals(PDPayoff.I_DEFECT_HE_COOPERATE, testClass.calculatePayoff(PDStrategy.DEFECT, PDStrategy.COOPERATE));
}

@Test
public void testCalculatePayoffBothDefect() {
assertEquals(PBPayoff.BOTH_DEFECT, testClass.calculatePayoff(PDStrategy.DEFECT, PDStrategy.DEFECT));
assertEquals(PDPayoff.BOTH_DEFECT, testClass.calculatePayoff(PDStrategy.DEFECT, PDStrategy.DEFECT));
}

@Test
Expand Down
98 changes: 98 additions & 0 deletions tests/TestBoSAgent.java
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;
}
}
130 changes: 130 additions & 0 deletions tests/TestPDAgent.java
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);
}
}

0 comments on commit a6d2922

Please sign in to comment.