(use-modules (d-) (d- test) (srfi srfi-1)) (define-test-suite "~>" (define-test "unwrapped" (define value (~> 1 1+ 1+)) (assert-equal 3 value)) (define-test "wrapped" (define value (~> 1 (/ 2) (/ 2))) (assert-equal 1/4 value))) (define-test-suite "~>>" (define-test "unwrapped" (define value (~>> 1 1+ 1+)) (assert-equal 3 value)) (define-test "wrapped" (define value (~>> 1 (/ 2) (/ 2))) (assert-equal 1 value))) (define-test-suite "as~>" (define-test "switch sides" (define value (as~> % 'apples (cons % '()) (cons 'pears %))) (assert-equal '(pears apples) value)) (define-test "middle argument" (define value (as~> % '((apples . pears)) (assoc 'apples % equal?))) (assert-equal '(apples . pears) value))) (define-test-suite "if-not" (define-test "test" (assert-equal 'a (if-not #f 'a 'b)))) (define-test-suite "for" (define-test "permutation" (define value (for ([i (iota 2)] [j (iota 2)]) (cons i j))) (assert-equal '((0 . 0) (0 . 1) (1 . 0) (1 . 1)) value))) (define-test-suite "if-let" (define-test "positive-case" (assert-equal 2 (if-let ([v (or #f 1)]) (+ v 1)))) (define-test "negative-case" (assert-equal #f (if-let ([v (and #f 1)]) (+ v 1))))) (define-test-suite "partial" (define-test "test" (define value (partial / 2)) (assert-equal 1/2 (value 4)) (assert-equal 2 (value 1)))) (define-test-suite "argmin" (define-test "test" (assert-equal '(b . 2) (argmin cdr < '(a . 5) '(b . 2) '(c . 3))))) (define-test-suite "iterate" (define-test "test" (define value (iterate 2 1+ 3)) (assert-equal 5 value))) (define-test-suite "upply" (define-test "test" (define value (upply -3 5 = abs 1-)) (assert-equal #t value))) (define-test-suite "conjoin" (define-test "test" (assert-equal #t ((conjoin negative? odd? rational?) -3)) (assert-equal #f ((conjoin negative? odd? rational?) -4))) (define-test "vacuous" (assert-equal #t ((conjoin) '(some donkus))))) (define-test-suite "generator" (define-test "test" (define number-generator (generator (let loop ([i 1]) (yield i) (loop (1+ i))))) (assert-equal 1 (number-generator)) (assert-equal 2 (number-generator)) (assert-equal 3 (number-generator)) (assert-equal 4 (number-generator)))) (define-test-suite "amb" (define-test "liars" (define (liars) (amb-reset) (let ([betty (amb 1 2 3 4 5)] [ethel (amb 1 2 3 4 5)] [joan (amb 1 2 3 4 5)] [kitty (amb 1 2 3 4 5)] [mary (amb 1 2 3 4 5)]) ;; The "Liars" problem (amb-require (distinct? betty ethel joan kitty mary)) (amb-require (or (= kitty 1) (= betty 3))) (amb-require (or (= ethel 1) (= joan 2))) (amb-require (or (= joan 3) (= ethel 5))) (amb-require (or (= kitty 2) (= mary 4))) (amb-require (or (= mary 4) (= betty 1))) (list betty ethel joan kitty mary))) (assert-equal '(3 5 2 1 4) (liars))))