Add oving 5
This commit is contained in:
78
src/test/java/oving5/card/CardComparatorTest.java
Normal file
78
src/test/java/oving5/card/CardComparatorTest.java
Normal file
@@ -0,0 +1,78 @@
|
||||
package oving5.card;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class CardComparatorTest {
|
||||
|
||||
private Card s1;
|
||||
private Card h1;
|
||||
private Card d1;
|
||||
private Card c1;
|
||||
private Card s13;
|
||||
private Card h13;
|
||||
private Card d13;
|
||||
private Card c13;
|
||||
private Collection<Card> expected;
|
||||
private List<Card> cards;
|
||||
|
||||
private static void testCards(Collection<Card> actualCards, Collection<Card> expectedCards) {
|
||||
Iterator<Card> actual = actualCards.iterator();
|
||||
Iterator<Card> expected = expectedCards.iterator();
|
||||
|
||||
while (expected.hasNext()) {
|
||||
assertTrue(actual.hasNext());
|
||||
|
||||
Card actualCard = actual.next();
|
||||
Card expectedCard = expected.next();
|
||||
assertEquals(expectedCard.getSuit(), actualCard.getSuit(), String.format(
|
||||
"The card deck should have been %s, but was %s", expectedCards, actualCards));
|
||||
assertEquals(expectedCard.getFace(), actualCard.getFace(), String.format(
|
||||
"The card deck should have been %s, but was %s", expectedCards, actualCards));
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
s1 = new Card('S', 1);
|
||||
h1 = new Card('H', 1);
|
||||
d1 = new Card('D', 1);
|
||||
c1 = new Card('C', 1);
|
||||
s13 = new Card('S', 13);
|
||||
h13 = new Card('H', 13);
|
||||
d13 = new Card('D', 13);
|
||||
c13 = new Card('C', 13);
|
||||
cards = new ArrayList<>(List.of(s1, s13, h1, h13, d1, d13, c1, c13));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that the deck is sorted with aces as the lowest")
|
||||
public void testNormal() {
|
||||
expected = List.of(c1, c13, d1, d13, h1, h13, s1, s13);
|
||||
cards.sort(new CardComparator(false, ' '));
|
||||
CardComparatorTest.testCards(cards, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that the deck is sorted with aces as the highest")
|
||||
public void testAceIsHighest() {
|
||||
expected = List.of(c13, c1, d13, d1, h13, h1, s13, s1);
|
||||
cards.sort(new CardComparator(true, ' '));
|
||||
CardComparatorTest.testCards(cards, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that the deck is sorted correctly with diamonds as trump")
|
||||
public void testDiamondIsTrump() {
|
||||
expected = List.of(c1, c13, h1, h13, s1, s13, d1, d13);
|
||||
cards.sort(new CardComparator(false, 'D'));
|
||||
CardComparatorTest.testCards(cards, expected);
|
||||
}
|
||||
}
|
||||
69
src/test/java/oving5/card/CardContainerIteratorTest.java
Normal file
69
src/test/java/oving5/card/CardContainerIteratorTest.java
Normal file
@@ -0,0 +1,69 @@
|
||||
package oving5.card;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class CardContainerIteratorTest {
|
||||
|
||||
private Card c1;
|
||||
private Card c2;
|
||||
private Card d1;
|
||||
private Card d2;
|
||||
private Card h1;
|
||||
private Card h2;
|
||||
private Card s1;
|
||||
private Card s2;
|
||||
private CardContainerIterator iterator;
|
||||
|
||||
private static void testCards(Iterator<Card> actual, Iterator<Card> expected) {
|
||||
while (expected.hasNext()) {
|
||||
assertTrue(actual.hasNext());
|
||||
|
||||
Card actualCard = actual.next();
|
||||
Card expectedCard = expected.next();
|
||||
assertEquals(expectedCard.getSuit(), actualCard.getSuit(), String
|
||||
.format("The card should have been %s, but was %s", expectedCard, actualCard));
|
||||
assertEquals(expectedCard.getFace(), actualCard.getFace(), String
|
||||
.format("The card should have been %s, but was %s", expectedCard, actualCard));
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
iterator = new CardContainerIterator(new CardDeck(2));
|
||||
s1 = new Card('S', 1);
|
||||
s2 = new Card('S', 2);
|
||||
h1 = new Card('H', 1);
|
||||
h2 = new Card('H', 2);
|
||||
d1 = new Card('D', 1);
|
||||
d2 = new Card('D', 2);
|
||||
c1 = new Card('C', 1);
|
||||
c2 = new Card('C', 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that the iterator for a new deck of cards outputs [S1, S2, H1, H2, D1, D2, C1, C2]")
|
||||
public void testConstructor() {
|
||||
CardContainerIteratorTest.testCards(iterator,
|
||||
List.of(s1, s2, h1, h2, d1, d2, c1, c2).iterator());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that the first card in the iterator is correct")
|
||||
public void testNext() {
|
||||
Card nextCard = iterator.next();
|
||||
assertEquals(s1.toString(), nextCard.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that the deck contains at least one card")
|
||||
public void testHasNext() {
|
||||
boolean hasNext = iterator.hasNext();
|
||||
assertTrue(hasNext);
|
||||
}
|
||||
}
|
||||
86
src/test/java/oving5/card/CardDeckTest.java
Normal file
86
src/test/java/oving5/card/CardDeckTest.java
Normal file
@@ -0,0 +1,86 @@
|
||||
package oving5.card;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class CardDeckTest {
|
||||
|
||||
private Card s1;
|
||||
private Card h1;
|
||||
private Card d1;
|
||||
private Card c1;
|
||||
private Card s2;
|
||||
private Card h2;
|
||||
private Card d2;
|
||||
private Card c2;
|
||||
private CardDeck deck;
|
||||
private Collection<Card> expected;
|
||||
|
||||
private static void testCards(Iterable<Card> actual, Iterator<Card> expected) {
|
||||
Iterator<Card> actualIt = actual.iterator();
|
||||
|
||||
while (expected.hasNext()) {
|
||||
assertTrue(actualIt.hasNext());
|
||||
|
||||
Card expectedCard = expected.next();
|
||||
Card actualCard = actualIt.next();
|
||||
assertEquals(expectedCard.getSuit(), actualCard.getSuit(), String
|
||||
.format("The card should have been %s, but was %s", expectedCard, actualCard));
|
||||
assertEquals(expectedCard.getFace(), actualCard.getFace(), String
|
||||
.format("The card should have been %s, but was %s", expectedCard, actualCard));
|
||||
}
|
||||
}
|
||||
|
||||
private static void testCards(CardContainer it, Collection<Card> expected) {
|
||||
assertEquals(expected.size(), it.getCardCount());
|
||||
|
||||
Iterator<Card> expectedIt = expected.iterator();
|
||||
int i = 0;
|
||||
|
||||
while (expectedIt.hasNext()) {
|
||||
Card expectedCard = expectedIt.next();
|
||||
Card actualCard = it.getCard(i);
|
||||
assertEquals(expectedCard.getSuit(), actualCard.getSuit(),
|
||||
String.format("Card number %d should have been %s, but was %s", i + 1,
|
||||
expectedCard, actualCard));
|
||||
assertEquals(expectedCard.getFace(), actualCard.getFace(),
|
||||
String.format("Card number %d should have been %s, but was %s", i + 1,
|
||||
expectedCard, actualCard));
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
deck = new CardDeck(2);
|
||||
s1 = new Card('S', 1);
|
||||
s2 = new Card('S', 2);
|
||||
h1 = new Card('H', 1);
|
||||
h2 = new Card('H', 2);
|
||||
d1 = new Card('D', 1);
|
||||
d2 = new Card('D', 2);
|
||||
c1 = new Card('C', 1);
|
||||
c2 = new Card('C', 2);
|
||||
expected = new ArrayList<>(List.of(s1, s2, h1, h2, d1, d2, c1, c2));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Checks that CardContainer works with CardDeck")
|
||||
public void testCardContainer() {
|
||||
CardDeckTest.testCards(deck, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Checks that the iterator works with CardDeck")
|
||||
public void testDeckIterator() {
|
||||
CardDeckTest.testCards(deck, expected.iterator());
|
||||
}
|
||||
}
|
||||
78
src/test/java/oving5/card/CardHandTest.java
Normal file
78
src/test/java/oving5/card/CardHandTest.java
Normal file
@@ -0,0 +1,78 @@
|
||||
package oving5.card;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class CardHandTest {
|
||||
|
||||
private Card s1;
|
||||
private Card c2;
|
||||
private CardHand hand;
|
||||
private Collection<Card> expected;
|
||||
|
||||
private static void testCards(CardContainer it, Collection<Card> expected) {
|
||||
assertEquals(expected.size(), it.getCardCount());
|
||||
|
||||
Iterator<Card> expectedIt = expected.iterator();
|
||||
int i = 0;
|
||||
|
||||
while (expectedIt.hasNext()) {
|
||||
Card expectedCard = expectedIt.next();
|
||||
Card actualCard = it.getCard(i);
|
||||
assertEquals(expectedCard.getSuit(), actualCard.getSuit(),
|
||||
String.format("Card number %d should have been %s, but was %s", i + 1,
|
||||
expectedCard, actualCard));
|
||||
assertEquals(expectedCard.getFace(), actualCard.getFace(),
|
||||
String.format("Card number %d should have been %s, but was %s", i + 1,
|
||||
expectedCard, actualCard));
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
private static void testCards(Iterable<Card> actual, Iterator<Card> expected) {
|
||||
Iterator<Card> actualIt = actual.iterator();
|
||||
|
||||
while (expected.hasNext()) {
|
||||
assertTrue(actualIt.hasNext());
|
||||
|
||||
Card expectedCard = expected.next();
|
||||
Card actualCard = actualIt.next();
|
||||
assertEquals(expectedCard.getSuit(), actualCard.getSuit(), String
|
||||
.format("The card should have been %s, but was %s", expectedCard, actualCard));
|
||||
assertEquals(expectedCard.getFace(), actualCard.getFace(), String
|
||||
.format("The card should have been %s, but was %s", expectedCard, actualCard));
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
s1 = new Card('S', 1);
|
||||
c2 = new Card('C', 2);
|
||||
hand = new CardHand();
|
||||
expected = new ArrayList<>(List.of(s1, c2));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Checks that CardContainer works with CardHand")
|
||||
public void testCardContainer() {
|
||||
hand.addCard(s1);
|
||||
hand.addCard(c2);
|
||||
CardHandTest.testCards(hand, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Checks that the iterator works with CardHand")
|
||||
public void testDeckIterator() {
|
||||
hand.addCard(s1);
|
||||
hand.addCard(c2);
|
||||
CardHandTest.testCards(hand, expected.iterator());
|
||||
}
|
||||
}
|
||||
63
src/test/java/oving5/card/CardPredicateTest.java
Normal file
63
src/test/java/oving5/card/CardPredicateTest.java
Normal file
@@ -0,0 +1,63 @@
|
||||
package oving5.card;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class CardPredicateTest {
|
||||
|
||||
private CardDeck deck;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
deck = new CardDeck(10);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that hasCard() works as expected")
|
||||
public void testHasCard() {
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
deck.hasCard(null);
|
||||
}, "Predicate cannot be null");
|
||||
|
||||
assertTrue(deck.hasCard(c -> c.getSuit() == 'S'));
|
||||
assertFalse(deck.hasCard(c -> c.getFace() == 13));
|
||||
assertTrue(deck.hasCard(c -> c.getSuit() == 'S' && c.getFace() == 8));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that getCardCount() works as expected")
|
||||
public void testGetCardCount() {
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
deck.getCardCount(null);
|
||||
}, "Predicate cannot be null");
|
||||
|
||||
assertEquals(10, deck.getCardCount(c -> c.getSuit() == 'S'));
|
||||
assertEquals(4, deck.getCardCount(c -> c.getFace() == 4));
|
||||
assertEquals(1, deck.getCardCount(c -> c.getFace() == 4 && c.getSuit() == 'H'));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check that getCards() works as expected")
|
||||
public void testGetCards() {
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
deck.getCards(null);
|
||||
}, "Predicate cannot be null");
|
||||
|
||||
Card card = new Card('S', 4);
|
||||
Card card2 = new Card('S', 5);
|
||||
List<Card> matching = List.of(card, card2);
|
||||
assertEquals(matching.size(),
|
||||
deck.getCards(c -> (c.getFace() == 4 || c.getFace() == 5) && c.getSuit() == 'S')
|
||||
.size(),
|
||||
"getCards should have returned two cards that were spades and had the numbers 4 "
|
||||
+ "or 5");
|
||||
assertEquals(10, deck.getCards(c -> c.getSuit() == 'S').size(),
|
||||
"getCards should have returned 10 cards of the spades suit");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user