combinators.scm 3.0 KB

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