/* ----------------------------------------------------------------------------- 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 "counter tests" { success TestChecker } unittest zeroIndexed { Int index <- 0 traverse (Counter.zeroIndexed(10) -> Int i) { \ i `Matches:with` CheckValue:equals(index) index <- index+1 } \ index `Matches:with` CheckValue:equals(10) } unittest zeroIndexedEmpty { traverse (Counter.zeroIndexed(0) -> Int i) { fail("not empty") } } unittest revZeroIndexed { Int index <- 9 traverse (Counter.revZeroIndexed(10) -> Int i) { \ i `Matches:with` CheckValue:equals(index) index <- index-1 } \ index `Matches:with` CheckValue:equals(-1) } unittest revZeroIndexedEmpty { traverse (Counter.revZeroIndexed(0) -> Int i) { fail("not empty") } } unittest unlimitedCount { Int index <- 0 traverse (Counter.unlimited() -> Int i) { \ i `Matches:with` CheckValue:equals(index) if ((index <- index+1) >= 10) { break } } \ index `Matches:with` CheckValue:equals(10) } unittest builderReversePositive { // Forward sequence: 3 8 13 Int index <- 13 traverse (Counter.builder() .start(3) .increment(5) .limit(17) .reverse() .done() -> Int i) { \ i `Matches:with` CheckValue:equals(index) index <- index-5 } \ index `Matches:with` CheckValue:equals(-2) } unittest builderReverseNegative { // Forward sequence: 17 12 7 Int index <- 7 traverse (Counter.builder() .start(17) .increment(-5) .limit(3) .reverse() .done() -> Int i) { \ i `Matches:with` CheckValue:equals(index) index <- index+5 } \ index `Matches:with` CheckValue:equals(22) } unittest times { Int index <- 0 traverse ("message" `Repeat:times` 10 -> String m) { \ m `Matches:with` CheckValue:equals("message") index <- index+1 } \ index `Matches:with` CheckValue:equals(10) } unittest timesEmpty { traverse ("message" `Repeat:times` 0 -> _) { fail("not empty") } } unittest unlimitedValue { Int index <- 0 traverse (Repeat:unlimited("message") -> String m) { \ m `Matches:with` CheckValue:equals("message") if ((index <- index+1) >= 10) { break } } \ index `Matches:with` CheckValue:equals(10) } unittest minMax { scoped { Int x, Int y <- Ranges:minMax(10, 3) } in { \ x `Matches:with` CheckValue:equals(3) \ y `Matches:with` CheckValue:equals(10) } scoped { Int x, Int y <- Ranges:minMax(3, 10) } in { \ x `Matches:with` CheckValue:equals(3) \ y `Matches:with` CheckValue:equals(10) } } unittest min { scoped { Int x <- Ranges:min(10, 3) } in \ x `Matches:with` CheckValue:equals(3) scoped { Int x <- Ranges:min(3, 10) } in \ x `Matches:with` CheckValue:equals(3) } unittest max { scoped { Int x <- Ranges:max(10, 3) } in \ x `Matches:with` CheckValue:equals(10) scoped { Int x <- Ranges:max(3, 10) } in \ x `Matches:with` CheckValue:equals(10) }