| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 |
|
| 20 | (define-module (test-combinators)
|
| 21 | #:use-module (guix combinators)
|
| 22 | #:use-module (srfi srfi-1)
|
| 23 | #:use-module (srfi srfi-64)
|
| 24 | #:use-module (ice-9 vlist))
|
| 25 |
|
| 26 | (test-begin "combinators")
|
| 27 |
|
| 28 | (test-equal "fold2, 1 list"
|
| 29 | (list (reverse (iota 5))
|
| 30 | (map - (reverse (iota 5))))
|
| 31 | (call-with-values
|
| 32 | (lambda ()
|
| 33 | (fold2 (lambda (i r1 r2)
|
| 34 | (values (cons i r1)
|
| 35 | (cons (- i) r2)))
|
| 36 | '() '()
|
| 37 | (iota 5)))
|
| 38 | list))
|
| 39 |
|
| 40 | (test-equal "fold2, 2 lists"
|
| 41 | (list (reverse '((a . 0) (b . 1) (c . 2) (d . 3)))
|
| 42 | (reverse '((a . 0) (b . -1) (c . -2) (d . -3))))
|
| 43 | (call-with-values
|
| 44 | (lambda ()
|
| 45 | (fold2 (lambda (k v r1 r2)
|
| 46 | (values (alist-cons k v r1)
|
| 47 | (alist-cons k (- v) r2)))
|
| 48 | '() '()
|
| 49 | '(a b c d)
|
| 50 | '(0 1 2 3)))
|
| 51 | list))
|
| 52 |
|
| 53 | (let* ((tree (alist->vhash
|
| 54 | '((0 2 3) (1 3 4) (2) (3 5 6) (4 6) (5) (6))
|
| 55 | hashq))
|
| 56 | (add-one (lambda (_ r) (1+ r)))
|
| 57 | (tree-lookup (lambda (n) (cdr (vhash-assq n tree)))))
|
| 58 | (test-equal "fold-tree, single root"
|
| 59 | 5 (fold-tree add-one 0 tree-lookup '(0)))
|
| 60 | (test-equal "fold-tree, two roots"
|
| 61 | 7 (fold-tree add-one 0 tree-lookup '(0 1)))
|
| 62 | (test-equal "fold-tree, sum"
|
| 63 | 16 (fold-tree + 0 tree-lookup '(0)))
|
| 64 | (test-equal "fold-tree, internal"
|
| 65 | 18 (fold-tree + 0 tree-lookup '(3 4)))
|
| 66 | (test-equal "fold-tree, cons"
|
| 67 | '(1 3 4 5 6)
|
| 68 | (sort (fold-tree cons '() tree-lookup '(1)) <))
|
| 69 | (test-equal "fold-tree, overlapping paths"
|
| 70 | '(1 3 4 5 6)
|
| 71 | (sort (fold-tree cons '() tree-lookup '(1 4)) <))
|
| 72 | (test-equal "fold-tree, cons, two roots"
|
| 73 | '(0 2 3 4 5 6)
|
| 74 | (sort (fold-tree cons '() tree-lookup '(0 4)) <))
|
| 75 | (test-equal "fold-tree-leaves, single root"
|
| 76 | 2 (fold-tree-leaves add-one 0 tree-lookup '(1)))
|
| 77 | (test-equal "fold-tree-leaves, single root, sum"
|
| 78 | 11 (fold-tree-leaves + 0 tree-lookup '(1)))
|
| 79 | (test-equal "fold-tree-leaves, two roots"
|
| 80 | 3 (fold-tree-leaves add-one 0 tree-lookup '(0 1)))
|
| 81 | (test-equal "fold-tree-leaves, two roots, sum"
|
| 82 | 13 (fold-tree-leaves + 0 tree-lookup '(0 1))))
|
| 83 |
|
| 84 | (test-end)
|
| 85 |
|