Skip to content
Snippets Groups Projects
Commit d3fbe901 authored by Benedikt Wetzel's avatar Benedikt Wetzel
Browse files

add test for binary search, small code adjustments

parent afdf794a
No related branches found
No related tags found
4 merge requests!159Re-implementation of Theodolite with Kotlin/Quarkus,!157Update Graal Image in CI pipeline,!83WIP: Re-implementation of Theodolite with Kotlin/Quarkus,!78Resolve "Implement Quarkus/Kotlin protype"
This commit is part of merge request !78. Comments created here will be created in the context of that merge request.
...@@ -15,10 +15,10 @@ class BinarySearch(benchmarkExecutor: BenchmarkExecutor) : SearchStrategy(benchm ...@@ -15,10 +15,10 @@ class BinarySearch(benchmarkExecutor: BenchmarkExecutor) : SearchStrategy(benchm
} }
private fun search (load: LoadDimension, resources: List<Resource>, lower: Int, upper: Int): Int { private fun search (load: LoadDimension, resources: List<Resource>, lower: Int, upper: Int): Int {
if (lower + 1 < upper ) { if (lower > upper) {
throw IllegalArgumentException() throw IllegalArgumentException()
} }
if (lower >= upper ) { if (lower == upper ) {
if (this.benchmarkExecutor.runExperiment(load, resources[upper])) return upper; if (this.benchmarkExecutor.runExperiment(load, resources[upper])) return upper;
else { else {
if (lower + 1 == resources.size) return - 1 if (lower + 1 == resources.size) return - 1
...@@ -27,6 +27,9 @@ class BinarySearch(benchmarkExecutor: BenchmarkExecutor) : SearchStrategy(benchm ...@@ -27,6 +27,9 @@ class BinarySearch(benchmarkExecutor: BenchmarkExecutor) : SearchStrategy(benchm
} else { } else {
val mid = (upper + lower) / 2 val mid = (upper + lower) / 2
if (this.benchmarkExecutor.runExperiment(load, resources[mid])) { if (this.benchmarkExecutor.runExperiment(load, resources[mid])) {
if (mid == lower) {
return search(load, resources, lower, lower );
}
return search(load, resources, lower, mid - 1 ); return search(load, resources, lower, mid - 1 );
} else { } else {
return search(load, resources, mid + 1 , upper); return search(load, resources, mid + 1 , upper);
......
...@@ -4,6 +4,7 @@ import io.quarkus.test.junit.QuarkusTest ...@@ -4,6 +4,7 @@ import io.quarkus.test.junit.QuarkusTest
import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import theodolite.strategies.searchstrategy.LinearSearch import theodolite.strategies.searchstrategy.LinearSearch
import theodolite.strategies.searchstrategy.BinarySearch
import theodolite.strategies.restriction.LowerBoundRestriction import theodolite.strategies.restriction.LowerBoundRestriction
import theodolite.strategies.searchstrategy.CompositeStrategy import theodolite.strategies.searchstrategy.CompositeStrategy
import theodolite.execution.TestBenchmarkExecutor import theodolite.execution.TestBenchmarkExecutor
...@@ -15,7 +16,7 @@ import theodolite.util.Results ...@@ -15,7 +16,7 @@ import theodolite.util.Results
class CompositeStrategyTest { class CompositeStrategyTest {
@Test @Test
fun testEnd2End() { fun testEnd2EndLinearSearch() {
val mockResults = arrayOf( val mockResults = arrayOf(
arrayOf( true , true , true , true , true , true , true), arrayOf( true , true , true , true , true , true , true),
arrayOf( false, false, true , true , true , true , true), arrayOf( false, false, true , true , true , true , true),
...@@ -44,6 +45,36 @@ class CompositeStrategyTest { ...@@ -44,6 +45,36 @@ class CompositeStrategyTest {
assertEquals(actual, expected) 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment