Add oving3

This commit is contained in:
Andreas Omholt Olsen
2026-01-22 22:01:08 +01:00
parent 0586d24f49
commit 26163d3029
12 changed files with 684 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
package oving3.card;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class CardDeckTest {
private static void checkDeck(CardDeck deck, List<String> expected) {
assertEquals(expected.size(), deck.getCardCount(), "Wrong number of cards in deck");
for (int i = 0; i < expected.size(); i++) {
String expectedCard = expected.get(i);
Card actual = deck.getCard(i);
String cardString = String.valueOf(actual.getSuit()) + actual.getFace();
assertEquals(expectedCard, cardString);
}
}
@Test
@DisplayName("Constructor")
public void testConstructor() {
assertThrows(IllegalArgumentException.class, () -> new CardDeck(-1));
assertThrows(IllegalArgumentException.class, () -> new CardDeck(14));
CardDeckTest.checkDeck(new CardDeck(0), Collections.emptyList());
CardDeckTest.checkDeck(new CardDeck(2),
List.of("S1", "S2", "H1", "H2", "D1", "D2", "C1", "C2"));
}
@Test
@DisplayName("#shufflePerfectly()")
public void testShufflePerfectly() {
CardDeck cardDeck = new CardDeck(2);
cardDeck.shufflePerfectly();
CardDeckTest.checkDeck(cardDeck, List.of("S1", "D1", "S2", "D2", "H1", "C1", "H2", "C2"));
}
}

View File

@@ -0,0 +1,46 @@
package oving3.card;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class CardTest {
private static boolean checkState(Card card, char suit, int face) {
return card.getSuit() == suit && card.getFace() == face;
}
@Test
@DisplayName("Constructor")
public void testConstructor() {
assertTrue(CardTest.checkState(new Card('S', 1), 'S', 1));
assertTrue(CardTest.checkState(new Card('S', 13), 'S', 13));
assertTrue(CardTest.checkState(new Card('H', 1), 'H', 1));
assertTrue(CardTest.checkState(new Card('H', 13), 'H', 13));
assertTrue(CardTest.checkState(new Card('D', 1), 'D', 1));
assertTrue(CardTest.checkState(new Card('D', 13), 'D', 13));
assertTrue(CardTest.checkState(new Card('C', 1), 'C', 1));
assertTrue(CardTest.checkState(new Card('C', 13), 'C', 13));
assertThrows(IllegalArgumentException.class, () -> {
new Card('X', 1);
});
assertThrows(IllegalArgumentException.class, () -> {
new Card('S', 0);
});
assertThrows(IllegalArgumentException.class, () -> {
new Card('C', 14);
});
}
@Test
@DisplayName("#toString()")
public void testToString() {
assertEquals("S1", new Card('S', 1).toString());
assertEquals("H13", new Card('H', 13).toString());
}
}