~ chicken-core (chicken-5) /tests/loopy-test.scm
Trap1(import (only chicken.format printf)2 (only chicken.time current-process-milliseconds)3 chicken.load)45(load-relative "loopy-loop.scm")6(load-relative "matchable.scm")789;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;10;; SRFI-64 subset + test-approx=1112(define *pass* 0)13(define *fail* 0)14(define *start* 0)1516(define (run-test name thunk expect eq pass-msg fail-msg)17 (let ((result (thunk)))18 (cond19 ((eq expect result)20 (set! *pass* (+ *pass* 1))21 (format-result pass-msg name expect result))22 (else23 (set! *fail* (+ *fail* 1))24 (format-result fail-msg name expect result)))))2526(define (format-result ls name expect result)27 (let lp ((ls ls))28 (cond29 ((null? ls) (newline))30 ((eq? (car ls) 'expect) (display expect) (lp (cdr ls)))31 ((eq? (car ls) 'result) (display result) (lp (cdr ls)))32 ((eq? (car ls) 'name) (if name (begin (display #\space) (display name))) (lp (cdr ls)))33 (else (display (car ls)) (lp (cdr ls))))))3435(define (test-begin . o)36 (set! *pass* 0)37 (set! *fail* 0)38 (set! *start* (current-process-milliseconds)))3940(define (format-float n prec)41 (let* ((str (number->string n))42 (len (string-length str)))43 (let lp ((i (- len 1)))44 (cond45 ((negative? i)46 (string-append str "." (make-string prec #\0)))47 ((eqv? #\. (string-ref str i))48 (let ((diff (+ 1 (- prec (- len i)))))49 (cond50 ((positive? diff)51 (string-append str (make-string diff #\0)))52 ((negative? diff)53 (substring str 0 (+ i prec 1)))54 (else55 str))))56 (else57 (lp (- i 1)))))))5859(define (format-percent num denom)60 (let ((x (if (zero? denom) num (exact->inexact (/ num denom)))))61 (format-float (* 100 x) 2)))6263(define (test-end . o)64 (let ((end (current-process-milliseconds))65 (total (+ *pass* *fail*)))66 (printf " ~A tests completed in ~A seconds\n"67 total (format-float (exact->inexact (/ (- end *start*) 1000)) 3))68 (printf " ~A (~A%) tests passed\n"69 *pass* (format-percent *pass* total))70 (printf " ~A (~A%) tests failed\n"71 *fail* (format-percent *fail* total))))7273(define-syntax test-assert74 (syntax-rules ()75 ((_ x opt)76 (run-assert x (lambda () opt)))77 ((_ x ) (run-assert 'x (lambda () x)))))7879(define (run-equal name thunk expect eq)80 (run-test name thunk expect eq81 '("(PASS)" name)82 '("(FAIL)" name ": expected " expect " but got " result)))8384(define-syntax test-equal85 (syntax-rules ()86 ((_ x y opt)87 (run-equal x (lambda () y) opt equal?))88 ((_ x y) (run-equal 'x (lambda () x) y equal?))))8990;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;91;; run tests9293(test-begin "loop")9495(test-equal96 "stepping"97 (loop lp ((i 0 (+ i 1)) (res '() (cons i res)))98 (if (= i 3)99 (reverse res)100 (lp)))101 '(0 1 2))102103(test-equal104 "basic in-list"105 (let ((res '()))106 (loop ((x <- in-list '(a b c)))107 (set! res (cons x res)))108 res)109 '(c b a))110111(test-equal112 "in-list with result"113 (loop ((x <- in-list '(a b c))114 (res '() (cons x res)))115 => res)116 '(c b a))117118(test-equal119 "in-list with collecting"120 (loop ((x <- in-list '(a b c)) (res <- collecting x)) => res)121 '(a b c))122123(test-equal124 "uneven length in-list's"125 (loop ((x <- in-list '(a b c))126 (y <- in-list '(1 2 3 4))127 (res <- collecting (cons x y)))128 => res)129 '((a . 1) (b . 2) (c . 3)))130131(test-equal132 "in-lists"133 (loop ((ls <- in-lists '((a b c) (1 2 3)))134 (res <- collecting ls))135 => res)136 '((a 1) (b 2) (c 3)))137138(define (flatten ls)139 (reverse140 (loop lp ((x ls <- in-list ls) (res '()))141 => res142 (if (pair? x)143 (lp res <- (lp ls <- x))144 (lp res <- (cons x res))))))145146(test-equal147 "flatten (recursion test)"148 (flatten '(1 (2) (3 (4 (5)) 6) 7))149 '(1 2 3 4 5 6 7))150151(test-equal152 "in-string"153 (loop ((c <- in-string "hello") (res <- collecting c)) => res)154 '(#\h #\e #\l #\l #\o))155156(test-equal157 "in-string with start"158 (loop ((c <- in-string "hello" 3) (res <- collecting c)) => res)159 '(#\l #\o))160161(test-equal162 "in-string with start and end"163 (loop ((c <- in-string "hello" 0 4) (res <- collecting c)) => res)164 '(#\h #\e #\l #\l))165166(test-equal167 "in-string with start, end and step"168 (loop ((c <- in-string "hello" 1 4 2) (res <- collecting c)) => res)169 '(#\e #\l))170171(test-equal172 "in-string-reverse"173 (loop ((c <- in-string-reverse "hello") (res <- collecting c)) => res)174 '(#\o #\l #\l #\e #\h))175176(test-equal177 "in-vector"178 (loop ((x <- in-vector '#(1 2 3)) (res <- collecting x)) => res)179 '(1 2 3))180181(test-equal182 "in-permutations"183 (loop ((p <- in-permutations '(a b c)) (res <- collecting p)) => res)184 '((a b c) (a c b) (b a c) (b c a) (c a b) (c b a)))185186(test-equal187 "in-permutations with length"188 (loop ((p <- in-permutations '(a b c) 2) (res <- collecting p)) => res)189 '((a b) (a c) (b a) (b c) (c a) (c b)))190191(test-equal192 "in-combinations"193 (loop ((p <- in-combinations '(a b c) 2) (res <- collecting p)) => res)194 '((a b) (a c) (b c)))195196(test-end "loop")197