Skip to content
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

Merged
merged 3 commits into from
Jan 31, 2019

Conversation

sksamuel
Copy link
Member

No description provided.

@sksamuel sksamuel changed the title Provide range-based numeric generators #530 Provide range-based numeric generators and javax.time generators #530 Jan 13, 2019
@@ -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()
Copy link
Member

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) }
Copy link
Member

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> {
Copy link
Member

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()
Copy link
Member

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)) }
Copy link
Member

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)

Copy link
Member

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)) {
Copy link
Member

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())

Copy link
Member

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.
Copy link
Member

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> {
Copy link
Member

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.
Copy link
Member

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

Copy link
Member

@LeoColman LeoColman left a 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

@LeoColman
Copy link
Member

Also missing tests for the new gens and functions.

@sksamuel
Copy link
Member Author

sksamuel commented Jan 13, 2019 via email

@LeoColman
Copy link
Member

@sksamuel
I tweaked some of the generators. Could you take a look, please?

@sksamuel
Copy link
Member Author

Looks excellent. Feel free to merge.

@LeoColman LeoColman merged commit ad9f9b4 into master Jan 31, 2019
@LeoColman LeoColman deleted the feature/530 branch January 31, 2019 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants