From 0779b2eb24bcc04f02f40afb69350db8c55d38d8 Mon Sep 17 00:00:00 2001 From: amandelpie Date: Thu, 21 Jul 2022 15:08:20 +0300 Subject: [PATCH] Handled the bad case with the empty paths --- .../summary/clustering/ExecutionMetric.kt | 15 ++++++++++- .../summary/clustering/ExecutionMetricTest.kt | 26 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 utbot-summary/src/test/kotlin/org/utbot/summary/clustering/ExecutionMetricTest.kt diff --git a/utbot-summary/src/main/kotlin/org/utbot/summary/clustering/ExecutionMetric.kt b/utbot-summary/src/main/kotlin/org/utbot/summary/clustering/ExecutionMetric.kt index 318368d207..44043d9136 100644 --- a/utbot-summary/src/main/kotlin/org/utbot/summary/clustering/ExecutionMetric.kt +++ b/utbot-summary/src/main/kotlin/org/utbot/summary/clustering/ExecutionMetric.kt @@ -9,6 +9,9 @@ class ExecutionMetric : Metric> { * Minimum Edit Distance */ private fun compareTwoPaths(path1: Iterable, path2: Iterable): 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()) { @@ -22,7 +25,17 @@ class ExecutionMetric : Metric> { 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 { diff --git a/utbot-summary/src/test/kotlin/org/utbot/summary/clustering/ExecutionMetricTest.kt b/utbot-summary/src/test/kotlin/org/utbot/summary/clustering/ExecutionMetricTest.kt new file mode 100644 index 0000000000..853260f360 --- /dev/null +++ b/utbot-summary/src/test/kotlin/org/utbot/summary/clustering/ExecutionMetricTest.kt @@ -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() + val object2 = listOf() + + + 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 + ) + } +} \ No newline at end of file