Commit | Line | Data |
---|---|---|
233e7676 | 1 | ;;; GNU Guix --- Functional package management for GNU |
bbb7a00e | 2 | ;;; Copyright © 2012, 2013 Ludovic Courtès <ludo@gnu.org> |
e3deeebb | 3 | ;;; |
233e7676 | 4 | ;;; This file is part of GNU Guix. |
e3deeebb | 5 | ;;; |
233e7676 | 6 | ;;; GNU Guix is free software; you can redistribute it and/or modify it |
e3deeebb LC |
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 | ;;; | |
233e7676 | 11 | ;;; GNU Guix is distributed in the hope that it will be useful, but |
e3deeebb LC |
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 | |
233e7676 | 17 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. |
e3deeebb | 18 | |
e3deeebb LC |
19 | (define-module (test-utils) |
20 | #:use-module (guix utils) | |
b980f0f9 | 21 | #:use-module ((guix store) #:select (%store-prefix store-path-package-name)) |
e3deeebb | 22 | #:use-module (srfi srfi-1) |
98090557 | 23 | #:use-module (srfi srfi-11) |
e3deeebb | 24 | #:use-module (srfi srfi-64) |
f9c7080a | 25 | #:use-module (rnrs bytevectors) |
72d86963 | 26 | #:use-module (ice-9 match)) |
e3deeebb LC |
27 | |
28 | (test-begin "utils") | |
29 | ||
6d800a80 LC |
30 | (test-assert "bytevector->base16-string->bytevector" |
31 | (every (lambda (bv) | |
32 | (equal? (base16-string->bytevector | |
33 | (bytevector->base16-string bv)) | |
34 | bv)) | |
35 | (map string->utf8 '("" "f" "fo" "foo" "foob" "fooba" "foobar")))) | |
36 | ||
98090557 LC |
37 | (test-assert "gnu-triplet->nix-system" |
38 | (let ((samples '(("i586-gnu0.3" "i686-gnu") | |
39 | ("x86_64-unknown-linux-gnu" "x86_64-linux") | |
40 | ("i386-pc-linux-gnu" "i686-linux") | |
41 | ("x86_64-unknown-freebsd8.2" "x86_64-freebsd") | |
42 | ("x86_64-apple-darwin10.8.0" "x86_64-darwin") | |
43 | ("i686-pc-cygwin" "i686-cygwin")))) | |
44 | (let-values (((gnu nix) (unzip2 samples))) | |
45 | (every (lambda (gnu nix) | |
46 | (equal? nix (gnu-triplet->nix-system gnu))) | |
47 | gnu nix)))) | |
48 | ||
9b48fb88 LC |
49 | (test-assert "package-name->name+version" |
50 | (every (match-lambda | |
51 | ((name version) | |
52 | (let*-values (((full-name) | |
53 | (if version | |
54 | (string-append name "-" version) | |
55 | name)) | |
56 | ((name* version*) | |
57 | (package-name->name+version full-name))) | |
58 | (and (equal? name* name) | |
59 | (equal? version* version))))) | |
60 | '(("foo" "0.9.1b") | |
61 | ("foo-bar" "1.0") | |
62 | ("foo-bar2" #f) | |
63 | ("guile" "2.0.6.65-134c9") ; as produced by `git-version-gen' | |
64 | ("nixpkgs" "1.0pre22125_a28fe19") | |
65 | ("gtk2" "2.38.0")))) | |
66 | ||
04fd96ca LC |
67 | (test-equal "fold2, 1 list" |
68 | (list (reverse (iota 5)) | |
69 | (map - (reverse (iota 5)))) | |
70 | (call-with-values | |
71 | (lambda () | |
72 | (fold2 (lambda (i r1 r2) | |
73 | (values (cons i r1) | |
74 | (cons (- i) r2))) | |
75 | '() '() | |
76 | (iota 5))) | |
77 | list)) | |
78 | ||
79 | (test-equal "fold2, 2 lists" | |
80 | (list (reverse '((a . 0) (b . 1) (c . 2) (d . 3))) | |
81 | (reverse '((a . 0) (b . -1) (c . -2) (d . -3)))) | |
82 | (call-with-values | |
83 | (lambda () | |
84 | (fold2 (lambda (k v r1 r2) | |
85 | (values (alist-cons k v r1) | |
86 | (alist-cons k (- v) r2))) | |
87 | '() '() | |
88 | '(a b c d) | |
89 | '(0 1 2 3))) | |
90 | list)) | |
91 | ||
72d86963 LC |
92 | (test-assert "define-record-type*" |
93 | (begin | |
94 | (define-record-type* <foo> foo make-foo | |
95 | foo? | |
96 | (bar foo-bar) | |
97 | (baz foo-baz (default (+ 40 2)))) | |
98 | (and (match (foo (bar 1) (baz 2)) | |
99 | (($ <foo> 1 2) #t)) | |
100 | (match (foo (baz 2) (bar 1)) | |
101 | (($ <foo> 1 2) #t)) | |
102 | (match (foo (bar 1)) | |
103 | (($ <foo> 1 42) #t))))) | |
104 | ||
8fd5bd2b LC |
105 | (test-assert "define-record-type* with letrec* behavior" |
106 | ;; Make sure field initializers can refer to each other as if they were in | |
107 | ;; a `letrec*'. | |
108 | (begin | |
109 | (define-record-type* <bar> bar make-bar | |
110 | foo? | |
111 | (x bar-x) | |
112 | (y bar-y (default (+ 40 2))) | |
113 | (z bar-z)) | |
114 | (and (match (bar (x 1) (y (+ x 1)) (z (* y 2))) | |
115 | (($ <bar> 1 2 4) #t)) | |
116 | (match (bar (x 7) (z (* x 3))) | |
117 | (($ <bar> 7 42 21))) | |
118 | (match (bar (z 21) (x (/ z 3))) | |
119 | (($ <bar> 7 42 21)))))) | |
120 | ||
dcd60f43 LC |
121 | (test-assert "define-record-type* & inherit" |
122 | (begin | |
123 | (define-record-type* <foo> foo make-foo | |
124 | foo? | |
125 | (bar foo-bar) | |
126 | (baz foo-baz (default (+ 40 2)))) | |
127 | (let* ((a (foo (bar 1))) | |
128 | (b (foo (inherit a) (baz 2))) | |
129 | (c (foo (inherit b) (bar -2))) | |
130 | (d (foo (inherit c))) | |
131 | (e (foo (inherit (foo (bar 42))) (baz 77)))) | |
132 | (and (match a (($ <foo> 1 42) #t)) | |
133 | (match b (($ <foo> 1 2) #t)) | |
134 | (match c (($ <foo> -2 2) #t)) | |
135 | (equal? c d) | |
136 | (match e (($ <foo> 42 77) #t)))))) | |
137 | ||
138 | (test-assert "define-record-type* & inherit & letrec* behavior" | |
139 | (begin | |
140 | (define-record-type* <foo> foo make-foo | |
141 | foo? | |
142 | (bar foo-bar) | |
143 | (baz foo-baz (default (+ 40 2)))) | |
144 | (let* ((a (foo (bar 77))) | |
145 | (b (foo (inherit a) (bar 1) (baz (+ bar 1)))) | |
146 | (c (foo (inherit b) (baz 2) (bar (- baz 1))))) | |
147 | (and (match a (($ <foo> 77 42) #t)) | |
148 | (match b (($ <foo> 1 2) #t)) | |
149 | (equal? b c))))) | |
150 | ||
bbb7a00e LC |
151 | (test-assert "define-record-type* & thunked" |
152 | (begin | |
153 | (define-record-type* <foo> foo make-foo | |
154 | foo? | |
155 | (bar foo-bar) | |
156 | (baz foo-baz (thunked))) | |
157 | ||
158 | (let* ((calls 0) | |
159 | (x (foo (bar 2) | |
160 | (baz (begin (set! calls (1+ calls)) 3))))) | |
161 | (and (zero? calls) | |
162 | (equal? (foo-bar x) 2) | |
163 | (equal? (foo-baz x) 3) (= 1 calls) | |
164 | (equal? (foo-baz x) 3) (= 2 calls))))) | |
165 | ||
166 | (test-assert "define-record-type* & thunked & default" | |
167 | (begin | |
168 | (define-record-type* <foo> foo make-foo | |
169 | foo? | |
170 | (bar foo-bar) | |
171 | (baz foo-baz (thunked) (default 42))) | |
172 | ||
173 | (let ((mark (make-parameter #f))) | |
174 | (let ((x (foo (bar 2) (baz (mark)))) | |
175 | (y (foo (bar 2)))) | |
176 | (and (equal? (foo-bar x) 2) | |
177 | (parameterize ((mark (cons 'a 'b))) | |
178 | (eq? (foo-baz x) (mark))) | |
179 | (equal? (foo-bar y) 2) | |
180 | (equal? (foo-baz y) 42)))))) | |
181 | ||
182 | (test-assert "define-record-type* & thunked & inherited" | |
183 | (begin | |
184 | (define-record-type* <foo> foo make-foo | |
185 | foo? | |
186 | (bar foo-bar (thunked)) | |
187 | (baz foo-baz (thunked) (default 42))) | |
188 | ||
189 | (let ((mark (make-parameter #f))) | |
190 | (let* ((x (foo (bar 2) (baz (mark)))) | |
191 | (y (foo (inherit x) (bar (mark))))) | |
192 | (and (equal? (foo-bar x) 2) | |
193 | (parameterize ((mark (cons 'a 'b))) | |
194 | (eq? (foo-baz x) (mark))) | |
195 | (parameterize ((mark (cons 'a 'b))) | |
196 | (eq? (foo-bar y) (mark))) | |
197 | (parameterize ((mark (cons 'a 'b))) | |
198 | (eq? (foo-baz y) (mark)))))))) | |
199 | ||
e3d74106 LC |
200 | ;; This is actually in (guix store). |
201 | (test-equal "store-path-package-name" | |
202 | "bash-4.2-p24" | |
203 | (store-path-package-name | |
b980f0f9 LC |
204 | (string-append (%store-prefix) |
205 | "/qvs2rj2ia5vci3wsdb7qvydrmacig4pg-bash-4.2-p24"))) | |
e3d74106 | 206 | |
e3deeebb LC |
207 | (test-end) |
208 | ||
209 | \f | |
210 | (exit (= (test-runner-fail-count (test-runner-current)) 0)) |