zlib.scm 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. ;;; Guile-zlib --- Functional package management for GNU
  2. ;;; Copyright © 2016, 2019 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of Guile-zlib.
  5. ;;;
  6. ;;; Guile-zlib 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. ;;; Guile-zlib 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 Guile-zlib. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (test-zlib)
  19. #:use-module (zlib)
  20. #:use-module (srfi srfi-64)
  21. #:use-module (rnrs bytevectors)
  22. #:use-module (rnrs io ports)
  23. #:use-module (ice-9 match))
  24. (test-begin "zlib")
  25. (define (random-seed)
  26. (logxor (getpid) (car (gettimeofday))))
  27. (define %seed
  28. (let ((seed (random-seed)))
  29. (format (current-error-port) "random seed for tests: ~a~%"
  30. seed)
  31. (seed->random-state seed)))
  32. (define (random-bytevector n)
  33. "Return a random bytevector of N bytes."
  34. (let ((bv (make-bytevector n)))
  35. (let loop ((i 0))
  36. (if (< i n)
  37. (begin
  38. (bytevector-u8-set! bv i (random 256 %seed))
  39. (loop (1+ i)))
  40. bv))))
  41. (test-assert "compression/decompression pipe"
  42. (let ((data (random-bytevector (+ (random 10000)
  43. (* 20 1024)))))
  44. (match (pipe)
  45. ((parent . child)
  46. (match (primitive-fork)
  47. (0 ;compress
  48. (dynamic-wind
  49. (const #t)
  50. (lambda ()
  51. (close-port parent)
  52. (call-with-gzip-output-port child
  53. (lambda (port)
  54. (put-bytevector port data))))
  55. (lambda ()
  56. (primitive-exit 0))))
  57. (pid ;decompress
  58. (begin
  59. (close-port child)
  60. (let ((received (call-with-gzip-input-port parent
  61. (lambda (port)
  62. (get-bytevector-all port))
  63. #:buffer-size (* 64 1024))))
  64. (match (waitpid pid)
  65. ((_ . status)
  66. (and (zero? status)
  67. (port-closed? parent)
  68. (bytevector=? received data))))))))))))
  69. (test-end)