/* ----------------------------------------------------------------------------- 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 "CategoricalTree tests" { success TestChecker } unittest simpleInsertion { CategoricalTree tree <- CategoricalTree.default() .setWeight(1, 3) .setWeight(6, 2) .setWeight(3, 1) .setWeight(5, 4) .setWeight(4, 3) .setWeight(2, 7) Vector expected <- Vector.new() .push(1).push(1).push(1) .push(2).push(2).push(2).push(2).push(2).push(2).push(2) .push(3) .push(4).push(4).push(4) .push(5).push(5).push(5).push(5) .push(6).push(6) \ tree.getTotal() `Matches:with` CheckValue:equals(expected.size()) \ tree.getWeight(1) `Matches:with` CheckValue:equals(3) \ tree.getWeight(2) `Matches:with` CheckValue:equals(7) \ tree.getWeight(3) `Matches:with` CheckValue:equals(1) \ tree.getWeight(4) `Matches:with` CheckValue:equals(3) \ tree.getWeight(5) `Matches:with` CheckValue:equals(4) \ tree.getWeight(6) `Matches:with` CheckValue:equals(2) traverse (Counter.zeroIndexed(tree.getTotal()) -> Int pos) { \ tree.locate(pos) `Matches:with` CheckValue:equals(expected.readAt(pos)) } } unittest modifySizes { CategoricalTree tree <- CategoricalTree.new() .setWeight(1, 3) .setWeight(6, 2) .setWeight(3, 1) .setWeight(5, 4) .setWeight(4, 3) .setWeight(2, 7) .setWeight(7, 3) // modifications start here .setWeight(5, 1) .setWeight(3, 0) Vector expected <- Vector.new() .push(1).push(1).push(1) .push(2).push(2).push(2).push(2).push(2).push(2).push(2) .push(4).push(4).push(4) .push(5) .push(6).push(6) .push(7).push(7).push(7) \ tree.getTotal() `Matches:with` CheckValue:equals(expected.size()) \ tree.getWeight(1) `Matches:with` CheckValue:equals(3) \ tree.getWeight(2) `Matches:with` CheckValue:equals(7) \ tree.getWeight(3) `Matches:with` CheckValue:equals(0) \ tree.getWeight(4) `Matches:with` CheckValue:equals(3) \ tree.getWeight(5) `Matches:with` CheckValue:equals(1) \ tree.getWeight(6) `Matches:with` CheckValue:equals(2) \ tree.getWeight(7) `Matches:with` CheckValue:equals(3) traverse (Counter.zeroIndexed(tree.getTotal()) -> Int pos) { \ tree.locate(pos) `Matches:with` CheckValue:equals(expected.readAt(pos)) } // Use ReadAt aliases for the same functionality. traverse (Counter.zeroIndexed(tree.size()) -> Int pos) { \ tree.readAt(pos) `Matches:with` CheckValue:equals(expected.readAt(pos)) } } unittest increment { CategoricalTree tree <- CategoricalTree.new() .incrWeight(1) .incrWeight(1) .incrWeight(1) \ tree.getTotal() `Matches:with` CheckValue:equals(3) \ tree.getWeight(1) `Matches:with` CheckValue:equals(3) } unittest decrement { CategoricalTree tree <- CategoricalTree.new() .setWeight(1, 10) .decrWeight(1) .decrWeight(1) .decrWeight(1) \ tree.getTotal() `Matches:with` CheckValue:equals(7) \ tree.getWeight(1) `Matches:with` CheckValue:equals(7) } unittest duplicate { CategoricalTree tree <- CategoricalTree.new().setWeight(1, 1).setWeight(2, 2) CategoricalTree copy <- tree.duplicate() \ copy.size() `Matches:with` CheckValue:equals(3) \ copy.getWeight(1) `Matches:with` CheckValue:equals(1) \ copy.getWeight(2) `Matches:with` CheckValue:equals(2) \ copy.setWeight(2, 0) \ copy.size() `Matches:with` CheckValue:equals(1) \ copy.getWeight(1) `Matches:with` CheckValue:equals(1) \ tree.getWeight(2) `Matches:with` CheckValue:equals(2) } unittest integrationTest { $DisableCoverage$ ValidatedTree tree <- ValidatedTree.new() SortedMap expected <- SortedMap.new() Int count <- 137 Int maxSize <- 15 Int hash1 <- 379 Int hash2 <- 457 $ReadOnly[count, maxSize, hash1, hash2]$ // Populate the tree with arbitrary data. traverse (Counter.zeroIndexed(count) -> Int i) { \ tree.setWeight((i*hash1)%count, (i*hash2)%maxSize) } // Overwrite the tree with real data. traverse (Counter.zeroIndexed(count) -> Int i) { Int key <- (i*hash2)%count Int size <- (i*hash1)%maxSize \ tree.setWeight(key, size) \ expected.set(key, size) } optional Order> node <- expected.defaultOrder() Int key <- 0 Int remaining <- 0 traverse (Counter.zeroIndexed(tree.getTotal()) -> Int pos) { while (remaining < 1) { key <- require(node).get().getKey() remaining <- require(node).get().getValue() node <- require(node).next() } $ReadOnly[key]$ $Hidden[node]$ \ tree.locate(pos) `Matches:with` CheckValue:equals(key) remaining <- remaining-1 } \ present(node) `Matches:with` CheckValue:equals(false) \ remaining `Matches:with` CheckValue:equals(0) } testcase "negative size crashes" { failure require "size.+negative" } unittest test { \ CategoricalTree.new().setWeight(0, -10) } testcase "negative position crashes" { failure require "position.+negative" } unittest test { \ CategoricalTree.new().locate(-10) } testcase "position past end crashes" { failure require "position.+total" } unittest test { \ CategoricalTree.new().setWeight(0, 1).locate(1) }