/* ----------------------------------------------------------------------------- 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 "basic list tests" { success TestChecker timeout 5 } unittest forwardTraverse { optional LinkedNode head, _ <- LinkedNode.builder() .append(3) .append(2) .append(5) .append(1) .append(6) .append(4) .build() DefaultOrder expected <- Vector.new() .append(3) .append(2) .append(5) .append(1) .append(6) .append(4) traverse (expected.defaultOrder() -> Int value) { \ require(head).get() `Matches:with` CheckValue:equals(value) } update { head <- require(head).next() } \ present(head) `Matches:with` CheckValue:equals(false) } unittest reverseTraverse { // NOTE: Since the list is weak in reverse, we need to store head. optional LinkedNode head, optional LinkedNode tail <- LinkedNode.builder() .append(3) .append(2) .append(5) .append(1) .append(6) .append(4) .build() DefaultOrder expected <- Vector.new() .append(4) .append(6) .append(1) .append(5) .append(2) .append(3) traverse (expected.defaultOrder() -> Int value) { \ require(tail).get() `Matches:with` CheckValue:equals(value) } update { tail <- require(tail).prev() } \ present(tail) `Matches:with` CheckValue:equals(false) } unittest setNext { optional LinkedNode head1, _ <- LinkedNode.builder() .append(3) .append(2) .append(5) .append(1) .append(6) .append(4) .build() optional LinkedNode after1 <- Helper.jumpBy(3, head1) optional LinkedNode before1 <- require(after1).prev() optional LinkedNode head2, _ <- LinkedNode.builder() .append(13) .append(12) .append(15) .append(11) .append(16) .append(14) .build() optional LinkedNode after2 <- Helper.jumpBy(3, head2) optional LinkedNode before2 <- require(after2).prev() \ require(require(before1).setNext(after2)).get() `Matches:with` CheckValue:equals(1) \ present(require(after1).prev()) `Matches:with` CheckValue:equals(false) \ require(require(before1).next()).get() `Matches:with` CheckValue:equals(11) \ require(require(after2).prev()).get() `Matches:with` CheckValue:equals(5) \ present(require(before2).next()) `Matches:with` CheckValue:equals(false) } unittest setPrev { optional LinkedNode head1, _ <- LinkedNode.builder() .append(3) .append(2) .append(5) .append(1) .append(6) .append(4) .build() optional LinkedNode after1 <- Helper.jumpBy(3, head1) optional LinkedNode before1 <- require(after1).prev() optional LinkedNode head2, _ <- LinkedNode.builder() .append(13) .append(12) .append(15) .append(11) .append(16) .append(14) .build() optional LinkedNode after2 <- Helper.jumpBy(3, head2) optional LinkedNode before2 <- require(after2).prev() \ require(require(after1).setPrev(before2)).get() `Matches:with` CheckValue:equals(5) \ require(require(after1).prev()).get() `Matches:with` CheckValue:equals(15) \ present(require(before1).next()) `Matches:with` CheckValue:equals(false) \ present(require(after2).prev()) `Matches:with` CheckValue:equals(false) \ require(require(before2).next()).get() `Matches:with` CheckValue:equals(1) } unittest setNextSame { optional LinkedNode head, _ <- LinkedNode.builder() .append(3) .append(2) .append(5) .append(1) .append(6) .append(4) .build() optional LinkedNode after <- Helper.jumpBy(3, head) optional LinkedNode before <- require(after).prev() \ require(before).setNext(after) \ require(require(after).prev()).get() `Matches:with` CheckValue:equals(5) \ require(require(before).next()).get() `Matches:with` CheckValue:equals(1) } unittest setPrevSame { optional LinkedNode head, _ <- LinkedNode.builder() .append(3) .append(2) .append(5) .append(1) .append(6) .append(4) .build() optional LinkedNode after <- Helper.jumpBy(3, head) optional LinkedNode before <- require(after).prev() \ require(after).setPrev(before) \ require(require(after).prev()).get() `Matches:with` CheckValue:equals(5) \ require(require(before).next()).get() `Matches:with` CheckValue:equals(1) } unittest setNextSelf { optional LinkedNode head, _ <- LinkedNode.builder() .append(3) .append(2) .append(5) .append(1) .append(6) .append(4) .build() optional LinkedNode after <- Helper.jumpBy(3, head) optional LinkedNode before <- require(after).prev() \ require(before).setNext(before) \ present(require(after).prev()) `Matches:with` CheckValue:equals(false) \ require(require(before).prev()).get() `Matches:with` CheckValue:equals(5) \ require(require(before).next()).get() `Matches:with` CheckValue:equals(5) } unittest setPrevSelf { optional LinkedNode head, _ <- LinkedNode.builder() .append(3) .append(2) .append(5) .append(1) .append(6) .append(4) .build() optional LinkedNode after <- Helper.jumpBy(3, head) optional LinkedNode before <- require(after).prev() \ require(after).setPrev(after) \ present(require(before).next()) `Matches:with` CheckValue:equals(false) \ require(require(after).prev()).get() `Matches:with` CheckValue:equals(1) \ require(require(after).next()).get() `Matches:with` CheckValue:equals(1) } unittest duplicate { optional LinkedNode head, _ <- LinkedNode.builder() .append(3) .append(2) .append(5) .append(1) .append(6) .append(4) .build() DefaultOrder expected <- Vector.new() .append(3) .append(2) .append(5) .append(1) .append(6) .append(4) optional LinkedNode head2 <- require(head).duplicate() \ require(Helper.jumpBy(3, head)).set(7).setNext(empty) $Hidden[head]$ traverse (expected.defaultOrder() -> Int value) { \ require(head2).get() `Matches:with` CheckValue:equals(value) } update { head2 <- require(head2).next() } \ present(head2) `Matches:with` CheckValue:equals(false) } unittest hugeCleanup { $DisableCoverage$ scoped { ListBuilder> builder <- LinkedNode.builder() } in traverse (Counter.zeroIndexed(1000000) -> _) { \ builder.append(0) } } unittest forwardTraverseSingle { optional ForwardNode head, _ <- ForwardNode.builder() .append(3) .append(2) .append(5) .append(1) .append(6) .append(4) .build() DefaultOrder expected <- Vector.new() .append(3) .append(2) .append(5) .append(1) .append(6) .append(4) traverse (expected.defaultOrder() -> Int value) { \ require(head).get() `Matches:with` CheckValue:equals(value) } update { head <- require(head).next() } \ present(head) `Matches:with` CheckValue:equals(false) } unittest duplicateSingle { optional ForwardNode head, _ <- ForwardNode.builder() .append(3) .append(2) .append(5) .append(1) .append(6) .append(4) .build() DefaultOrder expected <- Vector.new() .append(3) .append(2) .append(5) .append(1) .append(6) .append(4) optional ForwardNode head2 <- require(head).duplicate() \ require(Helper.jumpBy(3, head)).set(7).setNext(empty) $Hidden[head]$ traverse (expected.defaultOrder() -> Int value) { \ require(head2).get() `Matches:with` CheckValue:equals(value) } update { head2 <- require(head2).next() } \ present(head2) `Matches:with` CheckValue:equals(false) } unittest hugeCleanupSingle { $DisableCoverage$ scoped { ListBuilder> builder <- ForwardNode.builder() } in traverse (Counter.zeroIndexed(1000000) -> _) { \ builder.append(0) } } concrete Helper { @type jumpBy<#x> #x requires Order (Int, optional #x) -> (optional #x) } define Helper { jumpBy (n, x) (y) { y <- x traverse (Counter.zeroIndexed(n) -> _) { y <- require(y).next() } } }