Skip to content

Added pre-condition for non-empty paths in the ExecutionMetric to avoid unclear NPE #267 #567

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

Merged
merged 1 commit into from
Jul 21, 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class ExecutionMetric : Metric<Iterable<Step>> {
* Minimum Edit Distance
*/
private fun compareTwoPaths(path1: Iterable<Step>, path2: Iterable<Step>): Double {
require(path1.count() > 0) { "Two paths can not be compared: path1 is empty!"}
require(path2.count() > 0) { "Two paths can not be compared: path2 is empty!"}

val distances = Array(path1.count()) { i -> Array(path2.count()) { j -> i + j } }

for (i in 1 until path1.count()) {
Expand All @@ -22,7 +25,17 @@ class ExecutionMetric : Metric<Iterable<Step>> {
distances[i][j] = minOf(d1, d2, d3)
}
}
return distances.last().last().toDouble()

if (distances.isNotEmpty()) {
val last = distances.last()
if (last.isNotEmpty()) {
return last.last().toDouble()
} else {
throw IllegalStateException("Last row in the distance matrix has 0 columns. It should contain more or equal 1 column.")
}
} else {
throw IllegalStateException("Distance matrix has 0 rows. It should contain more or equal 1 row.")
}
}

private fun distance(stmt1: Step, stmt2: Step): Int {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.utbot.summary.clustering

import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test

import org.utbot.framework.plugin.api.Step
import java.lang.IllegalArgumentException

internal class ExecutionMetricTest {
@Test
fun computeWithTwoEmptySteps() {
val executionMetric = ExecutionMetric()
val object1 = listOf<Step>()
val object2 = listOf<Step>()


val exception = Assertions.assertThrows(IllegalArgumentException::class.java) {
executionMetric.compute(object1 = object1, object2 = object2)
}

Assertions.assertEquals(
"Two paths can not be compared: path1 is empty!",
exception.message
)
}
}