/* ----------------------------------------------------------------------------- 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 "SortedSet tests" { success TestChecker } unittest addAndRemove { SortedSet set <- SortedSet.default() Int hash <- 13 Int max <- 20 $ReadOnly[hash, max]$ // Populate the set in a pseudo-random order. traverse (Counter.zeroIndexed(max) -> Int i) { \ set.add((i*hash)%max) \ set.size() `Matches:with` CheckValue:equals(i+1) } // Remove only the odd elements. traverse (Counter.zeroIndexed(max) -> Int i) { if (i%2 == 1) { // Remove it twice to ensure idempotence. \ set.remove(i).remove(i) } \ set.size() `Matches:with` CheckValue:equals(max-(i+1)/2) } // Validate set membership. traverse (Counter.zeroIndexed(max) -> Int i) { \ set.member(i) `Matches:with` CheckValue:equals(i%2 == 0) } } unittest append { SortedSet set <- SortedSet.new() \ set.append(3) \ set.size() `Matches:with` CheckValue:equals(1) \ set.member(3) `Matches:with` CheckValue:equals(true) } unittest removeNotPresent { SortedSet set <- SortedSet.new().add(1).add(2).add(4) \ set.remove(3) \ set.size() `Matches:with` CheckValue:equals(3) } unittest defaultOrder { SortedSet set <- SortedSet.new() Int hash <- 13 Int max <- 20 $ReadOnly[hash, max]$ // Populate the set in a pseudo-random order. traverse (Counter.zeroIndexed(max) -> Int i) { \ set.add((i*hash)%max) } // Validate the traversal order. Int index <- 0 traverse (set.defaultOrder() -> Int entry) { \ entry `Matches:with` CheckValue:equals(index) index <- index+1 } \ index `Matches:with` CheckValue:equals(max) } unittest defaultOrderEmpty { traverse (SortedSet.new().defaultOrder() -> _) { fail("not empty") } } unittest reverseOrder { SortedSet set <- SortedSet.new() Int hash <- 13 Int max <- 20 $ReadOnly[hash, max]$ // Populate the set in a pseudo-random order. traverse (Counter.zeroIndexed(max) -> Int i) { \ set.add((i*hash)%max) } // Validate the traversal order. Int index <- max traverse (set.reverseOrder() -> Int entry) { index <- index-1 \ entry `Matches:with` CheckValue:equals(index) } \ index `Matches:with` CheckValue:equals(0) } unittest getForward { SortedSet set <- SortedSet.new() Int hash <- 13 Int max <- 20 $ReadOnly[hash, max]$ // Populate the set in a pseudo-random order. traverse (Counter.zeroIndexed(max) -> Int i) { \ set.add((i*hash)%max) } // Validate the traversal order from every starting point. traverse (Counter.zeroIndexed(max) -> Int i) { Int index <- i traverse (set.getForward(index) -> Int entry) { \ entry `Matches:with` CheckValue:equals(index) index <- index+1 } \ index `Matches:with` CheckValue:equals(max) } } unittest getForwardNotFound { SortedSet set <- SortedSet.new() \ set.add(1).add(3).add(5) scoped { optional Order start <- set.getForward(4) } in if (!present(start)) { fail("Failed") } else { \ require(start).get() `Matches:with` CheckValue:equals(5) } scoped { optional Order start <- set.getForward(6) } in if (present(start)) { fail("Failed") } } unittest getReverse { SortedSet set <- SortedSet.new() Int hash <- 13 Int max <- 20 $ReadOnly[hash, max]$ // Populate the set in a pseudo-random order. traverse (Counter.zeroIndexed(max) -> Int i) { \ set.add((i*hash)%max) } // Validate the traversal order from every starting point. traverse (Counter.zeroIndexed(max) -> Int i) { Int index <- i traverse (set.getReverse(index) -> Int entry) { \ entry `Matches:with` CheckValue:equals(index) index <- index-1 } \ index `Matches:with` CheckValue:equals(-1) } } unittest getReverseNotFound { SortedSet set <- SortedSet.new() \ set.add(1).add(3).add(5) scoped { optional Order start <- set.getReverse(2) } in if (!present(start)) { fail("Failed") } else { \ require(start).get() `Matches:with` CheckValue:equals(1) } scoped { optional Order start <- set.getReverse(0) } in if (present(start)) { fail("Failed") } } unittest duplicate { SortedSet set <- SortedSet.new().add(1).add(2) SortedSet copy <- set.duplicate() \ copy.size() `Matches:with` CheckValue:equals(2) \ copy.member(1) `Matches:with` CheckValue:equals(true) \ copy.member(1) `Matches:with` CheckValue:equals(true) \ copy.remove(2) \ copy.size() `Matches:with` CheckValue:equals(1) \ copy.member(1) `Matches:with` CheckValue:equals(true) \ set.member(2) `Matches:with` CheckValue:equals(true) }