d-/test.scm

103 lines
2.9 KiB
Scheme
Raw Permalink Normal View History

2024-10-23 09:02:27 -05:00
(use-modules (d-)
2024-11-21 18:15:13 -06:00
(d- test)
2024-11-11 21:35:26 -06:00
(srfi srfi-1))
2024-10-23 09:02:27 -05:00
2024-11-21 18:15:13 -06:00
(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)))
2024-11-11 21:58:27 -06:00
2024-11-21 18:15:13 -06:00
(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)))
2024-11-11 21:58:27 -06:00
2024-11-21 23:13:27 -06:00
(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)))
2024-11-21 18:15:13 -06:00
(define-test-suite "if-not"
(define-test "test"
(assert-equal 'a (if-not #f 'a 'b))))
2024-11-11 21:58:27 -06:00
2024-11-21 18:15:13 -06:00
(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)))
2024-10-23 09:02:27 -05:00
2024-11-21 18:15:13 -06:00
(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)))))
2024-10-23 09:02:27 -05:00
2024-11-21 18:15:13 -06:00
(define-test-suite "partial"
(define-test "test"
(define value (partial / 2))
(assert-equal 1/2 (value 4))
(assert-equal 2 (value 1))))
2024-10-23 09:02:27 -05:00
2024-11-21 18:15:13 -06:00
(define-test-suite "argmin"
(define-test "test"
(assert-equal '(b . 2) (argmin cdr < '(a . 5) '(b . 2) '(c . 3)))))
2024-10-25 15:48:01 -05:00
2024-11-21 23:13:27 -06:00
(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)))
2025-01-03 09:25:49 -06:00
(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)))))
2024-11-21 18:15:13 -06:00
(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))))
2024-12-16 10:35:33 -06:00
(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))))