124 lines
3.7 KiB
Kotlin
124 lines
3.7 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.Option
|
|
import arrow.core.Some
|
|
import org.amshove.kluent.`should equal`
|
|
import org.spekframework.spek2.Spek
|
|
import org.spekframework.spek2.style.gherkin.Feature
|
|
|
|
object DescendentsTest : Spek({
|
|
Feature("descendents") {
|
|
Scenario("get and set") {
|
|
val tree by memoized { DescendentsTree<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("descendents") {
|
|
val tree by memoized { DescendentsTree<Int, Int>() }
|
|
|
|
Given("tree 15, 23, 7, 18, 25, 21, 22, 20") {
|
|
tree[15] = 15
|
|
tree[23] = 23
|
|
tree[7] = 7
|
|
tree[18] = 18
|
|
tree[25] = 25
|
|
tree[21] = 21
|
|
tree[22] = 22
|
|
tree[20] = 20
|
|
}
|
|
|
|
lateinit var result: Option<Int>
|
|
|
|
When("compute descendents of 21") {
|
|
result = tree.descendents(21)
|
|
}
|
|
|
|
Then("result should equal 2") {
|
|
result `should equal` Some(2)
|
|
}
|
|
|
|
When("compute descendents of 7") {
|
|
result = tree.descendents(7)
|
|
}
|
|
|
|
Then("result should equal 0") {
|
|
result `should equal` Some(0)
|
|
}
|
|
|
|
When("compute descendents of 23") {
|
|
result = tree.descendents(23)
|
|
}
|
|
|
|
Then("result should equal 5") {
|
|
result `should equal` Some(5)
|
|
}
|
|
|
|
When("compute descendents of 8") {
|
|
result = tree.descendents(8)
|
|
}
|
|
|
|
Then("result should equal None") {
|
|
result `should equal` None
|
|
}
|
|
}
|
|
}
|
|
}) |