Skip to content

Commit 04980dd

Browse files
committed
write progression overflow tests
1 parent bb8efa5 commit 04980dd

3 files changed

Lines changed: 58 additions & 4 deletions

File tree

src/main/kotlin/com/tegonal/variist/generators/arbFromProgression.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ fun ArbExtensionPoint.fromProgression(progression: IntProgression): ArbArgsGener
3939
//TODO 2.1.0 bench at what point it makes sense to calculate it, I just guess that for small progressions
4040
// storing it in a list is more efficient and memory is neglectable. But maybe this can be increased to x
4141
// or should be decreased to y
42-
if (numberOfSteps < 50) fromList(progression.toList())
42+
if (numberOfSteps < 50L) fromList(progression.toList())
4343
else {
4444
// check that toInt() did not overflow
4545
if (numberOfSteps == numberOfStepsI.toLong()) {
@@ -66,7 +66,7 @@ fun ArbExtensionPoint.fromProgression(progression: LongProgression): ArbArgsGene
6666
-1L -> longFromTo(progression.last, progression.first)
6767
else -> {
6868
val (start, endInclusive) =
69-
if (step > 0) Tuple(progression.first, progression.last)
69+
if (step > 0L) Tuple(progression.first, progression.last)
7070
else Tuple(progression.last, progression.first)
7171
val stepAbs = abs(step)
7272
val diff = endInclusive.toBigInt() - start.toBigInt()

src/test/kotlin/com/tegonal/variist/generators/ArbFromProgressionTest.kt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package com.tegonal.variist.generators
22

3+
import ch.tutteli.atrium.api.fluent.en_GB.toBeGreaterThanOrEqualTo
4+
import ch.tutteli.atrium.api.verbs.expect
35
import ch.tutteli.kbox.Tuple
6+
import com.tegonal.variist.providers.ArgsSource
7+
import com.tegonal.variist.providers.ArgsSourceOptions
8+
import org.junit.jupiter.params.ParameterizedTest
49

510
class ArbFromProgressionTest : AbstractArbArgsGeneratorTest<Any>() {
611

@@ -10,5 +15,19 @@ class ArbFromProgressionTest : AbstractArbArgsGeneratorTest<Any>() {
1015
Tuple("fromLongProgression", modifiedArb.fromProgression(1L..3L step 1), listOf(1L, 2L, 3L)),
1116
)
1217

13-
//TODO 2.0.0 add tests which cover the case that numberOfSteps is out of Int/Long range (i.e. would overflow)
18+
@ParameterizedTest
19+
@ArgsSource("arbIntNegative")
20+
@ArgsSourceOptions(maxArgs = 1)
21+
fun intProgression_numberOfSteps_overflow_Int__still_works(from: Int) {
22+
expect(arb.fromProgression(from..Int.MAX_VALUE step 1).generate().first())
23+
.toBeGreaterThanOrEqualTo(from)
24+
}
25+
26+
@ParameterizedTest
27+
@ArgsSource("arbLongNegative")
28+
@ArgsSourceOptions(maxArgs = 1)
29+
fun longProgression_numberOfSteps_overflow_Long__still_works(from: Long) {
30+
expect(arb.fromProgression(from..Long.MAX_VALUE step 1).generate().first())
31+
.toBeGreaterThanOrEqualTo(from)
32+
}
1433
}

src/test/kotlin/com/tegonal/variist/generators/OrderedFromProgressionTest.kt

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package com.tegonal.variist.generators
22

3+
import ch.tutteli.atrium.api.fluent.en_GB.messageToContain
34
import ch.tutteli.atrium.api.fluent.en_GB.toEqual
5+
import ch.tutteli.atrium.api.fluent.en_GB.toThrow
46
import ch.tutteli.atrium.api.verbs.expect
57
import ch.tutteli.kbox.Tuple
68
import com.tegonal.variist.providers.ArgsRange
79
import com.tegonal.variist.providers.ArgsSource
10+
import com.tegonal.variist.providers.ArgsSourceOptions
811
import com.tegonal.variist.utils.repeatForever
12+
import org.junit.jupiter.api.Test
913
import org.junit.jupiter.params.ParameterizedTest
1014

1115
class OrderedFromProgressionTest : AbstractOrderedArgsGeneratorTest<Any>() {
@@ -34,7 +38,38 @@ class OrderedFromProgressionTest : AbstractOrderedArgsGeneratorTest<Any>() {
3438
)
3539
}
3640

37-
//TODO 2.0.0 write test for intProgression and longProgression, especially for cases where the range size overflows Int/Long Domain
41+
@ParameterizedTest
42+
@ArgsSource("arbIntNegative")
43+
@ArgsSourceOptions(maxArgs = 1)
44+
fun intProgression_numberOfSteps_overflow_Int__throws(from: Int) {
45+
expect {
46+
ordered.fromProgression(from..Int.MAX_VALUE step 1)
47+
}.toThrow<IllegalStateException> {
48+
messageToContain("OrderedArgsGenerator.size only supports Int")
49+
}
50+
}
51+
52+
@ParameterizedTest
53+
@ArgsSource("arbIntNegative")
54+
@ArgsSourceOptions(maxArgs = 1)
55+
fun longProgression_numberOfSteps_overflow_Int__throws(from: Int) {
56+
expect {
57+
ordered.fromProgression(from.toLong()..Int.MAX_VALUE.toLong() step 1)
58+
}.toThrow<IllegalStateException> {
59+
messageToContain("OrderedArgsGenerator.size only supports Int")
60+
}
61+
}
62+
63+
@ParameterizedTest
64+
@ArgsSource("arbLongNegative")
65+
@ArgsSourceOptions(maxArgs = 1)
66+
fun longProgression_numberOfSteps_overflow_Long__throws(from: Long) {
67+
expect {
68+
ordered.fromProgression(from..Long.MAX_VALUE step 1)
69+
}.toThrow<IllegalStateException> {
70+
messageToContain("OrderedArgsGenerator.size only supports Int")
71+
}
72+
}
3873

3974
companion object {
4075
@JvmStatic

0 commit comments

Comments
 (0)