/* ----------------------------------------------------------------------------- Copyright 2019-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 "SortedMap tests" { success TestChecker } unittest integrationTest { ValidatedTree map <- ValidatedTree.new() 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 { SortedMap map <- SortedMap.new().set(1, 1).set(2, 2).set(4, 4) \ map.remove(3) \ map.size() `Matches:with` CheckValue:equals(3) } unittest defaultOrder { SortedMap map <- SortedMap.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 entry) { \ entry.getKey() `Matches:with` CheckValue:equals(index) \ (entry.getValue()*hash)%max `Matches:with` CheckValue:equals(entry.getKey()) index <- index+1 } \ index `Matches:with` CheckValue:equals(max) } unittest defaultOrderEmpty { traverse (SortedMap.new().defaultOrder() -> _) { fail("not empty") } } unittest keyOrder { SortedMap map <- SortedMap.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) { \ key `Matches:with` CheckValue:equals(index) index <- index+1 } \ index `Matches:with` CheckValue:equals(max) } unittest valueOrder { SortedMap map <- SortedMap.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) { \ (value*hash)%max `Matches:with` CheckValue:equals(index) index <- index+1 } \ index `Matches:with` CheckValue:equals(max) } unittest reverseOrder { SortedMap map <- SortedMap.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 entry) { index <- index-1 \ entry.getKey() `Matches:with` CheckValue:equals(index) \ (entry.getValue()*hash)%max `Matches:with` CheckValue:equals(entry.getKey()) } \ index `Matches:with` CheckValue:equals(0) } unittest getForward { SortedMap map <- SortedMap.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 entry) { \ entry.getKey() `Matches:with` CheckValue:equals(index) \ (entry.getValue()*hash)%max `Matches:with` CheckValue:equals(entry.getKey()) index <- index+1 } \ index `Matches:with` CheckValue:equals(max) } } unittest getForwardNotFound { SortedMap map <- SortedMap.new() \ map.set(1, 1).set(3, 3).set(5, 5) scoped { optional Order> start <- map.getForward(4) } in if (!present(start)) { fail("Failed") } else { \ require(start).get().getKey() `Matches:with` CheckValue:equals(5) } scoped { optional Order> start <- map.getForward(6) } in if (present(start)) { fail("Failed") } } unittest getReverse { SortedMap map <- SortedMap.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 entry) { \ entry.getKey() `Matches:with` CheckValue:equals(index) \ (entry.getValue()*hash)%max `Matches:with` CheckValue:equals(entry.getKey()) index <- index-1 } \ index `Matches:with` CheckValue:equals(-1) } } unittest getReverseNotFound { SortedMap map <- SortedMap.new() \ map.set(1, 1).set(3, 3).set(5, 5) scoped { optional Order> start <- map.getReverse(2) } in if (!present(start)) { fail("Failed") } else { \ require(start).get().getKey() `Matches:with` CheckValue:equals(1) } scoped { optional Order> start <- map.getReverse(0) } in if (present(start)) { fail("Failed") } } unittest duplicate { SortedMap map <- SortedMap.default() Int max <- 31 $ReadOnly[max]$ traverse (Counter.zeroIndexed(max) -> Int i) { \ map.set(i, i) } SortedMap 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 { SortedMap map <- SortedMap.new() SortedMap copy <- map.duplicate() \ map.set(1, 1) \ copy.size() `Matches:with` CheckValue:equals(0) } unittest weakSetExists { SortedMap map <- SortedMap.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 { SortedMap map <- SortedMap.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 { SortedMap map <- SortedMap.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 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() } }