Skip to content

Commit c8946b7

Browse files
committed
Added tests for primitive streams
1 parent d975a88 commit c8946b7

12 files changed

Lines changed: 3343 additions & 38 deletions

File tree

utbot-core/src/main/kotlin/org/utbot/common/FileUtil.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ object FileUtil {
227227
// https://stackoverflow.com/a/68822715
228228
fun byteCountToDisplaySize(bytes: Long): String =
229229
when {
230-
bytes >= 1 shl 30 -> "%.1f GB".format(bytes / (1 shl 30))
231-
bytes >= 1 shl 20 -> "%.1f MB".format(bytes / (1 shl 20))
232-
bytes >= 1 shl 10 -> "%.0f kB".format(bytes / (1 shl 10))
230+
bytes >= 1 shl 30 -> "%.1f GB".format(bytes.toDouble() / (1 shl 30))
231+
bytes >= 1 shl 20 -> "%.1f MB".format(bytes.toDouble() / (1 shl 20))
232+
bytes >= 1 shl 10 -> "%.0f kB".format(bytes.toDouble() / (1 shl 10))
233233
else -> "$bytes bytes"
234234
}
235235
}

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,13 @@ public OptionalDouble average() {
453453
return OptionalDouble.empty();
454454
}
455455

456-
double average = sum() / count();
456+
// "reopen" this stream to use sum and count
457+
isClosed = false;
458+
final double sum = sum();
459+
isClosed = false;
460+
final long count = count();
461+
462+
double average = sum / count;
457463

458464
return OptionalDouble.of(average);
459465
}
@@ -478,13 +484,16 @@ public boolean anyMatch(DoublePredicate predicate) {
478484
preconditionCheckWithClosingStream();
479485

480486
int size = elementData.end;
487+
boolean matches = false;
481488
for (int i = 0; i < size; i++) {
482-
if (predicate.test(elementData.get(i))) {
483-
return true;
484-
}
489+
matches |= predicate.test(elementData.get(i));
490+
// if (predicate.test(elementData.get(i))) {
491+
// return true;
492+
// }
485493
}
486494

487-
return false;
495+
// return false;
496+
return matches;
488497
}
489498

490499
@Override
@@ -503,8 +512,6 @@ public boolean allMatch(DoublePredicate predicate) {
503512

504513
@Override
505514
public boolean noneMatch(DoublePredicate predicate) {
506-
preconditionCheckWithClosingStream();
507-
508515
return !anyMatch(predicate);
509516
}
510517

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,13 @@ public OptionalDouble average() {
455455
return OptionalDouble.empty();
456456
}
457457

458-
double average = (double) sum() / count();
458+
// "reopen" this stream to use sum and count
459+
isClosed = false;
460+
final double sum = sum();
461+
isClosed = false;
462+
final long count = count();
463+
464+
double average = sum / count;
459465

460466
return OptionalDouble.of(average);
461467
}
@@ -505,8 +511,6 @@ public boolean allMatch(IntPredicate predicate) {
505511

506512
@Override
507513
public boolean noneMatch(IntPredicate predicate) {
508-
preconditionCheckWithClosingStream();
509-
510514
return !anyMatch(predicate);
511515
}
512516

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,13 @@ public OptionalDouble average() {
454454
return OptionalDouble.empty();
455455
}
456456

457-
double average = (double) sum() / count();
457+
// "reopen" this stream to use sum and count
458+
isClosed = false;
459+
final double sum = sum();
460+
isClosed = false;
461+
final long count = count();
462+
463+
double average = sum / count;
458464

459465
return OptionalDouble.of(average);
460466
}
@@ -504,8 +510,6 @@ public boolean allMatch(LongPredicate predicate) {
504510

505511
@Override
506512
public boolean noneMatch(LongPredicate predicate) {
507-
preconditionCheckWithClosingStream();
508-
509513
return !anyMatch(predicate);
510514
}
511515

utbot-framework/src/test/kotlin/org/utbot/examples/stream/BaseStreamExampleTest.kt

Lines changed: 82 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.utbot.examples.stream
33
import org.junit.jupiter.api.Disabled
44
import org.junit.jupiter.api.Tag
55
import org.junit.jupiter.api.Test
6+
import org.utbot.examples.AtLeast
67
import org.utbot.examples.UtValueTestCaseChecker
78
import org.utbot.examples.DoNotCalculate
89
import org.utbot.examples.Full
@@ -69,10 +70,43 @@ class BaseStreamExampleTest : UtValueTestCaseChecker(
6970
fun testMapExample() {
7071
checkWithException(
7172
BaseStreamExample::mapExample,
72-
eq(2),
73+
ignoreExecutionsNumber,
7374
{ c, r -> null in c && r.isException<NullPointerException>() },
7475
{ c, r -> r.getOrThrow().contentEquals(c.map { it * 2 }.toTypedArray()) },
75-
coverage = DoNotCalculate
76+
coverage = AtLeast(90)
77+
)
78+
}
79+
80+
@Test
81+
fun testMapToIntExample() {
82+
checkWithException(
83+
BaseStreamExample::mapToIntExample,
84+
ignoreExecutionsNumber,
85+
{ c, r -> null in c && r.isException<NullPointerException>() },
86+
{ c, r -> r.getOrThrow().contentEquals(c.map { it.toInt() }.toIntArray()) },
87+
coverage = AtLeast(90)
88+
)
89+
}
90+
91+
@Test
92+
fun testMapToLongExample() {
93+
checkWithException(
94+
BaseStreamExample::mapToLongExample,
95+
ignoreExecutionsNumber,
96+
{ c, r -> null in c && r.isException<NullPointerException>() },
97+
{ c, r -> r.getOrThrow().contentEquals(c.map { it.toLong() }.toLongArray()) },
98+
coverage = AtLeast(90)
99+
)
100+
}
101+
102+
@Test
103+
fun testMapToDoubleExample() {
104+
checkWithException(
105+
BaseStreamExample::mapToDoubleExample,
106+
ignoreExecutionsNumber,
107+
{ c, r -> null in c && r.isException<NullPointerException>() },
108+
{ c, r -> r.getOrThrow().contentEquals(c.map { it.toDouble() }.toDoubleArray()) },
109+
coverage = AtLeast(90)
76110
)
77111
}
78112

@@ -86,6 +120,36 @@ class BaseStreamExampleTest : UtValueTestCaseChecker(
86120
)
87121
}
88122

123+
@Test
124+
fun testFlatMapToIntExample() {
125+
check(
126+
BaseStreamExample::flatMapToIntExample,
127+
ignoreExecutionsNumber,
128+
{ c, r -> r.contentEquals(c.flatMap { listOf(it?.toInt() ?: 0, it?.toInt() ?: 0) }.toIntArray()) },
129+
coverage = FullWithAssumptions(assumeCallsNumber = 1)
130+
)
131+
}
132+
133+
@Test
134+
fun testFlatMapToLongExample() {
135+
check(
136+
BaseStreamExample::flatMapToLongExample,
137+
ignoreExecutionsNumber,
138+
{ c, r -> r.contentEquals(c.flatMap { listOf(it?.toLong() ?: 0L, it?.toLong() ?: 0L) }.toLongArray()) },
139+
coverage = FullWithAssumptions(assumeCallsNumber = 1)
140+
)
141+
}
142+
143+
@Test
144+
fun testFlatMapToDoubleExample() {
145+
check(
146+
BaseStreamExample::flatMapToDoubleExample,
147+
ignoreExecutionsNumber,
148+
{ c, r -> r.contentEquals(c.flatMap { listOf(it.toDouble(), it.toDouble()) }.toDoubleArray()) },
149+
coverage = FullWithAssumptions(assumeCallsNumber = 1)
150+
)
151+
}
152+
89153
@Test
90154
fun testDistinctExample() {
91155
check(
@@ -145,17 +209,17 @@ class BaseStreamExampleTest : UtValueTestCaseChecker(
145209
fun testForEachExample() {
146210
checkThisAndStaticsAfter(
147211
BaseStreamExample::forEachExample,
148-
eq(2),
212+
ignoreExecutionsNumber,
149213
*streamConsumerStaticsMatchers,
150-
coverage = DoNotCalculate
214+
coverage = AtLeast(92)
151215
)
152216
}
153217

154218
@Test
155219
fun testToArrayExample() {
156220
check(
157221
BaseStreamExample::toArrayExample,
158-
ignoreExecutionsNumber,
222+
eq(2),
159223
{ c, r -> c.toTypedArray().contentEquals(r) },
160224
coverage = FullWithAssumptions(assumeCallsNumber = 1)
161225
)
@@ -310,10 +374,11 @@ class BaseStreamExampleTest : UtValueTestCaseChecker(
310374
fun testIteratorExample() {
311375
checkWithException(
312376
BaseStreamExample::iteratorSumExample,
313-
eq(2),
377+
ignoreExecutionsNumber,
378+
{ c, r -> c.isEmpty() && r.getOrThrow() == 0 },
314379
{ c, r -> null in c && r.isException<NullPointerException>() },
315-
{ c, r -> null !in c && r.getOrThrow() == c.sum() },
316-
coverage = DoNotCalculate
380+
{ c, r -> c.isNotEmpty() && null !in c && r.getOrThrow() == c.sum() },
381+
coverage = AtLeast(75)
317382
)
318383
}
319384

@@ -393,15 +458,15 @@ class BaseStreamExampleTest : UtValueTestCaseChecker(
393458
coverage = Full
394459
)
395460
}
461+
}
396462

397-
private val streamConsumerStaticsMatchers = arrayOf(
398-
{ _: BaseStreamExample, c: List<Int?>, _: StaticsType, _: Int? -> null in c },
399-
{ _: BaseStreamExample, c: List<Int?>, statics: StaticsType, r: Int? ->
400-
val x = statics.values.single().value as Int
463+
internal val streamConsumerStaticsMatchers = arrayOf(
464+
{ _: Any, c: List<Int?>, _: StaticsType, _: Int? -> null in c },
465+
{ _: Any, c: List<Int?>, statics: StaticsType, r: Int? ->
466+
val x = statics.values.single().value as Int
401467

402-
r!! + c.sumOf { it ?: 0 } == x
403-
}
404-
)
405-
}
468+
r!! + c.sumOf { it ?: 0 } == x
469+
}
470+
)
406471

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

0 commit comments

Comments
 (0)