zeolite-lang-0.17.0.0: lib/container/search-tree.0rt
/* -----------------------------------------------------------------------------
Copyright 2019-2021 Kevin P. Barry
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
----------------------------------------------------------------------------- */
// Author: Kevin P. Barry [ta0kira@gmail.com]
testcase "SearchTree tests" {
success
}
unittest integrationTest {
ValidatedTree<Int,Int> tree <- ValidatedTree<Int,Int>.new()
Int count <- 30
$ReadOnly[count]$
// Insert values.
traverse (Counter.zeroIndexed(count) -> Int i) {
Int new <- ((i + 13) * 3547) % count
\ tree.set(new,i)
\ Testing.checkEquals<?>(tree.size(),i+1)
}
// Check and remove values.
traverse (Counter.zeroIndexed(count) -> Int i) {
Int new <- ((i + 13) * 3547) % count
scoped {
optional Int value <- tree.get(new)
} in if (!present(value)) {
\ LazyStream<Formatted>.new()
.append("Not found ")
.append(new)
.append(" but should have been ")
.append(i)
.writeTo(SimpleOutput.error())
} elif (require(value) != i) {
\ LazyStream<Formatted>.new()
.append("Element ")
.append(new)
.append(" should have been ")
.append(i)
.append(" but was ")
.append(require(value))
.writeTo(SimpleOutput.error())
}
\ tree.remove(new)
\ Testing.checkEquals<?>(tree.size(),count-i-1)
}
}
unittest defaultOrder {
SearchTree<Int,Int> tree <- SearchTree<Int,Int>.new()
Int hash <- 13
Int max <- 20
$ReadOnly[hash,max]$
// Populate the tree in a pseudo-random order.
traverse (Counter.zeroIndexed(max) -> Int i) {
\ ((i*hash)%max) `tree.set` i
}
// Validate the traversal order.
Int index <- 0
traverse (tree.defaultOrder() -> KeyValue<Int,Int> entry) {
\ Testing.checkEquals<?>(entry.getKey(),index)
\ Testing.checkEquals<?>((entry.getValue()*hash)%max,entry.getKey())
index <- index+1
}
\ Testing.checkEquals<?>(index,max)
}
unittest reverseOrder {
SearchTree<Int,Int> tree <- SearchTree<Int,Int>.new()
Int hash <- 13
Int max <- 20
$ReadOnly[hash,max]$
// Populate the tree in a pseudo-random order.
traverse (Counter.zeroIndexed(max) -> Int i) {
\ ((i*hash)%max) `tree.set` i
}
// Validate the traversal order.
Int index <- max
traverse (tree.reverseOrder() -> KeyValue<Int,Int> entry) {
index <- index-1
\ Testing.checkEquals<?>(entry.getKey(),index)
\ Testing.checkEquals<?>((entry.getValue()*hash)%max,entry.getKey())
}
\ Testing.checkEquals<?>(index,0)
}
unittest getForward {
SearchTree<Int,Int> tree <- SearchTree<Int,Int>.new()
Int hash <- 13
Int max <- 20
$ReadOnly[hash,max]$
// Populate the tree in a pseudo-random order.
traverse (Counter.zeroIndexed(max) -> Int i) {
\ ((i*hash)%max) `tree.set` i
}
// Validate the traversal order from every starting point.
traverse (Counter.zeroIndexed(max) -> Int i) {
Int index <- i
traverse (tree.getForward(index) -> KeyValue<Int,Int> entry) {
\ Testing.checkEquals<?>(entry.getKey(),index)
\ Testing.checkEquals<?>((entry.getValue()*hash)%max,entry.getKey())
index <- index+1
}
\ Testing.checkEquals<?>(index,max)
}
}
unittest getForwardNotFound {
SearchTree<Int,Int> tree <- SearchTree<Int,Int>.new()
\ tree.set(1,1).set(3,3).set(5,5)
scoped {
optional Order<KeyValue<Int,Int>> start <- tree.getForward(4)
} in if (!present(start)) {
fail("Failed")
} else {
\ Testing.checkEquals<?>(require(start).get().getKey(),5)
}
scoped {
optional Order<KeyValue<Int,Int>> start <- tree.getForward(6)
} in if (present(start)) {
fail("Failed")
}
}
unittest getReverse {
SearchTree<Int,Int> tree <- SearchTree<Int,Int>.new()
Int hash <- 13
Int max <- 20
$ReadOnly[hash,max]$
// Populate the tree in a pseudo-random order.
traverse (Counter.zeroIndexed(max) -> Int i) {
\ ((i*hash)%max) `tree.set` i
}
// Validate the traversal order from every starting point.
traverse (Counter.zeroIndexed(max) -> Int i) {
Int index <- i
traverse (tree.getReverse(index) -> KeyValue<Int,Int> entry) {
\ Testing.checkEquals<?>(entry.getKey(),index)
\ Testing.checkEquals<?>((entry.getValue()*hash)%max,entry.getKey())
index <- index-1
}
\ Testing.checkEquals<?>(index,-1)
}
}
unittest getReverseNotFound {
SearchTree<Int,Int> tree <- SearchTree<Int,Int>.new()
\ tree.set(1,1).set(3,3).set(5,5)
scoped {
optional Order<KeyValue<Int,Int>> start <- tree.getReverse(2)
} in if (!present(start)) {
fail("Failed")
} else {
\ Testing.checkEquals<?>(require(start).get().getKey(),1)
}
scoped {
optional Order<KeyValue<Int,Int>> start <- tree.getReverse(0)
} in if (present(start)) {
fail("Failed")
}
}