You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a question issue, and I hope that's an acceptable use in this project.
I try to use quickcheck to write tests for function accepting two strings. I haven't been able to adapt the example test to my use case. This is what I have. Is quickcheck able to provide random parameters for strings (or &strs)?
externcrate quickcheck;// at least difference of the sizes of a and b#[test]fnprop_test_(){use quickcheck::quickcheck;fnat_least_size_difference(a:&str,b:&str) -> bool{let size_a = a.chars().count()asi32;let size_b = b.chars().count()asi32;let diff = (size_a - size_b).abs();
edit_distance::edit_distance(a, b) > diff;}quickcheck(at_least_size_difference asfn(a:&str,b:&str) -> bool);}
The text was updated successfully, but these errors were encountered:
No. QuickCheck doesn't generate values with type &str. It is much more convenient to limit QuickCheck to owned values (or non-borrowed types that implement Copy, like ints). I don't think it's possible to write a useful implementation of Arbitrary for &str (because it needs to be able to manipulate the strings to do shrinking).
With that said, given your code here, I don't see any reason why you can't use String. If you have a String, then you can get an &str. It will cost you an allocation, but it's usually not a huge deal in this context.
I think if you s/&str/String and change edit_distance(a, b) to edit_distance(&a, &b), then it should work (assuming edit_distance accepts two values with type &str).
Thank you! It makes sense the way you explain it, and I'm just in the process of forming an idea what the difference between &str and String entails. With this change it works beautifully.
This is a question issue, and I hope that's an acceptable use in this project.
I try to use quickcheck to write tests for function accepting two strings. I haven't been able to adapt the example test to my use case. This is what I have. Is quickcheck able to provide random parameters for strings (or
&str
s)?The text was updated successfully, but these errors were encountered: