次のように定義された非常に単純な Pair クラスがあります。
public class Pair<L, R> {
private L left;
private R right;
public Pair(L left, R right) {
this.left = left;
this.right = right;
}
public L getLeft() {
return this.left;
}
public R getRight() {
return this.right;
}
public String toString() {
return String.format("(%s, %s)", left, right);
}
public int hashCode() {
int hashFirst = left != null ? left.hashCode() : 0;
int hashSecond = left != null ? right.hashCode() : 0;
return (hashFirst + hashSecond) * hashSecond + hashFirst;
}
public Boolean equals(Pair other) {
if (other == null) {
return false;
}
return left.equals(other.getLeft()) && right.equals(other.getRight());
}
}
次に、Position クラスを次のように定義します。
public class Position {
private Pair<Integer, Integer> pair;
public Position(Integer x, Integer y) {
this.pair = new Pair<Integer, Integer>(x, y);
}
public Integer getX() {
return this.pair.getLeft();
}
public Integer getY() {
return this.pair.getRight();
}
public boolean equals(Position other) {
if (other == null) {
return false;
}
boolean b = getX() == other.getX() && getY() == other.getY();
System.out.println(String.format("%s.equals(%s): %s", this, other, b));
return b;
}
public int hashCode() {
return pair.hashCode();
}
public String toString() {
return String.format("(%d, %d)", this.pair.getLeft(),
this.pair.getRight());
}
}
それはすべていいです。しかし、それをテストしようとすると、奇妙なことが起こります。
public class PositionTests extends TestCase {
private Position posOne;
private Position posTwo;
private Position posThree;
public PositionTests() {
posOne = new Position(7, 6);
posTwo = new Position(12, 7);
posThree = new Position(7, 6);
}
public void testCreationX() {
assertEquals(posOne.getX(), (Integer) 7);
}
public void testCreationY() {
assertEquals(posOne.getY(), (Integer) 6);
}
public void testEquality() {
// System.out.println("p1.e(p3): " + posOne.equals(posThree));
// System.out.println("p3.e(p1): " + posThree.equals(posOne));
//assertEquals(posOne, posThree);
assertEquals(posThree, posOne);
}
public void testInequality() {
assertFalse(posOne.equals(posTwo));
}
public void testXEquality() {
assertEquals(posOne.getX(), posThree.getX());
}
public void testYEquality() {
assertEquals(posOne.getY(), posThree.getY());
}
public void testSymmTrue() {
assertTrue(posOne.equals(posThree) == posThree.equals(posOne));
}
public void testSymmFalse() {
assertTrue(posOne.equals(posTwo) == posThree.equals(posTwo));
}
public void testHashSetSame() {
Set<Position> hSet = new HashSet<Position>();
hSet.add(posOne);
hSet.add(posThree);
hSet.add(posTwo);
hSet.add(posOne);
hSet.add(posOne);
hSet.add(posOne);
assertTrue(hSet.size() == 3);
}
public void testListContainsSuccess() {
List<Position> pList = new ArrayList<Position>();
pList.add(posOne);
pList.add(posTwo);
assertTrue(pList.contains(posOne));
}
public void testListContainsSuccessDiff() {
List<Position> pList = new ArrayList<Position>();
pList.add(posOne);
pList.add(posTwo);
// System.out.println(pList);
// System.out.println(posThree);
Boolean b = pList.contains(posThree);
System.out.println("contains: " + b);
assertTrue(pList.contains(posThree));
}
public void testListContainsFail() {
List<Position> pList = new ArrayList<Position>();
pList.add(posOne);
pList.add(posThree);
assertFalse(pList.contains(posTwo));
}
}
テストからの関連する出力は次のとおりです。
[junit] ------------- Standard Output ---------------
[junit] JUnit version is: 3.8.2
[junit] contains: false
[junit] (7, 6).equals((7, 6)): true
[junit] (7, 6).equals((7, 6)): true
[junit] (7, 6).equals((12, 7)): false
[junit] (7, 6).equals((12, 7)): false
[junit] (7, 6).equals((12, 7)): false
[junit] (7, 3).equals((7, 4)): false
[junit] ------------- ---------------- ---------------
[junit]
[junit] Testcase: testListContainsSuccessDiff took 0.005 sec
[junit] FAILED
[junit] null
[junit] junit.framework.AssertionFailedError
[junit] at com.group7.dragonwars.tests.PositionTests.testListContainsSuccessDiff(PositionTests.java:92)
[junit]
[junit] Testcase: testListContainsFail took 0 sec
[junit] Testcase: testYEquality took 0 sec
[junit] Testcase: testSymmTrue took 0.014 sec
[junit] Testcase: testSymmFalse took 0.001 sec
[junit] Testcase: testHashSetSame took 0 sec
[junit] Testcase: testListContainsSuccess took 0 sec
[junit] Testcase: testCreationY took 0 sec
[junit] Testcase: testEquality took 0.001 sec
[junit] FAILED
[junit] expected:<(7, 6)> but was:<(7, 6)>
[junit] junit.framework.AssertionFailedError: expected:<(7, 6)> but was:<(7, 6)>
[junit] at com.group7.dragonwars.tests.PositionTests.testEquality(PositionTests.java:43)
[junit]
[junit] Testcase: testInequality took 0.001 sec
[junit] Testcase: testXEquality took 0 sec
[junit] Testcase: testCreationX took 0 sec
[junit] Test com.group7.dragonwars.tests.AllTests FAILED
関連するすべてのコードとテスト結果を投稿したので、私の質問は、なぜ assertEquals(posOne, posThree) と contains() の呼び出しが失敗するのかということです。テスト出力の上部にあるように、.equals() は true を返します。なぜこれらが失敗するのか、私はとても混乱しています。私のプログラム全体を通して、リストのメンバーシップを確認するために手動でリストをトラバーサルし、equals() を手動で呼び出す必要がありましたが、なぜですか? リストのドキュメントには
Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).
それでも、.contains()
失敗します。なに…?