저는 C#을 처음 사용합니다. 아마도 IEquatable
을 올바르게 구현하지 않을 것입니다. 내 유형의 객체는 동일하게 간주되어야하기 때문입니다.C# XNA : 사전에 문제가 발생했습니다.
을 :
class CompPoint : IComparable {
public int X;
public int Y;
public CompPoint(int X, int Y) {
this.X = X;
this.Y = Y;
}
public override bool Equals(Object o) {
if (!(o is CompPoint)) {
throw new ArgumentException(String.Format("argument is not a CompPoint (%s given)", o));
}
CompPoint cp = (CompPoint)o;
return this.X == cp.X && this.Y == cp.Y;
}
public override int GetHashCode() {
int hash = base.GetHashCode(); // this is a problem. replace with a constant?
hash = (hash * 73) + this.X.GetHashCode();
hash = (hash * 73) + this.Y.GetHashCode();
return hash;
}
}
(. 그것은 클래스 인 정당화 않는 이것보다 CompPoint
에 더있다가)
그런 다음,이 테스트는 실패
[TestMethod()]
public void compPointTest() {
Assert.AreEqual(new CompPoint(0, 0), new CompPoint(0, 0));
}
무엇 오전 내가 오해 한단 말이야? 참조 용평을 사용하는 Assert.AreEqual()
입니까? 내 Equals()
기능이 CompPoint
에 엉망이 되었습니까?
:
public void EqualsTest() {
Assert.IsTrue(new CompPoint(1, 1).Equals(new CompPoint(1, 1)));
}
이에 대한이 이유는 것 나는 Dictionary
을 사용하고, 그것이 내가 희망을 줄 방법을 작동하지 않는 것입니다 :
[TestMethod()]
public void dictCompPointTest() {
IDictionary<CompPoint, int> dict = new Dictionary<CompPoint, int>();
dict[new CompPoint(0, 0)] = 4;
dict[new CompPoint(0, 0)] = 24;
dict[new CompPoint(0, 0)] = 31;
Assert.AreEqual(31, dict[new CompPoint(0, 0)]);
Assert.AreEqual(1, dict.Count);
}
이 메시지와 함께 테스트가 실패합니다.
테스트 방법 ShipAILabTest.BoardUtilsTest.dictCompPointTest 던져 예외 : System.Collections.Generic.KeyNotFoundException : 주어진 키가 사전에 없습니다.
이 테스트는 나의 기대를 캡슐화합니다. 키가 매번 동일하기 때문에 값을 덮어 쓰길 바라고 있습니다. 평등성을 테스트하기 위해 사용하는 Dictionary
은 무엇입니까?
UPDATE
: 나는 토마스의 제안에 따라, 동등의 기능을 추가, 지금은CompPoint
비교 테스트가 작동하고
dictCompPointTest
작품. 키가
new CompPoint(0, 1)
을 때 키가
new CompPoint(4, 1)
을 때
[TestMethod()]
public void dictCPTest2() {
IDictionary<CompPoint, int> dict = new Dictionary<CompPoint, int>();
dict[new CompPoint(2, 2)] = 2;
dict[new CompPoint(2, 2)] = 2;
Assert.AreEqual(1, dict.Count);
}
테스트는 실패가 아니라 :
public override bool Equals(Object o) {
if (!(o is CompPoint)) {
throw new ArgumentException(String.Format("argument is not a CompPoint (%s given)", o));
}
CompPoint cp = (CompPoint)o;
return this.X == cp.X && this.Y == cp.Y;
}
불가사의,이 테스트는 계속 실패. 왜 이것이 다른 가치가 아닌 일부 가치에 작용할 수 있습니까?
더 많은 수수께끼 : 해시 코드 기능이 제대로 작동하지 않는 것 같습니다. 이 테스트는 실패합니다.
[TestMethod()]
public void hashCodeTest() {
int x = 0;
int y = 0;
Assert.AreEqual(new CompPoint(x, y).GetHashCode(), new CompPoint(x, y).GetHashCode());
}
해시 코드 기능은 위에 나와 있습니다. 여기에 어떤 문제가 있습니까? 두 개의 CompPoint
객체가 동일한 해시 코드를 가져야하지 않습니까? 어쩌면 base.getHashCode()
에 대한 내 전화가 문제입니까?
이것은 흥미롭지 만 궁극적으로는 결실입니다. '사전'은 여전히 중복을 감지하지 못합니다. –
'getHashCode()'가 문제의 원인 일 수 있다고 생각합니다. (위 참조) –
이 문제를 해결했는지 모르겠지만 GetHashCode 메서드가 여전히 위의 코드에 게시 한 것이면 여전히 두 인스턴스에 다른 코드가 생성됩니다 (즉, 두 개의 새로운 CompPoint (0, 0)) base.GetHashCode() – jeffora