Skip to content

Commit

Permalink
Fix randomOrNull(Random) using wrong implementation (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikc5000 committed May 17, 2021
1 parent 0a3474c commit dfe6399
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 deletions.
12 changes: 7 additions & 5 deletions core/src/commonMain/generated/io/islandtime/ranges/_Operators.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public fun DateRange.randomOrNull(): Date? = randomOrNullImpl(Random)
*
* @see DateRange.random
*/
public fun DateRange.randomOrNull(random: Random): Date? = randomImpl(random)
public fun DateRange.randomOrNull(random: Random): Date? = randomOrNullImpl(random)

/**
* Returns a random date within this interval using the default random number generator.
Expand Down Expand Up @@ -83,7 +83,7 @@ public fun DateTimeInterval.randomOrNull(): DateTime? = randomOrNullImpl(Random)
*
* @see DateTimeInterval.random
*/
public fun DateTimeInterval.randomOrNull(random: Random): DateTime? = randomImpl(random)
public fun DateTimeInterval.randomOrNull(random: Random): DateTime? = randomOrNullImpl(random)

/**
* Returns a random date within this interval using the default random number generator. The offset
Expand Down Expand Up @@ -119,7 +119,8 @@ public fun OffsetDateTimeInterval.randomOrNull(): OffsetDateTime? = randomOrNull
*
* @see OffsetDateTimeInterval.random
*/
public fun OffsetDateTimeInterval.randomOrNull(random: Random): OffsetDateTime? = randomImpl(random)
public fun OffsetDateTimeInterval.randomOrNull(random: Random): OffsetDateTime? =
randomOrNullImpl(random)

/**
* Returns a random date within this interval using the default random number generator. The zone of
Expand Down Expand Up @@ -155,7 +156,8 @@ public fun ZonedDateTimeInterval.randomOrNull(): ZonedDateTime? = randomOrNullIm
*
* @see ZonedDateTimeInterval.random
*/
public fun ZonedDateTimeInterval.randomOrNull(random: Random): ZonedDateTime? = randomImpl(random)
public fun ZonedDateTimeInterval.randomOrNull(random: Random): ZonedDateTime? =
randomOrNullImpl(random)

/**
* Returns a random date within this interval using the default random number generator.
Expand Down Expand Up @@ -189,4 +191,4 @@ public fun InstantInterval.randomOrNull(): Instant? = randomOrNullImpl(Random)
*
* @see InstantInterval.random
*/
public fun InstantInterval.randomOrNull(random: Random): Instant? = randomImpl(random)
public fun InstantInterval.randomOrNull(random: Random): Instant? = randomOrNullImpl(random)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.islandtime.measures.*
import io.islandtime.parser.DateTimeParseException
import io.islandtime.parser.DateTimeParsers
import io.islandtime.test.AbstractIslandTimeTest
import kotlin.random.Random
import kotlin.test.*

class DateRangeTest : AbstractIslandTimeTest() {
Expand Down Expand Up @@ -186,11 +187,13 @@ class DateRangeTest : AbstractIslandTimeTest() {
@Test
fun `random() throws an exception when the range is empty`() {
assertFailsWith<NoSuchElementException> { DateRange.EMPTY.random() }
assertFailsWith<NoSuchElementException> { DateRange.EMPTY.random(Random) }
}

@Test
fun `random() throws an exception when the range is not bounded`() {
assertFailsWith<UnsupportedOperationException> { DateRange.UNBOUNDED.random() }
assertFailsWith<UnsupportedOperationException> { DateRange.UNBOUNDED.random(Random) }

assertFailsWith<UnsupportedOperationException> {
DateRange(start = Date(2020, Month.APRIL, 1)).random()
Expand All @@ -204,11 +207,13 @@ class DateRangeTest : AbstractIslandTimeTest() {
@Test
fun `randomOrNull() returns null when the range is empty`() {
assertNull(DateRange.EMPTY.randomOrNull())
assertNull(DateRange.EMPTY.randomOrNull(Random))
}

@Test
fun `randomOrNull() returns null when the range is not bounded`() {
assertNull(DateRange.UNBOUNDED.randomOrNull())
assertNull(DateRange.UNBOUNDED.randomOrNull(Random))
assertNull(DateRange(start = Date(2020, Month.APRIL, 1)).randomOrNull())
assertNull(DateRange(endInclusive = Date(2020, Month.APRIL, 1)).randomOrNull())
}
Expand All @@ -217,6 +222,7 @@ class DateRangeTest : AbstractIslandTimeTest() {
fun `randomOrNull() returns a date within range`() {
val range = Date(2018, Month.FEBRUARY, 20)..Date(2018, Month.FEBRUARY, 20)
assertTrue { range.randomOrNull()!! in range }
assertTrue { range.randomOrNull(Random)!! in range }
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.islandtime.parser.DateTimeParseException
import io.islandtime.parser.DateTimeParsers
import io.islandtime.parser.groupedDateTimeParser
import io.islandtime.test.AbstractIslandTimeTest
import kotlin.random.Random
import kotlin.test.*

class DateTimeIntervalTest : AbstractIslandTimeTest() {
Expand Down Expand Up @@ -125,12 +126,14 @@ class DateTimeIntervalTest : AbstractIslandTimeTest() {
@Test
fun `randomOrNull() returns null when the interval is empty`() {
assertNull(DateTimeInterval.EMPTY.randomOrNull())
assertNull(DateTimeInterval.EMPTY.randomOrNull(Random))
}

@Test
fun `randomOrNull() returns null when the interval is not bounded`() {
val dateTime = DateTime(2019, Month.MARCH, 1, 13, 0)
assertNull(DateTimeInterval.UNBOUNDED.randomOrNull())
assertNull(DateTimeInterval.UNBOUNDED.randomOrNull(Random))
assertNull(DateTimeInterval(start = dateTime).randomOrNull())
assertNull(DateTimeInterval(endExclusive = dateTime).randomOrNull())
}
Expand All @@ -144,7 +147,9 @@ class DateTimeIntervalTest : AbstractIslandTimeTest() {
).forEach { start ->
val interval = start until start + 1.nanoseconds
assertEquals(start, interval.random())
assertEquals(start, interval.random(Random))
assertEquals(start, interval.randomOrNull())
assertEquals(start, interval.randomOrNull(Random))
}

listOf(
Expand All @@ -160,7 +165,9 @@ class DateTimeIntervalTest : AbstractIslandTimeTest() {
).forEach { end ->
val interval = start until end
assertTrue { interval.random() in interval }
assertTrue { interval.random(Random) in interval }
assertTrue { interval.randomOrNull()!! in interval }
assertTrue { interval.randomOrNull(Random)!! in interval }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.islandtime.parser.DateTimeParseException
import io.islandtime.parser.DateTimeParsers
import io.islandtime.parser.groupedDateTimeParser
import io.islandtime.test.AbstractIslandTimeTest
import kotlin.random.Random
import kotlin.test.*

class OffsetDateTimeIntervalTest : AbstractIslandTimeTest() {
Expand Down Expand Up @@ -112,25 +113,29 @@ class OffsetDateTimeIntervalTest : AbstractIslandTimeTest() {
@Test
fun `random() throws an exception when the interval is empty`() {
assertFailsWith<NoSuchElementException> { OffsetDateTimeInterval.EMPTY.random() }
assertFailsWith<NoSuchElementException> { OffsetDateTimeInterval.EMPTY.random(Random) }
}

@Test
fun `random() throws an exception when the interval is not bounded`() {
val dateTime = Date(2019, Month.NOVEMBER, 1) at MIDNIGHT at UtcOffset((-4).hours)
assertFailsWith<UnsupportedOperationException> { OffsetDateTimeInterval.UNBOUNDED.random() }
assertFailsWith<UnsupportedOperationException> { OffsetDateTimeInterval.UNBOUNDED.random(Random) }
assertFailsWith<UnsupportedOperationException> { OffsetDateTimeInterval(start = dateTime).random() }
assertFailsWith<UnsupportedOperationException> { OffsetDateTimeInterval(endExclusive = dateTime).random() }
}

@Test
fun `randomOrNull() returns null when the interval is empty`() {
assertNull(OffsetDateTimeInterval.EMPTY.randomOrNull())
assertNull(OffsetDateTimeInterval.EMPTY.randomOrNull(Random))
}

@Test
fun `randomOrNull() returns null when the interval is not bounded`() {
val dateTime = Date(2019, Month.NOVEMBER, 1) at MIDNIGHT at UtcOffset((-4).hours)
assertNull(OffsetDateTimeInterval.UNBOUNDED.randomOrNull())
assertNull(OffsetDateTimeInterval.UNBOUNDED.randomOrNull(Random))
assertNull(OffsetDateTimeInterval(start = dateTime).randomOrNull())
assertNull(OffsetDateTimeInterval(endExclusive = dateTime).randomOrNull())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.islandtime.*
import io.islandtime.Time.Companion.MIDNIGHT
import io.islandtime.measures.*
import io.islandtime.test.AbstractIslandTimeTest
import kotlin.random.Random
import kotlin.test.*

class ZonedDateTimeIntervalTest : AbstractIslandTimeTest() {
Expand Down Expand Up @@ -110,25 +111,29 @@ class ZonedDateTimeIntervalTest : AbstractIslandTimeTest() {
@Test
fun `random() throws an exception when the interval is empty`() {
assertFailsWith<NoSuchElementException> { ZonedDateTimeInterval.EMPTY.random() }
assertFailsWith<NoSuchElementException> { ZonedDateTimeInterval.EMPTY.random(Random) }
}

@Test
fun `random() throws an exception when the interval is not bounded`() {
val dateTime = Date(2019, Month.NOVEMBER, 1) at MIDNIGHT at nyZone
assertFailsWith<UnsupportedOperationException> { ZonedDateTimeInterval.UNBOUNDED.random() }
assertFailsWith<UnsupportedOperationException> { ZonedDateTimeInterval.UNBOUNDED.random(Random) }
assertFailsWith<UnsupportedOperationException> { ZonedDateTimeInterval(start = dateTime).random() }
assertFailsWith<UnsupportedOperationException> { ZonedDateTimeInterval(endExclusive = dateTime).random() }
}

@Test
fun `randomOrNull() returns null when the interval is empty`() {
assertNull(ZonedDateTimeInterval.EMPTY.randomOrNull())
assertNull(ZonedDateTimeInterval.EMPTY.randomOrNull(Random))
}

@Test
fun `randomOrNull() returns null when the interval is not bounded`() {
val dateTime = Date(2019, Month.NOVEMBER, 1) at MIDNIGHT at nyZone
assertNull(ZonedDateTimeInterval.UNBOUNDED.randomOrNull())
assertNull(ZonedDateTimeInterval.UNBOUNDED.randomOrNull(Random))
assertNull(ZonedDateTimeInterval(start = dateTime).randomOrNull())
assertNull(ZonedDateTimeInterval(endExclusive = dateTime).randomOrNull())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private fun FileBuilder.buildOperatorsForClass(receiverClass: IntervalDescriptio
returns(receiverClass.elementDescription.typeName.copy(nullable = true))

code {
using("impl", rangesInternal("randomImpl"))
using("impl", rangesInternal("randomOrNullImpl"))
"return %impl:T(%random:N)"
}
}
Expand Down

0 comments on commit dfe6399

Please sign in to comment.