Add a `base32' macro, for convenience.
[guix.git] / tests / utils.scm
blob83a78b7a7889a3a3eff7d3f8b2bace2282210470
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/>.
20 (define-module (test-utils)
21   #:use-module (guix utils)
22   #:use-module (srfi srfi-1)
23   #:use-module (srfi srfi-11)
24   #:use-module (srfi srfi-26)
25   #:use-module (srfi srfi-64)
26   #:use-module (rnrs bytevectors)
27   #:use-module (rnrs io ports)
28   #:use-module (ice-9 rdelim)
29   #:use-module (ice-9 popen)
30   #:use-module (ice-9 match))
32 (test-begin "utils")
34 (test-assert "bytevector->base32-string"
35   (fold (lambda (bv expected result)
36           (and result
37                (string=? (bytevector->base32-string bv)
38                          expected)))
39         #t
41         ;; Examples from RFC 4648.
42         (map string->utf8 '("" "f" "fo" "foo" "foob" "fooba" "foobar"))
43         '(""
44           "my"
45           "mzxq"
46           "mzxw6"
47           "mzxw6yq"
48           "mzxw6ytb"
49           "mzxw6ytboi")))
51 (test-assert "base32-string->bytevector"
52   (every (lambda (bv)
53            (equal? (base32-string->bytevector
54                     (bytevector->base32-string bv))
55                    bv))
56          ;; Examples from RFC 4648.
57          (map string->utf8 '("" "f" "fo" "foo" "foob" "fooba" "foobar"))))
59 (test-assert "nix-base32-string->bytevector"
60   (every (lambda (bv)
61            (equal? (nix-base32-string->bytevector
62                     (bytevector->nix-base32-string bv))
63                    bv))
64          ;; Examples from RFC 4648.
65          (map string->utf8 '("" "f" "fo" "foo" "foob" "fooba" "foobar"))))
67 (test-assert "bytevector->base16-string->bytevector"
68   (every (lambda (bv)
69            (equal? (base16-string->bytevector
70                     (bytevector->base16-string bv))
71                    bv))
72          (map string->utf8 '("" "f" "fo" "foo" "foob" "fooba" "foobar"))))
74 ;; The following tests requires `nix-hash' in $PATH.
75 (test-skip (if (false-if-exception (system* "nix-hash" "--version"))
76                0
77                1))
79 (test-assert "sha256 & bytevector->nix-base32-string"
80   (let ((file (search-path %load-path "tests/test.drv")))
81     (equal? (bytevector->nix-base32-string
82              (sha256 (call-with-input-file file get-bytevector-all)))
83             (let* ((c (format #f "nix-hash --type sha256 --base32 --flat \"~a\""
84                               file))
85                    (p (open-input-pipe c))
86                    (l (read-line p)))
87               (close-pipe p)
88               l))))
90 (test-assert "gnu-triplet->nix-system"
91   (let ((samples '(("i586-gnu0.3" "i686-gnu")
92                    ("x86_64-unknown-linux-gnu" "x86_64-linux")
93                    ("i386-pc-linux-gnu" "i686-linux")
94                    ("x86_64-unknown-freebsd8.2" "x86_64-freebsd")
95                    ("x86_64-apple-darwin10.8.0" "x86_64-darwin")
96                    ("i686-pc-cygwin" "i686-cygwin"))))
97     (let-values (((gnu nix) (unzip2 samples)))
98       (every (lambda (gnu nix)
99                (equal? nix (gnu-triplet->nix-system gnu)))
100              gnu nix))))
102 (test-assert "define-record-type*"
103   (begin
104     (define-record-type* <foo> foo make-foo
105       foo?
106       (bar foo-bar)
107       (baz foo-baz (default (+ 40 2))))
108     (and (match (foo (bar 1) (baz 2))
109            (($ <foo> 1 2) #t))
110          (match (foo (baz 2) (bar 1))
111            (($ <foo> 1 2) #t))
112          (match (foo (bar 1))
113            (($ <foo> 1 42) #t)))))
115 (test-end)
118 (exit (= (test-runner-fail-count (test-runner-current)) 0))
120 ;;; Local Variables:
121 ;;; eval: (put 'test-assert 'scheme-indent-function 1)
122 ;;; End: