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

161 lines
4.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.Some
import org.amshove.kluent.`should equal`
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.gherkin.Feature
object ThreeWayHeapTest : Spek({
Feature("three way heap") {
Scenario("min heap") {
val heap by memoized { ThreeWayHeap.createMinHeap<Int>() }
When("add 23, 12, 45 into heap") {
heap.insert(12)
heap.insert(45)
heap.insert(23)
}
Then("pop should equal 12") {
heap.pop() `should equal` Some(12)
}
Then("peek should equal 23") {
heap.peek() `should equal` Some(23)
}
When("add 25, 33, 24, 11, 15, 18, 34, 12 into heap") {
heap.insert(25)
heap.insert(18)
heap.insert(11)
heap.insert(33)
heap.insert(24)
heap.insert(12)
heap.insert(15)
heap.insert(34)
}
Then("pop should equal 11") {
heap.pop() `should equal` Some(11)
}
Then("pop should equal 12") {
heap.pop() `should equal` Some(12)
}
When("add 30, 10, 12, 5, 3, 8, 9, 2, 11") {
heap.insert(12)
heap.insert(8)
heap.insert(3)
heap.insert(30)
heap.insert(2)
heap.insert(10)
heap.insert(5)
heap.insert(11)
heap.insert(9)
}
Then("pop should equal 2") {
heap.pop() `should equal` Some(2)
}
Then("pop should equal 3") {
heap.pop() `should equal` Some(3)
}
Then("pop should equal 5") {
heap.pop() `should equal` Some(5)
}
Then("pop should equal 8") {
heap.pop() `should equal` Some(8)
}
Then("pop should equal 9") {
heap.pop() `should equal` Some(9)
}
Then("pop should equal 10") {
heap.pop() `should equal` Some(10)
}
Then("pop should equal 11") {
heap.pop() `should equal` Some(11)
}
Then("pop should equal 12") {
heap.pop() `should equal` Some(12)
}
Then("pop should equal 15") {
heap.pop() `should equal` Some(15)
}
Then("pop should equal 18") {
heap.pop() `should equal` Some(18)
}
Then("pop should equal 23") {
heap.pop() `should equal` Some(23)
}
Then("pop should equal 24") {
heap.pop() `should equal` Some(24)
}
Then("pop should equal 25") {
heap.pop() `should equal` Some(25)
}
Then("pop should equal 30") {
heap.pop() `should equal` Some(30)
}
Then("pop should equal 33") {
heap.pop() `should equal` Some(33)
}
Then("pop should equal 34") {
heap.pop() `should equal` Some(34)
}
Then("pop should equal 45") {
heap.pop() `should equal` Some(45)
}
Then("pop should equal None") {
heap.pop() `should equal` None
}
Then("peek should equal None") {
heap.peek() `should equal` None
}
}
}
})