Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Primitive Stream wrappers (no laziness and source mutations support) #871

Merged
merged 19 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions utbot-api/src/main/java/org/utbot/api/mock/UtMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ public static void assumeOrExecuteConcretely(boolean predicate) {
// In oppose to assume, we don't have predicate check here
// to avoid RuntimeException during concrete execution
}

@SuppressWarnings("unused")
public static void disableClassCastExceptionCheck(Object object) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,13 @@ inline fun <reified T> withoutSandbox(block: () -> T): T {
UtSettings.useSandbox = prev
}
}

inline fun <reified T> withPathSelectorStepsLimit(stepsLimit: Int, block: () -> T): T {
val prev = UtSettings.pathSelectorStepsLimit
UtSettings.pathSelectorStepsLimit = stepsLimit
try {
return block()
} finally {
UtSettings.pathSelectorStepsLimit = prev
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.utbot.tests.infrastructure.isException
import org.utbot.framework.plugin.api.CodegenLanguage
import org.utbot.testcheckers.eq
import org.utbot.testcheckers.withoutConcrete
import org.utbot.tests.infrastructure.AtLeast
import org.utbot.tests.infrastructure.CodeGeneration
import java.util.Optional
import java.util.stream.Stream
Expand Down Expand Up @@ -69,10 +70,46 @@ class BaseStreamExampleTest : UtValueTestCaseChecker(
fun testMapExample() {
checkWithException(
BaseStreamExample::mapExample,
eq(2),
ignoreExecutionsNumber,
{ c, r -> null in c && r.isException<NullPointerException>() },
{ c, r -> r.getOrThrow().contentEquals(c.map { it * 2 }.toTypedArray()) },
coverage = DoNotCalculate
coverage = AtLeast(90)
)
}

@Test
@Tag("slow")
fun testMapToIntExample() {
checkWithException(
BaseStreamExample::mapToIntExample,
ignoreExecutionsNumber,
{ c, r -> null in c && r.isException<NullPointerException>() },
{ c, r -> r.getOrThrow().contentEquals(c.map { it.toInt() }.toIntArray()) },
coverage = AtLeast(90)
)
}

@Test
@Tag("slow")
fun testMapToLongExample() {
checkWithException(
BaseStreamExample::mapToLongExample,
ignoreExecutionsNumber,
{ c, r -> null in c && r.isException<NullPointerException>() },
{ c, r -> r.getOrThrow().contentEquals(c.map { it.toLong() }.toLongArray()) },
coverage = AtLeast(90)
)
}

@Test
@Tag("slow")
fun testMapToDoubleExample() {
checkWithException(
BaseStreamExample::mapToDoubleExample,
ignoreExecutionsNumber,
{ c, r -> null in c && r.isException<NullPointerException>() },
{ c, r -> r.getOrThrow().contentEquals(c.map { it.toDouble() }.toDoubleArray()) },
coverage = AtLeast(90)
)
}

Expand All @@ -86,6 +123,37 @@ class BaseStreamExampleTest : UtValueTestCaseChecker(
)
}

@Test
@Tag("slow")
fun testFlatMapToIntExample() {
check(
BaseStreamExample::flatMapToIntExample,
ignoreExecutionsNumber,
{ c, r -> r.contentEquals(c.flatMap { listOf(it?.toInt() ?: 0, it?.toInt() ?: 0) }.toIntArray()) },
coverage = FullWithAssumptions(assumeCallsNumber = 1)
)
}

@Test
fun testFlatMapToLongExample() {
check(
BaseStreamExample::flatMapToLongExample,
ignoreExecutionsNumber,
{ c, r -> r.contentEquals(c.flatMap { listOf(it?.toLong() ?: 0L, it?.toLong() ?: 0L) }.toLongArray()) },
coverage = FullWithAssumptions(assumeCallsNumber = 1)
)
}

@Test
fun testFlatMapToDoubleExample() {
check(
BaseStreamExample::flatMapToDoubleExample,
ignoreExecutionsNumber,
{ c, r -> r.contentEquals(c.flatMap { listOf(it?.toDouble() ?: 0.0, it?.toDouble() ?: 0.0) }.toDoubleArray()) },
coverage = FullWithAssumptions(assumeCallsNumber = 1)
)
}

@Test
@Tag("slow")
fun testDistinctExample() {
Expand Down Expand Up @@ -146,17 +214,17 @@ class BaseStreamExampleTest : UtValueTestCaseChecker(
fun testForEachExample() {
checkThisAndStaticsAfter(
BaseStreamExample::forEachExample,
eq(2),
ignoreExecutionsNumber,
*streamConsumerStaticsMatchers,
coverage = DoNotCalculate
coverage = AtLeast(92)
)
}

@Test
fun testToArrayExample() {
check(
BaseStreamExample::toArrayExample,
ignoreExecutionsNumber,
eq(2),
{ c, r -> c.toTypedArray().contentEquals(r) },
coverage = FullWithAssumptions(assumeCallsNumber = 1)
)
Expand Down Expand Up @@ -311,10 +379,11 @@ class BaseStreamExampleTest : UtValueTestCaseChecker(
fun testIteratorExample() {
checkWithException(
BaseStreamExample::iteratorSumExample,
eq(2),
ignoreExecutionsNumber,
{ c, r -> c.isEmpty() && r.getOrThrow() == 0 },
{ c, r -> null in c && r.isException<NullPointerException>() },
{ c, r -> null !in c && r.getOrThrow() == c.sum() },
coverage = DoNotCalculate
{ c, r -> c.isNotEmpty() && null !in c && r.getOrThrow() == c.sum() },
coverage = AtLeast(75)
)
}

Expand Down Expand Up @@ -393,15 +462,15 @@ class BaseStreamExampleTest : UtValueTestCaseChecker(
coverage = Full
)
}
}

private val streamConsumerStaticsMatchers = arrayOf(
{ _: BaseStreamExample, c: List<Int?>, _: StaticsType, _: Int? -> null in c },
{ _: BaseStreamExample, c: List<Int?>, statics: StaticsType, r: Int? ->
val x = statics.values.single().value as Int
internal val streamConsumerStaticsMatchers = arrayOf(
{ _: Any, c: List<Int?>, _: StaticsType, _: Int? -> null in c },
{ _: Any, c: List<Int?>, statics: StaticsType, r: Int? ->
val x = statics.values.single().value as Int

r!! + c.sumOf { it ?: 0 } == x
}
)
}
r!! + c.sumOf { it ?: 0 } == x
}
)

private fun <E : Comparable<E>> Sequence<E>.isSorted(): Boolean = zipWithNext { a, b -> a <= b }.all { it }
internal fun <E : Comparable<E>> Sequence<E>.isSorted(): Boolean = zipWithNext { a, b -> a <= b }.all { it }
Loading