From d3fbe9017fe0275b66b9f5f39626bc56e73b4ac8 Mon Sep 17 00:00:00 2001 From: "stu126940@mail.uni-kiel.de" <stu126940@mail.uni-kiel.de> Date: Fri, 15 Jan 2021 18:17:15 +0100 Subject: [PATCH] add test for binary search, small code adjustments --- .../strategies/searchstrategy/BinarySearch.kt | 7 ++-- .../theodolite/CompositeStrategyTest.kt | 33 ++++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/theodolite-quarkus/src/main/kotlin/theodolite/strategies/searchstrategy/BinarySearch.kt b/theodolite-quarkus/src/main/kotlin/theodolite/strategies/searchstrategy/BinarySearch.kt index 1bc22f7ca..d238bc955 100644 --- a/theodolite-quarkus/src/main/kotlin/theodolite/strategies/searchstrategy/BinarySearch.kt +++ b/theodolite-quarkus/src/main/kotlin/theodolite/strategies/searchstrategy/BinarySearch.kt @@ -15,10 +15,10 @@ class BinarySearch(benchmarkExecutor: BenchmarkExecutor) : SearchStrategy(benchm } private fun search (load: LoadDimension, resources: List<Resource>, lower: Int, upper: Int): Int { - if (lower + 1 < upper ) { + if (lower > upper) { throw IllegalArgumentException() } - if (lower >= upper ) { + if (lower == upper ) { if (this.benchmarkExecutor.runExperiment(load, resources[upper])) return upper; else { if (lower + 1 == resources.size) return - 1 @@ -27,6 +27,9 @@ class BinarySearch(benchmarkExecutor: BenchmarkExecutor) : SearchStrategy(benchm } else { val mid = (upper + lower) / 2 if (this.benchmarkExecutor.runExperiment(load, resources[mid])) { + if (mid == lower) { + return search(load, resources, lower, lower ); + } return search(load, resources, lower, mid - 1 ); } else { return search(load, resources, mid + 1 , upper); diff --git a/theodolite-quarkus/src/test/kotlin/theodolite/CompositeStrategyTest.kt b/theodolite-quarkus/src/test/kotlin/theodolite/CompositeStrategyTest.kt index 3bf4815e1..0fa48dafc 100644 --- a/theodolite-quarkus/src/test/kotlin/theodolite/CompositeStrategyTest.kt +++ b/theodolite-quarkus/src/test/kotlin/theodolite/CompositeStrategyTest.kt @@ -4,6 +4,7 @@ import io.quarkus.test.junit.QuarkusTest import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.Test import theodolite.strategies.searchstrategy.LinearSearch +import theodolite.strategies.searchstrategy.BinarySearch import theodolite.strategies.restriction.LowerBoundRestriction import theodolite.strategies.searchstrategy.CompositeStrategy import theodolite.execution.TestBenchmarkExecutor @@ -15,7 +16,7 @@ import theodolite.util.Results class CompositeStrategyTest { @Test - fun testEnd2End() { + fun testEnd2EndLinearSearch() { val mockResults = arrayOf( arrayOf( true , true , true , true , true , true , true), arrayOf( false, false, true , true , true , true , true), @@ -44,6 +45,36 @@ class CompositeStrategyTest { assertEquals(actual, expected) } + @Test + fun testEnd2EndBinarySearch() { + val mockResults = arrayOf( + arrayOf( true , true , true , true , true , true , true), + arrayOf( false, false, true , true , true , true , true), + arrayOf( false, false, true , true , true , true , true), + arrayOf( false, false, false, true , true , true , true), + arrayOf( false, false, false, false, true , true , true), + arrayOf( false, false, false, false, false, false, true), + arrayOf( false, false, false, false, false, false, false) + ) + val mockLoads: List<LoadDimension> = (0..6).map{number -> LoadDimension(number)} + val mockResources: List<Resource> = (0..6).map{number -> Resource(number)} + val benchmarkExecutor: TestBenchmarkExecutor = TestBenchmarkExecutor(mockResults) + val binarySearch: BinarySearch = BinarySearch(benchmarkExecutor); + val results: Results = Results(); + val lowerBoundRestriction: LowerBoundRestriction = LowerBoundRestriction(results, mockLoads); + val strategy: CompositeStrategy = CompositeStrategy(benchmarkExecutor, binarySearch, listOf(lowerBoundRestriction)) + + val actual: ArrayList<Resource?> = ArrayList<Resource?>() + val expected: ArrayList<Resource?> = ArrayList(listOf(0,2,2,3,4,6).map{ x -> Resource(x)}) + expected.add(null) + + for(load in mockLoads) { + actual.add(strategy.findSuitableResources(load, mockResources)) + } + + assertEquals(actual, expected) + } + } \ No newline at end of file -- GitLab