/* * 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 be equal to` import org.amshove.kluent.`should equal` import org.spekframework.spek2.Spek import org.spekframework.spek2.style.gherkin.Feature object MaxWidthTreeTest : Spek({ Feature("width") { Scenario("get and set") { val tree by memoized { MaxWidthTree() } 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("width") { val tree by memoized { MaxWidthTree() } var width = -1 Then("width should equal 0") { tree.getMaxWidth() `should be equal to` 0 } 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 tree[10] = 10 } Then("width should equal 3") { tree.getMaxWidth() `should be equal to` 3 } When("add 27, 26, 28") { tree[27] = 27 tree[26] = 26 tree[28] = 28 } Then("width should equal 4") { tree.getMaxWidth() `should be equal to` 4 } } } })