-
Notifications
You must be signed in to change notification settings - Fork 637
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide range-based numeric generators and javax.time generators #530 #543
Conversation
@@ -363,6 +380,33 @@ interface Gen<T> { | |||
override fun random(): Sequence<UUID> = generateSequence { UUID.randomUUID() } | |||
} | |||
|
|||
fun localDate(minYear: Int = 1, maxYear: Int = 2030): Gen<LocalDate> = object : Gen<LocalDate> { | |||
override fun constants(): Iterable<LocalDate> = emptyList() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting dates to be constants: February 28th, February 29th, Jan 1st and Dec 31st
@@ -363,6 +380,33 @@ interface Gen<T> { | |||
override fun random(): Sequence<UUID> = generateSequence { UUID.randomUUID() } | |||
} | |||
|
|||
fun localDate(minYear: Int = 1, maxYear: Int = 2030): Gen<LocalDate> = object : Gen<LocalDate> { | |||
override fun constants(): Iterable<LocalDate> = emptyList() | |||
override fun random(): Sequence<LocalDate> = generateSequence { LocalDate.of(RANDOM.nextInt(maxYear - minYear) + minYear, RANDOM.nextInt(12) + 1, RANDOM.nextInt(31) + 1) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bad randoms. Will generate February 30th, April 31st
@@ -363,6 +380,33 @@ interface Gen<T> { | |||
override fun random(): Sequence<UUID> = generateSequence { UUID.randomUUID() } | |||
} | |||
|
|||
fun localDate(minYear: Int = 1, maxYear: Int = 2030): Gen<LocalDate> = object : Gen<LocalDate> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that minYear should default to 1970, but I liked to be able to customize!
} | ||
|
||
fun localTime(): Gen<LocalTime> = object : Gen<LocalTime> { | ||
override fun constants(): Iterable<LocalTime> = emptyList() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe 00:00 and 23:59 could be good constants here?
|
||
fun localDateTime(minYear: Int = 1, maxYear: Int = 2030): Gen<LocalDateTime> = object : Gen<LocalDateTime> { | ||
override fun constants(): Iterable<LocalDateTime> = emptyList() | ||
override fun random(): Sequence<LocalDateTime> = generateSequence { LocalDateTime.of(RANDOM.nextInt(maxYear - minYear) + minYear, RANDOM.nextInt(12) + 1, RANDOM.nextInt(31) + 1, RANDOM.nextInt(24), RANDOM.nextInt(60), RANDOM.nextInt(60)) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe better to use the other generators here. generatedLocalDate.atTime(generatedTime)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prevents extra maintance points (although unlikely)
} | ||
|
||
fun duration(): Gen<Duration> = object : Gen<Duration> { | ||
private fun randomDuration(): Duration = when (RANDOM.nextInt(5)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can reduce this code a bit, and include more constants:
val wantedUnits = ChronoUnit.values().filter { it <= ChronoUnit.WEEKS } // Will get all crono units that represent a week or less
return Duration.of(Random.nextLong(), wantedUnits.random())
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be also cool to have a Gen<> for Period
@@ -374,6 +418,26 @@ interface Gen<T> { | |||
override fun shrinker(): Shrinker<Double>? = DoubleShrinker | |||
} | |||
|
|||
/** | |||
* Returns a [[Gen]] which is the same as [[Gen.double]]] but does not include infinity or NaN. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does not include -INFINITY, +INFINITY or NaN
/** | ||
* Returns a [[Gen]] which is the same as [[Gen.double]]] but does not include infinity or NaN. | ||
*/ | ||
fun numericDoubles(): Gen<Double> = object : Gen<Double> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this to be range-based, we should add a minValue
and maxValue
} | ||
|
||
/** | ||
* Returns a [[Gen]] which is the same as [[Gen.float]]] but does not include infinity or NaN. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comments from Double Gen
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some changes should be made
Also missing tests for the new gens and functions. |
Good feedback.
…On Sun, 13 Jan 2019, 22:49 Leonardo Colman Lopes ***@***.*** wrote:
Also missing tests for the new gens and functions.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#543 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAtZGokqPr4Dzk09Jsi9zUkdIlOXS49qks5vC7gOgaJpZM4Z9WML>
.
|
@sksamuel |
Looks excellent. Feel free to merge. |
No description provided.