zlib.scm 4.0 KB

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