Mirror of GNU Guix
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
4.7 KiB

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