Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Used a specific exception for intermediate modifiable operations in s…
…treams
  • Loading branch information
Damtev committed Oct 28, 2022
commit baded68500a0e6cc174f18f78679dff8fa716377
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,17 @@ class TimeoutException(s: String) : Exception(s)
data class UtTimeoutException(override val exception: TimeoutException) : UtExecutionFailure()

/**
* Represents an exception that occurs during consuming a stream. Stores it in [innerException].
* Represents an exception that occurs during consuming a stream.
* [innerException] stores original exception (if possible), null if [UtStreamConsumingException] was constructed by the engine.
*/
data class UtStreamConsumingException(val innerException: Exception) : RuntimeException() {
override fun toString(): String = innerException.toString()
data class UtStreamConsumingException(private val innerException: Exception?) : RuntimeException() {
/**
* Returns the original exception [innerException] if possible, and any [RuntimeException] otherwise.
*/
val innerExceptionOrAny: Throwable
get() = innerException ?: RuntimeException("Unknown runtime exception during consuming stream")

override fun toString(): String = innerExceptionOrAny.toString()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.utbot.testcheckers.eq
import org.utbot.tests.infrastructure.CodeGeneration
import org.utbot.tests.infrastructure.FullWithAssumptions
import org.utbot.tests.infrastructure.UtValueTestCaseChecker
import org.utbot.tests.infrastructure.isException
import kotlin.streams.toList

// TODO 1 instruction is always uncovered https://github.com/UnitTestBot/UTBotJava/issues/193
Expand Down Expand Up @@ -39,7 +40,7 @@ class StreamsAsMethodResultExampleTest : UtValueTestCaseChecker(
eq(3),
{ c, r -> c.isEmpty() && c == r.getOrThrow().toList() },
{ c, r -> c.isNotEmpty() && c.none { it == null } && c.toIntArray().contentEquals(r.getOrThrow().toArray()) },
{ c, r -> c.isNotEmpty() && c.any { it == null } && r.isNPE() },
{ c, r -> c.isNotEmpty() && c.any { it == null } && r.isException<UtStreamConsumingException>() },
coverage = FullWithAssumptions(assumeCallsNumber = 2)
)
}
Expand All @@ -51,7 +52,7 @@ class StreamsAsMethodResultExampleTest : UtValueTestCaseChecker(
eq(3),
{ c, r -> c.isEmpty() && c == r.getOrThrow().toList() },
{ c, r -> c.isNotEmpty() && c.none { it == null } && c.map { it.toLong() }.toLongArray().contentEquals(r.getOrThrow().toArray()) },
{ c, r -> c.isNotEmpty() && c.any { it == null } && r.isNPE() },
{ c, r -> c.isNotEmpty() && c.any { it == null } && r.isException<UtStreamConsumingException>() },
coverage = FullWithAssumptions(assumeCallsNumber = 2)
)
}
Expand All @@ -63,21 +64,8 @@ class StreamsAsMethodResultExampleTest : UtValueTestCaseChecker(
eq(3),
{ c, r -> c.isEmpty() && c == r.getOrThrow().toList() },
{ c, r -> c.isNotEmpty() && c.none { it == null } && c.map { it.toDouble() }.toDoubleArray().contentEquals(r.getOrThrow().toArray()) },
{ c, r -> c.isNotEmpty() && c.any { it == null } && r.isNPE() },
{ c, r -> c.isNotEmpty() && c.any { it == null } && r.isException<UtStreamConsumingException>() },
coverage = FullWithAssumptions(assumeCallsNumber = 2)
)
}

/**
* Checks the result in [NullPointerException] from the engine or
* [UtStreamConsumingException] with [NullPointerException] from concrete execution.
*/
private fun Result<*>.isNPE(): Boolean =
exceptionOrNull()?.let {
if (it is UtStreamConsumingException) {
return@let it.innerException is NullPointerException
}

return@let it is NullPointerException
} ?: false
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.jetbrains.annotations.NotNull;
import org.utbot.engine.overrides.collections.RangeModifiableUnlimitedArray;
import org.utbot.engine.overrides.collections.UtGenericStorage;
import org.utbot.framework.plugin.api.*;

import java.util.DoubleSummaryStatistics;
import java.util.NoSuchElementException;
Expand Down Expand Up @@ -122,8 +123,13 @@ public DoubleStream filter(DoublePredicate predicate) {
int j = 0;
for (int i = 0; i < size; i++) {
double element = elementData.get(i);
if (predicate.test(element)) {
filtered[j++] = element;

try {
if (predicate.test(element)) {
filtered[j++] = element;
}
} catch (Exception e) {
throw new UtStreamConsumingException(e);
}
}

Expand All @@ -137,7 +143,11 @@ public DoubleStream map(DoubleUnaryOperator mapper) {
int size = elementData.end;
Double[] mapped = new Double[size];
for (int i = 0; i < size; i++) {
mapped[i] = mapper.applyAsDouble(elementData.get(i));
try {
mapped[i] = mapper.applyAsDouble(elementData.get(i));
} catch (Exception e) {
throw new UtStreamConsumingException(e);
}
}

return new UtDoubleStream(mapped, size);
Expand All @@ -153,7 +163,11 @@ public <U> Stream<U> mapToObj(DoubleFunction<? extends U> mapper) {
int size = elementData.end;
Object[] mapped = new Object[size];
for (int i = 0; i < size; i++) {
mapped[i] = mapper.apply(elementData.get(i));
try {
mapped[i] = mapper.apply(elementData.get(i));
} catch (Exception e) {
throw new UtStreamConsumingException(e);
}
}

return new UtStream<>((U[]) mapped, size);
Expand All @@ -166,7 +180,11 @@ public IntStream mapToInt(DoubleToIntFunction mapper) {
int size = elementData.end;
Integer[] mapped = new Integer[size];
for (int i = 0; i < size; i++) {
mapped[i] = mapper.applyAsInt(elementData.get(i));
try {
mapped[i] = mapper.applyAsInt(elementData.get(i));
} catch (Exception e) {
throw new UtStreamConsumingException(e);
}
}

return new UtIntStream(mapped, size);
Expand All @@ -179,7 +197,11 @@ public LongStream mapToLong(DoubleToLongFunction mapper) {
int size = elementData.end;
Long[] mapped = new Long[size];
for (int i = 0; i < size; i++) {
mapped[i] = mapper.applyAsLong(elementData.get(i));
try {
mapped[i] = mapper.applyAsLong(elementData.get(i));
} catch (Exception e) {
throw new UtStreamConsumingException(e);
}
}

return new UtLongStream(mapped, size);
Expand Down Expand Up @@ -256,7 +278,11 @@ public DoubleStream peek(DoubleConsumer action) {

int size = elementData.end;
for (int i = 0; i < size; i++) {
action.accept(elementData.get(i));
try {
action.accept(elementData.get(i));
} catch (Exception e) {
throw new UtStreamConsumingException(e);
}
}

// returned stream should be opened, so we "reopen" this stream to return it
Expand Down Expand Up @@ -315,13 +341,16 @@ public DoubleStream skip(long n) {
@SuppressWarnings("ResultOfMethodCallIgnored")
@Override
public void forEach(DoubleConsumer action) {
peek(action);
try {
peek(action);
} catch (UtStreamConsumingException e) {
// Since this is a terminal operation, we should throw an original exception
}
}

@SuppressWarnings("ResultOfMethodCallIgnored")
@Override
public void forEachOrdered(DoubleConsumer action) {
peek(action);
forEach(action);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.jetbrains.annotations.NotNull;
import org.utbot.engine.overrides.collections.RangeModifiableUnlimitedArray;
import org.utbot.engine.overrides.collections.UtGenericStorage;
import org.utbot.framework.plugin.api.*;

import java.util.IntSummaryStatistics;
import java.util.NoSuchElementException;
Expand Down Expand Up @@ -123,8 +124,13 @@ public IntStream filter(IntPredicate predicate) {
int j = 0;
for (int i = 0; i < size; i++) {
int element = elementData.get(i);
if (predicate.test(element)) {
filtered[j++] = element;

try {
if (predicate.test(element)) {
filtered[j++] = element;
}
} catch (Exception e) {
throw new UtStreamConsumingException(e);
}
}

Expand All @@ -138,7 +144,11 @@ public IntStream map(IntUnaryOperator mapper) {
int size = elementData.end;
Integer[] mapped = new Integer[size];
for (int i = 0; i < size; i++) {
mapped[i] = mapper.applyAsInt(elementData.get(i));
try {
mapped[i] = mapper.applyAsInt(elementData.get(i));
} catch (Exception e) {
throw new UtStreamConsumingException(e);
}
}

return new UtIntStream(mapped, size);
Expand All @@ -154,7 +164,11 @@ public <U> Stream<U> mapToObj(IntFunction<? extends U> mapper) {
int size = elementData.end;
U[] mapped = (U[]) new Object[size];
for (int i = 0; i < size; i++) {
mapped[i] = mapper.apply(elementData.get(i));
try {
mapped[i] = mapper.apply(elementData.get(i));
} catch (Exception e) {
throw new UtStreamConsumingException(e);
}
}

return new UtStream<>(mapped, size);
Expand All @@ -167,7 +181,11 @@ public LongStream mapToLong(IntToLongFunction mapper) {
int size = elementData.end;
Long[] mapped = new Long[size];
for (int i = 0; i < size; i++) {
mapped[i] = mapper.applyAsLong(elementData.get(i));
try {
mapped[i] = mapper.applyAsLong(elementData.get(i));
} catch (Exception e) {
throw new UtStreamConsumingException(e);
}
}

return new UtLongStream(mapped, size);
Expand All @@ -180,7 +198,11 @@ public DoubleStream mapToDouble(IntToDoubleFunction mapper) {
int size = elementData.end;
Double[] mapped = new Double[size];
for (int i = 0; i < size; i++) {
mapped[i] = mapper.applyAsDouble(elementData.get(i));
try {
mapped[i] = mapper.applyAsDouble(elementData.get(i));
} catch (Exception e) {
throw new UtStreamConsumingException(e);
}
}

return new UtDoubleStream(mapped, size);
Expand Down Expand Up @@ -257,7 +279,11 @@ public IntStream peek(IntConsumer action) {

int size = elementData.end;
for (int i = 0; i < size; i++) {
action.accept(elementData.get(i));
try {
action.accept(elementData.get(i));
} catch (Exception e) {
throw new UtStreamConsumingException(e);
}
}

// returned stream should be opened, so we "reopen" this stream to return it
Expand Down Expand Up @@ -316,13 +342,16 @@ public IntStream skip(long n) {
@SuppressWarnings("ResultOfMethodCallIgnored")
@Override
public void forEach(IntConsumer action) {
peek(action);
try {
peek(action);
} catch (UtStreamConsumingException e) {
// Since this is a terminal operation, we should throw an original exception
}
}

@SuppressWarnings("ResultOfMethodCallIgnored")
@Override
public void forEachOrdered(IntConsumer action) {
peek(action);
forEach(action);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.jetbrains.annotations.NotNull;
import org.utbot.engine.overrides.collections.RangeModifiableUnlimitedArray;
import org.utbot.engine.overrides.collections.UtGenericStorage;
import org.utbot.framework.plugin.api.*;

import java.util.LongSummaryStatistics;
import java.util.NoSuchElementException;
Expand Down Expand Up @@ -123,8 +124,13 @@ public LongStream filter(LongPredicate predicate) {
int j = 0;
for (int i = 0; i < size; i++) {
long element = elementData.get(i);
if (predicate.test(element)) {
filtered[j++] = element;

try {
if (predicate.test(element)) {
filtered[j++] = element;
}
} catch (Exception e) {
throw new UtStreamConsumingException(e);
}
}

Expand All @@ -138,7 +144,11 @@ public LongStream map(LongUnaryOperator mapper) {
int size = elementData.end;
Long[] mapped = new Long[size];
for (int i = 0; i < size; i++) {
mapped[i] = mapper.applyAsLong(elementData.get(i));
try {
mapped[i] = mapper.applyAsLong(elementData.get(i));
} catch (Exception e) {
throw new UtStreamConsumingException(e);
}
}

return new UtLongStream(mapped, size);
Expand All @@ -154,7 +164,11 @@ public <U> Stream<U> mapToObj(LongFunction<? extends U> mapper) {
int size = elementData.end;
Object[] mapped = new Object[size];
for (int i = 0; i < size; i++) {
mapped[i] = mapper.apply(elementData.get(i));
try {
mapped[i] = mapper.apply(elementData.get(i));
} catch (Exception e) {
throw new UtStreamConsumingException(e);
}
}

return new UtStream<>((U[]) mapped, size);
Expand All @@ -167,7 +181,11 @@ public IntStream mapToInt(LongToIntFunction mapper) {
int size = elementData.end;
Integer[] mapped = new Integer[size];
for (int i = 0; i < size; i++) {
mapped[i] = mapper.applyAsInt(elementData.get(i));
try {
mapped[i] = mapper.applyAsInt(elementData.get(i));
} catch (Exception e) {
throw new UtStreamConsumingException(e);
}
}

return new UtIntStream(mapped, size);
Expand All @@ -180,7 +198,11 @@ public DoubleStream mapToDouble(LongToDoubleFunction mapper) {
int size = elementData.end;
Double[] mapped = new Double[size];
for (int i = 0; i < size; i++) {
mapped[i] = mapper.applyAsDouble(elementData.get(i));
try {
mapped[i] = mapper.applyAsDouble(elementData.get(i));
} catch (Exception e) {
throw new UtStreamConsumingException(e);
}
}

return new UtDoubleStream(mapped, size);
Expand Down Expand Up @@ -257,7 +279,11 @@ public LongStream peek(LongConsumer action) {

int size = elementData.end;
for (int i = 0; i < size; i++) {
action.accept(elementData.get(i));
try {
action.accept(elementData.get(i));
} catch (Exception e) {
throw new UtStreamConsumingException(e);
}
}

// returned stream should be opened, so we "reopen" this stream to return it
Expand Down Expand Up @@ -316,13 +342,16 @@ public LongStream skip(long n) {
@SuppressWarnings("ResultOfMethodCallIgnored")
@Override
public void forEach(LongConsumer action) {
peek(action);
try {
peek(action);
} catch (UtStreamConsumingException e) {
// Since this is a terminal operation, we should throw an original exception
}
}

@SuppressWarnings("ResultOfMethodCallIgnored")
@Override
public void forEachOrdered(LongConsumer action) {
peek(action);
forEach(action);
}

@Override
Expand Down
Loading