Built-in generators¶
Kwik provide a collection of generators to satisfy a wide variety of uses-cases.
They are all available as extension functions on Generator.Companion so that we can find them easily and invoke them like this:
val generator = Generator.ints()
Primitives¶
Generator.ints(min = Int.MIN_VALUE, max = Int.MAX_VALUE)Generate integers. Includes the samples:
0,1,-1,minandmax.Note that there are also
positiveInts,naturalInts,negativeIntsandnonZeroIntsalternativesGenerator.longs(min = Long.MIN_VALUE, max = Long.MAX_VALUE)Generate longs. Includes the samples:
0,1,-1,minandmax.Note that there are also
positiveLongs,naturalLongs,negativeLongsandnonZeroLongsalternativesGenerator.floats(min = -Float.MAX_VALUE, max = Float.MAX_VALUE)Generate longs. Includes the samples:
0.0,1.0,-1.0,minandmax.Note that
NaN,POSITIVE_INFINITYandPOSITIVE_INFINITYare not generated. To test theses, we can usewithSamples()orwithNaN()Example:
Generator.floats().withNaN().withSamples(Float.POSITIVE_INFINITY)Note that there are also
positiveFloats,negativeFloatsandnonZeroFloatsalternativesGenerator.doubles(min = -Double.MAX_VALUE, max = Double.MAX_VALUE)Generate doubles. Includes the samples:
0.0,1.0,-1.0,minandmax.Note that
NaN,POSITIVE_INFINITYandPOSITIVE_INFINITYare not generated. To test theses, we can usewithSamples()orwithNaN()Example:
Generator.doubles().withNaN().withSamples(Double.POSITIVE_INFINITY)Note that there are also
positiveDoubles,negativeDoublesandnonZeroDoublesalternativesGenerator.booleans()- Generate booleans
Text¶
Generator.characters(charset = CharSets.printable, exclude = emptySet())Generate strings. Use the parameter
charsetandexcludeto customize the characters which can be used.Generation includes space (’ ‘) as a sample.
Note
CharSetsprovide few common set of characters such asalpha,alphaNumericand othersIt is there to help quickly configure the Character generator.
By default, it will generate any printable characters.
Generator.strings(minLength = 0, maxLength = 50, charGenerator = Generator.characters())Generate strings. Use the parameter
charGeneratorto provide a character generator which is used to make the string.Generation includes empty (“”) string as a sample.
Collections¶
Generator.lists(elementGen = Generator.default(), minSize = 0, maxSize = 50)Generate lists.
elementGencan be used to define the generator of the elements.Generation include empty and singleton lists as samples
Note that there is also a
nonEmptyListsalternativeGenerator.sets(elementGen = Generator.default(), minSize = 0, maxSize = 50)Generate sets.
elementGencan be used to define the generator of the elements.Generation include empty and singleton sets as samples
Will fail in it takes too much iteration to reach the
minSize(so make sure the element generator can generate enough different values)Note that there is also a
nonEmptySetsalternativeGenerator.maps(keyGen = Generator.default(), valueGen = Generator.default(), minSize = 0, maxSize = 50)Generate sets.
keyGencan be used to define the generator of the elements.Generation include empty and singleton maps as samples
Will fail in it takes too much iteration to reach the
minSize(so make sure the element generator can generate enough different values)Note that there is also a
nonEmptyMapsalternative
Sequences¶
Generator.sequences(elementGen = Generator.default(), minSize = 0, maxSize = 50)Generate sequences.
elementGencan be used to define the generator of the elements.Generation include empty and singleton sequences as samples
Note that there is also a
nonEmptySequencesalternative
Ranges¶
Generator.ranges(elementGen)- Generate ranges.
elementGencan be used to define the generator of the elements. Generator.intRanges(elementGen = Generator.ints())- Generates
ClosedRanged<Int>. Includes empty and singleton ranges as samples Generator.longRanges(elementGen = Generator.longs())- Generates
ClosedRanged<Long>. Includes empty and singleton ranges as samples Generator.charRanges(elementGen = Generator.characters())- Generates
ClosedRanged<Char>. Includes empty and singleton ranges as samples
Enums¶
Generator.enum<T>()Create a generator for the given enum type
T.The enum must contains at least one enumeration.
Java¶
Generator.uuids()- Create a generator for UUID
Java Time API¶
Generator.instants(min: Instant.MIN, max: Instant.MAX)- Generates Instants (with nano seconds), includes the samples:
Instant.EPOCH(1970-01-01T00:00:00.000Z),minandmax Generator.durations(min, max)- Generates Durations (with nano seconds), Includes the samples:
Duration.ZERO,minandmax Generator.localTimes(min: LocalTime.MIN, max: LocalTime.MAX)- Generates LocalTimes (with nano seconds), Includes the samples:
LocalTime.NOON,minandmax Generator.localDates(min: LocalDate.MIN, max: LocalDate.MAX)- Generates LocalDates, includes the samples:
LocalDate.EPOCH(1970-01-01),minandmax Generator.localDateTimes(min: LocalDateTime.MIN, max: LocalDateTime.MAX)- Generates LocalDateTimes, includes the samples:
LocalDate.EPOCH.atStartOfDay(1970-01-01T00:00:00.000Z),minandmax