Search code examples
testingrustrust-cargo

In the rust test, I don't care about the sequence of vec![]. What should i do?


enter image description here

Is there any disorderd arrays Or Do not judge the order when asserting?


Solution

  • What I do is sort both sides in the assert call itself. This only works if T implements Ord.

    let result = my_function();
    my_function.sort();
    let target = vec![];
    target.sort();
    
    assert_eq!(result, target);
    

    If your datatype does not support Ord, you can use sort_by with a FnMut that returns an instance of Ordering.

    Note that this can have issues when there isn't one specific way a vector can be sorted.