I'm trying to compare 2 Lists (wrapped in an object) containing custom objects. I don't care about the order, but if list 1 contains "1,2,3,4" then list 2 must and only contain those elements. E.g.: "4,2,3,1"
Based on Compare two List<T> objects for equality, ignoring order ignoring-order I've used the Except and Any but it doesn't give me the desired results.
If I use Assert.Equals
it fails, but Assert.IsTry(list1.equals(list2))
succeeds.
Further more if I remove the Equals and GetHashCode implementation then both tests fail.
public class AppointmentCollection : List<Appointment>
{
public override bool Equals(object obj)
{
var appCol = obj as AppointmentCollection;
if (appCol == null)
{
return false;
}
return (appCol.Count == this.Count) && !(this.Except(appCol).Any());
}
public override int GetHashCode()
{
unchecked
{
//use 2 primes
int hash = 17;
foreach (var appointment in this)
{
hash = hash * 19 + appointment.GetHashCode();
}
return hash;
}
}
}
public class Appointment
{
public string Title {get; set;}
public DateTime StartTime {get; set;}
public DateTime EndTime { get; set;}
public override bool Equals(object obj)
{
var appointment = obj as Appointment;
if (appointment == null)
{
return false;
}
return Title.Equals(appointment.Title) &&
StartTime.Equals(appointment.StartTime) &&
EndTime.Equals(appointment.EndTime);
}
public override int GetHashCode()
{
unchecked
{
//use 2 primes
int hash = 17;
hash = hash * 19 + Title.GetHashCode();
hash = hash * 19 + StartTime.GetHashCode();
hash = hash * 19 + EndTime.GetHashCode();
return hash;
}
}
}
[Test]
public void TestAppointmentListComparisonDifferentOrder()
{
var appointment1 = new Appointment(
"equals test1",
new DateTime(2013, 9, 4),
new DateTime(2013, 9, 4));
var appointment2 = new Appointment(
"equals test2",
new DateTime(2013, 9, 4),
new DateTime(2013, 9, 4));
var list1 = new AppointmentCollection() { appointment1, appointment2 };
var list2 = new AppointmentCollection() { appointment2, appointment1 };
//With Equals/GetHashCode in AppointmentCollection implemented
CollectionAssert.AreEqual(list1, list2); //fails
Assert.IsTrue(list1.Equals(list2)); //success
//Without Equals/GetHashCode in AppointmentCollection implemented
CollectionAssert.AreEqual(list1, list2); //fails
Assert.IsTrue(list1.Equals(list2)); //fails
}