Skip to content

Commit 4897f57

Browse files
committed
Fixed review issues
1 parent 5f65fff commit 4897f57

13 files changed

+138
-88
lines changed

utbot-framework/src/main/java/org/utbot/engine/overrides/stream/IntStream.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ static java.util.stream.IntStream generate(IntSupplier s) {
3737
}
3838

3939
static java.util.stream.IntStream range(int startInclusive, int endExclusive) {
40+
if (startInclusive >= endExclusive) {
41+
return new UtIntStream();
42+
}
43+
4044
int size = endExclusive - startInclusive;
4145
Integer[] data = new Integer[size];
4246
for (int i = startInclusive; i < endExclusive; i++) {
@@ -48,7 +52,18 @@ static java.util.stream.IntStream range(int startInclusive, int endExclusive) {
4852

4953
@SuppressWarnings("unused")
5054
static java.util.stream.IntStream rangeClosed(int startInclusive, int endInclusive) {
51-
return range(startInclusive, endInclusive + 1);
55+
if (startInclusive > endInclusive) {
56+
return new UtIntStream();
57+
}
58+
59+
// Do not use `range` above to prevent overflow
60+
int size = endInclusive - startInclusive + 1;
61+
Integer[] data = new Integer[size];
62+
for (int i = startInclusive; i <= endInclusive; i++) {
63+
data[i - startInclusive] = i;
64+
}
65+
66+
return new UtIntStream(data, size);
5267
}
5368

5469
@SuppressWarnings("unused")

utbot-framework/src/main/java/org/utbot/engine/overrides/stream/LongStream.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ static java.util.stream.LongStream generate(LongSupplier s) {
3939
}
4040

4141
static java.util.stream.LongStream range(long startInclusive, long endExclusive) {
42+
if (startInclusive >= endExclusive) {
43+
return new UtLongStream();
44+
}
45+
4246
int start = (int) startInclusive;
4347
int end = (int) endExclusive;
4448

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

5963
@SuppressWarnings("unused")
6064
static java.util.stream.LongStream rangeClosed(long startInclusive, long endInclusive) {
61-
return range(startInclusive, endInclusive + 1);
65+
if (startInclusive > endInclusive) {
66+
return new UtLongStream();
67+
}
68+
69+
// Do not use `range` above to prevent overflow
70+
int start = (int) startInclusive;
71+
int end = (int) endInclusive;
72+
73+
// check that borders fit in int range
74+
UtMock.assumeOrExecuteConcretely(start == startInclusive);
75+
UtMock.assumeOrExecuteConcretely(end == endInclusive);
76+
77+
int size = end - start + 1;
78+
79+
Long[] data = new Long[size];
80+
for (int i = start; i <= end; i++) {
81+
data[i - start] = (long) i;
82+
}
83+
84+
return new UtLongStream(data, size);
6285
}
6386

6487
@SuppressWarnings("unused")

utbot-framework/src/main/java/org/utbot/engine/overrides/stream/UtDoubleStream.java

+38-20
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,12 @@ void preconditionCheck() {
9090

9191
assume(elementData.end >= 0);
9292
// we can create a stream for an array using Stream.of
93-
assume(elementData.end <= HARD_MAX_ARRAY_SIZE);
93+
assumeOrExecuteConcretely(elementData.end <= HARD_MAX_ARRAY_SIZE);
94+
95+
// As real primitive streams contain primitives, we cannot accept nulls.
96+
for (int i = 0; i < elementData.end; i++) {
97+
assume(elementData.get(i) != null);
98+
}
9499

95100
visit(this);
96101
}
@@ -290,17 +295,13 @@ public DoubleStream skip(long n) {
290295
}
291296

292297
int curSize = elementData.end;
293-
if (n > curSize) {
298+
if (n >= curSize) {
294299
return new UtDoubleStream();
295300
}
296301

297-
// n is 1...Integer.MAX_VALUE here
302+
// n is 0...(Integer.MAX_VALUE - 1) here
298303
int newSize = (int) (curSize - n);
299304

300-
if (newSize == 0) {
301-
return new UtDoubleStream();
302-
}
303-
304305
Double[] elements = elementData.toCastedArray((int) n, newSize);
305306

306307
return new UtDoubleStream(elements, newSize);
@@ -353,17 +354,13 @@ public OptionalDouble reduce(DoubleBinaryOperator op) {
353354
return OptionalDouble.empty();
354355
}
355356

356-
Double result = null;
357-
for (int i = 0; i < size; i++) {
357+
double result = elementData.get(0);
358+
for (int i = 1; i < size; i++) {
358359
double element = elementData.get(i);
359-
if (result == null) {
360-
result = element;
361-
} else {
362-
result = op.applyAsDouble(result, element);
363-
}
360+
result = op.applyAsDouble(result, element);
364361
}
365362

366-
return result == null ? OptionalDouble.empty() : OptionalDouble.of(result);
363+
return OptionalDouble.of(result);
367364
}
368365

369366
@Override
@@ -391,16 +388,38 @@ public double sum() {
391388
}
392389

393390
double sum = 0;
391+
boolean anyNaN = false;
392+
boolean anyPositiveInfinity = false;
393+
boolean anyNegativeInfinity = false;
394394

395395
for (int i = 0; i < size; i++) {
396396
double element = elementData.get(i);
397397
sum += element;
398+
399+
anyNaN |= Double.isNaN(element);
400+
anyPositiveInfinity |= element == Double.POSITIVE_INFINITY;
401+
anyNegativeInfinity |= element == Double.NEGATIVE_INFINITY;
402+
}
403+
404+
if (anyNaN) {
405+
return Double.NaN;
406+
}
407+
408+
if (anyPositiveInfinity && anyNegativeInfinity) {
409+
return Double.NaN;
410+
}
411+
412+
if (anyPositiveInfinity && sum == Double.NEGATIVE_INFINITY) {
413+
return Double.NaN;
414+
}
415+
416+
if (anyNegativeInfinity && sum == Double.POSITIVE_INFINITY) {
417+
return Double.NaN;
398418
}
399419

400420
return sum;
401421
}
402422

403-
@SuppressWarnings("ManualMinMaxCalculation")
404423
@Override
405424
public OptionalDouble min() {
406425
preconditionCheckWithClosingStream();
@@ -413,13 +432,12 @@ public OptionalDouble min() {
413432
double min = elementData.get(0);
414433
for (int i = 1; i < size; i++) {
415434
final double element = elementData.get(i);
416-
min = (element < min) ? element : min;
435+
min = Math.min(element, min);
417436
}
418437

419438
return OptionalDouble.of(min);
420439
}
421440

422-
@SuppressWarnings("ManualMinMaxCalculation")
423441
@Override
424442
public OptionalDouble max() {
425443
preconditionCheckWithClosingStream();
@@ -432,7 +450,7 @@ public OptionalDouble max() {
432450
double max = elementData.get(0);
433451
for (int i = 1; i < size; i++) {
434452
final double element = elementData.get(i);
435-
max = (element > max) ? element : max;
453+
max = Math.max(element, max);
436454
}
437455

438456
return OptionalDouble.of(max);
@@ -629,7 +647,7 @@ public void close() {
629647
closeHandlers.get(i).run();
630648
}
631649

632-
// clear handlers
650+
// clear handlers (we do not need to manually clear all elements)
633651
closeHandlers.end = 0;
634652
}
635653

utbot-framework/src/main/java/org/utbot/engine/overrides/stream/UtIntStream.java

+12-15
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,12 @@ void preconditionCheck() {
9191

9292
assume(elementData.end >= 0);
9393
// we can create a stream for an array using Stream.of
94-
assume(elementData.end <= HARD_MAX_ARRAY_SIZE);
94+
assumeOrExecuteConcretely(elementData.end <= HARD_MAX_ARRAY_SIZE);
95+
96+
// As real primitive streams contain primitives, we cannot accept nulls.
97+
for (int i = 0; i < elementData.end; i++) {
98+
assume(elementData.get(i) != null);
99+
}
95100

96101
visit(this);
97102
}
@@ -291,17 +296,13 @@ public IntStream skip(long n) {
291296
}
292297

293298
int curSize = elementData.end;
294-
if (n > curSize) {
299+
if (n >= curSize) {
295300
return new UtIntStream();
296301
}
297302

298-
// n is 1...Integer.MAX_VALUE here
303+
// n is 0...(Integer.MAX_VALUE - 1) here
299304
int newSize = (int) (curSize - n);
300305

301-
if (newSize == 0) {
302-
return new UtIntStream();
303-
}
304-
305306
Integer[] newData = elementData.toCastedArray((int) n, newSize);
306307

307308
return new UtIntStream(newData, newSize);
@@ -355,17 +356,13 @@ public OptionalInt reduce(IntBinaryOperator op) {
355356
return OptionalInt.empty();
356357
}
357358

358-
Integer result = null;
359-
for (int i = 0; i < size; i++) {
359+
int result = elementData.get(0);
360+
for (int i = 1; i < size; i++) {
360361
int element = elementData.get(i);
361-
if (result == null) {
362-
result = element;
363-
} else {
364-
result = op.applyAsInt(result, element);
365-
}
362+
result = op.applyAsInt(result, element);
366363
}
367364

368-
return result == null ? OptionalInt.empty() : OptionalInt.of(result);
365+
return OptionalInt.of(result);
369366
}
370367

371368
@Override

utbot-framework/src/main/java/org/utbot/engine/overrides/stream/UtLongStream.java

+12-15
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,12 @@ void preconditionCheck() {
9191

9292
assume(elementData.end >= 0);
9393
// we can create a stream for an array using Stream.of
94-
assume(elementData.end <= HARD_MAX_ARRAY_SIZE);
94+
assumeOrExecuteConcretely(elementData.end <= HARD_MAX_ARRAY_SIZE);
95+
96+
// As real primitive streams contain primitives, we cannot accept nulls.
97+
for (int i = 0; i < elementData.end; i++) {
98+
assume(elementData.get(i) != null);
99+
}
95100

96101
visit(this);
97102
}
@@ -291,17 +296,13 @@ public LongStream skip(long n) {
291296
}
292297

293298
int curSize = elementData.end;
294-
if (n > curSize) {
299+
if (n >= curSize) {
295300
return new UtLongStream();
296301
}
297302

298-
// n is 1...Integer.MAX_VALUE here
303+
// n is 0...(Integer.MAX_VALUE - 1) here
299304
int newSize = (int) (curSize - n);
300305

301-
if (newSize == 0) {
302-
return new UtLongStream();
303-
}
304-
305306
Long[] elements = elementData.toCastedArray((int) n, newSize);
306307

307308
return new UtLongStream(elements, newSize);
@@ -354,17 +355,13 @@ public OptionalLong reduce(LongBinaryOperator op) {
354355
return OptionalLong.empty();
355356
}
356357

357-
Long result = null;
358-
for (int i = 0; i < size; i++) {
358+
long result = elementData.get(0);
359+
for (int i = 1; i < size; i++) {
359360
long element = elementData.get(i);
360-
if (result == null) {
361-
result = element;
362-
} else {
363-
result = op.applyAsLong(result, element);
364-
}
361+
result = op.applyAsLong(result, element);
365362
}
366363

367-
return result == null ? OptionalLong.empty() : OptionalLong.of(result);
364+
return OptionalLong.of(result);
368365
}
369366

370367
@Override

utbot-framework/src/main/java/org/utbot/engine/overrides/stream/UtStream.java

+4-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.utbot.engine.overrides.stream;
22

3-
import org.utbot.api.mock.UtMock;
3+
import org.jetbrains.annotations.NotNull;
44
import org.utbot.engine.overrides.UtArrayMock;
55
import org.utbot.engine.overrides.collections.RangeModifiableUnlimitedArray;
66
import org.utbot.engine.overrides.collections.UtGenericStorage;
@@ -27,7 +27,6 @@
2727
import java.util.stream.IntStream;
2828
import java.util.stream.LongStream;
2929
import java.util.stream.Stream;
30-
import org.jetbrains.annotations.NotNull;
3130

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

146-
@SuppressWarnings({"unchecked", "CastCanBeRemovedNarrowingVariableType"})
147145
@Override
148146
public IntStream mapToInt(ToIntFunction<? super E> mapper) {
149147
preconditionCheckWithClosingStream();
150148

151149
int size = elementData.end;
152150
Integer[] data = new Integer[size];
153151
for (int i = 0; i < size; i++) {
154-
final Object object = elementData.get(i);
155-
UtMock.disableClassCastExceptionCheck(object);
156-
data[i] = mapper.applyAsInt((E) object);
152+
data[i] = mapper.applyAsInt(elementData.get(i));
157153
}
158154

159155
return new UtIntStream(data, size);
160156
}
161157

162-
@SuppressWarnings({"unchecked", "CastCanBeRemovedNarrowingVariableType"})
163158
@Override
164159
public LongStream mapToLong(ToLongFunction<? super E> mapper) {
165160
preconditionCheckWithClosingStream();
166161

167162
int size = elementData.end;
168163
Long[] data = new Long[size];
169164
for (int i = 0; i < size; i++) {
170-
final Object object = elementData.get(i);
171-
UtMock.disableClassCastExceptionCheck(object);
172-
data[i] = mapper.applyAsLong((E) object);
165+
data[i] = mapper.applyAsLong(elementData.get(i));
173166
}
174167

175168
return new UtLongStream(data, size);
176169
}
177170

178-
@SuppressWarnings({"unchecked", "CastCanBeRemovedNarrowingVariableType"})
179171
@Override
180172
public DoubleStream mapToDouble(ToDoubleFunction<? super E> mapper) {
181173
preconditionCheckWithClosingStream();
182174

183175
int size = elementData.end;
184176
Double[] data = new Double[size];
185-
assume(data.length == elementData.end);
186177
for (int i = 0; i < size; i++) {
187-
final Object object = elementData.get(i);
188-
UtMock.disableClassCastExceptionCheck(object);
189-
data[i] = mapper.applyAsDouble((E) object);
178+
data[i] = mapper.applyAsDouble(elementData.get(i));
190179
}
191180

192181
return new UtDoubleStream(data, size);

0 commit comments

Comments
 (0)