use std::is_permutation() for unit testing?

Hi

I'm writing some unit tests where the method tested should return a vector of objects. The method does not guarantee the order of the objects in the returned vector.

As part of the unit tests I have created a vector with the correct objects. I then need to check that the vector returned by the method contains the same objects as the vector with the correct objects.

I'm thinking that std::is_permutation can help here:

1
2
3
4
5
vector<thing> correct = {a,b,c,d};
vector<thing> results = test.method();

assert(is_permutation(correct.begin(),correct.end(),results.begin(),results.end()));


I just wondered if this was a good approach?

Thanks
That's what the function is for. Alternatively, you could sort both vectors according to the same total order, and it should yield two identical vectors.
By default, is_permutation uses the == operator between two thing objects to determine equality. So just make sure you have that defined in a sane way (a == b should always be the same as b == a). Other than that, yeah it should work -- if you're having a specific issue, say what it is and we can help.
If your correct vector is already in order, then sorting the result and comparing them is probably more efficient than is_permutation.
Topic archived. No new replies allowed.