lm-tecniche-di-programmazione/src/test/kotlin/it/norangeb/algorithms/graph/operations/PrimTest.kt

80 lines
2.9 KiB
Kotlin

/*
* MIT License
*
* Copyright (c) 2019 norangebit
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
package it.norangeb.algorithms.graph.operations
import it.norangeb.algorithms.graph.WeightGraph
import it.norangeb.algorithms.graph.data.Edge
import org.amshove.kluent.`should be equal to`
import org.amshove.kluent.`should equal`
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.gherkin.Feature
object PrimTest : Spek({
Feature("prim") {
Scenario("compute mst") {
val graph by memoized { WeightGraph() }
Given("a graph") {
graph.addEdge(Edge(0, 2, 0.26))
graph.addEdge(Edge(0, 4, 0.38))
graph.addEdge(Edge(0, 7, 0.16))
graph.addEdge(Edge(1, 2, 0.36))
graph.addEdge(Edge(1, 3, 0.29))
graph.addEdge(Edge(1, 5, 0.32))
graph.addEdge(Edge(1, 7, 0.19))
graph.addEdge(Edge(2, 3, 0.17))
graph.addEdge(Edge(2, 7, 0.34))
graph.addEdge(Edge(3, 6, 0.52))
graph.addEdge(Edge(4, 5, 0.35))
graph.addEdge(Edge(4, 7, 0.37))
graph.addEdge(Edge(5, 7, 0.28))
graph.addEdge(Edge(6, 2, 0.40))
}
lateinit var result: MinimumSpanningTree
When("compute the mst") {
result = Prim(graph)
}
Then("weight should be 1.81") {
result.weight() `should be equal to` 1.81
}
Then("edge should be") {
result.edges().sorted() `should equal` listOf(
Edge(0, 7, 0.16),
Edge(2, 3, 0.17),
Edge(7, 1, 0.19),
Edge(0, 2, 0.26),
Edge(7, 5, 0.28),
Edge(5, 4, 0.35),
Edge(2, 6, 0.40)
)
}
}
}
})