build: Require GNU libgcrypt.
[jackhill/guix/guix.git] / tests / utils.scm
CommitLineData
e3deeebb
LC
1;;; Guix --- Nix package management from Guile. -*- coding: utf-8 -*-
2;;; Copyright (C) 2012 Ludovic Courtès <ludo@gnu.org>
3;;;
4;;; This file is part of Guix.
5;;;
6;;; 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;;; 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 Guix. If not, see <http://www.gnu.org/licenses/>.
18
19
20(define-module (test-utils)
21 #:use-module (guix utils)
e3d74106 22 #:use-module ((guix store) #:select (store-path-package-name))
e3deeebb 23 #:use-module (srfi srfi-1)
98090557 24 #:use-module (srfi srfi-11)
e3deeebb
LC
25 #:use-module (srfi srfi-26)
26 #:use-module (srfi srfi-64)
f9c7080a
LC
27 #:use-module (rnrs bytevectors)
28 #:use-module (rnrs io ports)
29 #:use-module (ice-9 rdelim)
72d86963
LC
30 #:use-module (ice-9 popen)
31 #:use-module (ice-9 match))
e3deeebb 32
b86b0056
LC
33(define %nix-hash
34 (or (getenv "NIX_HASH")
35 "nix-hash"))
36
e3deeebb
LC
37(test-begin "utils")
38
39(test-assert "bytevector->base32-string"
40 (fold (lambda (bv expected result)
41 (and result
42 (string=? (bytevector->base32-string bv)
43 expected)))
44 #t
45
46 ;; Examples from RFC 4648.
47 (map string->utf8 '("" "f" "fo" "foo" "foob" "fooba" "foobar"))
48 '(""
49 "my"
50 "mzxq"
51 "mzxw6"
52 "mzxw6yq"
53 "mzxw6ytb"
54 "mzxw6ytboi")))
55
c8369cac
LC
56(test-assert "base32-string->bytevector"
57 (every (lambda (bv)
58 (equal? (base32-string->bytevector
59 (bytevector->base32-string bv))
60 bv))
61 ;; Examples from RFC 4648.
62 (map string->utf8 '("" "f" "fo" "foo" "foob" "fooba" "foobar"))))
63
64(test-assert "nix-base32-string->bytevector"
65 (every (lambda (bv)
66 (equal? (nix-base32-string->bytevector
67 (bytevector->nix-base32-string bv))
68 bv))
69 ;; Examples from RFC 4648.
70 (map string->utf8 '("" "f" "fo" "foo" "foob" "fooba" "foobar"))))
71
6d800a80
LC
72(test-assert "bytevector->base16-string->bytevector"
73 (every (lambda (bv)
74 (equal? (base16-string->bytevector
75 (bytevector->base16-string bv))
76 bv))
77 (map string->utf8 '("" "f" "fo" "foo" "foob" "fooba" "foobar"))))
78
f9c7080a 79;; The following tests requires `nix-hash' in $PATH.
b86b0056 80(test-skip (if (false-if-exception (system* %nix-hash "--version"))
f9c7080a
LC
81 0
82 1))
83
84(test-assert "sha256 & bytevector->nix-base32-string"
85 (let ((file (search-path %load-path "tests/test.drv")))
86 (equal? (bytevector->nix-base32-string
87 (sha256 (call-with-input-file file get-bytevector-all)))
b86b0056
LC
88 (let* ((c (format #f "~a --type sha256 --base32 --flat \"~a\""
89 %nix-hash file))
f9c7080a
LC
90 (p (open-input-pipe c))
91 (l (read-line p)))
92 (close-pipe p)
93 l))))
94
98090557
LC
95(test-assert "gnu-triplet->nix-system"
96 (let ((samples '(("i586-gnu0.3" "i686-gnu")
97 ("x86_64-unknown-linux-gnu" "x86_64-linux")
98 ("i386-pc-linux-gnu" "i686-linux")
99 ("x86_64-unknown-freebsd8.2" "x86_64-freebsd")
100 ("x86_64-apple-darwin10.8.0" "x86_64-darwin")
101 ("i686-pc-cygwin" "i686-cygwin"))))
102 (let-values (((gnu nix) (unzip2 samples)))
103 (every (lambda (gnu nix)
104 (equal? nix (gnu-triplet->nix-system gnu)))
105 gnu nix))))
106
72d86963
LC
107(test-assert "define-record-type*"
108 (begin
109 (define-record-type* <foo> foo make-foo
110 foo?
111 (bar foo-bar)
112 (baz foo-baz (default (+ 40 2))))
113 (and (match (foo (bar 1) (baz 2))
114 (($ <foo> 1 2) #t))
115 (match (foo (baz 2) (bar 1))
116 (($ <foo> 1 2) #t))
117 (match (foo (bar 1))
118 (($ <foo> 1 42) #t)))))
119
8fd5bd2b
LC
120(test-assert "define-record-type* with letrec* behavior"
121 ;; Make sure field initializers can refer to each other as if they were in
122 ;; a `letrec*'.
123 (begin
124 (define-record-type* <bar> bar make-bar
125 foo?
126 (x bar-x)
127 (y bar-y (default (+ 40 2)))
128 (z bar-z))
129 (and (match (bar (x 1) (y (+ x 1)) (z (* y 2)))
130 (($ <bar> 1 2 4) #t))
131 (match (bar (x 7) (z (* x 3)))
132 (($ <bar> 7 42 21)))
133 (match (bar (z 21) (x (/ z 3)))
134 (($ <bar> 7 42 21))))))
135
dcd60f43
LC
136(test-assert "define-record-type* & inherit"
137 (begin
138 (define-record-type* <foo> foo make-foo
139 foo?
140 (bar foo-bar)
141 (baz foo-baz (default (+ 40 2))))
142 (let* ((a (foo (bar 1)))
143 (b (foo (inherit a) (baz 2)))
144 (c (foo (inherit b) (bar -2)))
145 (d (foo (inherit c)))
146 (e (foo (inherit (foo (bar 42))) (baz 77))))
147 (and (match a (($ <foo> 1 42) #t))
148 (match b (($ <foo> 1 2) #t))
149 (match c (($ <foo> -2 2) #t))
150 (equal? c d)
151 (match e (($ <foo> 42 77) #t))))))
152
153(test-assert "define-record-type* & inherit & letrec* behavior"
154 (begin
155 (define-record-type* <foo> foo make-foo
156 foo?
157 (bar foo-bar)
158 (baz foo-baz (default (+ 40 2))))
159 (let* ((a (foo (bar 77)))
160 (b (foo (inherit a) (bar 1) (baz (+ bar 1))))
161 (c (foo (inherit b) (baz 2) (bar (- baz 1)))))
162 (and (match a (($ <foo> 77 42) #t))
163 (match b (($ <foo> 1 2) #t))
164 (equal? b c)))))
165
e3d74106
LC
166;; This is actually in (guix store).
167(test-equal "store-path-package-name"
168 "bash-4.2-p24"
169 (store-path-package-name
170 "/nix/store/qvs2rj2ia5vci3wsdb7qvydrmacig4pg-bash-4.2-p24"))
171
e3deeebb
LC
172(test-end)
173
174\f
175(exit (= (test-runner-fail-count (test-runner-current)) 0))
176
177;;; Local Variables:
178;;; eval: (put 'test-assert 'scheme-indent-function 1)
179;;; End: