Skip to content

Commit

Permalink
javatime.kt: minor cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
dconeybe committed Feb 11, 2025
1 parent 4382e2f commit 71d9448
Showing 1 changed file with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ sealed interface TimeOffset {

data class HhMm(val hours: Int, val minutes: Int, val sign: Sign) : TimeOffset {
init {
require(hours + 100 in validHours) {
require(hours in validHours) {
"invalid hours: $hours (must be in the closed range $validHours)"
}
require(minutes in validMinutes) {
Expand Down Expand Up @@ -396,6 +396,9 @@ object JavaTimeArbs {
"a difference of ${min.toSeconds() - max.toSeconds()} seconds"
}

fun isBetweenMinAndMax(other: TimeOffset.HhMm): Boolean =
(min === null || other >= min) && (max === null || other <= max)

return arbitrary(
edgecases =
listOf(
Expand All @@ -406,14 +409,14 @@ object JavaTimeArbs {
TimeOffset.HhMm(hours = 18, minutes = 0, sign = TimeOffset.HhMm.Sign.Positive),
TimeOffset.HhMm(hours = 18, minutes = 0, sign = TimeOffset.HhMm.Sign.Negative),
)
.filter { (min === null || it >= min) || (max === null || it <= max) }
.filter(::isBetweenMinAndMax)
) {
var count = 0
var hhmm: TimeOffset.HhMm
while (true) {
count++
hhmm = TimeOffset.HhMm(hours = hour.bind(), minutes = minute.bind(), sign = sign.bind())
if ((min === null || hhmm >= min) && (max === null || hhmm <= max)) {
if (isBetweenMinAndMax(hhmm)) {
break
} else if (count > 1000) {
throw Exception("internal error j878fp4gmr: exhausted attempts to generate HhMm")
Expand Down

0 comments on commit 71d9448

Please sign in to comment.