internal static const Person[] persons :=
[
Person {name = "John"; age = 51; canCode = true},
Person {name = "Daniel"; age = 23; canCode = false},
Person {name = "Donovan"; age = 41; canCode = true}
]
data := persons.rw.sort|Person p1, Person p2| {p1.age<=>p2.age}
expected := Persons[,]{
Person {name = "Daniel"; age = 23; canCode = false},
Person {name = "Donovan"; age = 41; canCode = true},
Person {name = "John"; age = 51; canCode = true},
}
verifyEq(expected,data)
And the test result tells me things are equal but there is a hash mismatch. Any ideas how to avoid such problems?
brianFri 20 May 2011
The verifyEq test will test both equals and hash. If you have overridden equals, but not hash then the test will fail. For example two objects should never return true for equals, yet return different hash codes. So you should ensure that your objects do in fact override hash with a suitable hashcode that will work in conjunction with your equals implementation.
DanielFathSun 22 May 2011
Oh yeah I forgot the hash. A big facepalm for me. Tnx brian.
DanielFath Thu 19 May 2011
So I have a small array of information.
internal static const Person[] persons := [ Person {name = "John"; age = 51; canCode = true}, Person {name = "Daniel"; age = 23; canCode = false}, Person {name = "Donovan"; age = 41; canCode = true} ] data := persons.rw.sort|Person p1, Person p2| {p1.age<=>p2.age} expected := Persons[,]{ Person {name = "Daniel"; age = 23; canCode = false}, Person {name = "Donovan"; age = 41; canCode = true}, Person {name = "John"; age = 51; canCode = true}, } verifyEq(expected,data)And the test result tells me things are equal but there is a hash mismatch. Any ideas how to avoid such problems?
brian Fri 20 May 2011
The
verifyEqtest will test bothequalsandhash. If you have overriddenequals, but nothashthen the test will fail. For example two objects should never return true for equals, yet return different hash codes. So you should ensure that your objects do in fact overridehashwith a suitable hashcode that will work in conjunction with yourequalsimplementation.DanielFath Sun 22 May 2011
Oh yeah I forgot the
hash. A big facepalm for me. Tnx brian.