lm-tecniche-di-programmazione/src/test/kotlin/it/norangeb/algorithms/exam/DistanceToLeafTest.kt

108 lines
3.4 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.exam
import arrow.core.None
import arrow.core.Some
import org.amshove.kluent.`should equal`
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.gherkin.Feature
object DistanceToLeafTest : Spek({
Feature("distance to leaf") {
Scenario("get and set") {
val tree by memoized { DistanceToLeafTree<Int, Int>() }
When("add couple 15-15, 13-13, 23-23, 5-5") {
tree[15] = 15
tree[13] = 13
tree[23] = 24
tree[5] = 5
}
Then("tree of 23 should equal 24") {
tree[23] `should equal` Some(24)
}
Then("tree of 25 should equal None") {
tree[25] `should equal` None
}
Then("tree of 5 should equal 5") {
tree[5] `should equal` Some(5)
}
When("add couple 1-1, 17-17, 25-25, 23-23") {
tree[1] = 1
tree[17] = 17
tree[25] = 25
tree[23] = 23
}
Then("tree of 23 should equal 23") {
tree[23] `should equal` Some(23)
}
Then("tree of 25 should equal 25") {
tree[25] `should equal` Some(25)
}
}
Scenario("distance to leaf") {
val tree by memoized { DistanceToLeafTree<Int, Int>() }
Given("") {
tree[18] = 18
tree[15] = 15
tree[23] = 23
tree[7] = 7
tree[17] = 17
tree[3] = 3
tree[10] = 10
tree[1] = 1
tree[4] = 4
tree[12] = 12
tree[14] = 14
}
Then("distance to leaf of 25 should equal None") {
tree.distanceToLeaf(25) `should equal` None
}
Then("distance to leaf of 15 should equal 1") {
tree.distanceToLeaf(15) `should equal` Some(1)
}
Then("distance to leaf of 7 should equal 2") {
tree.distanceToLeaf(7) `should equal` Some(2)
}
Then("distance to leaf of 4 should equal 0") {
tree.distanceToLeaf(4) `should equal` Some(0)
}
}
}
})