package cse1030; import static org.junit.Assert.*; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Set; import org.junit.BeforeClass; import org.junit.Test; public class PlayingCardMTest { private static final String[] RANKS = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" }; private static final String[] SUITS = { "S", "C", "D", "H" }; private static final List cards = new ArrayList(); @BeforeClass public static void init() { // generate a 52 card deck in sorted order for (String r : RANKS) { for (String s : SUITS) { cards.add(PlayingCardM.getCard(r, s)); } } } @Test public void testHashCode() { // count of the number of hash code collisions // oddly enough, 2 cards have hash codes that collide if // the eclipse generated hashCode method is used int collisions = 0; // compare each card to every other card for (int i = 0; i < cards.size(); i++) { PlayingCardM ci = cards.get(i); // does hashCode always return the same value for a card? assertTrue(ci.hashCode() == ci.hashCode()); for (int j = i + 1; j < cards.size(); j++) { PlayingCardM cj = cards.get(j); // are hash codes different for different cards? if (ci.hashCode() == cj.hashCode()) { collisions++; } } } assertTrue(collisions < 3); } @Test public void testGetCard() { // does the getCard create cards from valid ranks and suits? for (String r : RANKS) { for (String s : SUITS) { PlayingCardM.getCard(r, s); // do repeated calls to getCard return the same object? assertTrue(PlayingCardM.getCard(r, s) == PlayingCardM.getCard(r, s)); } } } @Test public void testGetCardFailure() { // does get card throw with invalid rank? try { PlayingCardM.getCard("1", "D"); fail("Expected an IllegalArgumentException to be thrown!"); } catch (IllegalArgumentException x) { // this is expected! } // does get card throw with invalid suit? try { PlayingCardM.getCard("2", "d"); fail("Expected an IllegalArgumentException to be thrown!"); } catch (IllegalArgumentException x) { // this is expected! } } @Test public void testGetRank() { for (int i = 0; i < cards.size(); i++) { int idx = i / SUITS.length; String rank = RANKS[idx]; PlayingCardM c = cards.get(i); assertTrue(c.getRank().equals(rank)); } } @Test public void testGetSuit() { for (int i = 0; i < cards.size(); i++) { int idx = i % SUITS.length; String suit = SUITS[idx]; PlayingCardM c = cards.get(i); assertTrue(c.getSuit().equals(suit)); } } @Test public void testEqualsObject() { // compare each card to every other card for (int i = 0; i < cards.size(); i++) { PlayingCardM ci = cards.get(i); // is a card always equal to itself? assertTrue(ci.equals(ci)); for (int j = i + 1; j < cards.size(); j++) { PlayingCardM cj = cards.get(j); // are different cards not equal? assertFalse(ci.equals(cj)); } } } @Test public void testCompareTo() { // compare each card to every other card for (int i = 0; i < cards.size(); i++) { PlayingCardM ci = cards.get(i); // is a card always equal to itself? assertTrue(ci.compareTo(ci) == 0); for (int j = i + 1; j < cards.size(); j++) { PlayingCardM cj = cards.get(j); // is ci less than cj? assertTrue(ci.compareTo(cj) < 0); // is cj greater than ci? assertTrue(cj.compareTo(ci) > 0); } } } @Test public void testToString() { int i = 0; for (String r : RANKS) { for (String s : SUITS) { PlayingCardM c = cards.get(i++); String expected = r + " of " + s; assertTrue(c.toString().equals(expected)); } } } @Test public void testGetAllRanks() { Set t = PlayingCardM.getAllRanks(); List u = Arrays.asList(RANKS); assertTrue(t.containsAll(u)); assertTrue(u.containsAll(t)); } @Test public void testGetAllSuits() { Set t = PlayingCardM.getAllSuits(); List u = Arrays.asList(SUITS); assertTrue(t.containsAll(u)); assertTrue(u.containsAll(t)); } }