Closed michaelmp closed 6 years ago
Dy default quicktheories will currently make ten attempts to generate a valid value before giving up (so assumptions shouldn't filter more than about 10% of the values).
Presumably you are filtering out somewhere near 12 of the 128 possible values?
This default can be changed globally with the QT_ATTEMPTS system property, and overridden on a per theory basic by calling.
withGenerateAttempts(xxx);
Playing with these parameters is easiest solution, but the number of attempts is not attached to the Gen, so it needs to be either changed globally or on a per usage basis.
As the domain you are working with is small an alternative approach would be to do this as a pick list of the valid values.
Something like
public static Gen<Character> basicLatinCharactersOmitting(Set<Character> chars) {
List<Character> allChars = <generate a list of all 128 latin characters>;
return Generate.pick(allChars.stream().filter(chars::contains).collect(Collectors.toList()));
}
Thanks Henry, I went with the exhaustive list for my tests since the range of code points is small.
Using
assuming
tends to result inInvalidStateException
if the assumption is overly constraining. The README mentions this and warns against overuse ofassuming
. In practice, I run into this exception frequently enough (say, 1/10 runs) that it is obnoxious.My use case is that I want to generate all possible strings (or characters, for starters) with the exception of a handful of defined values.
I wrote this code to get a
Gen<Character>
that omits some defined Characters:This helper method is more complex than I'd like to have in my codebase. Is there a simpler way to achieve this using the API? Is it worth extending the API with this kind of functionality? Thanks.