-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5759 from espoon-voltti/dateset-binary-search
Parannetaan RangeBasedSet-tietorakenteiden suorituskykyä binäärihaulla
- Loading branch information
Showing
10 changed files
with
381 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
service/src/test/kotlin/fi/espoo/evaka/shared/ArbExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// SPDX-FileCopyrightText: 2017-2024 City of Espoo | ||
// | ||
// SPDX-License-Identifier: LGPL-2.1-or-later | ||
|
||
package fi.espoo.evaka.shared | ||
|
||
import fi.espoo.evaka.shared.data.DateSet | ||
import fi.espoo.evaka.shared.data.DateTimeSet | ||
import fi.espoo.evaka.shared.domain.FiniteDateRange | ||
import fi.espoo.evaka.shared.domain.HelsinkiDateTime | ||
import fi.espoo.evaka.shared.domain.HelsinkiDateTimeRange | ||
import io.kotest.property.Arb | ||
import io.kotest.property.arbitrary.bind | ||
import io.kotest.property.arbitrary.list | ||
import io.kotest.property.arbitrary.localDate | ||
import io.kotest.property.arbitrary.long | ||
import io.kotest.property.arbitrary.map | ||
import io.kotest.property.arbitrary.positiveLong | ||
import java.time.Duration | ||
import java.time.LocalDate | ||
import java.time.LocalDateTime | ||
|
||
fun Arb.Companion.finiteDateRange( | ||
start: Arb<LocalDate> = | ||
Arb.localDate(minDate = LocalDate.of(2019, 1, 1), maxDate = LocalDate.of(2030, 12, 1)), | ||
durationDays: Arb<Long> = Arb.positiveLong(max = 365_0L), | ||
): Arb<FiniteDateRange> = | ||
Arb.bind(start, durationDays) { startDate, days -> | ||
FiniteDateRange(startDate, startDate.plusDays(days)) | ||
} | ||
|
||
fun Arb.Companion.dateSet(): Arb<DateSet> = | ||
Arb.list(Arb.finiteDateRange()).map { ranges -> DateSet.of(ranges) } | ||
|
||
fun Arb.Companion.helsinkiDateTime( | ||
min: HelsinkiDateTime = HelsinkiDateTime.of(LocalDateTime.of(2019, 1, 1, 12, 0)), | ||
max: HelsinkiDateTime = HelsinkiDateTime.of(LocalDateTime.of(2030, 12, 31, 12, 0)), | ||
): Arb<HelsinkiDateTime> = | ||
Arb.long(min = 0, max = Duration.between(min.toInstant(), max.toInstant()).seconds).map { | ||
secondsSinceMin -> | ||
min.plusSeconds(secondsSinceMin) | ||
} | ||
|
||
fun Arb.Companion.helsinkiDateTimeRange( | ||
start: Arb<HelsinkiDateTime> = Arb.helsinkiDateTime(), | ||
durationSeconds: Arb<Long> = Arb.positiveLong(max = 48L * 60L * 60L), | ||
): Arb<HelsinkiDateTimeRange> = | ||
Arb.bind(start, durationSeconds) { startTimestamp, seconds -> | ||
HelsinkiDateTimeRange(startTimestamp, startTimestamp.plusSeconds(seconds)) | ||
} | ||
|
||
fun Arb.Companion.dateTimeSet(): Arb<DateTimeSet> = | ||
Arb.list(Arb.helsinkiDateTimeRange()).map { ranges -> DateTimeSet.of(ranges) } |
50 changes: 50 additions & 0 deletions
50
service/src/test/kotlin/fi/espoo/evaka/shared/data/DateSetPropertyTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// SPDX-FileCopyrightText: 2017-2024 City of Espoo | ||
// | ||
// SPDX-License-Identifier: LGPL-2.1-or-later | ||
|
||
package fi.espoo.evaka.shared.data | ||
|
||
import fi.espoo.evaka.shared.dateSet | ||
import fi.espoo.evaka.shared.domain.FiniteDateRange | ||
import fi.espoo.evaka.shared.finiteDateRange | ||
import io.kotest.common.runBlocking | ||
import io.kotest.property.Arb | ||
import io.kotest.property.arbitrary.list | ||
import io.kotest.property.arbitrary.positiveLong | ||
import io.kotest.property.checkAll | ||
import java.time.LocalDate | ||
import kotlin.test.Test | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertTrue | ||
|
||
class DateSetPropertyTest : RangeBasedSetPropertyTest<LocalDate, FiniteDateRange, DateSet>() { | ||
@Test | ||
fun `it includes every date of every range added to it`() { | ||
runBlocking { | ||
checkAll(Arb.list(Arb.finiteDateRange(durationDays = Arb.positiveLong(max = 10)))) { | ||
ranges -> | ||
val set = emptySet().addAll(ranges) | ||
assertTrue(set.ranges().flatMap { it.dates() }.all { set.includes(it) }) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `when a range is removed, it no longer includes any points of the range`() { | ||
runBlocking { | ||
checkAll( | ||
Arb.list(arbitraryRange()), | ||
Arb.finiteDateRange(durationDays = Arb.positiveLong(max = 10L)), | ||
) { otherRanges, range -> | ||
val set = DateSet.of(range).addAll(otherRanges).remove(range) | ||
assertFalse(range.dates().any { set.includes(it) }) | ||
} | ||
} | ||
} | ||
|
||
override fun emptySet(): DateSet = DateSet.empty() | ||
|
||
override fun arbitrarySet(): Arb<DateSet> = Arb.dateSet() | ||
|
||
override fun arbitraryRange(): Arb<FiniteDateRange> = Arb.finiteDateRange() | ||
} |
21 changes: 21 additions & 0 deletions
21
service/src/test/kotlin/fi/espoo/evaka/shared/data/DateTimeSetPropertyTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// SPDX-FileCopyrightText: 2017-2024 City of Espoo | ||
// | ||
// SPDX-License-Identifier: LGPL-2.1-or-later | ||
|
||
package fi.espoo.evaka.shared.data | ||
|
||
import fi.espoo.evaka.shared.dateTimeSet | ||
import fi.espoo.evaka.shared.domain.HelsinkiDateTime | ||
import fi.espoo.evaka.shared.domain.HelsinkiDateTimeRange | ||
import fi.espoo.evaka.shared.helsinkiDateTimeRange | ||
import io.kotest.property.Arb | ||
|
||
class DateTimeSetPropertyTest : | ||
RangeBasedSetPropertyTest<HelsinkiDateTime, HelsinkiDateTimeRange, DateTimeSet>() { | ||
|
||
override fun emptySet(): DateTimeSet = DateTimeSet.empty() | ||
|
||
override fun arbitrarySet(): Arb<DateTimeSet> = Arb.dateTimeSet() | ||
|
||
override fun arbitraryRange(): Arb<HelsinkiDateTimeRange> = Arb.helsinkiDateTimeRange() | ||
} |
Oops, something went wrong.