1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2019 Pierre Neidhardt <mail@ambrevar.xyz>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
19 (define-module (test-lzlib)
20 #:use-module (guix lzlib)
21 #:use-module (guix tests)
22 #:use-module (srfi srfi-64)
23 #:use-module (rnrs bytevectors)
24 #:use-module (rnrs io ports)
25 #:use-module (ice-9 match))
27 ;; Test the (guix lzlib) module.
29 (define-syntax-rule (test-assert* description exp)
31 (unless (lzlib-available?)
33 (test-assert description exp)))
37 (define (compress-and-decompress data)
38 "DATA must be a bytevector."
39 (pk "Uncompressed bytes:" (bytevector-length data))
42 (match (primitive-fork)
48 (call-with-lzip-output-port child
50 (put-bytevector port data))))
56 (let ((received (call-with-lzip-input-port parent
58 (get-bytevector-all port)))))
62 (pk "Length data" (bytevector-length data) "received" (bytevector-length received))
63 ;; The following loop is a debug helper.
65 (if (and (< i (bytevector-length received))
66 (= (bytevector-u8-ref received i)
67 (bytevector-u8-ref data i)))
69 (pk "First diff at index" i)))
72 (bytevector=? received data)))))))))))
74 (test-assert* "null bytevector"
75 (compress-and-decompress (make-bytevector (+ (random 100000)
78 (test-assert* "random bytevector"
79 (compress-and-decompress (random-bytevector (+ (random 100000)
81 (test-assert* "small bytevector"
82 (compress-and-decompress (random-bytevector 127)))
84 (test-assert* "1 bytevector"
85 (compress-and-decompress (random-bytevector 1)))
87 (test-assert* "Bytevector of size relative to Lzip internal buffers (2 * dictionary)"
88 (compress-and-decompress
90 (* 2 (car (car (assoc-ref (@@ (guix lzlib) %compression-levels)
91 (@@ (guix lzlib) %default-compression-level))))))))
93 (test-assert* "Bytevector of size relative to Lzip internal buffers (64KiB)"
94 (compress-and-decompress (random-bytevector (* 64 1024))))
96 (test-assert* "Bytevector of size relative to Lzip internal buffers (64KiB-1)"
97 (compress-and-decompress (random-bytevector (1- (* 64 1024)))))
99 (test-assert* "Bytevector of size relative to Lzip internal buffers (64KiB+1)"
100 (compress-and-decompress (random-bytevector (1+ (* 64 1024)))))
102 (test-assert* "Bytevector of size relative to Lzip internal buffers (1MiB)"
103 (compress-and-decompress (random-bytevector (* 1024 1024))))
105 (test-assert* "Bytevector of size relative to Lzip internal buffers (1MiB-1)"
106 (compress-and-decompress (random-bytevector (1- (* 1024 1024)))))
108 (test-assert* "Bytevector of size relative to Lzip internal buffers (1MiB+1)"
109 (compress-and-decompress (random-bytevector (1+ (* 1024 1024)))))
111 (test-assert "make-lzip-input-port/compressed"
112 (let* ((len (pk 'len (+ 10 (random 4000 %seed))))
113 (data (random-bytevector len))
114 (compressed (make-lzip-input-port/compressed
115 (open-bytevector-input-port data)))
116 (result (call-with-lzip-input-port compressed
117 get-bytevector-all)))
118 (pk (bytevector-length result) (bytevector-length data))
119 (bytevector=? result data)))