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
Fixed review issues
  • Loading branch information
Damtev committed Sep 28, 2022
commit ff79a92b74923b808a6694710296f139f5c8a99f
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ static java.util.stream.IntStream generate(IntSupplier s) {
}

static java.util.stream.IntStream range(int startInclusive, int endExclusive) {
if (startInclusive >= endExclusive) {
return new UtIntStream();
}

int size = endExclusive - startInclusive;
Integer[] data = new Integer[size];
Comment thread
CaelmBleidd marked this conversation as resolved.
for (int i = startInclusive; i < endExclusive; i++) {
Expand All @@ -48,7 +52,18 @@ static java.util.stream.IntStream range(int startInclusive, int endExclusive) {

@SuppressWarnings("unused")
static java.util.stream.IntStream rangeClosed(int startInclusive, int endInclusive) {
return range(startInclusive, endInclusive + 1);
if (startInclusive > endInclusive) {
return new UtIntStream();
}

// Do not use `range` above to prevent overflow
int size = endInclusive - startInclusive + 1;
Integer[] data = new Integer[size];
for (int i = startInclusive; i <= endInclusive; i++) {
data[i - startInclusive] = i;
}

return new UtIntStream(data, size);
}

@SuppressWarnings("unused")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ static java.util.stream.LongStream generate(LongSupplier s) {
}

static java.util.stream.LongStream range(long startInclusive, long endExclusive) {
if (startInclusive >= endExclusive) {
return new UtLongStream();
}

int start = (int) startInclusive;
int end = (int) endExclusive;

Expand All @@ -58,7 +62,26 @@ static java.util.stream.LongStream range(long startInclusive, long endExclusive)

@SuppressWarnings("unused")
static java.util.stream.LongStream rangeClosed(long startInclusive, long endInclusive) {
return range(startInclusive, endInclusive + 1);
if (startInclusive > endInclusive) {
return new UtLongStream();
}

// Do not use `range` above to prevent overflow
int start = (int) startInclusive;
int end = (int) endInclusive;

// check that borders fit in int range
UtMock.assumeOrExecuteConcretely(start == startInclusive);
UtMock.assumeOrExecuteConcretely(end == endInclusive);

int size = end - start + 1;

Long[] data = new Long[size];
for (int i = start; i <= end; i++) {
data[i - start] = (long) i;
}

return new UtLongStream(data, size);
}

@SuppressWarnings("unused")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ void preconditionCheck() {

assume(elementData.end >= 0);
// we can create a stream for an array using Stream.of
assume(elementData.end <= HARD_MAX_ARRAY_SIZE);
assumeOrExecuteConcretely(elementData.end <= HARD_MAX_ARRAY_SIZE);

// As real primitive streams contain primitives, we cannot accept nulls.
for (int i = 0; i < elementData.end; i++) {
assume(elementData.get(i) != null);
}

visit(this);
}
Expand Down Expand Up @@ -290,17 +295,13 @@ public DoubleStream skip(long n) {
}

int curSize = elementData.end;
if (n > curSize) {
if (n >= curSize) {
return new UtDoubleStream();
}

// n is 1...Integer.MAX_VALUE here
// n is 0...(Integer.MAX_VALUE - 1) here
Comment thread
Damtev marked this conversation as resolved.
Outdated
int newSize = (int) (curSize - n);
Comment thread
CaelmBleidd marked this conversation as resolved.

if (newSize == 0) {
return new UtDoubleStream();
}

Double[] elements = elementData.toCastedArray((int) n, newSize);

return new UtDoubleStream(elements, newSize);
Expand Down Expand Up @@ -353,17 +354,13 @@ public OptionalDouble reduce(DoubleBinaryOperator op) {
return OptionalDouble.empty();
}

Double result = null;
for (int i = 0; i < size; i++) {
double result = elementData.get(0);
for (int i = 1; i < size; i++) {
double element = elementData.get(i);
if (result == null) {
result = element;
} else {
result = op.applyAsDouble(result, element);
}
result = op.applyAsDouble(result, element);
}

return result == null ? OptionalDouble.empty() : OptionalDouble.of(result);
return OptionalDouble.of(result);
}

@Override
Expand Down Expand Up @@ -391,16 +388,38 @@ public double sum() {
}

double sum = 0;
boolean anyNaN = false;
boolean anyPositiveInfinity = false;
boolean anyNegativeInfinity = false;

for (int i = 0; i < size; i++) {
double element = elementData.get(i);
sum += element;

anyNaN |= Double.isNaN(element);
anyPositiveInfinity |= element == Double.POSITIVE_INFINITY;
anyNegativeInfinity |= element == Double.NEGATIVE_INFINITY;
}

if (anyNaN) {
return Double.NaN;
}

if (anyPositiveInfinity && anyNegativeInfinity) {
return Double.NaN;
}

if (anyPositiveInfinity && sum == Double.NEGATIVE_INFINITY) {
return Double.NaN;
}

if (anyNegativeInfinity && sum == Double.POSITIVE_INFINITY) {
return Double.NaN;
}

return sum;
Comment thread
Damtev marked this conversation as resolved.
}

@SuppressWarnings("ManualMinMaxCalculation")
@Override
public OptionalDouble min() {
preconditionCheckWithClosingStream();
Expand All @@ -413,13 +432,12 @@ public OptionalDouble min() {
double min = elementData.get(0);
for (int i = 1; i < size; i++) {
final double element = elementData.get(i);
min = (element < min) ? element : min;
min = Math.min(element, min);
}

return OptionalDouble.of(min);
}

@SuppressWarnings("ManualMinMaxCalculation")
@Override
public OptionalDouble max() {
preconditionCheckWithClosingStream();
Expand All @@ -432,7 +450,7 @@ public OptionalDouble max() {
double max = elementData.get(0);
for (int i = 1; i < size; i++) {
final double element = elementData.get(i);
max = (element > max) ? element : max;
max = Math.max(element, max);
}

return OptionalDouble.of(max);
Expand Down Expand Up @@ -629,7 +647,7 @@ public void close() {
closeHandlers.get(i).run();
}

// clear handlers
// clear handlers (we do not need to manually clear all elements)
closeHandlers.end = 0;
Comment thread
CaelmBleidd marked this conversation as resolved.
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ void preconditionCheck() {

assume(elementData.end >= 0);
// we can create a stream for an array using Stream.of
assume(elementData.end <= HARD_MAX_ARRAY_SIZE);
assumeOrExecuteConcretely(elementData.end <= HARD_MAX_ARRAY_SIZE);

// As real primitive streams contain primitives, we cannot accept nulls.
for (int i = 0; i < elementData.end; i++) {
assume(elementData.get(i) != null);
}

visit(this);
}
Expand Down Expand Up @@ -291,17 +296,13 @@ public IntStream skip(long n) {
}

int curSize = elementData.end;
if (n > curSize) {
if (n >= curSize) {
return new UtIntStream();
}

// n is 1...Integer.MAX_VALUE here
// n is 0...(Integer.MAX_VALUE - 1) here
int newSize = (int) (curSize - n);

if (newSize == 0) {
return new UtIntStream();
}

Integer[] newData = elementData.toCastedArray((int) n, newSize);

return new UtIntStream(newData, newSize);
Expand Down Expand Up @@ -355,17 +356,13 @@ public OptionalInt reduce(IntBinaryOperator op) {
return OptionalInt.empty();
}

Integer result = null;
for (int i = 0; i < size; i++) {
int result = elementData.get(0);
for (int i = 1; i < size; i++) {
int element = elementData.get(i);
if (result == null) {
result = element;
} else {
result = op.applyAsInt(result, element);
}
result = op.applyAsInt(result, element);
}

return result == null ? OptionalInt.empty() : OptionalInt.of(result);
return OptionalInt.of(result);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ void preconditionCheck() {

assume(elementData.end >= 0);
// we can create a stream for an array using Stream.of
assume(elementData.end <= HARD_MAX_ARRAY_SIZE);
assumeOrExecuteConcretely(elementData.end <= HARD_MAX_ARRAY_SIZE);

// As real primitive streams contain primitives, we cannot accept nulls.
for (int i = 0; i < elementData.end; i++) {
assume(elementData.get(i) != null);
}

visit(this);
}
Expand Down Expand Up @@ -291,17 +296,13 @@ public LongStream skip(long n) {
}

int curSize = elementData.end;
if (n > curSize) {
if (n >= curSize) {
return new UtLongStream();
}

// n is 1...Integer.MAX_VALUE here
// n is 0...(Integer.MAX_VALUE - 1) here
int newSize = (int) (curSize - n);

if (newSize == 0) {
return new UtLongStream();
}

Long[] elements = elementData.toCastedArray((int) n, newSize);

return new UtLongStream(elements, newSize);
Expand Down Expand Up @@ -354,17 +355,13 @@ public OptionalLong reduce(LongBinaryOperator op) {
return OptionalLong.empty();
}

Long result = null;
for (int i = 0; i < size; i++) {
long result = elementData.get(0);
for (int i = 1; i < size; i++) {
long element = elementData.get(i);
if (result == null) {
result = element;
} else {
result = op.applyAsLong(result, element);
}
result = op.applyAsLong(result, element);
}

return result == null ? OptionalLong.empty() : OptionalLong.of(result);
return OptionalLong.of(result);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.utbot.engine.overrides.stream;

import org.utbot.api.mock.UtMock;
import org.jetbrains.annotations.NotNull;
Comment thread
Damtev marked this conversation as resolved.
import org.utbot.engine.overrides.UtArrayMock;
import org.utbot.engine.overrides.collections.RangeModifiableUnlimitedArray;
import org.utbot.engine.overrides.collections.UtGenericStorage;
Expand All @@ -27,7 +27,6 @@
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
import org.jetbrains.annotations.NotNull;

import static org.utbot.api.mock.UtMock.assume;
import static org.utbot.api.mock.UtMock.assumeOrExecuteConcretely;
Expand Down Expand Up @@ -143,50 +142,40 @@ public <R> Stream<R> map(Function<? super E, ? extends R> mapper) {
return new UtStream<>((R[]) mapped, size);
}

@SuppressWarnings({"unchecked", "CastCanBeRemovedNarrowingVariableType"})
@Override
public IntStream mapToInt(ToIntFunction<? super E> mapper) {
preconditionCheckWithClosingStream();

int size = elementData.end;
Integer[] data = new Integer[size];
for (int i = 0; i < size; i++) {
final Object object = elementData.get(i);
UtMock.disableClassCastExceptionCheck(object);
data[i] = mapper.applyAsInt((E) object);
data[i] = mapper.applyAsInt(elementData.get(i));
Comment thread
Damtev marked this conversation as resolved.
Outdated
}

return new UtIntStream(data, size);
}

@SuppressWarnings({"unchecked", "CastCanBeRemovedNarrowingVariableType"})
@Override
public LongStream mapToLong(ToLongFunction<? super E> mapper) {
preconditionCheckWithClosingStream();

int size = elementData.end;
Long[] data = new Long[size];
for (int i = 0; i < size; i++) {
final Object object = elementData.get(i);
UtMock.disableClassCastExceptionCheck(object);
data[i] = mapper.applyAsLong((E) object);
data[i] = mapper.applyAsLong(elementData.get(i));
}

return new UtLongStream(data, size);
}

@SuppressWarnings({"unchecked", "CastCanBeRemovedNarrowingVariableType"})
@Override
public DoubleStream mapToDouble(ToDoubleFunction<? super E> mapper) {
preconditionCheckWithClosingStream();

int size = elementData.end;
Double[] data = new Double[size];
assume(data.length == elementData.end);
for (int i = 0; i < size; i++) {
final Object object = elementData.get(i);
UtMock.disableClassCastExceptionCheck(object);
data[i] = mapper.applyAsDouble((E) object);
data[i] = mapper.applyAsDouble(elementData.get(i));
}

return new UtDoubleStream(data, size);
Expand Down
Loading