zlib.scm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. ;;; Guile-zlib --- Functional package management for GNU
  2. ;;; Copyright © 2016, 2019, 2021 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-1)
  21. #:use-module (srfi srfi-11)
  22. #:use-module (srfi srfi-64)
  23. #:use-module (rnrs bytevectors)
  24. #:use-module (rnrs io ports)
  25. #:use-module (ice-9 match))
  26. (test-begin "zlib")
  27. (define (random-seed)
  28. (logxor (getpid) (car (gettimeofday))))
  29. (define %seed
  30. (let ((seed (random-seed)))
  31. (format (current-error-port) "random seed for tests: ~a~%"
  32. seed)
  33. (seed->random-state seed)))
  34. (define (random-bytevector n)
  35. "Return a random bytevector of N bytes."
  36. (let ((bv (make-bytevector n)))
  37. (let loop ((i 0))
  38. (if (< i n)
  39. (begin
  40. (bytevector-u8-set! bv i (random 256 %seed))
  41. (loop (1+ i)))
  42. bv))))
  43. (test-assert "compression/decompression pipe"
  44. (let ((data (random-bytevector (+ (random 10000)
  45. (* 20 1024)))))
  46. (match (pipe)
  47. ((parent . child)
  48. (match (primitive-fork)
  49. (0 ;compress
  50. (dynamic-wind
  51. (const #t)
  52. (lambda ()
  53. (close-port parent)
  54. (call-with-gzip-output-port child
  55. (lambda (port)
  56. (put-bytevector port data))))
  57. (lambda ()
  58. (primitive-exit 0))))
  59. (pid ;decompress
  60. (begin
  61. (close-port child)
  62. (let ((received (call-with-gzip-input-port parent
  63. (lambda (port)
  64. (get-bytevector-all port))
  65. #:buffer-size (* 64 1024))))
  66. (match (waitpid pid)
  67. ((_ . status)
  68. (and (zero? status)
  69. (port-closed? parent)
  70. (bytevector=? received data))))))))))))
  71. (test-assert "raw compress/decompress"
  72. (let* ((data (random-bytevector (+ (random 10000) (* 20 1024))))
  73. (cdata (compress data))
  74. (ucdata (uncompress cdata)))
  75. (equal? data ucdata)))
  76. (define (test-zlib n fmt level)
  77. (test-assert (format #f "zlib ports [size: ~a, format: ~a, level: ~a]"
  78. n fmt level)
  79. (let* ((size (pk 'size (+ (random n %seed) n)))
  80. (data (random-bytevector size)))
  81. (let*-values (((port get)
  82. (open-bytevector-output-port))
  83. ((compressed)
  84. (make-zlib-output-port port
  85. #:level level
  86. #:format fmt)))
  87. (put-bytevector compressed data)
  88. (close-port compressed)
  89. (let ((data2 (get-bytevector-all
  90. (make-zlib-input-port
  91. (open-bytevector-input-port (get))
  92. #:format fmt))))
  93. (pk 'sizes size 'vs (bytevector-length data2))
  94. (bytevector=? data2 data))))))
  95. (for-each (lambda (n)
  96. (for-each (lambda (format)
  97. (for-each (lambda (level)
  98. (test-zlib n format level))
  99. (iota 9 1)))
  100. '(deflate zlib gzip)))
  101. (list (expt 2 8) (expt 2 10) (expt 2 12)
  102. (expt 2 14) (expt 2 18)))
  103. (test-end)