zeolite-lang-0.24.0.0: lib/container/test/hashed-map.0rt
/* -----------------------------------------------------------------------------
Copyright 2021,2023 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 "HashedMap tests" {
success TestChecker
}
unittest integrationTest {
HashedMap<Int, Int> map <- HashedMap<Int, Int>.default()
Int count <- 33
$ReadOnly[count]$
// Insert values.
traverse (Counter.zeroIndexed(count) -> Int i) {
Int new <- ((i + 13) * 3547) % count
\ map.set(new, i)
\ map.size() `Matches:with` CheckValue:equals(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)
\ map.size() `Matches:with` CheckValue:equals(count-i-1)
}
}
unittest removeNotPresent {
HashedMap<Int, Int> map <- HashedMap<Int, Int>.new().set(1, 1).set(2, 2).set(4, 4)
\ map.remove(3)
\ map.size() `Matches:with` CheckValue:equals(3)
}
unittest defaultOrder {
HashedMap<Int, Int> map <- HashedMap<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
}
Vector<Bool> visited <- Vector:createSize<Bool>(max)
// Validate traversal coverage.
Int index <- 0
traverse (map.defaultOrder() -> KeyValue<Int, Int> entry) {
\ visited.readAt(entry.getKey()) `Matches:with` CheckValue:equals(false)
\ (entry.getValue()*hash)%max `Matches:with` CheckValue:equals(entry.getKey())
\ visited.writeAt(entry.getKey(), true)
index <- index+1
}
\ index `Matches:with` CheckValue:equals(max)
}
unittest defaultOrderEmpty {
traverse (HashedMap<Int, Int>.new().defaultOrder() -> _) {
fail("not empty")
}
}
unittest keyOrder {
HashedMap<Int, Int> map <- HashedMap<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
}
Vector<Bool> visited <- Vector:createSize<Bool>(max)
// Validate traversal coverage.
Int index <- 0
traverse (map.keyOrder() -> Int key) {
\ visited.readAt(key) `Matches:with` CheckValue:equals(false)
\ visited.writeAt(key, true)
index <- index+1
}
\ index `Matches:with` CheckValue:equals(max)
}
unittest valueOrder {
HashedMap<Int, Int> map <- HashedMap<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
}
Vector<Bool> visited <- Vector:createSize<Bool>(max)
// Validate traversal coverage.
Int index <- 0
traverse (map.valueOrder() -> Int value) {
\ visited.readAt(value) `Matches:with` CheckValue:equals(false)
\ visited.writeAt(value, true)
index <- index+1
}
\ index `Matches:with` CheckValue:equals(max)
}
unittest duplicate {
HashedMap<Int, Int> map <- HashedMap<Int, Int>.default()
Int max <- 31
$ReadOnly[max]$
traverse (Counter.zeroIndexed(max) -> Int i) {
\ map.set(i, i)
}
HashedMap<Int, Int> copy <- map.duplicate()
\ copy.size() `Matches:with` CheckValue:equals(map.size())
traverse (Counter.zeroIndexed(max) -> Int i) {
$Hidden[map]$
\ copy.get(i) `Matches:with` CheckValue:equals(i)
}
\ copy.set(2, 5)
\ copy.remove(7)
$Hidden[copy]$
\ map.size() `Matches:with` CheckValue:equals(max)
traverse (Counter.zeroIndexed(max) -> Int i) {
\ map.get(i) `Matches:with` CheckValue:equals(i)
}
}
unittest duplicateEmpty {
HashedMap<Int, Int> map <- HashedMap<Int, Int>.new()
HashedMap<Int, Int> copy <- map.duplicate()
\ map.set(1, 1)
\ copy.size() `Matches:with` CheckValue:equals(0)
}
unittest weakSetExists {
HashedMap<Int, Value> map <- HashedMap<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)
\ map.size() `Matches:with` CheckValue:equals(3)
\ value0.get() `Matches:with` CheckValue:equals(5)
\ value1.get() `Matches:with` CheckValue:equals(1)
\ value0.set(10)
\ value1.get() `Matches:with` CheckValue:equals(1)
\ map.get(1) `Matches:with` CheckValue:equals(Value.new(1))
}
unittest weakSetMissing {
HashedMap<Int, Value> map <- HashedMap<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)
\ map.size() `Matches:with` CheckValue:equals(4)
\ value0.get() `Matches:with` CheckValue:equals(5)
\ value1.get() `Matches:with` CheckValue:equals(5)
\ value0.set(10)
\ value1.get() `Matches:with` CheckValue:equals(10)
\ map.get(7) `Matches:with` CheckValue:equals(Value.new(10))
}
unittest setMember {
HashedMap<Int, Value> map <- HashedMap<Int, Value>.default()
.set(1, Value.new(1))
.set(2, Value.new(2))
.set(3, Value.new(3))
\ map.member(1) `Matches:with` CheckValue:equals(true)
\ map.member(5) `Matches:with` CheckValue:equals(false)
}
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()
}
}