/* ----------------------------------------------------------------------------- Copyright 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 "HashedSet tests" { success TestChecker } unittest addAndRemove { HashedSet set <- HashedSet.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 { HashedSet set <- HashedSet.new() \ set.append(3) \ set.size() `Matches:with` CheckValue:equals(1) \ set.member(3) `Matches:with` CheckValue:equals(true) } unittest removeNotPresent { HashedSet set <- HashedSet.new().add(1).add(2).add(4) \ set.remove(3) \ set.size() `Matches:with` CheckValue:equals(3) } unittest defaultOrder { HashedSet set <- HashedSet.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) } Vector visited <- Vector:createSize(max) // Validate traversal coverage. Int index <- 0 traverse (set.defaultOrder() -> Int entry) { \ visited.readAt(entry) `Matches:with` CheckValue:equals(false) \ visited.writeAt(entry, true) index <- index+1 } \ index `Matches:with` CheckValue:equals(max) } unittest defaultOrderEmpty { traverse (HashedSet.new().defaultOrder() -> _) { fail("not empty") } } unittest duplicate { HashedSet set <- HashedSet.new().add(1).add(2) HashedSet 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) }