#How can I assert that two BinaryHeaps are equals ?

1 messages · Page 1 of 1 (latest)

eager lichen
#

I have a BinaryHeap with values implementing std::cmp::Eq and I would like to assert in some test that my BinaryHeap content is equal to some expected values.

I am surprised that BinaryHeap does not implement the std::cmp::Eq for values implementing it.

Am I missing something here ?

spare matrix
#

I think there's a subtle difference between two heaps being equal (i.e. having the same structure) and two heaps containing the same elements

#

I'd expect equality to be the first, but you probably want the second.. I'm guessing Eq isn't implemented to avoid this.

eager lichen
#

Ok thank you @spare matrix

spare matrix
#

Im also not sure if there's a good way to implement Eq other than emptying and refilling the heap

eager lichen
#

In this case, what is the best way to check that two heaps contains the same elements ?

#

Comparing iterators onto the values ?

spare matrix
#

the cheap iterators are in "random" order

#

converting them into a sorted vec and comparing those is the easiest

eager lichen
#

like this ?
assert_eq!(first_heap.into_sorted_vec(),second_heap.into_sorted_vec());

spare matrix
#

converting those vecs back into heaps is O(n log(n)) though, just do you're aware

spare matrix
eager lichen
#

ok thank you. In my case it's ok to consume the heap (last assertion in my test)