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 PlayingCardTest { 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(new PlayingCard(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++) { PlayingCard 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++) { PlayingCard cj = cards.get(j); // are hash codes different for different cards? if (ci.hashCode() == cj.hashCode()) { collisions++; } } } assertTrue(collisions < 3); } @Test public void testConstructor() { // does the constructor create cards from valid ranks and suits? for (String r : RANKS) { for (String s : SUITS) { new PlayingCard(r, s); } } } @Test public void testConstructorFailure() { // does constructor throw with invalid rank? try { new PlayingCard("1", "D"); fail("Expected an IllegalArgumentException to be thrown!"); } catch (IllegalArgumentException x) { // this is expected! } // does constructor throw with invalid suit? try { new PlayingCard("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]; PlayingCard 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]; PlayingCard 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++) { PlayingCard ci = cards.get(i); // is a card always equal to itself? assertTrue(ci.equals(ci)); for (int j = i + 1; j < cards.size(); j++) { PlayingCard 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++) { PlayingCard 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++) { PlayingCard 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) { PlayingCard c = cards.get(i++); String expected = r + " of " + s; assertTrue(c.toString().equals(expected)); } } } @Test public void testGetAllRanks() { Set t = PlayingCard.getAllRanks(); List u = Arrays.asList(RANKS); assertTrue(t.containsAll(u)); assertTrue(u.containsAll(t)); } @Test public void testGetAllSuits() { Set t = PlayingCard.getAllSuits(); List u = Arrays.asList(SUITS); assertTrue(t.containsAll(u)); assertTrue(u.containsAll(t)); } }