;;;; stack-test.scm -*- Scheme -*- ;;;; Kon Lovett, Jul '18 (import test) (test-begin "Stack") ;;; (import stack) (test-group "Empty Stack" (test-assert (make-stack)) (test-assert (not (eq? (make-stack) (make-stack)))) (test-assert (stack? (make-stack))) (test-assert (stack-empty? (make-stack))) (test 0 (stack-count (make-stack))) ) (test-group "Push!/Pop!/Peek/Poke!" (let ((stk (make-stack))) (stack-push! stk 1) (stack-push! stk 2 3) (test 3 (stack-count stk)) (test 3 (stack-pop! stk)) (test 2 (stack-pop! stk)) (test 1 (stack-pop! stk)) (test-assert (stack-empty? stk)) (test-error (stack-pop! stk)) (stack-push! stk 1 2 3) (test 2 (stack-peek stk 1)) (stack-poke! stk 4 1) (test 3 (stack-pop! stk)) (test 4 (stack-pop! stk)) (test 1 (stack-count stk)) ) ) (test-group "Cut!" (let ((stk (make-stack))) ;3 2 1 (stack-push! stk 1 2 3) (test '(2) (stack-cut! stk 1 2)) (test 2 (stack-count stk)) ;3 1 (test '(3) (stack-cut! stk 0 1)) (test 1 (stack-count stk)) ;5 4 1 (stack-push! stk 4 5) (test '(4 1) (stack-cut! stk 1 3)) (test 1 (stack-count stk)) ; (test-error (stack-cut! stk -1 3)) (test-error (stack-cut! stk 0 3)) (test-error (stack-cut! stk 0 -3)) ) ) (test-group "Stack from List" (let ((stk (make-stack)) (stk1 (list->stack '(1 2 3)))) ; (stack-push! stk 1 2 3) (test '(3 2 1) (stack->list stk)) ; (test-assert (stack? stk1)) (test 3 (stack-count stk1)) (test 1 (stack-pop! stk1)) ) ) (test-group "Stack HOF" (let ((stk (list->stack '(1 2 3)))) (test 6 (stack-fold stk + 0)) (test '(2 3 4) (stack-map stk add1)) ) ) ;;; (test-end "Stack") (test-exit)