Package test.main

Source Code of test.main.PlayingCardTest

package test.main;
import net.sf.nebulacards.main.PlayingCard;
import net.sf.nebulacards.main.NullPlayingCard;
import junit.framework.*;

public class PlayingCardTest
  extends TestCase
{
  private PlayingCard aceSpades, twoClubs, jackDiamonds, tenHearts;
  private PlayingCard[] cards;
  private String[] stringReps;

  public PlayingCardTest(String s) { super(s); }

  protected void setUp()
  {
    aceSpades = new PlayingCard( 1, PlayingCard.SPADES );
    twoClubs = new PlayingCard( 2, PlayingCard.CLUBS );
    jackDiamonds = new PlayingCard( 11, PlayingCard.DIAMONDS );
    tenHearts = new PlayingCard( 10, PlayingCard.HEARTS );
    PlayingCard[] _cards =
    {
      aceSpades, twoClubs, jackDiamonds, tenHearts,
      new PlayingCard(12,PlayingCard.HEARTS),
      new PlayingCard(13,PlayingCard.HEARTS)
    };
    cards = _cards;
    String[] _stringReps = { "As", "2c", "Jd", "10h", "Qh", "Kh" };
    stringReps = _stringReps;
  }
 
  public void testGetVal()
  {
    for (int i = 0; i < cards.length; i++)
      assertTrue(cards[i].getValue() == cards[i].getVal());
  }

  public void testGetValue()
  {
    assertTrue(aceSpades.getValue() == 1);
    assertTrue(twoClubs.getValue() == 2);
    assertTrue(jackDiamonds.getValue() == 11);
    assertTrue(tenHearts.getValue() == 10);
  }
 
  public void testGetSuit()
  {
    assertTrue(aceSpades.getSuit() == PlayingCard.SPADES);
    assertTrue(twoClubs.getSuit() == PlayingCard.CLUBS);
    assertTrue(jackDiamonds.getSuit() == PlayingCard.DIAMONDS);
    assertTrue(tenHearts.getSuit() == PlayingCard.HEARTS);
  }

  public void testHashCode()
  {
    // test uniqueness of hash codes
    for (int i = 0; i < cards.length; i++)
    {
      final int h = cards[i].hashCode();
      for (int j = i + 1; j < cards.length; j++)
        assertTrue(h != cards[j].hashCode());
    }
  }
               
  public void testIsNull()
  {
    for (int i = 0; i < cards.length; i++)
      assertFalse(cards[i].isNull());
  }

  public void testEquals()
  {
    assertTrue( !aceSpades.equals(twoClubs) );
    assertTrue( !twoClubs.equals(aceSpades) );
    assertTrue( !twoClubs.equals(new PlayingCard(2,PlayingCard.HEARTS)) );
    assertTrue( !twoClubs.equals(new PlayingCard(5,PlayingCard.CLUBS)) );
    assertEquals( twoClubs, (new PlayingCard(2,PlayingCard.CLUBS)) );
    assertTrue( !twoClubs.equals(NullPlayingCard.getInstance()) );
  }

  public void testToString()
  {
    for (int i = 0; i < cards.length; i++)
      assertEquals( cards[i].toString(), stringReps[i] );
  }

  public void testStringConstructor()
  {
    for (int i = 0; i < cards.length; i++)
      assertEquals( cards[i], new PlayingCard(stringReps[i]) );
  }

  public void testFromString()
  {
    for (int i = 0; i < cards.length; i++)
      assertEquals(cards[i], PlayingCard.fromString(stringReps[i]));
  }
}
TOP

Related Classes of test.main.PlayingCardTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.