| 1 | ;;; GNU Guix --- Functional package management for GNU |
| 2 | ;;; Copyright © 2012, 2013, 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org> |
| 3 | ;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org> |
| 4 | ;;; |
| 5 | ;;; This file is part of GNU Guix. |
| 6 | ;;; |
| 7 | ;;; GNU Guix is free software; you can redistribute it and/or modify it |
| 8 | ;;; under the terms of the GNU General Public License as published by |
| 9 | ;;; the Free Software Foundation; either version 3 of the License, or (at |
| 10 | ;;; your option) any later version. |
| 11 | ;;; |
| 12 | ;;; GNU Guix is distributed in the hope that it will be useful, but |
| 13 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | ;;; GNU General Public License for more details. |
| 16 | ;;; |
| 17 | ;;; You should have received a copy of the GNU General Public License |
| 18 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. |
| 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 | |