Commit | Line | Data |
---|---|---|
b860f382 | 1 | ;;; GNU Guix --- Functional package management for GNU |
462a3fa3 | 2 | ;;; Copyright © 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org> |
b860f382 LC |
3 | ;;; |
4 | ;;; This file is part of GNU Guix. | |
5 | ;;; | |
6 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
7 | ;;; under the terms of the GNU General Public License as published by | |
8 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
9 | ;;; your option) any later version. | |
10 | ;;; | |
11 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
12 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | ;;; GNU General Public License for more details. | |
15 | ;;; | |
16 | ;;; You should have received a copy of the GNU General Public License | |
17 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
18 | ||
19 | (define-module (test-monads) | |
c1bc358f | 20 | #:use-module (guix tests) |
b860f382 LC |
21 | #:use-module (guix store) |
22 | #:use-module (guix monads) | |
23 | #:use-module (guix derivations) | |
24 | #:use-module ((guix packages) | |
25 | #:select (package-derivation %current-system)) | |
26 | #:use-module (gnu packages) | |
27 | #:use-module (gnu packages bootstrap) | |
4231f05b | 28 | #:use-module ((gnu packages base) #:select (coreutils)) |
b860f382 LC |
29 | #:use-module (ice-9 match) |
30 | #:use-module (rnrs io ports) | |
31 | #:use-module (srfi srfi-1) | |
32 | #:use-module (srfi srfi-26) | |
33 | #:use-module (srfi srfi-64)) | |
34 | ||
405a9d4e | 35 | ;; Test the (guix monads) module. |
b860f382 LC |
36 | |
37 | (define %store | |
c1bc358f | 38 | (open-connection-for-tests)) |
b860f382 LC |
39 | |
40 | (define %monads | |
41 | (list %identity-monad %store-monad)) | |
42 | ||
43 | (define %monad-run | |
44 | (list identity | |
45 | (cut run-with-store %store <>))) | |
46 | ||
47 | \f | |
48 | (test-begin "monads") | |
49 | ||
aeb7ec5c LC |
50 | (test-assert "monad?" |
51 | (and (every monad? %monads) | |
52 | (every (compose procedure? monad-bind) %monads) | |
53 | (every (compose procedure? monad-return) %monads))) | |
54 | ||
b860f382 LC |
55 | ;; The 3 "monad laws": <http://www.haskell.org/haskellwiki/Monad_laws>. |
56 | ||
57 | (test-assert "left identity" | |
58 | (every (lambda (monad run) | |
59 | (let ((number (random 777))) | |
60 | (with-monad monad | |
61 | (define (f x) | |
62 | (return (* (1+ number) 2))) | |
63 | ||
64 | (= (run (>>= (return number) f)) | |
65 | (run (f number)))))) | |
66 | %monads | |
67 | %monad-run)) | |
68 | ||
69 | (test-assert "right identity" | |
70 | (every (lambda (monad run) | |
71 | (with-monad monad | |
72 | (let ((number (return (random 777)))) | |
73 | (= (run (>>= number return)) | |
74 | (run number))))) | |
75 | %monads | |
76 | %monad-run)) | |
77 | ||
78 | (test-assert "associativity" | |
79 | (every (lambda (monad run) | |
80 | (with-monad monad | |
81 | (define (f x) | |
82 | (return (+ 1 x))) | |
83 | (define (g x) | |
84 | (return (* 2 x))) | |
85 | ||
86 | (let ((number (return (random 777)))) | |
87 | (= (run (>>= (>>= number f) g)) | |
88 | (run (>>= number (lambda (x) (>>= (f x) g)))))))) | |
89 | %monads | |
90 | %monad-run)) | |
91 | ||
92 | (test-assert "lift" | |
93 | (every (lambda (monad run) | |
94 | (let ((f (lift1 1+ monad))) | |
95 | (with-monad monad | |
96 | (let ((number (random 777))) | |
97 | (= (run (>>= (return number) f)) | |
98 | (1+ number)))))) | |
99 | %monads | |
100 | %monad-run)) | |
101 | ||
405a9d4e LC |
102 | (test-assert "mbegin" |
103 | (every (lambda (monad run) | |
104 | (with-monad monad | |
105 | (let* ((been-there? #f) | |
106 | (number (mbegin monad | |
107 | (return 1) | |
108 | (begin | |
109 | (set! been-there? #t) | |
110 | (return 2)) | |
111 | (return 3)))) | |
112 | (and (= (run number) 3) | |
113 | been-there?)))) | |
114 | %monads | |
115 | %monad-run)) | |
116 | ||
b860f382 LC |
117 | (test-assert "mlet* + text-file + package-file" |
118 | (run-with-store %store | |
119 | (mlet* %store-monad ((guile (package-file %bootstrap-guile "bin/guile")) | |
120 | (file (text-file "monadic" guile))) | |
121 | (return (equal? (call-with-input-file file get-string-all) | |
122 | guile))) | |
123 | #:guile-for-build (package-derivation %store %bootstrap-guile))) | |
124 | ||
c90ddc8f LC |
125 | (test-assert "package-file, default system" |
126 | ;; The default system should be the one at '>>=' time, not the one at | |
127 | ;; invocation time. See <http://bugs.gnu.org/18002>. | |
128 | (run-with-store %store | |
129 | (mlet* %store-monad | |
130 | ((system -> (%current-system)) | |
131 | (file (parameterize ((%current-system "foobar64-linux")) | |
132 | (package-file coreutils "bin/ls"))) | |
133 | (cu (package->derivation coreutils))) | |
134 | (return (string=? file | |
135 | (string-append (derivation->output-path cu) | |
136 | "/bin/ls")))) | |
137 | #:guile-for-build (package-derivation %store %bootstrap-guile))) | |
138 | ||
4231f05b LC |
139 | (test-assert "package-file + package->cross-derivation" |
140 | (run-with-store %store | |
b4469d8c LC |
141 | (mlet* %store-monad ((target -> "mips64el-linux-gnu") |
142 | (file (package-file coreutils "bin/ls" | |
143 | #:target target)) | |
144 | (xcu (package->cross-derivation coreutils target))) | |
4231f05b LC |
145 | (let ((output (derivation->output-path xcu))) |
146 | (return (string=? file (string-append output "/bin/ls"))))) | |
147 | #:guile-for-build (package-derivation %store %bootstrap-guile))) | |
148 | ||
0a90af15 LC |
149 | (test-assert "interned-file" |
150 | (run-with-store %store | |
151 | (mlet* %store-monad ((file -> (search-path %load-path "guix.scm")) | |
152 | (a (interned-file file)) | |
153 | (b (interned-file file "b"))) | |
154 | (return (equal? (call-with-input-file file get-string-all) | |
155 | (call-with-input-file a get-string-all) | |
156 | (call-with-input-file b get-string-all)))) | |
157 | #:guile-for-build (package-derivation %store %bootstrap-guile))) | |
158 | ||
b860f382 LC |
159 | (test-assert "mapm" |
160 | (every (lambda (monad run) | |
161 | (with-monad monad | |
162 | (equal? (run (mapm monad (lift1 1+ monad) (map return (iota 10)))) | |
163 | (map 1+ (iota 10))))) | |
164 | %monads | |
165 | %monad-run)) | |
166 | ||
167 | (test-assert "sequence" | |
168 | (every (lambda (monad run) | |
169 | (let* ((input (iota 100)) | |
170 | (order '())) | |
171 | (define (frob i) | |
f62435e2 LC |
172 | (mlet monad ((foo (return 'foo))) |
173 | ;; The side effect here is used to keep track of the order in | |
174 | ;; which monadic values are bound. Perform the side effect | |
175 | ;; within a '>>=' so that it is performed when the return | |
176 | ;; value is actually bound. | |
177 | (set! order (cons i order)) | |
178 | (return i))) | |
b860f382 LC |
179 | |
180 | (and (equal? input | |
f62435e2 | 181 | (run (sequence monad (map frob input)))) |
b860f382 LC |
182 | |
183 | ;; Make sure this is from left to right. | |
184 | (equal? order (reverse input))))) | |
185 | %monads | |
186 | %monad-run)) | |
187 | ||
188 | (test-assert "listm" | |
189 | (every (lambda (monad run) | |
190 | (run (with-monad monad | |
191 | (let ((lst (listm monad | |
192 | (return 1) (return 2) (return 3)))) | |
193 | (mlet monad ((lst lst)) | |
194 | (return (equal? '(1 2 3) lst))))))) | |
195 | %monads | |
196 | %monad-run)) | |
197 | ||
593c3fe6 LC |
198 | (test-assert "anym" |
199 | (every (lambda (monad run) | |
200 | (eq? (run (with-monad monad | |
201 | (let ((lst (list (return 1) (return 2) (return 3)))) | |
202 | (anym monad | |
203 | (lambda (x) | |
204 | (and (odd? x) 'odd!)) | |
205 | lst)))) | |
206 | 'odd!)) | |
207 | %monads | |
208 | %monad-run)) | |
209 | ||
b860f382 LC |
210 | (test-end "monads") |
211 | ||
212 | \f | |
213 | (exit (= (test-runner-fail-count (test-runner-current)) 0)) |