我正在寫一個紙牌遊戲並擁有一個Card類和一個Playable類。可玩牌代表玩家可能選擇爲給定遊戲狀態玩的牌的集合。我正在測試一個獲得合法Playables集合的函數。我的方法是手動構造正確的答案,然後聲明它等於我的函數返回的集合。具有相同字段的自定義對象集不計算爲「相等」
這是我的設置功能。 cards
是List<Card>
類型的私人成員。
@Before
public void setup() {
cards = new ArrayList<Card>();
cards.add(new Card(Suit.HEARTS, CardValue.ACE));
cards.add(new Card(Suit.DIAMONDS, CardValue.ACE));
cards.add(new Card(Suit.CLUBS, CardValue.ACE));
cards.add(new Card(Suit.SPADES, CardValue.ACE));
cards.add(new Card(Suit.CLUBS, CardValue.EIGHT));
cards.add(new Card(Suit.HEARTS, CardValue.EIGHT));
cards.add(new Card(Suit.SPADES, CardValue.EIGHT));
cards.add(new Card(Suit.DIAMONDS, CardValue.SIX));
cards.add(new Card(Suit.SPADES, CardValue.SIX));
hand = new Hand(cards);
}
這裏是測試用例。
@Test
public void testGetLegalDoubles() throws Exception {
Card toBeat = new Card(Suit.CLUBS, CardValue.NINE);
Set<Playable> correct = new HashSet<Playable>();
Collection<Card> doubles1 = Arrays.asList(new Card[] {cards.get(0), cards.get(1)});
Collection<Card> doubles2 = Arrays.asList(new Card[] {cards.get(0), cards.get(2)});
Collection<Card> doubles3 = Arrays.asList(new Card[] {cards.get(0), cards.get(3)});
Collection<Card> doubles4 = Arrays.asList(new Card[] {cards.get(1), cards.get(2)});
Collection<Card> doubles5 = Arrays.asList(new Card[] {cards.get(1), cards.get(3)});
Collection<Card> doubles6 = Arrays.asList(new Card[] {cards.get(2), cards.get(3)});
Playable move1 = new Playable(doubles1, PlayMode.DOUBLES, cards.get(0));
Playable move2 = new Playable(doubles2, PlayMode.DOUBLES, cards.get(0));
Playable move3 = new Playable(doubles3, PlayMode.DOUBLES, cards.get(0));
Playable move4 = new Playable(doubles4, PlayMode.DOUBLES, cards.get(1));
Playable move5 = new Playable(doubles5, PlayMode.DOUBLES, cards.get(1));
Playable move6 = new Playable(doubles6, PlayMode.DOUBLES, cards.get(2));
correct.add(move1);
correct.add(move2);
correct.add(move3);
correct.add(move4);
correct.add(move5);
correct.add(move6);
Set<Playable> doubles = hand.getLegalNOfAKind(2, toBeat);
assertEquals(doubles, correct);
}
這個測試用例失敗,但我已經試過打印出的doubles
和correct
內容和它們匹配 - 我toString()
爲Playable
實施顯示了這些類的所有領域。我也覆蓋equals()
和hashCode()
爲Card
和Playable
如下所示(由Eclipse自動生成)。
Card
:
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((suit == null) ? 0 : suit.hashCode());
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Card other = (Card) obj;
if (suit != other.suit)
return false;
if (value != other.value)
return false;
return true;
}
Playable
:
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((cards == null) ? 0 : cards.hashCode());
result = prime * result + ((highCard == null) ? 0 : highCard.hashCode());
result = prime * result + ((playmode == null) ? 0 : playmode.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Playable other = (Playable) obj;
if (cards == null) {
if (other.cards != null)
return false;
} else if (!cards.equals(other.cards))
return false;
if (highCard == null) {
if (other.highCard != null)
return false;
} else if (!highCard.equals(other.highCard))
return false;
if (playmode != other.playmode)
return false;
return true;
}
我懷疑是東西了與我是如何構建correct
- 也許怎麼我引用cards
?
''hand.getLegalNOfAKind()'使用'List'作爲可玩構造函數或其他集合的第一個參數嗎?該方法的代碼應該是問題的一部分。 –
請提供[mcve]。請注意,這並不意味着要發佈所有代碼。它意味着發佈任何人都可以編譯的代碼,並獲得您正在討論的相同結果。 –
你的'.equals()'方法除了比較對象的**身份**之外什麼都不做,你需要了解'=='和'.equals()'之間的區別,它們**不相同。重複內容應解釋不同之處。 –