Rydder i tester og kode

Fjerner en del unødvendige variabler i ProgrammingCardDeckTest
Forenkler shuffle testen vesentlig
Legger til en ekstra sjekk for integritet etter stokking av kort
Fjerner ICardWithoutSuit siden den legger til mer kompleksitet enn nytteverdi
This commit is contained in:
Kristian Knarvik 2020-03-03 20:51:50 +01:00
parent e4d6e49721
commit c207c56bca
4 changed files with 56 additions and 67 deletions

View File

@ -1,20 +0,0 @@
package inf112.fiasko.roborally.objects;
/**
* This Interface describes a card without a card suit
* @param <S> The value type
* @param <T> The symbol type
*/
public interface ICardWithoutSuit<S,T> {
/**
* Gets the value of the card
* @return The card value
*/
S getValue();
/**
* Gets the symbol of the card
* @return The card symbol
*/
T getSymbol();
}

View File

@ -5,9 +5,9 @@ import inf112.fiasko.roborally.element_properties.Action;
/** /**
* This class represents a programming card * This class represents a programming card
*/ */
public class ProgrammingCard implements ICardWithoutSuit<Integer, Action> { public class ProgrammingCard {
private final Integer cardValue; private final int cardValue;
private final Action cardAction; private final Action cardAction;
/** /**
@ -20,12 +20,18 @@ public class ProgrammingCard implements ICardWithoutSuit<Integer, Action> {
this.cardAction = cardAction; this.cardAction = cardAction;
} }
@Override /**
public Integer getValue() { * Gets the value of the programming card
* @return The programming card value
*/
public int getValue() {
return cardValue; return cardValue;
} }
@Override /**
* Gets the symbol of the programming card
* @return The programming card symbol
*/
public Action getSymbol() { public Action getSymbol() {
return cardAction; return cardAction;
} }

View File

@ -7,22 +7,19 @@ import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
public class ProgrammingCardDeckTest { public class ProgrammingCardDeckTest {
private ProgrammingCardDeck fullDeck; private ProgrammingCard programmingCard1;
private final ProgrammingCard programmingCard1 = new ProgrammingCard(5, Action.MOVE_1); private ProgrammingCard programmingCard3;
private final ProgrammingCard programmingCard2 = new ProgrammingCard(6, Action.MOVE_2); private IDeck<ProgrammingCard> testDeck;
private final ProgrammingCard programmingCard3 = new ProgrammingCard(7, Action.MOVE_3); private IDeck<ProgrammingCard> testDeck2;
private final ProgrammingCard programmingCard4 = new ProgrammingCard(55, Action.MOVE_1); private IDeck<ProgrammingCard> fullDeck;
private final ProgrammingCard programmingCard5 = new ProgrammingCard(66, Action.MOVE_2);
private final ProgrammingCard programmingCard6 = new ProgrammingCard(756, Action.MOVE_3);
private final ArrayList<ProgrammingCard> cardlist = new ArrayList<>();
private final ArrayList<ProgrammingCard> cardlist2 = new ArrayList<>();
private ProgrammingCardDeck testDeck;
private ProgrammingCardDeck testDeck2;
@Before @Before
public void setUp() { public void setUp() {
try { try {
@ -30,21 +27,24 @@ public class ProgrammingCardDeckTest {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
cardlist.add(programmingCard1); ArrayList<ProgrammingCard> cardList = new ArrayList<>();
cardlist.add(programmingCard2); programmingCard1 = new ProgrammingCard(5, Action.MOVE_1);
cardlist.add(programmingCard3); ProgrammingCard programmingCard2 = new ProgrammingCard(6, Action.MOVE_2);
cardlist2.add(programmingCard4); programmingCard3 = new ProgrammingCard(7, Action.MOVE_3);
cardlist2.add(programmingCard5); cardList.add(programmingCard1);
cardlist2.add(programmingCard6); cardList.add(programmingCard2);
testDeck = new ProgrammingCardDeck(cardlist); cardList.add(programmingCard3);
testDeck2 = new ProgrammingCardDeck(cardlist2); testDeck = new ProgrammingCardDeck(cardList);
testDeck2 = new ProgrammingCardDeck(cardList);
} }
@Test @Test
public void testSize() { public void testSize() {
assertEquals(3,testDeck.size()); assertEquals(3,testDeck.size());
testDeck.emptyInto(testDeck2); testDeck.emptyInto(testDeck2);
assertEquals(0,testDeck.size()); assertEquals(0,testDeck.size());
} }
@Test @Test
public void testDrawCard() { public void testDrawCard() {
assertEquals(3,testDeck.size()); assertEquals(3,testDeck.size());
@ -53,6 +53,7 @@ public class ProgrammingCardDeckTest {
assertEquals(4,testDeck.size()); assertEquals(4,testDeck.size());
assertEquals(2,testDeck2.size()); assertEquals(2,testDeck2.size());
} }
@Test @Test
public void testDrawMultipleCards() { public void testDrawMultipleCards() {
assertEquals(3, testDeck.size()); assertEquals(3, testDeck.size());
@ -61,6 +62,7 @@ public class ProgrammingCardDeckTest {
assertEquals(6, testDeck.size()); assertEquals(6, testDeck.size());
assertEquals(0, testDeck2.size()); assertEquals(0, testDeck2.size());
} }
@Test @Test
public void testEmptyInto() { public void testEmptyInto() {
assertEquals(3, testDeck.size()); assertEquals(3, testDeck.size());
@ -69,6 +71,7 @@ public class ProgrammingCardDeckTest {
assertEquals(0, testDeck.size()); assertEquals(0, testDeck.size());
assertEquals(6, testDeck2.size()); assertEquals(6, testDeck2.size());
} }
@Test @Test
public void testIsEmpty() { public void testIsEmpty() {
assertFalse(testDeck.isEmpty()); assertFalse(testDeck.isEmpty());
@ -81,33 +84,33 @@ public class ProgrammingCardDeckTest {
testDeck2.emptyInto(testDeck); testDeck2.emptyInto(testDeck);
assertEquals(programmingCard1, testDeck.getCards().get(0)); assertEquals(programmingCard1, testDeck.getCards().get(0));
assertEquals(programmingCard3, testDeck.getCards().get(2)); assertEquals(programmingCard3, testDeck.getCards().get(2));
assertEquals(programmingCard6, testDeck.getCards().get(5)); assertEquals(programmingCard3, testDeck.getCards().get(5));
} }
@Test @Test
public void testShuffle() { public void testShuffle() {
Boolean atLeastOneShuffle = false; List<ProgrammingCard> beforeShuffle = testDeck.getCards();
ArrayList<Boolean> resultList = new ArrayList<>(); for (int i = 0; i < 10; i++) {
ArrayList<ProgrammingCard> beforeShuffle = (ArrayList<ProgrammingCard>)testDeck.getCards();
for (int i = 0; i < 10; i++){ //Saves result of ten shuffles
testDeck.shuffle(); testDeck.shuffle();
ArrayList<ProgrammingCard> afterShuffle = (ArrayList<ProgrammingCard>)testDeck.getCards(); List<ProgrammingCard> afterShuffle = testDeck.getCards();
if (beforeShuffle != afterShuffle) { if (beforeShuffle != afterShuffle) {
resultList.add(true); return;
} }
else {
resultList.add(false);
}
} }
//Looks to see if at least one shuffle is different from before fail();
for (int i = 0; i < resultList.size(); i++) { }
if (resultList.get(i)==true) {
atLeastOneShuffle = true;
}
@Test
public void testShuffleIntegrity() {
//Checks that no cards disappear or are duplicated during shuffle
List<ProgrammingCard> cards = fullDeck.getCards();
fullDeck.shuffle();
assertEquals(cards.size(), fullDeck.size());
for (ProgrammingCard card : fullDeck.getCards()) {
assertTrue(cards.contains(card));
}
for (ProgrammingCard card : cards) {
assertTrue(fullDeck.getCards().contains(card));
} }
assertTrue(atLeastOneShuffle);
} }
} }

View File

@ -19,13 +19,13 @@ public class ProgrammingCardTest {
} }
@Test @Test
public void testGetProgrammingCardAction(){ public void testGetProgrammingCardAction(){
assertEquals(Action.MOVE_1,programmingCard1.getSymbol()); assertEquals(Action.MOVE_1, programmingCard1.getSymbol());
assertEquals(Action.ROTATE_LEFT,programmingCard2.getSymbol()); assertEquals(Action.ROTATE_LEFT, programmingCard2.getSymbol());
} }
@Test @Test
public void testGetProgrammingCardValue(){ public void testGetProgrammingCardValue(){
assertEquals((Integer) 5,programmingCard1.getValue()); assertEquals(5, programmingCard1.getValue());
assertEquals((Integer) 234,programmingCard2.getValue()); assertEquals(234, programmingCard2.getValue());
assertEquals((Integer) 2334,programmingCard3.getValue()); assertEquals(2334, programmingCard3.getValue());
} }
} }