Skip to content

Commit 0fdc611

Browse files
committed
Add more tests for 'until'
1 parent 2cd6566 commit 0fdc611

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

core/commonTest/src/InstantTest.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,29 @@ class InstantTest {
137137
assertEquals(0, instant6.minus(instant1, DateTimeUnit.DAY, zone))
138138
}
139139

140+
@OptIn(ExperimentalTime::class)
141+
@Test
142+
fun unitMultiplesUntil() {
143+
val unit1000days = DateTimeUnit.DAY * 1000
144+
val unit4years = DateTimeUnit.YEAR * 4 // longer than 1000-DAY
145+
146+
val zone = TimeZone.UTC
147+
val min = LocalDateTime.MIN.toInstant(zone)
148+
val max = LocalDateTime.MAX.toInstant(zone)
149+
val diffDays = min.until(max, unit1000days, zone)
150+
val diffYears = min.until(max, unit4years, zone)
151+
assertTrue(diffDays in 0..Int.MAX_VALUE, "difference in $unit1000days should fit in Int, was $diffDays")
152+
assertTrue(diffDays > diffYears, "difference in $unit1000days unit must be more than in $unit4years unit, was $diffDays $diffYears")
153+
154+
val unit500ns = DateTimeUnit.NANOSECOND * 500
155+
val start = Instant.parse("1700-01-01T00:00:00Z")
156+
val end = start.plus(300, DateTimeUnit.YEAR, zone)
157+
val diffNs = start.until(end, unit500ns, zone)
158+
val diffUs = start.until(end, DateTimeUnit.MICROSECOND, zone)
159+
// TODO: avoid clamping/overflowing in intermediate results
160+
// assertEquals(diffUs * 2, diffNs)
161+
}
162+
140163
@OptIn(ExperimentalTime::class)
141164
@Test
142165
fun instantOffset() {

core/commonTest/src/LocalDateTest.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class LocalDateTest {
101101

102102
// based on threetenbp test for until()
103103
@Test
104-
fun until() {
104+
fun unitsUntil() {
105105
val data = listOf(
106106
Pair(Pair("2012-06-30", "2012-06-30"), Pair(DateTimeUnit.DAY, 0)),
107107
Pair(Pair("2012-06-30", "2012-06-30"), Pair(DateTimeUnit.WEEK, 0)),
@@ -136,6 +136,18 @@ class LocalDateTest {
136136

137137
}
138138

139+
@Test
140+
fun unitMultiplesUntil() {
141+
val unit1000days = DateTimeUnit.DAY * 1000
142+
val unit4years = DateTimeUnit.YEAR * 4 // longer than 1000-DAY
143+
144+
val diffDays = LocalDate.MIN.until(LocalDate.MAX, unit1000days)
145+
val diffYears = LocalDate.MIN.until(LocalDate.MAX, unit4years)
146+
assertTrue(diffDays in 0..Int.MAX_VALUE, "difference in $unit1000days should fit in Int, was $diffDays")
147+
// TODO: make pass in JVM
148+
// assertTrue(diffDays > diffYears, "difference in $unit1000days unit must be more than in $unit4years unit, was $diffDays $diffYears")
149+
}
150+
139151
@Test
140152
fun constructInvalidDate() = checkInvalidDate(::LocalDate)
141153

0 commit comments

Comments
 (0)