zeolite-lang-0.22.0.0: lib/container/test/sorted-map.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 "SortedMap tests" {
success
}
unittest integrationTest {
ValidatedTree<Int,Int> map <- ValidatedTree<Int,Int>.new()
Int count <- 33
$ReadOnly[count]$
// Insert values.
traverse (Counter.zeroIndexed(count) -> Int i) {
Int new <- ((i + 13) * 3547) % count
\ map.set(new,i)
\ Testing.checkEquals(map.size(),i+1)
}
// Check and remove values.
traverse (Counter.zeroIndexed(count) -> Int i) {
Int new <- ((i + 13) * 3547) % count
scoped {
optional Int value <- map.get(new)
} in if (!present(value)) {
\ BasicOutput.error()
.write("Not found ")
.write(new)
.write(" but should have been ")
.write(i)
.flush()
} elif (require(value) != i) {
\ BasicOutput.error()
.write("Element ")
.write(new)
.write(" should have been ")
.write(i)
.write(" but was ")
.write(require(value))
.flush()
}
\ map.remove(new)
\ Testing.checkEquals(map.size(),count-i-1)
}
}
unittest removeNotPresent {
SortedMap<Int,Int> map <- SortedMap<Int,Int>.new().set(1,1).set(2,2).set(4,4)
\ map.remove(3)
\ Testing.checkEquals(map.size(),3)
}
unittest defaultOrder {
SortedMap<Int,Int> map <- SortedMap<Int,Int>.new()
Int hash <- 13
Int max <- 20
$ReadOnly[hash,max]$
// Populate the map in a pseudo-random order.
traverse (Counter.zeroIndexed(max) -> Int i) {
\ ((i*hash)%max) `map.set` i
}
// Validate the traversal order.
Int index <- 0
traverse (map.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 defaultOrderEmpty {
traverse (SortedMap<Int,Int>.new().defaultOrder() -> _) {
fail("not empty")
}
}
unittest keyOrder {
SortedMap<Int,Int> map <- SortedMap<Int,Int>.new()
Int hash <- 13
Int max <- 20
$ReadOnly[hash,max]$
// Populate the map in a pseudo-random order.
traverse (Counter.zeroIndexed(max) -> Int i) {
\ ((i*hash)%max) `map.set` i
}
// Validate the traversal order.
Int index <- 0
traverse (map.keyOrder() -> Int key) {
\ Testing.checkEquals(key,index)
index <- index+1
}
\ Testing.checkEquals(index,max)
}
unittest valueOrder {
SortedMap<Int,Int> map <- SortedMap<Int,Int>.new()
Int hash <- 13
Int max <- 20
$ReadOnly[hash,max]$
// Populate the map in a pseudo-random order.
traverse (Counter.zeroIndexed(max) -> Int i) {
\ ((i*hash)%max) `map.set` i
}
// Validate the traversal order.
Int index <- 0
traverse (map.valueOrder() -> Int value) {
\ Testing.checkEquals((value*hash)%max,index)
index <- index+1
}
\ Testing.checkEquals(index,max)
}
unittest reverseOrder {
SortedMap<Int,Int> map <- SortedMap<Int,Int>.new()
Int hash <- 13
Int max <- 20
$ReadOnly[hash,max]$
// Populate the map in a pseudo-random order.
traverse (Counter.zeroIndexed(max) -> Int i) {
\ ((i*hash)%max) `map.set` i
}
// Validate the traversal order.
Int index <- max
traverse (map.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 {
SortedMap<Int,Int> map <- SortedMap<Int,Int>.new()
Int hash <- 13
Int max <- 20
$ReadOnly[hash,max]$
// Populate the map in a pseudo-random order.
traverse (Counter.zeroIndexed(max) -> Int i) {
\ ((i*hash)%max) `map.set` i
}
// Validate the traversal order from every starting point.
traverse (Counter.zeroIndexed(max) -> Int i) {
Int index <- i
traverse (map.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 {
SortedMap<Int,Int> map <- SortedMap<Int,Int>.new()
\ map.set(1,1).set(3,3).set(5,5)
scoped {
optional Order<KeyValue<Int,Int>> start <- map.getForward(4)
} in if (!present(start)) {
fail("Failed")
} else {
\ Testing.checkEquals(require(start).get().getKey(),5)
}
scoped {
optional Order<KeyValue<Int,Int>> start <- map.getForward(6)
} in if (present(start)) {
fail("Failed")
}
}
unittest getReverse {
SortedMap<Int,Int> map <- SortedMap<Int,Int>.new()
Int hash <- 13
Int max <- 20
$ReadOnly[hash,max]$
// Populate the map in a pseudo-random order.
traverse (Counter.zeroIndexed(max) -> Int i) {
\ ((i*hash)%max) `map.set` i
}
// Validate the traversal order from every starting point.
traverse (Counter.zeroIndexed(max) -> Int i) {
Int index <- i
traverse (map.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 {
SortedMap<Int,Int> map <- SortedMap<Int,Int>.new()
\ map.set(1,1).set(3,3).set(5,5)
scoped {
optional Order<KeyValue<Int,Int>> start <- map.getReverse(2)
} in if (!present(start)) {
fail("Failed")
} else {
\ Testing.checkEquals(require(start).get().getKey(),1)
}
scoped {
optional Order<KeyValue<Int,Int>> start <- map.getReverse(0)
} in if (present(start)) {
fail("Failed")
}
}
unittest duplicate {
SortedMap<Int,Int> map <- SortedMap<Int,Int>.default()
Int max <- 31
$ReadOnly[max]$
traverse (Counter.zeroIndexed(max) -> Int i) {
\ map.set(i,i)
}
SortedMap<Int,Int> copy <- map.duplicate()
\ Testing.checkEquals(copy.size(),map.size())
traverse (Counter.zeroIndexed(max) -> Int i) {
$Hidden[map]$
\ Testing.checkOptional(copy.get(i),i)
}
\ copy.set(2,5)
\ copy.remove(7)
$Hidden[copy]$
\ Testing.checkEquals(map.size(),max)
traverse (Counter.zeroIndexed(max) -> Int i) {
\ Testing.checkOptional(map.get(i),i)
}
}
unittest duplicateEmpty {
SortedMap<Int,Int> map <- SortedMap<Int,Int>.new()
SortedMap<Int,Int> copy <- map.duplicate()
\ map.set(1,1)
\ Testing.checkEquals(copy.size(),0)
}
unittest weakSetExists {
SortedMap<Int,Value> map <- SortedMap<Int,Value>.default()
.set(1,Value.new(1))
.set(2,Value.new(2))
.set(3,Value.new(3))
Value value0 <- Value.new(5)
Value value1 <- map.weakSet(1,value0)
\ Testing.checkEquals(map.size(),3)
\ Testing.checkEquals(value0.get(),5)
\ Testing.checkEquals(value1.get(),1)
\ value0.set(10)
\ Testing.checkEquals(value1.get(),1)
\ Testing.checkOptional(map.get(1),Value.new(1))
}
unittest weakSetMissing {
SortedMap<Int,Value> map <- SortedMap<Int,Value>.default()
.set(1,Value.new(1))
.set(2,Value.new(2))
.set(3,Value.new(3))
Value value0 <- Value.new(5)
Value value1 <- map.weakSet(7,value0)
\ Testing.checkEquals(map.size(),4)
\ Testing.checkEquals(value0.get(),5)
\ Testing.checkEquals(value1.get(),5)
\ value0.set(10)
\ Testing.checkEquals(value1.get(),10)
\ Testing.checkOptional(map.get(7),Value.new(10))
}
concrete Value {
defines Equals<Value>
refines Formatted
@type new (Int) -> (Value)
@value get () -> (Int)
@value set (Int) -> ()
}
define Value {
@value Int value
new (value) { return Value{ value } }
get () { return value }
set (value2) { value <- value2 }
formatted () {
return value.formatted()
}
equals (x,y) {
return x.get() == y.get()
}
}